Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

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

View 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

View 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

View Answer

Answer: d [Reason:] None.

4. What will be the output for the given set of code?

  1.  namespace ConsoleApplication4
  2.  {   
  3.      abstract class A 
  4.      {
  5.          int i;
  6.          public  abstract void display();
  7.      }    
  8.      class B: A 
  9.      {
  10.          public  int j;
  11.          public override void display() 
  12.          {
  13.              Console.WriteLine(j);
  14.          }
  15.      }    
  16.      class Program
  17.      {
  18.          static void Main(string[] args)
  19.          {
  20.              B obj = new B();
  21.              obj.j = 2;
  22.              obj.display();
  23.              Console.ReadLine();
  24.          }
  25.      }
  26.  }

a) 0
b) 2
c) Compile time error
d) 1

View 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?

  1.  namespace ConsoleApplication4
  2.  {
  3.      abstract class A
  4.      {
  5.          public int i ;
  6.          public int j ;
  7.          public abstract void display();
  8.      }    
  9.      class B: A    
  10.      {
  11.          public int j = 5;
  12.          public override void display() 
  13.          {
  14.              this.j = 3;
  15.              Console.WriteLine(i + "  " + j);
  16.          }
  17.      }    
  18.      class Program
  19.      {
  20.          static void Main(string[] args)
  21.          {
  22.              B obj = new B();
  23.              obj.i = 1;
  24.              obj.display();     
  25.              Console.ReadLine();
  26.          }
  27.      }
  28.  }

a) 1, 5
b) 0, 5
c) 1, 0
d) 1, 3

View 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?

  1. namespace ConsoleApplication4
  2. {   
  3.     abstract class A 
  4.     {
  5.         public int i;
  6.         public abstract void display();
  7.     }    
  8.     class B: A 
  9.     {
  10.         public  int j;
  11.         public int sum;
  12.         public override void display() 
  13.         {
  14.             sum = i + j;
  15.             Console.WriteLine(+i + "n" + +j);
  16.             Console.WriteLine("sum is:" +sum);
  17.         }
  18.     }    
  19.     class Program
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             A obj = new B();
  24.             obj.i = 2;
  25.             B obj1 = new B();
  26.             obj1.j = 10;
  27.             obj.display();
  28.             Console.ReadLine();
  29.         }
  30.     }
  31. }

a) 2, 10
12
b) 0, 10
10
c) 2, 0
2
d) 0, 0
0

View 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

View 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

View Answer

Answer: b [Reason:] None.

9. What will be the output for the given set of code?

  1.  namespace ConsoleApplication4
  2.  {
  3.      public abstract class A
  4.      {
  5.          public int i = 7;
  6.          public abstract void display();
  7.      }    
  8.      class B: A 
  9.      {
  10.          public int j;
  11.          public override void display() 
  12.          {
  13.              Console.WriteLine(i);
  14.              Console.WriteLine(j);
  15.          }
  16.      }    
  17.      class Program
  18.      {
  19.          static void Main(string[] args)
  20.          {
  21.              B obj = new B();
  22.              A obj1 = new B();
  23.              obj.j = 1;
  24.              obj1.i = 8;
  25.              obj.display();
  26.              Console.ReadLine();
  27.          }
  28.      }
  29.  }

a) 0, 8
b) 1, 8
c) 1, 7
d) 7, 1

View 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?

  1.  namespace ConsoleApplication4
  2.  {
  3.      class A
  4.      {
  5.          public int i;
  6.          public void display()
  7.          {
  8.              Console.WriteLine(i);
  9.          }
  10.      }    
  11.      class B: A 
  12.      {
  13.          public int j;
  14.          public void display() 
  15.          {
  16.              Console.WriteLine(j);
  17.          }
  18.      }    
  19.      class Program
  20.      {
  21.          static void Main(string[] args)
  22.          {
  23.              B obj = new B();
  24.              obj.j = 1;
  25.              obj.i = 8;
  26.              obj.display();
  27.              Console.ReadLine();
  28.          }
  29.      }
  30.  }

a) 8, 1
b) 8
c) 1
d) 1, 8

View 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.

  1.  static void Main(string[] args)
  2.  {
  3.      float a = 16.4f;
  4.      int b = 12;
  5.      float c;
  6.      c =  a * ( b + a) / (a - b) ;
  7.      Console.WriteLine("result is :" +c);
  8.      Console.ReadLine();
  9.  }

