C# MCQ Number 00774

C# MCQ Set 1

1. What is the String in C# meant for?
a) Variable
b) Character Array
c) Object
d) Class

Answer

Answer: c [Reason:] C# Supports a predefined reference type known as string.When we declare a string using string type we are declaring the object to be of type “System.String”.

2. What does the term ‘immutable’ means in term of string objects?
a) We can modify characters included in the string
b) We cannot modify characters contained in the string
c) We cannot perform various operation of comparison,inserting,appending etc
d) None of the mentioned

Answer

Answer: b [Reason:] String objects are ‘immutable’ means we cannot modify the characters contained in string. Also operation on string produce a modified version of string rather then modifying characters of string.

3. To perform comparison operation on strings supported operations are :
a) Compare()
b) Equals()
c) Assignment ‘==’ operator
d) All of the mentioned

Answer

Answer: d [Reason:] None.

4. What will be output of the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      string s1 = "Hello I Love Csharp ";
  4.      Console.WriteLine(Convert.ToChar( (s1.IndexOf('I') - s1.IndexOf('l')) * s1.IndexOf('p'));
  5.      Console.ReadLine();
  6.  }

a) I
b) Hello I
c) Love
d) H

Answer

Answer: d [Reason:] ‘I’ = index position[6] ,’l’ = index position[2].So, I – l = 6-2= 4*(index position of p = 18) = 72. Character with ASCII Value 72 = ‘H’.
Output : H

5. Correct way to convert a string to uppercase using string class method()?
a) Upper()
b) ToUpper()
c) Object.ToUpper()
d) None of the mentioned

Answer

Answer: c [Reason:] string s1 = “Hello I Love Csharp “;
Console.WriteLine(s1.ToUpper());
Output: HELLO I LOVE CSHARP.

6. What would be the output for the following set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      Console.WriteLine (obj.Equals(obj2) + "  " + obj2.CompareTo(obj) );
  7.      Console.ReadLine();
  8.  }

a) True True
b) False False
c) True 0
d) False 1

Answer

Answer: c [Reason:] Equal() checks if two string objects ‘obj’ and ‘obj2’ are equal or not and hence returns true or false.Similarly, “CompareTo” operator check two objects and since string obj2 = obj,it returns bool value ‘0’. Hence,they are equal.
Output : True 0

7. What would be the output for the code below?

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      Console.WriteLine(obj + "  " + obj1);
  7.      string s = obj + "  " + obj1;
  8.      Console.WriteLine(s.Length);
  9.      Console.ReadLine();
  10.  }

a) hello world
10
b) hello world
6
c) hello world
11
d) hello world
5

Answer

Answer: c [Reason:] Length() method calculates number of characters in a string . ‘Obj2’ assumes the value of object ‘obj’ in itself.
Output: hello world
11

8. What is output for the following set of Code?

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      string s = obj+" "+obj1;
  7.      Console.WriteLine(s.IndexOf('r'));
  8.      Console.ReadLine();
  9.  }

a) 7
b) 8
c) 9
d) 10

Answer

Answer: b [Reason:] IndexOf() used to find absolute position of a character of substring.
Output: 8

9. What is output for the set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      string s = obj + "  " + obj1;
  7.      Console.WriteLine(s.Substring(6 ,5));
  8.      Console.ReadLine();
  9.  }

a) hello
b) orld
c) world
d) o world

Answer

Answer: c [Reason:] ‘Substring()’ extract substrings from a given string using overloaded substring() method provided by string class.
Output: world

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

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "worn";   
  5.      String obj2 = obj;
  6.      Console.WriteLine(obj + "  " + (obj1.Replace('w' ,'c')));
  7.      Console.ReadLine();
  8.  }

a) hello hello
b) hello worn
c) hello corn
d) hello

Answer

Answer: c [Reason:] Replace() method provided by string builder class is used to replace characters.
Output: hello corn

C# MCQ Set 2

