C# MCQ Set 1
1. A type of class which does not have its own objects but acts as a base class for its subclass is known as?
a) Static class
b) Sealed class
c) Abstract class
d) None of the mentioned
Answer
Answer: c [Reason:] None.
2. The modifier used to define a class which does not have objects of its own but acts as a base class for its subclass is?
a) Sealed
b) Static
c) New
d) Abstract
Answer
Answer: d [Reason:] abstract class Base
{
}
class derived : Base
{
}
Base b1; /*object of Base class which can never be possible */
Derived d1; /*object of derived class which is possible */
3. Choose the correct statements among the following:
a) An abstract method does not have implementation
b) An abstract method can take either static or virtual modifiers
c) An abstract method can be declared only in abstract class
d) All of the mentioned
Answer
Answer: d [Reason:] None.
4. What will be the output for the given set of code?
-
namespace ConsoleApplication4
-
{
-
abstract class A
-
{
-
int i;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j;
-
public override void display()
-
{
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.j = 2;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 0
b) 2
c) Compile time error
d) 1
Answer
Answer: b [Reason:] Here in abstract class ‘A’ abstract method display() is declared and its full implementation i.e definition is given in subclass of class ‘A’.
Output :2
5. What will be the output for the given set of code?
-
namespace ConsoleApplication4
-
{
-
abstract class A
-
{
-
public int i ;
-
public int j ;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j = 5;
-
public override void display()
-
{
-
this.j = 3;
-
Console.WriteLine(i + " " + j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.i = 1;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 1, 5
b) 0, 5
c) 1, 0
d) 1, 3
Answer
Answer: d [Reason:] obj.i = 1 initializes value of i as 1 as it is the abstract member of abstract class ‘A’. Now,’j’ is also a same member as class ‘A’ .Since it is initialized the value of 5 when declared in subclass.But since abstract method is redefined in subclass using ‘this’ keyword as this.j = 3, method will execute only abstract class member ‘j’ not subclass ‘B’ own defined data member ‘j’.
Output : 1, 3
6. What will be the output for the given set of code?
-
namespace ConsoleApplication4
-
{
-
abstract class A
-
{
-
public int i;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j;
-
public int sum;
-
public override void display()
-
{
-
sum = i + j;
-
Console.WriteLine(+i + "n" + +j);
-
Console.WriteLine("sum is:" +sum);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
A obj = new B();
-
obj.i = 2;
-
B obj1 = new B();
-
obj1.j = 10;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 2, 10
12
b) 0, 10
10
c) 2, 0
2
d) 0, 0
0
Answer
Answer: c [Reason:] Abstract method implementation is processed in subclass ‘B’ .Also the object ‘obj’ of abstract class ‘A’ initializes value of i as 2.The object of class ‘B’ also initializes value of j as 10.Since, the method display() is called using object of class A which is ‘obj’ and hence i = 2 whereas j = 0 .So, sum = 2.
Output : 2 0
sum is : 2
7. If a class inheriting an abstract class does not define all of its functions then it is known as?
a) Abstract
b) A simple class
c) Static class
d) None of the mentioned
Answer
Answer: a [Reason:] Any subclass of an abstract class must either implement all of the abstract method in the super class or itself be declared abstract.
8. Which of the following modifiers is used when an abstract method is redefined by a derived class?
a) Overloads
b) Override
c) Base
d) Virtual
Answer
Answer: b [Reason:] None.
9. What will be the output for the given set of code?
-
namespace ConsoleApplication4
-
{
-
public abstract class A
-
{
-
public int i = 7;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j;
-
public override void display()
-
{
-
Console.WriteLine(i);
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
A obj1 = new B();
-
obj.j = 1;
-
obj1.i = 8;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 0, 8
b) 1, 8
c) 1, 7
d) 7, 1
Answer
Answer: d [Reason:] Data member ‘i’ of abstract class A will be preferred over variable initialized and executed by obj1 as obj1.i = 8 as ‘obj’ of class B executes display() method.
Output : 7, 1.
10. What will be the output for the given set of code?
-
namespace ConsoleApplication4
-
{
-
class A
-
{
-
public int i;
-
public void display()
-
{
-
Console.WriteLine(i);
-
}
-
}
-
class B: A
-
{
-
public int j;
-
public void display()
-
{
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.j = 1;
-
obj.i = 8;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 8, 1
b) 8
c) 1
d) 1, 8
Answer
Answer: c [Reason:] Class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
Output: 1.
C# MCQ Set 2
1. Predict the output for the following set of code.
-
static void Main(string[] args)
-
{
-
float a = 16.4f;
-
int b = 12;
-
float c;
-
c = a * ( b + a) / (a - b) ;
-
Console.WriteLine("result is :" +c);
-
Console.ReadLine();
-
}
a) 106
b) 104.789
c) 105.8546
d) 103.45
Answer
Answer: c [Reason:] The first expression evaluated is ‘b+a’ as both are combined. Next the expression is multiplied by operand ‘a’ i.e a (b+a) the whole result of numerator is combined and divided by denominator expression (a – b).
Output: result is : 105.8546.
2. Predict the solution for the following set of code.
-
static void Main(string[] args)
-
{
-
int a, b, c, x;
-
a = 90;
-
b = 15;
-
c = 3;
-
x = a - b / 3 + c * 2 - 1;
-
Console.WriteLine(x);
-
Console.ReadLine();
-
}
a) 92
b) 89
c) 90
d) 88
Answer
Answer: c [Reason:] The basic evaluation process includes two left to right passes through the expression.During first pass,the high priority operators are applied and during second pass,the low priority operators are applied as they are encountered.
First pass :
step 1 : x = 90 – 15 / 3 + 3 * 2 – 1 (15 / 3 evaluated)
step 2 : x = 90 – 5 + 3 * 2 – 1
step 3 : x = 90 – 5 + 3 * 2 -1 (3 * 2 is evaluated)
step 4 : x = 90 – 5 + 6 – 1
Second pass :
step 5 : x = 85 + 6 – 1 (90 – 5 is evaluated)
step 6 : x = 91 – 1( 85 + 6 is evaluated )
step 7 : x = 90(91 – 1 is evaluated)
Output : 90.
3. Predict the solution for the following set of code .
-
static void Main(string[] args)
-
{
-
int a, b, c, x;
-
a = 80;
-
b = 15;
-
c = 2;
-
x = a - b / (3 * c) * ( a + c);
-
Console.WriteLine(x);
-
Console.ReadLine();
-
}
a) 78
b) -84
c) 80
d) 98
Answer
Answer: b [Reason:] Whenever the parentheses are used,the expressions within parantheses assumes higher priority.If Two or more sets of parantheses appear one after another as shown above,the expression contained on the left side is evaluated first and right hand side last .
First pass:
Step 1: 80 – 15/(3*2)*(80 + 2)
Step 2: 80 – 15/6*82 ( (3 * 2) evaluated first and ( 80 + 2) evaluated later )
Second pass:
Step 3: 80 – 2*82
Step 4: 80 – 164.
Third pass:
Step 5 : -84. (80 – 164 is evaluated)
Output : -84 .
4. Correct order of priorities are :
a) ‘/’ > ‘%’ > ‘*’ > ‘+’
b) ‘/’ > ‘*’ > ‘%’ > ‘+’
c) ‘*’ > ‘/’ > ‘%’ > ‘+’
d) ‘%’ > ‘*’ > ‘/’ > ‘+’
Answer
Answer: c [Reason:] By definition.
5. Predict the output for the given snippet of code :
-
int i, j = 1, k;
-
for (i = 0; i < 3; i++)
-
{
-
k = j++ - ++j;
-
Console.Write(k + " ");
-
}
a) -4 -3 -2
b) -6 -4 -1
c) -2 -2 -2
d) -4 -4 -4
Answer
Answer: c [Reason:] Here i = 0 , j = 1.
k = 1 – 3 ( j++ = 2 and ++j = 3)
k = -2.
i = 1 , j = 3.
k = 3 – 5 ( j++ = 4 and ++j = 5)
k = -2.
i = 2 , j = 5.
k = 5 – 7 (j++ = 6 and ++j = 7)
k = -2.
Output : -2 ,-2 ,-2.
6. Predict the output for the given set of code correctly.
-
static void Main(string[] args)
-
{
-
int b= 11;
-
int c = 7;
-
int r = 5;
-
int e = 2;
-
int l;
-
int v = 109;
-
int k;
-
int z,t,p;
-
z = b * c;
-
t = b * b;
-
p = b * r * 2;
-
l = (b * c) + (r * e) + 10;
-
k = v - 8;
-
Console.WriteLine(Convert.ToString(Convert.ToChar(z)) + " " + Convert.ToString(Convert.ToChar(t)) + Convert.ToString(Convert.ToChar(p)) + Convert.ToString(Convert.ToChar(l)) + Convert.ToString(Convert.ToChar(v)) + Convert.ToString(Convert.ToChar(k)));
-
Console.ReadLine();
-
}
a) My Name
b) My nAme
c) My name
d) Myname
Answer
Answer: c [Reason:] Solving the expression l = (b * c) + (r * e) + 10 .While from left to right the parentheses are given preference first.
Step 1 : b * c is evaluated first inside first parentheses.
Step 2 : r * e is evaluated second on right side of first addition symbol .
Step 3 : After evaluating both parentheses 10 is added to value of both.
Output : My name.
7. Predict the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int n = 5;
-
int x = 4;
-
int z, c, k;
-
for (c = 1; c <= n; c++)
-
{
-
for (k = 1; k <= c; k++)
-
{
-
z = 3 * x * x + 2 * x + 4 / x + 8;
-
Console.Write(Convert.ToString(Convert.ToChar(z)));
-
}
-
Console.WriteLine("n");
-
}
-
Console.ReadLine();
-
}
a) A
AA
AAA
AAAA
b) A
AB
ABC
ABCD
c) A
AA
AAA
AAAA
AAAAA
d) A
BC
DEF
DEFG
Answer
Answer: c [Reason:] Solving the expression for value of ‘z’as 65.With,each passage of loop value number of ‘z’ increases for each row
as Row 1: A
Row 2: AA
–
–
Row 5: AAAAA
Output : A
AA
AAA
AAAA
AAAAA
8. Predict the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int n = 5;
-
int x = 4;
-
int z, c, k;
-
z = 3 * x * x + 2 * x + 4 / x + 8;
-
for (c = 1; c <= n; c++)
-
{
-
for (k = 1; k <= c; k++)
-
{
-
Console.Write(Convert.ToString(Convert.ToChar(z)));
-
z++;
-
}
-
Console.WriteLine("n");
-
}
-
Console.ReadLine();
-
}
a) A
AA
AAA
AAAA
AAAAA
b) A
AB
ABC
ABCD
ABCDE
c) A
BC
DEF
GHIJ
KLMNO
d) A
AB
BC
BCD
BCDE
Answer
Answer: c [Reason:] Solving expression ‘z’ value is 65.Before going inside first loop
Step 1: c = 1,n = 5
k = 1,k <= 1. (as c = 1)
z = 65 converted to ‘A’ as ascii value of ‘A’ is 65.
z++ increment for next loop condition by ‘1’as 66.
Row 1: A
Step 2: c = 2, n = 5
k = 2,k <= 2. (as c = 2)
z = 66 from step 1 converted value of 66 is ‘B’.Since,k < =2
loop will again loop to second value after 66 which is 67 as z is
incremented from 66 to +1 as ’67’.so,converting ascii value of 67 to character as ‘C’.
Row 2: B C
Similarly,
Step 3:
Row 3: D E F
Step 4:
Row 4: G H I J
Step 5:
Row 5: K L M N O.
Output : A
BC
DEF
GHIJ
KLMNO
9. Choose exact solution for the following code snippet :
-
static void Main(string[] args)
-
{
-
int a , b;
-
int c = 10;
-
int d = 12;
-
int e = 5;
-
int f = 6;
-
a = c * (d + e) / f + d;
-
Console.WriteLine(a);
-
b = c * ( d + e / f + d);
-
Console.WriteLine(b);
-
if (a < b)
-
{
-
Console.WriteLine(" parantheses changes values");
-
}
-
else if (a > b)
-
{
-
Counterintelligence("they have same value");
-
}
-
Console.ReadLine();
-
}
a) They have same value
b) Parentheses changes values
c) Since both have equal values, no conclusion
d) None of the mentioned
Answer
Answer: b [Reason:] Solving for expression ‘a’ expression inside parentheses are given preference evaluating (d+e) as 17.
a = 10 * 17/6 + 12.
a = 40.
Solving for expression ‘b’ expression inside parentheses (d + e /f + d ) are evaluated as (12 + 5/6 + 12 )
b = 10 *(12 + 5/6 + 12 ).
b = 240.
Output : 40
240
parantheses changes values.
10. The correct way of incrementing the operators are :
a) ++ a ++
b) b ++ 1
c) c += 1
d) d =+ 1
Answer
Answer: c [Reason:] This += is known as short hand operator which is same as variable = variable +1 .Similarly, a-= 1 is a = a-1, a*=1 is a = a * 1. They are used to make code short and efficient.
C# MCQ Set 3
1. What is the output of the following set of code ?
-
static void Main(string[] args)
-
{
-
int i, j;
-
int[, ] arr = new int[ 3, 3];
-
for (i = 0; i < 3; ++i)
-
{
-
for (j = 0; j < 3; ++j)
-
{
-
arr[i, j] = i * 2 + i * 2;
-
Console.WriteLine(arr[i, j]);
-
}
-
Console.ReadLine();
-
}
-
}
a) 0,0,0 4,4,4 8,8,8
b) 4,4,4 8,8,8 12,12,12
c) 8,8,8 12,12,12 16,16,16
d) 0,0,0 1,1,1, 2,2,2
Answer
Answer: a [Reason:] Since, with each value of of ‘i’ the value of ‘j’ is executed three times i.e
for i = 0, j = 0, 0, 0, i = 1, j = 2, 2, 2.
Output: 0, 0, 0 4, 4, 4 8, 8, 8.
2. What is the output for the following set of code?
-
static void Main(string[] args)
-
{
-
char A = 'K';
-
char B = Convert.ToChar(76);
-
A++;
-
B++;
-
Console.WriteLine(A+ " " +B);
-
Console.ReadLine();
-
}
a) M L
b) U L
c) L M
d) A B
Answer
Answer: c [Reason:] “++” increments the value of character by 1. A and B are given values K and 76, when we use increment operator their values increments by 1, A and B becomes L and M.
Output: L, M.
3. Complete the following set of code with “foreach condition”:
-
int[][]a = new int[2][];
-
a[0] = new int[3]{3, 4, 2};
-
a[1] = new int[2]{8, 5};
-
foreach( int[]i in a)
-
{
-
/* add for loop */
-
console.write( j+ " ");
-
console.writeline();
-
}
a) foreach (int j = 1;(j(<)(a(0).GetUpperBound)); (j++));
b) foreach (int j = 1;(j(<)(a.GetUpperBound(0))); (j++));
c) foreach (int j in a.Length);
d) foreach (int j in i);
Answer
Answer: d [Reason:] None.
4. What is the output for the following set of code ?
-
static void Main(string[] args)
-
{
-
double a = 345.09;
-
byte c = (byte) a;
-
Console.WriteLine(c);
-
Console.ReadLine();
-
}
a) 98
b) 89
c) 88
d) 84
Answer
Answer: b [Reason:] Type casting a larger variable into a smaller variable results in modules of larger variable by range of smaller variable. a is ‘345.09’ which is larger than byte’s range i:e -128 to 127.
Output : 89.
5. Which statement is correct about following c#.NET code ?
int[] a= {11, 3, 5, 9, 6};
a) ‘a’ is a reference to the array created on stack
b) ‘a’ is a reference to an object created on stack
c) ‘a’ is a reference to an object of a class that compiler drives from ‘System.Array’ class
d) None of the mentioned
Answer
Answer: c [Reason:] A perfect way of defining single array in C# which is derived automatically from class ‘System.Array’.
6. What is the advantage of using 2D jagged array over 2D rectangular array?
a) Easy initialization of elements
b) Allows unlimited elements as well as rows which had ‘0’ or are empty in nature
c) All of the mentioned
d) None of the mentioned
Answer
Answer: b [Reason:] In many applications where 2 dimensional arrays are used,not all rows need to have all the elements i.e they are sparse.Many rows have 0 elements.In such cases it is better to use 2D jagged arrays as they allow unequal number of elements in each row and also allow for empty rows.
7. Which statement is correct about following set of code ?
int[, ]a={{5, 4, 3},{9, 2, 6}};
a)’a’ represents 1-D array of 5 integers
b) a.GetUpperBound(0) gives 9
c)’a’ represents rectangular array of 2 columns and 3 arrays
d) a.GetUpperBound(0) gives 2
Answer
Answer: c [Reason:] By definition.
8. What is output of the following set of code?
-
static void Main(string[] args)
-
{
-
Program p = new Program();
-
p.display(2, 3, 8);
-
int []a = { 2, 56, 78, 66 };
-
Console.WriteLine("example of array");
-
Console.WriteLine("elements added are");
-
p.display(a);
-
Console.ReadLine();
-
}
-
public void display(params int[] b)
-
{
-
foreach (int i in b)
-
{
-
Console.WriteLine("ARRAY IS HAVING:{0}", i);
-
}
-
}
a) Compile time error
b) Run time error
c) Code runs successfully but prints nothing
d) Code runs successfully and prints given on console
Answer
Answer: d [Reason:] Object ‘p’ makes a call to invoke function display() and hence consecutively prints the output. Array ‘a’ is declared with elements again object ‘p’ makes a call to display() and hence, consecutively prints the output with given elements.
Output: ARRAY IS HAVING:2
ARRAY IS HAVING:3
ARRAY IS HAVING:8
elements added are:
ARRAY IS HAVING:2
ARRAY IS HAVING:56
ARRAY IS HAVING:78
ARRAY IS HAVING:66
9. Which is the correct way of defining and initializing an array of 3 integers?
a) int[] a={78, 54};
b) int[] a;
a = new int[3];
a[1] = 78;
a[2] = 9;
a[3] = 54;
c) int[] a;
a = new int{78, 9, 54};
d) int[] a;
a = new int[3]{78, 9, 54};
Answer
Answer: d [Reason:] None.
10. Choose selective differences between an array in c# and array in other programming languages.
a) Declaring array in C# the square bracket([]) comes after the type but not after identifier
b) It is necessary to declare size of an array with its type
c) No difference between declaration of array in c# as well as as in other programming languages
d) All of the mentioned
Answer
Answer: a [Reason:]
1. When declaring an array in C#, the square brackets ([]) come after the type, not the identifier.Brackets after the identifier is
not legal syntax in C#.
example : int[] IntegerArray;
2. The size of the array is not part of its type as it is in the C language. This allows to declare an array and assign any array
of int objects to it, regardless of the array’s length.
int[] IntegerArray;
IntegerArray = new int[10];
IntegerArray = new int[50];
C# MCQ Set 4
1. Which of the following cannot further inspect the attribute that is once applied?
a) Linker
b) ASP.NET Runtime
c) Language compilers
d) CLR
Answer
Answer: a [Reason:] None.
2. To apply an attribute to an Assembly, the correct way of implementation is?
a) [AssemblyInfo: AssemblyDescription (“Csharp”)].
b) [assembly: AssemblyDescription(“Csharp”)].
c) [AssemblyDescription(“Csharp”)].
d) (Assembly:AssemblyDescription(“Csharp”)].
Answer
Answer: b [Reason:] None.
3. The correct method to pass parameter to an attribute is?
a) By name
b) By address
c) By value
d) By reference
Answer
Answer: a [Reason:] None.
4. Which among the following is the correct form of applying an attribute?
a)<Serializable()>class sample { } b)(Serializable())class sample { } c)[Serializable()] class sample { } d) None of the mentioned
Answer
Answer: c [Reason:] By definition.
5. Which among the following cannot be a target for a custom attribute?
a) Enum
b) Event
c) Interface
d) Namespace
Answer
Answer: d [Reason:] None.
6. Select the correct statement about Attributes used in C#.NET?
a) The CLR can change the behaviour of the code depending on attributes applied to it
b) If a bugFixAttribute is to recieve three paramteres, then the BugFixAttribute class should implement a zero arguement constructor
c) To create a custom attribute we need to create a custom attribute structure and derive it from System.Attribute
d) None of the mentioned
Answer
Answer: a [Reason:] None.
7. The correct way to apply the custom attribute called Employer which receives two arguements – name of the employee and employeeid is?
a) Custom attribute can be applied to an assembly
b) [assembly : Employer(“Ankit”,employeeid.one)].
c) [ Employer(“Ankit”, employeeid.second)] class employee
{
}
d) All of the mentioned
Answer
Answer: d [Reason:] None.
8. Which of the following is the correct statement about inspecting an attribute in C#.NET?
a) An attribute can be inspected at link time
b) An attribute can be inspected at design time
c) An attribute can be inspected at run time
d) None of the mentioned
Answer
Answer: a [Reason:] None.
9. Attributes could be applied to
a) Method
b) Class
c) Assembly
d) All of the mentioned
Answer
Answer: d [Reason:] None.
10. The [Serializable()] attributes gets inspected at:
a) compile time
b) run time
c) design time
d) linking time
Answer
Answer: b [Reason:] None.
C# MCQ Set 5
1. Which of these classes is used to read and write bytes in a file?
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader
Answer
Answer: c [Reason:] None.
2. Which of these data types is returned by every method of OutputStream?
a) int
b) float
c) byte
d) none of the mentioned
Answer
Answer: d [Reason:] Every method of OutputStream returns void and throws an IOExeption in case of errors.
3. Which of these classes is used for input and output operation when working with bytes?
a) InputStream
b) Reader
c) Writer
d) All of the mentioned
Answer
Answer: a [Reason:] InputStream & OutputStream are designed for byte stream. Reader and writer are designed for character stream.
4. Which among the following is used for storage of memory aspects?
a) BufferedStream
b) FileStream
c) MemoryStream
d) None of the mentioned
Answer
Answer: c [Reason:] A byte stream that uses memory for storage.
5. Which among the following is used for storage of unmanaged memory aspects?
a) BufferedStream
b) FileStream
c) MemoryStream
d) UnmanagedMemoryStream
Answer
Answer: d [Reason:] A byte stream that uses unmanaged memory for storage.
6. Which property among the following represents the current position of the stream?
a) long Length
b) long Position
c) int Length
d) all of the mentioned
Answer
Answer: a [Reason:] This property contains the length of the stream. This property is read-only.
7. Choose the filemode method which is used to create a new output file with the condition that the file with same name must not exist.
a) FileMode.CreateNew
b) FileMode.Create
c) FileMode.OpenOrCreate
d) FileMode.Truncate
Answer
Answer: a [Reason:] Creates a new output file. The file must not already be existing.
8. Choose the filemode method which is used to create a new output file with the condition that the file with same name if exists will destroy the old file:
a) FileMode.CreateNew
b) FileMode.Create
c) FileMode.OpenOrCreate
d) FileMode.Truncate
Answer
Answer: b [Reason:] Creates a new output file. Any pre-existing file with the same name will be destroyed.
9. Which of these is a method used to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()
Answer
Answer: b [Reason:] None.
10. Which of these is a method used for reading bytes from the file?
a) clear()
b) ReadByte()
c) put()
d) write()
Answer
Answer: b [Reason:] FileStream defines two methods that reads bytes from a file: ReadByte() and Read().
C# MCQ Set 6
1. What is the Size of ‘Char’ datatype?
a) 8 bit
b) 12 bit
c) 16 bit
d) 20 bit
Answer
Answer: c [Reason:] None.
2. Choose the output for the following set of code.
-
static void Main(string[] args)
-
{
-
char c = 'g';
-
string s = c.ToString();
-
string s1 = "I am a human bein" + c;
-
Console.WriteLine(s1);
-
Console.ReadLine();
-
}
a) I am a human bein c
b) I am a human being
c) I am a human being c
d) I am a human bein
Answer
Answer: b [Reason:] ‘g’is stored in character variable ‘c’ which later on is converted to string using method Convert.Tostring() and hence appended at last of the string in s1.
Output: I am a human being.
3. Given is the code of days(example:”MTWTFSS”) which i need to split and hence create a list of days of week in strings(example:”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”). A set of code is given for this purpose but there is error ocuring in that set of code related to conversion of char to strings.Hence,Select a code to solve the given error.
-
static void Main(string[] args)
-
{
-
var days = "MTWTFSS";
-
var daysArray = days.ToCharArray().Cast<string>().ToArray();
-
for (var i = 0; i < daysArray.Length; i++)
-
{
-
switch (daysArray[i])
-
{
-
case "M":
-
daysArray[i] = "Monday";
-
break;
-
case "T":
-
daysArray[i] = "Tuesday";
-
break;
-
case "W":
-
daysArray[i] = "Wednesday";
-
break;
-
case "R":
-
daysArray[i] = "Thursday";
-
break;
-
case "F":
-
daysArray[i] = "Friday";
-
break;
-
case "S":
-
daysArray[i] = "Saturday";
-
break;
-
case "U":
-
daysArray[i] = "Sunday";
-
break;
-
}
-
}
-
daysArray[daysArray.Length - 1] = "and " + daysArray[daysArray.Length - 1];
-
Console.WriteLine(string.Join(", ", daysArray));
-
}
a) var daysArray = new List
b) var daysArray = days.Select(c =>dayMapping[c]).ToArray();
c) var daysArray = days.ToCharArray().Select(c =>c.Tostring()).ToArray();
d) None of above mentioned.
Answer
Answer: c. [Reason:] The problem arises due to cast conversion from “char” to “string” as one is not inherited from other. So, quick way of conversion is just using Char.ToString().
4. Output for the following set of code is ?
-
static void Main(string[] args)
-
{
-
{
-
var dayCode = "MTWFS";
-
var daysArray = new List<string>();
-
var list = new Dictionary<string, string>
-
{ {"M", "Monday"}, {"T", "Tuesday"}, {"W", "Wednesday"},
-
{"R", "Thursday"}, {"F", "Friday"}, {"S", "Saturday"},
-
{"U", "Sunday"}
-
};
-
for (int i = 0,max = dayCode.Length; i < max; i++)
-
{
-
var tmp = dayCode[i].ToString();
-
if (list.ContainsKey(tmp))
-
{
-
daysArray.Add(list[tmp]);
-
}
-
}
-
Console.WriteLine(string.Join("n ", daysArray));
-
}
a) Monday ,Tuesday ,Wednesday ,Friday ,Saturday ,Sunday
b) Monday
Tuesday
Wednesday
Friday
Sunday
c) Monday
Tuesday
Wednesday
Friday
Saturday
d) Monday ,Tuesday ,Wednesday ,Friday ,Saturday
Answer
Answer: c. [Reason:]None.
Output: Monday
Tuesday
Wednesday
Friday
Saturday
5. Select the correct differences between char and varchar datatypes?
1. varchar is non unicode and char is unicode character datatype
2. char is ‘n’ bytes whereas varchar is actual length in bytes of data entered in terms of storage size
3. varchar is variable in length and char is the fixed length string
4. For varchar, if a string is less than the maximum length then it is stored in verbatim without any extra characters while for char if a string is less than the set length it is padded with extra characters to equalize its length to given length
a) 1, 3, 4
b) 2, 3, 4
c) 1, 2, 4
d) 3, 4
Answer
Answer: d. [Reason:] By definition.
6. Which is the String method used to compare two strings with each other ?
a) Compare To()
b) Compare()
c) Copy()
d) ConCat()
Answer
Answer: b [Reason:] Compare() used to compare two strings by taking length of strings in considerations.
7. Correct output for the given C#.NET code is ?
-
static void Main(string[] args)
-
{
-
string s1 = "Delhi";
-
string s2;
-
s2 = s1.Insert (6, "Jaipur");
-
Console.WriteLine(s2);
-
}
a) DelhJaipuri
b) Delhi Jaipur
c) Delhi
d) DelhiJaipur
Answer
Answer: d. [Reason:]Insert method() of string class used to join two strings s1 and s2.
Output : DelhiJaipur
8. For two strings s1 and s2 to be equal, which is the correct way to find if the contents of two strings are equal ?
a) if(s1 = s2)
b) int c;
c = s1.CompareTo(s2);
c) if (s1 is s2)
d) if(strcmp(s1, s2))
Answer
Answer : b
9. Select the output for given string ?
-
static void Main(string[] args)
-
{
-
string Str, Revstr = " ";
-
int Length;
-
Console.Write("Enter A String : ");
-
Str = Console.ReadLine();
-
Length = Str.Length - 1;
-
while (Length >= 0)
-
{
-
Revstr = Revstr + Str[Length];
-
Length --;
-
}
-
Console.WriteLine("Reverse String Is {0}", Revstr);
-
Console.ReadLine();
-
}
Enter a String:BOMBAY.
a) BOMBA
b) YABMOB
c) BOMAYB
d) YABMO
Answer
Answer : b. [Reason:] Explain the concept of reversal of string without using any string inbuilt method but using while loop conditions.
Output:YABMOB
10. Select the appropriate set of code for conversion of string to hexa form.
a)
-
static string ConvertstringToHex(string input, system.Text.Encoding encoding)
-
{
-
string Bytes = encoding.GetBytes(input);
-
string Builder sbBytes = new Builder sbBytes();
-
for each (byte 'b' in StringBytes)
-
{
-
sBytes.AppendFormat("{0:x2}", b);
-
}
-
return sbBytes.Tostring();
-
}
b)
-
static string ConvertstringToHex(string input, system.Text.Encoding encoding)
-
{
-
char[]string Bytes = encoding.GetBytes(input);
-
string Builder sbBytes = new Builder sbBytes(StringBytes.Length*2);
-
for each (byte 'b' in StringBytes)
-
{
-
sBytes.AppendFormat("{0:X2}", b);
-
}
-
return sbBytes.Tostring();
-
}
c)
-
public static string ConvertStringToHex(String input, System.Text.Encoding encoding)
-
{
-
{
-
Byte[] stringBytes = encoding.GetBytes(input);
-
StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
-
foreach (byte b in stringBytes)
-
{
-
sbBytes.AppendFormat("{0:X2}", b);
-
}
-
Console.WriteLine(sbBytes.ToString());//sbBytes.ToString());
-
return sbBytes.ToString();
-
}
-
}
d) None of the mentioned
Answer
Answer : c. [Reason:] None.
Output:public static string ConvertStringToHex (String input, System.Text.Encoding encoding)
{
{
Byte[] stringBytes = encoding.GetBytes(input);
StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
foreach (byte b in stringBytes)
{
sbBytes.AppendFormat(“{0:X2}”, b);
}
Console.WriteLine(sbBytes.ToString());//sbBytes.ToString());
return sbBytes.ToString();
}
11. Which of the following code is used for conversion of hex to string form ?
a)
-
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
-
{
-
char[] numberChars = hexInput.Length;
-
byte[] bytes = new byte[numberChars / 2];
-
for (int i = 0; i < numberChars; i += 2)
-
{
-
bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 0), 16);
-
}
-
return encoding.GetString(bytes);
-
}
b)
-
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
-
{
-
int numberChars = hexInput.Length;
-
byte[] bytes = new byte[numberChars / 2];
-
for (int i = 0; i < numberChars; i += 2)
-
{
-
bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
-
}
-
return encoding.GetString(bytes);
-
}
c)
-
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
-
{
-
string numberChars = hexInput.Length;
-
byte[] bytes = new byte[numberChars];
-
for (int i = 0; i < numberChars; i += 2)
-
{
-
bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
-
}
-
return encoding.GetString(bytes);
-
}
d) None of the mentioned
Answer
Answer : b
Output:
public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
{
int numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
}
return encoding.GetString(bytes);
}
12. Correct output for the C#.NET code given below is :
-
string s1 = " I AM BEST ";
-
string s2;
-
s2 = s1.substring (5, 4);
-
Console.WriteLine (s2);
a) AM BEST
b) I AM BES
c) BEST
d) I AM
Answer
Answer : c [Reason:] Substring() of string class used to extract substrings from given string.In the given substring condition, it extracts a substring beginning at 5th position and ending at 4th position.
Output: BEST.
13. Correct statement about strings are ?
a) a string is created on the stack
b) a string is primitive in nature
c) a string created on heap
d) created of string on a stack or on a heap depends on the length of the string
Answer
Answer : c [Reason:] None.
14. Verbatim string literal is better used for ?
a) Convenience and better readability of strings when string text consist of backlash characters
b) Used to initialize multi line strings
c) To embed a quotation mark by using double quotation marks inside a verbatim string
d) All of the mentioned
Answer
Answer : d [Reason:] By definition.
15. Why strings are of reference type in C#.NET ?
a) To create string on stack
b) To reduce size of string
c) To overcome problem of stackoverflow
d) None of the mentioned
Answer
Answer : b [Reason:] The problem of stack overflow very likely to occur since transport protocol used on web these days are ‘HTTP’ and data standard as ‘XML’.Hence, both make use of strings extensively which will result in stack overflow problem.So, to avoid this situation it is good idea to make strings a reference type and hence create it on heap.
C# MCQ Set 7
1. Select output for the following set of code.
-
class sample
-
{
-
public int i;
-
public int[] arr = new int[10];
-
public void fun(int i, int val)
-
{
-
arr[i] = val;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample s = new sample();
-
s.i = 10;
-
sample.fun(1, 5);
-
s.fun(1, 5);
-
Console.ReadLine();
-
}
-
}
a) sample.fun(1, 5) will not work correctly
b) s.i = 10 cannot work as i is ‘public’
c) sample.fun(1, 5) will set value as 5 in arr[1].
d) s.fun(1, 5) will work correctly
Answer
Answer: a [Reason:] An Object reference is required for non static field,method or property.
i.e sample s = new sample();
s.i = 10;
sample.fun(1, 5);
sample.fun(1, 5);
Console.ReadLine();
2. Which of the following is used to define the member of a class externally?
a) :
b) ::
c) #
d) none of the mentioned
Answer
Answer: b [Reason:] By definition.
3. The operator used to access member function of a class?
a) :
b) ::
c) .
d) #
Answer
Answer: c [Reason:] objectname.function name(actual arguments);
4. What is the most specified using class declaration ?
a) type
b) scope
c) type & scope
d) None of mentioned
Answer
Answer: c [Reason:] General form of class declaration in C# is :
class class_name
{
member variables
{
method body
}
{
method body
}
{
method body
}
}
5. Select the output for the following set of code :
-
class sample
-
{
-
public int i;
-
public int j;
-
public void fun(int i, int j)
-
{
-
this.i = i;
-
this.j = j;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample s = new sample();
-
s.i = 1;
-
s.j = 2;
-
s.fun(s.i, s.j);
-
Console.WriteLine(s.i + " " + s.j);
-
Console.ReadLine();
-
}
-
}
a) Error while calling s.fun() due to inaccessible level
b) Error as ‘this’ reference would not be able to call ‘i’ and ‘j’
c) 1 2
d) Runs successfully but prints nothing
Answer
Answer: c [Reason:] Variable ‘i’ and ‘j’ declared with scope public in sample class are accessed using object of class ‘sample’ which is ‘s’.
Output:1 2.
6. Which of following statements about objects in “C#” is correct?
a) Everything you use in C# is an object, including Windows Forms and controls
b) Objects have methods and events that allow them to perform actions
c) All objects created from a class will occupy equal number of bytes in memory
d) All of the mentioned
Answer
Answer: d [Reason:] By definition.
7. “A mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse.In short it isolates a particular code and data from all other codes and data. A well-defined interface controls the access to that particular code and data.”
a) Abstraction
b) Polymorphism
c) Inheritance
d) Encapsulation
Answer
Answer: d [Reason:] By definition.
8. Select the output for the following set of code :
-
class z
-
{
-
public int X;
-
public int Y;
-
public const int c1 = 5;
-
public const int c2 = c1 * 25;
-
public void set(int a, int b)
-
{
-
X = a;
-
Y = b;
-
}
-
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
z s = new z();
-
s.set(10, 20);
-
Console.WriteLine(s.X + " " + s.Y);
-
Console.WriteLine(z.c1 + " " + z.c2);
-
Console.ReadLine();
-
}
-
}
a) 10 20
5 25
b) 20 10
25 5
c) 10 20
5 125
d) 20 10
125 5
Answer
Answer: c [Reason:] Member fucntion() ‘set’ is accessed using object of class ‘z’ values are passed as parameter to ‘a’ and ‘b’.Since, variable ‘c1’ and ‘c2’ are public data member of class ‘z’.They are accessed using classname.
Output : 10 20
5 125.
9. Correct way of declaration of object of the following class is ?
class name
a) name n = new name();
b) n = name();
c) name n = name();
d) n = new name();
Answer
Answer: a [Reason:] None.
10. The data members of a class by default are ?
a) protected, public
b) private, public
c) private
d) public
Answer
Answer: c [Reason:] None.
11. Select the output for the following set of code :
-
class z
-
{
-
public string name1;
-
public string address;
-
public void show()
-
{
-
Console.WriteLine("{0} is in city{1}", name1, " ", address);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
z n = new z();
-
n.name1 = "harsh";
-
n.address = "new delhi";
-
n.show();
-
Console.ReadLine();
-
}
-
}
a) Syntax error
b) {0} is in city{1} harsh new delhi
c) harsh is in new delhi
d) Run successfully prints nothing
Answer
Answer: c [Reason:] Member function show() accessed using object of class ‘z’ which is ‘n’ as object.member().
Output : harsh is in new delhi.
12. What does the following code imply ?
csharp abc;
abc = new charp();
a) Object creation on class csharp
b) Create an object of type csharp on heap or on stack depending on the size of object
c) create a reference c on csharp and an object of type csharp on heap
d) create an object of type csharp on stack
Answer
Answer: c [Reason:] None.
13. The output of code is ?
-
class test
-
{
-
public void print()
-
{
-
Console.WriteLine("Csharp:");
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
test t;
-
t.print();
-
Console.ReadLine();
-
}
-
}
a) Code runs successfully prints nothing
b) Code runs and prints “Csharp”
c) Syntax error as t is unassigned variable which is never used
d) None of the mentioned
Answer
Answer: c [Reason:] object of class test should be declared as test t = new test();
test t = new test();
t.print();
Console.ReadLine();