a) 106
b) 104.789
c) 105.8546
d) 103.45

View 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.

  1.   static void Main(string[] args)
  2.   {
  3.       int a, b, c, x;
  4.       a = 90;
  5.       b = 15;
  6.       c = 3;
  7.       x = a - b / 3 + c * 2 - 1;
  8.       Console.WriteLine(x);
  9.       Console.ReadLine();
  10.   }

a) 92
b) 89
c) 90
d) 88

View 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 .

  1.   static void Main(string[] args)
  2.   {
  3.       int a, b, c, x;
  4.       a = 80;
  5.       b = 15;
  6.       c = 2;
  7.       x = a - b / (3 * c) * ( a + c);
  8.       Console.WriteLine(x);
  9.       Console.ReadLine();
  10.   }

a) 78
b) -84
c) 80
d) 98

View 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) ‘%’ > ‘*’ > ‘/’ > ‘+’

View Answer

Answer: c [Reason:] By definition.

5. Predict the output for the given snippet of code :

  1.     int i, j = 1, k;
  2.     for (i = 0; i < 3; i++)
  3.     {
  4.         k = j++ - ++j;
  5.         Console.Write(k + " ");
  6.     }

a) -4 -3 -2
b) -6 -4 -1
c) -2 -2 -2
d) -4 -4 -4

View 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.

  1.   static void Main(string[] args)
  2.   {
  3.       int b= 11;
  4.       int c = 7;
  5.       int r = 5;
  6.       int e = 2;
  7.       int l;
  8.       int v = 109;
  9.       int k;
  10.       int z,t,p;
  11.       z = b * c;
  12.       t = b * b;
  13.       p = b * r * 2;
  14.       l = (b * c) + (r * e) + 10;
  15.       k = v - 8;
  16.       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)));                
  17.       Console.ReadLine();
  18.   }

a) My Name
b) My nAme
c) My name
d) Myname

View 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 :

  1.  static void Main(string[] args)
  2.  {
  3.      int n = 5;
  4.      int x = 4;
  5.      int z, c, k;
  6.      for (c = 1; c <= n; c++)
  7.      {
  8.          for (k = 1; k <= c; k++)
  9.      {
  10.          z = 3 * x * x + 2 * x + 4 / x + 8;
  11.          Console.Write(Convert.ToString(Convert.ToChar(z)));
  12.      }
  13.          Console.WriteLine("n");
  14.      }
  15.      Console.ReadLine();
  16.  }

a) A
AA
AAA
AAAA
b) A
AB
ABC
ABCD
c) A
AA
AAA
AAAA
AAAAA
d) A
BC
DEF
DEFG

View 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 :

  1.  static void Main(string[] args)
  2.  {
  3.      int n = 5;
  4.      int x = 4;
  5.      int z, c, k;
  6.      z = 3 * x * x + 2 * x + 4 / x + 8;
  7.      for (c = 1; c <= n; c++)
  8.      {
  9.          for (k = 1; k <= c; k++)
  10.          {
  11.              Console.Write(Convert.ToString(Convert.ToChar(z)));
  12.              z++;
  13.          }
  14.          Console.WriteLine("n");
  15.      }
  16.      Console.ReadLine();
  17.  }

a) A
AA
AAA
AAAA
AAAAA
b) A
AB
ABC
ABCD
ABCDE
c) A
BC
DEF
GHIJ
KLMNO
d) A
AB
BC
BCD
BCDE

View 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 :

  1.   static void Main(string[] args)
  2.   {
  3.       int a , b;
  4.       int c = 10;
  5.       int d = 12;
  6.       int e = 5;
  7.       int f = 6;
  8.       a = c * (d + e) / f + d;
  9.       Console.WriteLine(a);
  10.       b = c * ( d + e / f + d);
  11.       Console.WriteLine(b);
  12.       if (a < b)
  13.       {
  14.           Console.WriteLine(" parantheses changes values");
  15.       }
  16.       else if (a > b)
  17.       {
  18.           Counterintelligence("they have same value");
  19.       }
  20.       Console.ReadLine();
  21.   }

a) They have same value
b) Parentheses changes values
c) Since both have equal values, no conclusion
d) None of the mentioned

View 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