1. Select output for the following set of code :

  1.   static void Main(string[] args)
  2.   {
  3.       int i;
  4.       Console.WriteLine("enter value of i:");
  5.       i = Convert.ToInt32(Console.ReadLine());
  6.       if (i < 7)
  7.       {
  8.           i++;
  9.           continue;
  10.       }
  11.       Console.WriteLine("final value of i:" +i);
  12.       Console.ReadLine();
  13.   }

a) 12
b) 11
c) Compile time error
d) 13

Answer

Answer: c [Reason:] ‘Continue’ loop cannot be used within ‘if’ loop .replace while with if(i <7).
Output: Compile time error.

2. Select the output for the following set of Code :

  1.  static void Main(string[] args)
  2.  {
  3.      int i;
  4.      Console.WriteLine("enter value of i:");
  5.      i = Convert.ToInt32(Console.ReadLine());
  6.      if ( i % 2 == 0)
  7.          goto even:
  8.      else
  9.      {
  10.          Console.WriteLine("number is odd:");
  11.          Console.ReadLine();
  12.      }
  13.      even:
  14.      Console.WriteLine("number is even:");
  15.      Console.ReadLine();
  16.  }
  17. for i = 4.

a) number is odd
b) number is even
c) Compile time error
d) none of the mentioned

Answer

Answer: c [Reason:] “Undefined label ‘even’ in main().The syntax ‘goto even:’ is incorrect instead use ‘goto even;’.
Output:

  1.     static void Main(string[] args)
  2.     {
  3.         int i;
  4.         Console.WriteLine("enter value of i:");
  5.         i = Convert.ToInt32(Console.ReadLine());
  6.         if (i % 2 == 0)
  7.         goto even;
  8.         else
  9.         {
  10.             Console.WriteLine("number is odd:");
  11.             Console.ReadLine();
  12.         }
  13.         even:
  14.         Console.WriteLine("number is even:");
  15.         Console.ReadLine();
  16.     }

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

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 1, j;
  4.       do
  5.       {
  6.           for (j = 1; ; j++)
  7.           {
  8.               if (j > 2)
  9.                   break;
  10.               if (i == j)
  11.                   continue;
  12.               Console.WriteLine(i + " " + j);
  13.           }
  14.           i++;
  15.       } while (i < 3);
  16.       Console.ReadLine();
  17.   }

a) 1 2
2 1
b) 2 1
1 2
c) 1 3
2 1
d) 1 1
2 1

Answer

Answer: a [Reason:] for i = 1.When control enters in loop first if condition is checked for where j = 1 and as (j > 2) which is false.Control is now passed to console statement with i = 1 and j = 2.Now, in while condition value of ‘i’ reflected is 2 i.e i = 2 as i++.Since, (i < 3) control again enters in for loop with i = 2 but j = 1 not j = 2 for j++ and hence,again same condition executes for console statement.
Output : 1 2
2 1

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

  1.    static void Main(string[] args)
  2.    {
  3.        int i = 10 , j = 0;
  4.        label:
  5.        i--;
  6.        if ( i > 0)
  7.        {
  8.            Console.WriteLine(i+ " ");
  9.            goto label;
  10.        }
  11.        Console.ReadLine();
  12.    }

a) 1 2 3 4 5 6 7 8 9 10
b) 10 9 8 7 6 5 4 3 2 1 0
c) 9 8 7 6 5 4 3 2 1
d) 10 9 8 7 6 5 4 3 2 1

Answer

Answer: c [Reason:] for i = 10,loop executes for first time in ‘if’ loop as (i>0) i.e (9 > 0) and hence printing ‘9’.Similarly,label condition executes again go for (i–) i.e (9-1=8) and hence again prints i = 8.In this way looping condition executes as 9 ,8 to 3, 2, 1.
OUTPUT :9 8 7 6 5 4 3 2 1.

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

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0, j = 0;
  4.       while (i < 2)
  5.       {
  6.           l1: i--;
  7.           while (j < 2)
  8.           {
  9.               Console.WriteLine("hin");
  10.               goto l1;
  11.           }
  12.       }
  13.       Console.ReadLine();
  14.   }

a) hi hi hi
b) hi hi
c) hi
d) hi hi hi…..infinite times

