Quiz
C# Lesson Questions Part 2
This quiz has question types that open in the usual player.
Questions in this quiz (45)
1What is the output of this code?
static int Vol(int x, int y=3, int z=1) {
return x*y*z;
}
static void Main(string[] args) {
Console.WriteLine(Vol(2,4));
}
[blank_start]8[blank_end]- A8
2Call the method using named arguments with the values 5 for "from",99 for "to" and 2 for "step":
static int calc(int from, int to, int step=1) {
int res=0;
for(int i=from; i<to; i+=step=1) {
res+=I;
}
return res;
}
static void Main (string[] args) {
int res = [blank_start]clac[blank_end]( [blank_start]step:[blank_end] 2, [blank_start]to:[blank_end] 99, [blank_start]from:[blank_end] 5);
Console.WriteLine(res);
}- Aclac
- Bstep:
- Cto:
- Dfrom:
3What is the output of this code?
static void Test(int x) {
x=8;
}
static void Main() {
int a=5;
Test(a);
Console.WriteLine(a);
}
[blank_start]5[blank_end]- A5
4Fill in the blanks to create a method that swaps the values of its two arguments.
void Swap ([blank_start]ref[blank_end] int x, [blank_start]ref[blank_end] int y) {
int temp;
temp=x;
x=y;
y=[blank_start]temp[blank_end];
}- Aref
- Bref
- Ctemp
5Fill in the blanks to ask for user input in the method and return the value entered using output parameters.
static void Ask( [blank_start]out[blank_end] string name) {
[blank_start]name[blank_end] = Console.ReadLine();
}
static void Main(string[] args) {
string nm;
Ask([blank_start]out[blank_end] nm);
}- Aout
- Bname
- Cout
6Method overloading means:
- ASame parameters, different method names
- Bprinting values
- CSame method name, different parameters
7What is the output of this code?
static void Print(int a) {
Console.WriteLine(a*A);
}
static void Print(double a) {
Console.WriteLine(a+a);
}
static void Main(string[] args) {
Print(3);
}
[blank_start]9[blank_end]- A9
8To use data types, you need to define
- Amethods
- Ban int variable
- Ca class
9The process of creating objects is called:
- Aobjection
- Binstantiation
- Ccreation
- Dclassification
10Which of the following determines the current state of an object?
- Aproperties
- Bthe name of the object
- Cclasses
11Fill in the blanks to create a class called Car.
[blank_start]class[blank_end] Car [blank_start]{[blank_end]
string color;
int year;
[blank_start]}[blank_end]- Aclass
- B{
- C}
12Fill in the blanks to create an object of type Car and call its horn() method.
Car c = [blank_start]new[blank_end] Car();
[blank_start]c.horn[blank_end] ();- Anew
- Bc.horn
13Dog d=new Dog();
[blank_start]d[blank_end].age=[blank_start]7[blank_end];- Ad
- B7
14Encapsulation allows you to:
- AHide details of a class realization
- BAssign values to variable
- CDeclare a method
15Fill in the blanks to declare a Person class, hide the age member, and make it accessible through the GetAge method.
[blank_start]class[blank_end] Person {
private int age;
[blank_start]public[blank_end] int GetAge() {
[blank_start]return[blank_end] age;
}
public void SetAge(int n) {
age =n;
}
}- Aclass
- Bpublic
- Creturn
16What is the output of this code?
class Dog
{
public Dog()
{Console.WriteLine(1);}
public Dog(string name)
{Console.WriteLine(name;}
}
static void Main(string[] args) {
Dog d = new Dog("2");
}
[blank_start]2[blank_end]- A2
17Which of the following are accessors?
Select all that apply.- Aget
- Bset
- Cundo
- Ddelete
18Fill in the blanks to define valid get and set accessors for the age member:
class Dog {
private int age;
public int Age {
[blank_start]get[blank_end] {[blank_start]return[blank_end] age; }
set {age = [blank_start]value[blank_end];}
}
}- Aget
- Breturn
- Cvalue
19Fill in the blanks to create a read-only property X. The return value of the accessor should be the square of x.
class A {
private int x=8;
public int X {
[blank_start]get[blank_end] {return x [blank_start]*[blank_end]x; }
}
}- Aget
- B*
20Fill in the blanks to create an auto-property named Age of type int.
class Dog
{
public [blank_start]int[blank_end] Age { [blank_start]get;[blank_end] set; }
}- Aint
- Bget;
21Fill in the blanks to instantiate an array of 42 elements of type double:
[blank_start]double[blank_end] [] a = [blank_start]new[blank_end] double [[blank_start]42[blank_end]];- Adouble
- Bnew
- C42
22What is the value of x after these statements execute?
int[] a={4,7,2};
int x = a[0] + a[2];
[blank_start]6[blank_end]- A6
23Fill in the blanks to print all elements of the array using a for loop.
int[] arr = new int [7];
[blank_start]for[blank_end](int k=0; k<[blank_start]7[blank_end]; k++) {
Console.WriteLine([blank_start]arr[blank_end][k]);
}- Afor
- B7
- Carr
24Fill in the blanks to create a valid foreach loop that displays all even elements of the array.
int[] nums = {5,2,3,4,7};
[blank_start]foreach[blank_end] (var n [blank_start]in[blank_end] nums) {
if(n%2 == 0)
Console.WriteLine([blank_start]n[blank_end]);
}- Aforeach
- Bin
- Cn
25What is the output of this code?
int[] arr = {8,2,6};
int y=0;
foreach (int x in arr) {
y+=x/2;
}
Console.Write(y);
[blank_start]8[blank_end]- A8
26Fill in the blanks to declare a two-dimensional array of integers with 8 rows and 8 columns.
int[[blank_start],[blank_end]] chessBoard = [blank_start]new[blank_end] int[8 , [blank_start]8[blank_end]];- A,
- Bnew
- C8
27Fill in the blanks to declare a jagged array that contains 8 two-dimensional arrays.
int[][,] a = new int[[blank_start]8[blank_end]][[blank_start],[blank_end]];- A8
- B,
28What is the output of this code?
int[ , , ] a = new int[2, 3, 4];
Console.Write(a.Rank);
[blank_start]3[blank_end]- A3
29What is the output of this code?
int[] a = {4, 6, 5, 2};
int x = a[0]+a.Min();
Console.Write(x);
[blank_start]6[blank_end]- A6
30What is the output of this code?
string s ="SoloLearn is awesome";
Console.Write(s.IndexOf('e'));
[blank_start]5[blank_end]- A5
31Fill in the blanks to assign the last character of the string to the x variable:
string s="Hello";
char x;
x=[blank_start]s[blank_end][s.Length-1[blank_start]][blank_end];- As
- B]
32Destructors are:
- Aclass member variables
- Binvoked when an object is deleted
- Cinvoked only if a constructor is not called
33Assign the value of the math class static PI variable to x.
double x = [blank_start]Math[blank_end].[blank_start]PI[blank_end]- AMath
- Bvoid
- Cstatic
- DPI
- Ereturn
- FPI
- Gvoid
- HMath
- Istatic
- Jreturn
34Initialize the static Age property of the Person class using a static constructor:
[blank_start]class[blank_end] Person {
public [blank_start]static[blank_end] int Age ( get; set; }
static [blank_start]Person()[blank_end] {
Age=0;
}
}- Aclass
- Bint
- CPerson()
- Dvoid
- Estatic
- Fstatic
- Gint
- HPerson()
- Ivoid
- Jclass
- KPerson()
- Lint
- Mvoid
- Nstatic
- Oclass
35A static class contains:
- Aat least one static member
- Bonly static members
36What is the output of this code?
int[] arr={9,4,10,42};
Array.Sort(arr);
double x = Math.Sqrt(arr[1]);
Console.WriteLine(x);
[blank_start]3[blank_end]- A3
37Fill in the blanks to print the age variable of the Person class using the this keyword.
[blank_start]class[blank_end] Person {
private int age;
public void Print() {
Console.Write([blank_start]this.[blank_end]age);
}
}- Aclass
- Bthis.
38Which statements are true?
SELECT ALL THAT APPLY- Areadonly fields can be initialized by the constructor
- Bconstants should be assigned a value when declared
- Cconstants can be initialized by the constructor
- Dreadonly field can be initialized only when declared
39What is the output of this code?
string str = "I know C#";
char x=str[3];
Console.WriteLine(x)
[blank_start]n[blank_end]- An
40Fill in the blanks to define a valid indexer:
class Person {
private string name;
public char [blank_start]this[blank_end][int index] {
get {
return [blank_start]name[blank_end] [index];
}
}
}- Athis
- Bname
41Fill in the blanks to overload the greater than operator for the Box class.
public static [blank_start]bool[blank_end] operator [blank_start]>[blank_end] (Box a, Box b) {
if (a.Height * a.Width > b.Height * b.Width )
[blank_start]return[blank_end] true;
else
return false;
}- Abool
- B>
- Creturn
42Assume the Employee class inherits the members of the Person class. What is the Person class called?
- AThe Derived class
- BThe Base class
- CThe Main class
43Fill in the blanks to derive Cat from Animal;
[blank_start]class[blank_end] Cat [blank_start]:[blank_end] Animal {
// some code
}- Aclass
- B:
44Fill in the blanks to make the Area method of the Shape class accessible only in the derived class:
[blank_start]class[blank_end] Shape {
public int H {get; set; }
public int W {get; set }
[blank_start]protected[blank_end] int Area () {
return H * W;
}
}- Aprotected
- Bclass
45Fill in the blanks to derive a class B from class A and prevent class B from being inherited.
[blank_start]class[blank_end] A {
}
[blank_start]sealed[blank_end] class B [blank_start]:[blank_end] A {
}- Aclass
- Bsealed
- C:
Information
- Description
- All of these questions are from SoloLearn. I am making these for myself to study them easier.
- Author
- April Rice
- Language
- English
- Views
- 12
- Tags
- Rating
- No ratings yet Rate this quiz