View 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 ?

  1. static void Main(string[] args)
  2. {
  3.     int i, j;
  4.     int[, ] arr = new int[ 3, 3];
  5.     for (i = 0; i < 3; ++i)
  6.     {
  7.         for (j = 0; j < 3; ++j)
  8.         {
  9.             arr[i, j] = i * 2 + i * 2;
  10.             Console.WriteLine(arr[i, j]);
  11.         }
  12.         Console.ReadLine();
  13.     }
  14. }

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

View 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?

  1.  static void Main(string[] args)
  2.  {
  3.      char A = 'K';
  4.      char B = Convert.ToChar(76);
  5.      A++;
  6.      B++;
  7.      Console.WriteLine(A+ "  " +B);
  8.      Console.ReadLine();
  9.  }

a) M L
b) U L
c) L M
d) A B

View 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”:

  1. int[][]a = new int[2][];
  2. a[0] = new int[3]{3, 4, 2};
  3. a[1] = new int[2]{8, 5};
  4. foreach( int[]i in a)
  5. {
  6. /* add for loop */
  7. console.write( j+ " ");
  8. console.writeline();
  9. }

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);

View Answer

Answer: d [Reason:] None.

4. What is the output for the following set of code ?

  1. static void Main(string[] args)
  2. {
  3.     double a = 345.09;
  4.     byte c = (byte) a;
  5.     Console.WriteLine(c);
  6.     Console.ReadLine();
  7. }

a) 98
b) 89
c) 88
d) 84

View 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

View 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

View 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

View Answer

Answer: c [Reason:] By definition.

8. What is output of the following set of code?

  1. static void Main(string[] args)
  2. {
  3.     Program p = new Program();
  4.     p.display(2, 3, 8);
  5.     int []a = { 2, 56, 78, 66 };
  6.     Console.WriteLine("example of array");
  7.     Console.WriteLine("elements added are");
  8.     p.display(a);
  9.     Console.ReadLine();
  10. }
  11. public void display(params int[] b)
  12. {
  13.     foreach (int i in b)
  14.     {
  15.         Console.WriteLine("ARRAY IS HAVING:{0}", i);
  16.     }
  17. }

a) Compile time error
b) Run time error
c) Code runs successfully but prints nothing
d) Code runs successfully and prints given on console

View 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};

View 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

View 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

View 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”)].

View 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

View 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

View 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

View 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

View 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

View 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

View Answer

Answer: a [Reason:] None.

9. Attributes could be applied to
a) Method
b) Class
c) Assembly
d) All of the mentioned

View Answer

Answer: d [Reason:] None.

10. The [Serializable()] attributes gets inspected at:
a) compile time
b) run time
c) design time
d) linking time

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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()

View 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()

View 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

View Answer

Answer: c [Reason:] None.

2. Choose the output for the following set of code.

  1.  static void Main(string[] args)
  2.  {
  3.      char c = 'g';
  4.      string s = c.ToString();
  5.      string s1 = "I am a human bein" + c;
  6.      Console.WriteLine(s1);
  7.      Console.ReadLine();
  8.  }

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

View 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.

  1. static void Main(string[] args)
  2. {
  3.     var days = "MTWTFSS";
  4.     var daysArray = days.ToCharArray().Cast<string>().ToArray();
  5.     for (var i = 0; i < daysArray.Length; i++)
  6.     {
  7.         switch (daysArray[i])
  8.         {
  9.         case "M":
  10.             daysArray[i] = "Monday";
  11.             break;
  12.         case "T":
  13.             daysArray[i] = "Tuesday";
  14.             break;
  15.         case "W":
  16.             daysArray[i] = "Wednesday";
  17.             break;
  18.         case "R":
  19.             daysArray[i] = "Thursday";
  20.             break;
  21.         case "F":
  22.             daysArray[i] = "Friday";
  23.             break;
  24.         case "S":
  25.             daysArray[i] = "Saturday";
  26.             break;
  27.         case "U":
  28.             daysArray[i] = "Sunday";
  29.             break;
  30.         }
  31.     }
  32.     daysArray[daysArray.Length - 1] = "and " + daysArray[daysArray.Length - 1];
  33.     Console.WriteLine(string.Join(", ", daysArray));
  34. }

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.