Answer

Answer: d [Reason:] Since,i– so,test condition for ‘i’ never satisfies it fails and hence infinite loop in occurs.
output: hi hi hi…..

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

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0;
  4.       if (i == 0)
  5.       {
  6.           goto label;
  7.       }
  8.       label: Console.WriteLine("HI...");
  9.       Console.ReadLine();
  10.   }

a) Hi…infinite times
b) Code runs prints nothing
c) Hi Hi
d) Hi…

Answer

Answer: d [Reason:] for i = 0 ,if condition is satisfied as (i == 0).So,label statement is printed.
Output : Hi

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

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0, j = 0;
  4.       l1: while (i < 2)
  5.       {  
  6.           i++;
  7.           while (j < 3)
  8.           {
  9.               Console.WriteLine("loopn");
  10.               goto l1;
  11.           }
  12.        }
  13.       Console.ReadLine();
  14.   }

a) loop is printed infinite times
b) loop
c) loop loop
d) Compile time error

Answer

Answer: c [Reason:] Since outer while loop i.e while(i<2) executes only for two times.Hence,loop while executing third time for (j<3) could not be able to satisfy condition i<2 as i = 2.hence,loop breaks and control goes out of loop.
Output : loop loop.

8. Select output for the following set of code :

  1.   static void Main(string[] args)
  2.   {
  3.       int i= 0,k;
  4.       label: Console.WriteLine(i);
  5.       if (i == 0)
  6.           goto label;
  7.       Console.ReadLine();
  8.   }

a) 0 0 0 0
b) 0 0 0
c) 0 infinite times
d) 0

Answer

Answer: c [Reason:] Since, if condition is always true.Loop will continue executing always without any end condition.
Output:0 0 0….

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

  1.    static void Main(string[] args)
  2.    {
  3.        int i = 0;
  4.        int j = 0;
  5.        for (i = 0; i < 4; i++)
  6.        {
  7.            for (j = 0; j < 3; j++)
  8.            {
  9.                if (i > 1)
  10.                    continue;
  11.                Console.WriteLine("Hi n");
  12.            }
  13.        }
  14.        Console.ReadLine();
  15.    }

a) Prints hi 4 times
b) Prints hi 3 times
c) Prints hi 6 times
d) Prints hi infinite times

Answer

Answer: c [Reason:] None.
Output : hi
hi
hi
hi
hi
hi.

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

  1.     static void Main(string[] args)
  2.     {
  3.         int a = 0;
  4.         int i = 0;
  5.         int b;
  6.         for (i = 0; i < 5; i++)
  7.         {
  8.              a++;
  9.              Console.WriteLine("Hello n");
  10.              continue;
  11.         }
  12.         Console.ReadLine();
  13.     }

a) print hello 4 times
b) print hello 3 times
c) print hello 5 times
d) print hello infinite times

Answer

Answer: c [Reason:] Condition executes until and unless i < 5.So,it prints “hello” until ‘i’ condition is satisfied.
Output : Hello
Hello
Hello
Hello
Hello

11. Select the output for the set of code:

  1.     static void Main(string[] args)
  2.     {
  3.         Console.WriteLine("HI");
  4.         continue;
  5.         Console.WriteLine("Hello");
  6.         Console.ReadLine();
  7.     }

a) Hi Hello
b) Hi
c) Hello
d) Compile time error

Answer

Answer: d [Reason:] Absence of any loop condition in order to make decision of break or continue.

C# MCQ Set 3

1. Which refrence modifier is used to define reference variable?
a) &
b) ref
c) #
d) $

Answer

Answer: b [Reason:] None.

2. Select the output for following set of code :

  1.    static void Main(string[] args)
  2.    {
  3.        int a = 5;
  4.        fun1 (ref a);
  5.        Console.WriteLine(a);
  6.        Console.ReadLine();
  7.     }
  8.     static void fun1(ref int a)
  9.     {
  10.         a = a * a;
  11.     }

a) 5
b) 0
c) 20
d) 25

Answer

Answer: d [Reason:] Here ‘a’ = 5 .Copy of variable is passed as reference to parameter ‘a’.
Output: 25.

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

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

a) 6 7 8 9 10
b) 15 17 8 8 20
c) 15 17 8 29 20
d) Syntax error while passing reference of array variable.

Answer

Answer: a [Reason:] array ‘arr’ after declaration is passed as reference parameter.
a[0] = 1 + 5 = 6.
a[1] = 2 + 5 = 7.
.
.
a[4] = 5 + 5 = 10.
Output : 15 17 8 29 20.

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

  1.   static void Main(string[] args)
  2.   {
  3.       int a = 10 , b = 20;
  4.       Console.WriteLine("Result before swap is: "+ a +" "+b);
  5.       swap(ref a, ref b);
  6.       Console.ReadLine();
  7.   }
  8.   static void swap(ref int i, ref int j)
  9.   {
  10.       int t;
  11.       t = i;
  12.       i = j;
  13.       j = t;
  14.       Console.WriteLine("Result after swap is:"+ i +" "+j);
  15.   }

a) Result before swap is: 20 10
Result after swap is: 20 10
b) Result before swap is: 10 20
Result after swap is:20 10
c) Result before swap is: 10 20
Result after swap is:10 20
d) Result before swap is: 20 10
Result after swap is:10 20

Answer

Answer: b [Reason:] Makes use of call by reference parameter.
Output:Result before swap is: 10 20.
Result after swap is:20 10.

5. Select output for the set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int []a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  4.      func(ref a);
  5.      Console.ReadLine();
  6.  }
  7.  static void func(ref int[] x)
  8.  {
  9.      Console.WriteLine(" numbers are:");
  10.      for (int i = 0; i < x.Length; i++)
  11.      {
  12.          if (x[i] % 2 == 0)
  13.          {
  14.              x[i] = x[i] + 1;
  15.              Console.WriteLine(x[i]);
  16.          }
  17.      }
  18.  }

a) numbers are : 2 4 6 8 10
b) numbers are : 3 5 7 9 11
c) numbers are : 2 3 4 5 6
d) None of the mentioned

Answer

Answer: b [Reason:] Those numbers divisible by 2 are 2,4,6,8,10 and when condition of loop is executed it increments by 1.
i.e for x[1] = 2%2 == 0.So, x[1] = 2 + 1 =3.
x[3] = 4%2 == 0.So, x[3] = 4 + 1 =5 and so on.
Output : 3 5 7 9 11.

6. Select the wrong statement about ‘ref’ keyword in C#?
a) References can be called recursively
b) The ‘ref’ keyword causes arguments to be passed by reference
c) When ‘ref’ are used, any changes made to parameters in method will be reflected in variable when control is passed back to calling method
d) All of above mentioned

Answer

Answer: a [Reason:] None.

7. Select correct differences between ‘=’ and ‘==’ in C#.
a) ‘==’ operator is used to assign values from one variable to another variable
‘=’ operator is used to compare value between two variables
b) ‘=’ operator is used to assign values from one variable to another variable
‘==’ operator is used to compare value between two variables
c) No difference between both operators
d) None of the mentioned

Answer

Answer: b [Reason:] None.

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

  1.   static void Main(string[] args)
  2.   {
  3.       int X = 0;
  4.       if (Convert.ToBoolean(X = 0))
  5.       Console.WriteLine("It is zero");
  6.       else 
  7.       Console.WriteLine("It is not zero");
  8.       Console.ReadLine();
  9.   }

a) It is zero
b) It is not zero
c) Infinite loop
d) None of the mentioned

Answer

Answer: b [Reason:] The operator ‘=’ used is not comparison operator it is assignment operator.Since value assigned to ‘X’ = 0.
So,’0′ value is stored in ‘X’ and with the help of if condition implementation it is converted to ‘false’ which directly means It is not zero but ‘1’ which means ‘true’.

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

  1.     static void Main(string[] args)
  2.     {
  3.        int X = 6,Y = 2;
  4.        X  *= X / Y;
  5.        Console.WriteLine(X);
  6.        Console.ReadLine();
  7.     }