View 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 ?

  1. static void Main(string[] args)
  2. {
  3.     { 
  4.         var dayCode = "MTWFS";
  5.         var daysArray = new List<string>();
  6.         var list = new Dictionary<string, string>
  7.         { {"M", "Monday"}, {"T", "Tuesday"}, {"W", "Wednesday"},
  8.           {"R", "Thursday"}, {"F", "Friday"}, {"S", "Saturday"},
  9.           {"U", "Sunday"}
  10.         };
  11.        for (int i = 0,max = dayCode.Length; i < max; i++)
  12.        {
  13.            var tmp = dayCode[i].ToString();
  14.            if (list.ContainsKey(tmp))
  15.            {
  16.                daysArray.Add(list[tmp]);
  17.             }
  18.        }
  19.        Console.WriteLine(string.Join("n ", daysArray)); 
  20.  }

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

View 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

View 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()

View 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 ?

  1. static void Main(string[] args)
  2. {
  3.     string s1 = "Delhi";
  4.     string s2;
  5.     s2 = s1.Insert (6, "Jaipur");
  6.     Console.WriteLine(s2);
  7. }

a) DelhJaipuri
b) Delhi Jaipur
c) Delhi
d) DelhiJaipur

View 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))

View Answer

Answer : b

9. Select the output for given string ?

  1. static void Main(string[] args)
  2. {
  3.     string Str,  Revstr = " "; 
  4.     int Length;
  5.     Console.Write("Enter A String : ");
  6.     Str = Console.ReadLine();
  7.     Length = Str.Length - 1;
  8.     while (Length >= 0)
  9.     {
  10.         Revstr = Revstr + Str[Length];
  11.         Length --;
  12.     }
  13.     Console.WriteLine("Reverse  String  Is  {0}",  Revstr);
  14.     Console.ReadLine();
  15. }

Enter a String:BOMBAY.
a) BOMBA
b) YABMOB
c) BOMAYB
d) YABMO

View 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)

  1. static string ConvertstringToHex(string input, system.Text.Encoding encoding)
  2. {
  3.     string Bytes = encoding.GetBytes(input);
  4.     string Builder sbBytes = new Builder sbBytes();
  5.     for each (byte 'b' in StringBytes)
  6.     {
  7.         sBytes.AppendFormat("{0:x2}", b);
  8.     }
  9.     return sbBytes.Tostring();
  10. }

b)

  1. static string ConvertstringToHex(string input, system.Text.Encoding encoding)
  2. {
  3.     char[]string Bytes = encoding.GetBytes(input);
  4.     string Builder sbBytes = new Builder sbBytes(StringBytes.Length*2);
  5.     for each (byte 'b' in StringBytes)
  6.     {
  7.         sBytes.AppendFormat("{0:X2}", b);
  8.     }
  9.     return sbBytes.Tostring();
  10. }

c)

  1. public static string ConvertStringToHex(String input,  System.Text.Encoding encoding)
  2. {
  3.     {
  4.        Byte[] stringBytes = encoding.GetBytes(input);
  5.        StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
  6.        foreach (byte b in stringBytes)
  7.        {
  8.            sbBytes.AppendFormat("{0:X2}",  b);
  9.        }
  10.    Console.WriteLine(sbBytes.ToString());//sbBytes.ToString());
  11.    return sbBytes.ToString();
  12.     }
  13. }

d) None of the mentioned

View 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)

  1. public static string ConvertHexToString(String hexInput,  System.Text.Encoding encoding)
  2. {
  3.     char[] numberChars = hexInput.Length;
  4.     byte[] bytes = new byte[numberChars / 2];
  5.     for (int i = 0; i < numberChars; i += 2)
  6.     {
  7.         bytes[i / 2] = Convert.ToByte(hexInput.Substring(i,  0),  16);
  8.     }
  9.     return encoding.GetString(bytes);
  10. }

b)

  1. public static string ConvertHexToString(String hexInput,  System.Text.Encoding encoding)
  2. {
  3.     int numberChars = hexInput.Length;
  4.     byte[] bytes = new byte[numberChars / 2];
  5.     for (int i = 0; i < numberChars; i += 2)
  6.     {
  7.         bytes[i / 2] = Convert.ToByte(hexInput.Substring(i,  2),  16);
  8.     }
  9.     return encoding.GetString(bytes);
  10. }