a) 12
b) 6
c) 18
d) Compile time error

Answer

Answer: c [Reason:] X*=X/Y.
X=x*(X/Y).
Output: 18.

10. Select the ouput for the following set of code :

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

a) 4 2
b) 0 4
c) 4 0
d) None of mentioned

Answer

Answer: c [Reason:] x = x – b and b = b/(x*b).
Output: 4 0

11. What is output for the following set of expression?
int a+= (float) b/= (long)c.
a) float
b) int
c) long
d) None of the mentioned

Answer

Answer: b [Reason:] None.

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

  1.    static void Main(string[] args)
  2.    {
  3.        int x = 8;
  4.        int b = 16;
  5.        int C = 64;
  6.        x /= b /= C;
  7.        Console.WriteLine(x + " " + b+ " " +C);
  8.        Console.ReadLine();
  9.    }

a) 8 2 32
b) 32 4 8
c) 32 2 8
d) Compile time error

Answer

Answer: d [Reason:] Exception handling error of dividing by zero.

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

  1.    static void Main(string[] args)
  2.    {
  3.        int x = 8;
  4.        int b = 16;
  5.        int C = 64;
  6.        x /= b /= C;
  7.        Console.WriteLine(x + " " + b+ " " +C);
  8.        Console.ReadLine();
  9.    }

a) 8 2 32
b) 32 4 8
c) 32 2 8
d) Compile time error

Answer

Answer: c [Reason:] x / = b / =C is x = x * c /b.
Output: 32 2 8.

C# MCQ Set 4

1. Which of the following keyword is used to overload user defined types by defining static member functions?
a) op
b) opoverload
c) operator
d) operatoroverload

Answer

Answer: c [Reason:] None.

2. Which of following statements are correct in nature?
a) The conditional logical operators cannot be overloaded
b) The array indexing operator can be overloaded
c) A public or nested public preference type does not overload the equality operator
d) None of the mentioned

Answer

Answer: a [Reason:] None.

3. Arrange the following overloaded operators in increasing order of precedence?
%, <<, &, /, +
a) ‘%’ < ‘<<‘ < ‘+’ < ‘-‘ < ‘&’ < ‘/’
b) ‘<<‘ < ‘&’ < ‘%’ < ‘-‘ < ‘/’ < ‘+’
c) ‘&’ < ‘-‘ <‘%’ < ‘<<‘ < ‘/’ < ‘+’
d) ‘/’ < ‘-‘ < ‘%’ < ‘+’ < ‘<<‘ < ‘&’

Answer

Answer: b [Reason:] None.

4. Operators that can be overloaded are?
a) ||
b) ‘+=’
c) +
d) [].

Answer

Answer: c [Reason:] None.

5. Which statements are correct about operator overloading?
a) Mathematical or physical modeling where we use classes to represent objects such as vectors,matrices,complex-numbers etc
b) Graphical programs where coordinate related objects are used to represent positions on the screen
c) Financial programs where a class represents an amount of money
d) All of the mentioned

Answer

Answer: d [Reason:] None.

6. Correct way to define operator method or to perform operator overloading is?

  1. a) public static op(arglist)
  2.    {
  3.  
  4.    }
  5. b) public static retval op(arglist)
  6.    {
  7.  
  8.    }
  9. c) public static retval operator op(arglist)
  10.    {
  11.  
  12.    }
  13. d) All of the mentioned
Answer

Answer: c [Reason:] None.

7. Correct method to define + operator is?
a) public sample operator +(int a, int b)
b) public abstract operator +(int a, int b)
c) public static sample operator +(int a, int b)
d) public abstract sample operator +(int a, int b)

Answer

Answer: c [Reason:] None.

8. Choose the correct statement among the below mentioned statements:
a) Forgetting to declare an operator method as public
b) Forgetting to declare an operator method as static
c) Forgetting to return a bool type value while overloading a relational operator
d) All of the mentioned

Answer

Answer: d [Reason:] None

9. What is vector in operator overloading?
a) class
b) method()
c) data type
d) none of the mentioned

Answer

Answer: c [Reason:] It is a data type of class . It is defined as : public static Vector operator + (Vector a, Vector b).

10. Choose the wrong statement from the given set of statements?
a) All operators in C#.NET cannot be overloaded
b) We can use the new modifier to modify a nested type if the nested type is hiding another type
c) Operator overloading permits the use of symbols to represent computations for a type
d) In case of operator overloading all parameters must be of different type than the class or struct that declares the operators

Answer

Answer: d [Reason:] None.

C# MCQ Set 5

1. What is output for the following code snippet?

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int i = 5;
  6.          int j;
  7.          method1(ref i);
  8.          method2(out j);
  9.          Console.writeline(i + "  " + j);
  10.      }
  11.      static void method1(ref int x)
  12.      { 
  13.          x = x + x;
  14.      }
  15.      static void method2(out int x)
  16.      {
  17.          x = 6;
  18.          x = x * x;
  19.      }
  20.  }

a) 36, 10
b) 10, 36
c) 0, 0
d) 36, 0

Answer

Answer: b [Reason:] Variable ‘i’ is passed as reference parameter declared with ‘ref’ modifier and variable ‘j’ is passed as a output parameter declared with ‘out’ keyword .Reference parameter used to pass value by reference is the same with out parameter.
Output : 10, 36

2. Statements about ‘ref’ keyword used in C#.NET are?
a) The ref keyword causes arguments to be passed by reference
b) While using ‘ref’ keyword any changes made to the parameter in the method will be reflected in the variable when control is passed back to the calling method
c) Ref usage eliminates overhead of copying large data items
d) All of the mentioned

Answer

Answer: d [Reason:] None.

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

  1.  static void main(string[] args)
  2.  {
  3.      int n = 1;
  4.      method(n);
  5.      console.Writeline(n);
  6.      method1(ref n);
  7.      console.Writeline(n);
  8.  }
  9.  static void method(int num)
  10.  {
  11.      num += 20;
  12.      console.writeline(num);
  13.  }
  14.  static void method1(ref int num)
  15.  {
  16.      num += 20;
  17.      console.writeline(num);
  18.  }

a) 1
1
1
1
b) 21
1
21
21
c) 11
21
21
11
d) 21
1
21
21

Answer

Answer: d [Reason:] None.
Output :21 1 21 21

4. Which method does following set of code explains?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 10, b = 20;
  4.      method(ref a,  ref b);
  5.      console.writeline(a + "  " + b);
  6.  }
  7.  static void swap(ref int i,  ref int j)
  8.  {  
  9.      int t;
  10.      t = i;
  11.      i = j;
  12.      j = t;
  13.  }

a) Call by reference
b) Call by value
c) Output parameter
d) parameter arrays

Answer

Answer: a [Reason:] The following set of code explains swapping of numbers by reference parameters which makes usage of call by reference process.

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

  1.  static void main(string[] args)
  2.  {
  3.      int []arr = new int[]{ 1, 2, 3, 4, 5};
  4.      fun (ref arr);
  5.      for (int i = 0; i < arr.Length ; i++)
  6.      Console.WriteLine( arr[i] + "  ");
  7.  }
  8.  static void fun(ref int[]a)
  9.  {
  10.      a = new int[6];
  11.      a[3] = 32;
  12.      a[1] = 24;
  13.  }

a) 0, 0, 32, 0, 0, 0
b) 0, 24, 0, 32, 0, 0
c) 24, 0, 32, 0, 0, 0
d) 0, 0, 32, 0, 0, 0

Answer

Answer: b [Reason:] index positions which are assigned the new values are passed as a reference parameter and hence rest positions are filled with zero values.
Output : 0 24 0 32 0 0

6. What will be the output of given set of code?

  1.  static void main(string[] args)
  2.  {
  3.      int i;
  4.      int res = fun (out i);
  5.      console.writeline(res);
  6.      console.readline();
  7.  }
  8.  static int fun(out int i)
  9.  {
  10.      int s = 1;
  11.      i = 7;
  12.      for (int j = 1; j <= i; j++ )
  13.      s = s * j;
  14.      return s;
  15.  }