c)

  1. public static string ConvertHexToString(String hexInput,  System.Text.Encoding encoding)
  2. {
  3.     string numberChars = hexInput.Length;
  4.     byte[] bytes = new byte[numberChars];
  5.     for (int i = 0; i &lt; numberChars; i += 2)
  6.     {
  7.         bytes[i / 2] = Convert.ToByte(hexInput.Substring(i,  2),  16);
  8.     }
  9.     return encoding.GetString(bytes);
  10. }

d) None of the mentioned

View 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 :

  1.    string s1 = " I AM BEST ";
  2.    string s2;
  3.    s2 = s1.substring (5, 4);
  4.    Console.WriteLine (s2);

a) AM BEST
b) I AM BES
c) BEST
d) I AM

View 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

View 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

View 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

View 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.

  1.  class sample
  2.  {
  3.      public int i;
  4.      public int[] arr = new int[10];
  5.      public void fun(int i, int val)
  6.      {
  7.          arr[i] = val;
  8.      }
  9.  }
  10.  class Program
  11.  {
  12.      static void Main(string[] args)
  13.      {
  14.          sample s = new sample();
  15.          s.i = 10;
  16.          sample.fun(1, 5);
  17.          s.fun(1, 5);
  18.          Console.ReadLine();
  19.      }
  20.  }

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

View 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

View Answer

Answer: b [Reason:] By definition.

3. The operator used to access member function of a class?
a) :
b) ::
c) .
d) #

View 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

View Answer

Answer: c [Reason:] General form of class declaration in C# is : class class_name { member variables variable1; variable2; variableN; method1(parameter_list) { method body } method2(parameter_list) { method body } methodN(parameter_list) { method body } }

5. Select the output for the following set of code :

  1.   class sample
  2.   {
  3.       public int i;
  4.       public int j;
  5.       public void fun(int i, int j)
  6.       {
  7.           this.i = i;
  8.           this.j = j;
  9.       }
  10.   }
  11.   class Program
  12.   {
  13.       static void Main(string[] args)
  14.       {
  15.           sample s = new sample();
  16.           s.i = 1;
  17.           s.j = 2;
  18.           s.fun(s.i, s.j);
  19.           Console.WriteLine(s.i + " " + s.j);
  20.           Console.ReadLine();
  21.       }
  22.   }

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

View 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

View 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

View Answer

Answer: d [Reason:] By definition.

8. Select the output for the following set of code :

  1.   class z
  2.   {
  3.       public int X;
  4.       public int Y;
  5.       public  const int c1 = 5;
  6.       public  const int c2 = c1 * 25;
  7.       public void set(int a, int b)
  8.       {
  9.           X = a;
  10.           Y = b;
  11.       }
  12.  
  13.   }
  14.   class Program
  15.   {
  16.       static void Main(string[] args)
  17.       {
  18.           z s = new z();
  19.           s.set(10, 20);
  20.           Console.WriteLine(s.X + " " + s.Y);
  21.           Console.WriteLine(z.c1 + " " + z.c2);
  22.           Console.ReadLine();
  23.       }
  24.    }

a) 10 20
5 25
b) 20 10
25 5
c) 10 20
5 125
d) 20 10
125 5

View 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();

View 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

View Answer

Answer: c [Reason:] None.

11. Select the output for the following set of code :

  1.   class z
  2.   {
  3.       public string name1;
  4.       public string address;
  5.       public void show()
  6.       {
  7.           Console.WriteLine("{0} is in city{1}", name1, " ", address);
  8.       }
  9.   }
  10.   class Program
  11.   {
  12.       static void Main(string[] args)
  13.       {
  14.           z n = new z();
  15.           n.name1 = "harsh";
  16.           n.address = "new delhi";
  17.           n.show();
  18.           Console.ReadLine();
  19.       }
  20.   }

a) Syntax error
b) {0} is in city{1} harsh new delhi
c) harsh is in new delhi
d) Run successfully prints nothing

View 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

View Answer

Answer: c [Reason:] None.

13. The output of code is ?

  1.    class test
  2.    {
  3.        public void print()
  4.        {
  5.            Console.WriteLine("Csharp:");
  6.        }
  7.    }
  8.    class Program
  9.    {
  10.        static void Main(string[] args)
  11.        {
  12.            test t;
  13.            t.print();
  14.            Console.ReadLine();
  15.        }
  16.    }

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

View Answer

Answer: c [Reason:] object of class test should be declared as test t = new test(); test t = new test(); t.print(); Console.ReadLine();