a) 4490
b) 5040
c) 5400
d) 3500

Answer

Answer: b [Reason:] None.
Output: 5040

7. What will be the output of the given set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 5;
  4.      int b = 0, c = 0;
  5.      method (a,  ref b, ref c);
  6.      Console.WriteLine(b + "  " + c);
  7.      Console.ReadLine();
  8.  }
  9.  static int method(int x, int p, ref int k)
  10.  {
  11.      p = x + x * x;
  12.      k = x * x + p;
  13.      return 0;
  14.  }

a) 30, 55
b) 55, 30
c) Compile time error
d) 0, 0

Answer

Answer: c [Reason:] Error occurrence as mismatch in parameter of method() definition.Keyword ‘ref’ should be used with parameter ‘p’ as ref int p.

8. Keyword used to define call by reference parameter in C# .NET?
a) &
b) out
c) ref
d) &&

Answer

Answer: c [Reason:] By definition.

9. Select the correct match of parameter declaration:

  1.  static Void main(string[] args)
  2.  {
  3.      int a = 5;
  4.      int b = 6;
  5.      float c = 7.2f;
  6.      math (ref a, ref b, ref c);
  7.      Console.WriteLine(a + "  " + b + "  " + c);
  8.  }
  9.  static int math(/*add parameter decelaration   */)
  10.  {
  11.      a += b;
  12.      b *= (int)c;
  13.      c += a * b;
  14.      return 0;
  15.  }

a) ref int a, int b, ref float c
b) ref int a, ref float c, ref int b
c) ref int a, ref int b, float c
d) ref int a, ref int b, ref float c

Answer

Answer: d [Reason:] static Void main(string[] args)
{
int a = 5;
int b = 6;
float c = 7.2f;
math(ref a, ref b, ref c);
console.writeLine(a + ” ” + b + ” ” + c);
}

10. Which statement is/are correct?
a) An argument passed to a ref parameter need not to be initialized first
b) Variables passed as out arguments need to be initialized prior to being passed
c) To use a ref parameter, only the calling method must explicitly use the ref keyword
d) None of the mentioned

Answer

Answer: d [Reason:] None.

C# MCQ Set 6

1. Select the class which is the base class for all arrays in C#?
a) Array
b) Text
c) arrays
d) Both Array & Text

Answer

Answer: a [Reason:] None.

2. Select the interfaces implemented by array class:
a) ICloneable, ICollection
b) IEnumerable, IStructuralComparable, IStructuralEquatable
c) ICloneable, ICollection, IList
d) Only b & c

Answer

Answer: d [Reason:] None.

3. Choose the correct statement about the IComparer interface in C#:
a) The IComparer interface is in System.Collections
b) It defines a method called Compare(), which compares the values of two objects
c) Both a & b
d) None of the mentioned

Answer

Answer: c [Reason:] The IComparer interface is in System.Collections. It defines a method called Compare(), which compares the values of two objects. It is shown here: int Compare(object x, object y) .It returns greater than zero if x is greater than y, less than zero if x is less than y, and zero if the two values are equal.

4. Choose the correct statement about the IComparer interface in C#:
a) The IComparer is in System.Collections.Generic
b) It defines a generic form of Compare()
c) Only a
d) Both a & b

Answer

Answer: d [Reason:] IComparer is in System.Collections.Generic. It defines a generic form of Compare(),which is shown here:
int Compare(T x, T y).It works the same as its non-generic relative: returning greater than zero if x is greater than
y, less than zero if x is less than y, and zero if the two values are equal.

5. What does the following property defined in the array class defines in C#?

  1.  public bool IsReadOnly { get; }

a) a property is read only by nature
b) property is true if the array object is read only and false otherwise
c) value is false for arrays
d) all of the mentioned

Answer

Answer: d [Reason:] A read-only property that is true if the Array object is read-only and false if it is not. This value is false for arrays.

6. What does the following property define in C#?

  1. public static int BinarySearch<T>(T[] array, int index, int length, T value)

a) Searches a portion of the array specified by array for the value specified by value
b) The search begins at the index specified by index and is restricted to length elements. Returns the index of the first match.
c) If value is not found, returns a zero value
d) All of the mentioned

Answer

Answer: b [Reason:] Searches a portion of the array specified by array for the value specified by value. The search begins at the index specified by index and is restricted to length elements. Returns the index of the first match. If the value is not found, returns a negative value. The array should be sorted and one-dimensional.

7. What will be the output of given code snippet?

  1.  static void Main(string[] args)
  2.  {
  3.      int[] nums = { 5, 4, 6, 3, 14, 9, 8, 17, 1, 24, -1, 0 };
  4.      Array.Sort(nums);
  5.      int idx = Array.BinarySearch(nums, 14);
  6.      if (idx == 9)
  7.      {
  8.          Console.WriteLine(Convert.ToBoolean(1));
  9.      }
  10.      else
  11.     {
  12.         Console.WriteLine(Convert.ToBoolean(0));
  13.     }
  14.     Console.WriteLine("Index of 14 is " + idx);
  15.     Console.ReadLine();
  16. }

a) True
0
b) Run time error
c) True
9
d) None of the mentioned

Answer

Answer: c [Reason:] Using Built in method Sort() , first array is sorted then index of number ’14’ is searched using array class built in method Array.BinarySearch(nums, 14) and hence at last if loop is used to make comparison of index position with random position ‘9’ chosen here.
Output:True
9

8. What will be the output of the given code snippet?

  1.   static void Main(string[] args)
  2.   {
  3.       string[] strings = {"beta", "alpha", "gamma"};
  4.       Console.WriteLine("Array elements: ");
  5.       DisplayArray(strings);
  6.       Array.Reverse(strings); 
  7.       Console.WriteLine("Array elements now: ");
  8.       DisplayArray(strings);
  9.       Console.ReadLine();
  10.    }
  11.    public static void DisplayArray(Array array)
  12.    {
  13.        foreach (object o in array)
  14.        {
  15.            Console.Write("{0} ", o);
  16.        }
  17.            Console.WriteLine();
  18.    }

a) Array elements:
beta alpha gamma
Array elements now:
ammag ahpla ateb
b) Array elements:
beta alpha gamma
Array elements now:
gamma beta alpha
c) Array elements:
beta alpha gamma
Array elements now:
gamma alpha beta
d) None of the mentioned

Answer

Answer: c [Reason:] ‘Reverse()’ a built in method to reverse an array of string defined in array class is used.
Output:Array elements:
beta alpha gamma
Array elements now:
gamma alpha beta

9. Which among the following is the wrong way to define and initialize an array of 4 integers?
a) int[] a = {25, 30, 40, 5}
b) int[] a;
a = new int[3]
a[0] = 25
a[1] = 30
a[2] = 40
a[3] = 5
c) int[] a
a = new int[4]{25, 30, 40, 5}
d) int[] a
a = new int[4]
a[0] = 25
a[1] = 30
a[2] = 40
a[3] = 5

Answer

Answer: b [Reason:] None.

10. Which method will be used to copy content from one array to another array?
a) Copy()
b) copy()
c) Both Copy() & copy()
d) None of the mentioned

Answer

Answer: a [Reason:] Copy() is a built in method of array class used to copy the elements from one array to another array

11. What will be the output of given code snippet?

  1.  static void Main() 
  2.  {
  3.      int[] nums = { 1, 2, 3, 4, 5 };
  4.      Console.Write("Original order: ");
  5.      foreach(int i in nums)
  6.      Console.Write(i + " ");
  7.      Array.Reverse(nums);
  8.      Console.Write("Reversed order: ");
  9.      foreach(int i in nums)
  10.      Console.Write(i + " ");
  11.      Console.WriteLine();
  12.  }

a) Run time error
b) 5, 4, 3, 2, 1
c) Compile time error
d) None of the mentioned

Answer

Answer: b [Reason:] Reverse built in method() of array class is used to reverse the given array.
Output:5, 4, 3, 2, 1

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.