C# MCQ Number 00770

C# MCQ Set 1

1. What will be the output of the given code?

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          int m = 10;
  6.          int *mptr = &m;
  7.          int **ptr = &mptr;
  8.          int n = 20;
  9.          int *nptr = &n;
  10.          int **prt = &nptr;
  11.          m = **prt + *nptr;
  12.          n = *mptr* **prt;
  13.          Console.WriteLine(n + " " + m);
  14.          Console.ReadLine();
  15.      }
  16.  }

a) 20 200
b) 40 200
c) 800 40
d) 40 800

Answer

Answer: c [Reason:] None.
Output: 800 40

2. What will be the output of the code snippet?

  1.  unsafe static void Main()
  2.  {
  3.      int a = 5;
  4.      int b = 5;
  5.      int c = 5;
  6.      int*[] ptr = new int* [3];
  7.      ptr[0] = &a;
  8.      ptr[1] = &b;
  9.      ptr[2] = &c;
  10.      for (a = 0; a < 3; a++)
  11.      {
  12.          c += *ptr[a];
  13.          Console.WriteLine(c);
  14.      }
  15.      Console.ReadLine();
  16.  }

a) 5 10
b) 10 20
c) Compile time error
d) 5 10 20

Answer

Answer: d [Reason:] None.
Output:5 10 20

3. What will be the output of the code snippet?

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int* ptrs = stackalloc int[3];
  6.         ptrs[0] = 1;
  7.         ptrs[1] = 2;
  8.         ptrs[2] = 3;
  9.         for (int i = 2; i >= 0; --i)
  10.         {
  11.             ptrs[i] = ptrs[i]* 3;
  12.             ptrs[i] = ptrs[i] + 4;
  13.             Console.WriteLine(ptrs[i]);
  14.         }
  15.         Console.ReadLine();
  16.     }
  17. }

a) 20, 10, 7
b) 13, 10, 7
c) 6, 9, 3
d) Compile time error

Answer

Answer: b [Reason:] None.
Output: 13, 10, 7

4. Among the given pointers which of following cannot be incremented?
a) int
b) char
c) float
d) void

Answer

Answer: d [Reason:] None.

5. A structure pointer points to __________
a) first member of structure
b) first two members of structure
c) whole structure
d) only to the last member of structure

Answer

Answer: c [Reason:] None.

6. What will be the declaration of the variable ptr as the pointer to array of 6 floats?
a) float *ptr[6].
b) float [6]*ptr
c) float(*ptr)[6].
d) float(*ptr)(6).

Answer

Answer: c [Reason:] None.

7. what will be the output of code snippet?

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          char[] arr = { 'A', 'B', 'C', 'D', 'E' };
  6.          fixed (char* P = arr)
  7.          {
  8.              int i;
  9.              for (i = 0 ;i < 5 ;i++)
  10.              if (*P % 2 == 0)
  11.              ++*P;
  12.              else
  13.              (*P)++;
  14.              Console.WriteLine(arr);
  15.          }
  16.          Console.ReadLine();
  17.      }
  18.  }

a) ACCEE
b) FBCDE
c) BBDDF
d) BBCEE

Answer

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

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

  1. class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      { 
  5.          int[] nums = new int[10];
  6.          Console.WriteLine("Index pointer like array.");
  7.          fixed (int* p = nums)
  8.          {
  9.              for (int i = 0 ;i <10 ;i++)
  10.              p[i] = i;
  11.              for (int i = 10 ;i >0 ;i--)
  12.              Console.WriteLine("p[{0}]: {1} ", i, p[i]);
  13.              Console.ReadLine();
  14.          }
  15.      }
  16.  }

a) p[10] :0, p[9] :9, p[8] :8…..p[1]:1
b) p[10] : 1, p[9] :2, p[8] :3…..p[1] :0
c) p[1] : 1, p[2] :2, p[3] :3…..p[10] :0
d) Compile time error

Answer

Answer: a [Reason:] None.
Output:Index pointer like array:
p[10] :0, p[9] :9, p[8] :8…p[1]:1

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

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          string str = "this is a test";
  6.  
  7.              fixed (char* p = str)
  8.             {
  9.                 for (int i = str.Length-1 ;p[i] != 0 ;i--)
  10.                 Console.Write(p[i]);
  11.             }
  12.         Console.ReadLine();
  13.     }
  14.  }

a) test a is this
b) compile time error
c) tset a si siht
d) run time error

Answer

Answer: c [Reason:] Reversal of string using pointers.
Output:tset a si siht

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

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          int* p ;
  6.          int ch = 13*5;
  7.          p = &(ch);
  8.          Console.WriteLine(Convert.ToChar(*p));
  9.          if (*p == 'A')
  10.          Console.WriteLine(Convert.ToBoolean(1));
  11.          else
  12.          Console.WriteLine(Convert.ToBoolean(0));
  13.          Console.ReadLine();
  14.      }
  15.  }

a) 65 False
b) 65 1
c) A True
d) A 1

Answer

Answer: c [Reason:] Convert.Tochar(*p) = A
Convert.ToBoolean(1) = True
Output: A
True

C# MCQ Set 2

1. Which among the given classes provides types of rounding functions?
a) Math
b) Process
c) System
d) Object

Answer

Answer: a [Reason:] None.

2. Which of these methods is a rounding function of Math class?
a) Max()
b) Min()
c) Abs()
d) Round()

Answer

Answer: d [Reason:] Round() rounds up a variable to nearest integer

3. Which of these classes contains only floating point functions?
a) Math
b) Process
c) System
d) Object

Answer

Answer: a [Reason:] Math class contains all the floating point functions that are used for general purpose mathematics methods. Example : sin(), cos(), exp(), sqrt() etc.

4. Which of these method returns a smallest whole number greater than or equal to variable X?
a) double Ciel(double X)
b) double Floor(double X)
c) double Max(double X)
d) double Min(double X)

Answer

Answer: a [Reason:] Ciel(double X) returns the smallest whole number greater than or equal to variable X.

5. Which of these methods return a largest whole number less than or equal to variable X?
a) double Ciel(double X)
b) double Floor(double X)
c) double Max(double X)
d) double Min(double X)

Answer

Answer: b [Reason:] double Floor(double X) returns a largest whole number less than or equal to variable X.

6. Which of the following functions return absolute value of a variable?
a) Abs()
b) Absolute()
c) absolutevariable()
d) None of the mentioned

Answer

Answer: a [Reason:] Abs() returns the absolute value of a variable

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

  1.  public class A
  2.  {
  3.      public int x;
  4.      public int y;
  5.      public void display() 
  6.      {
  7.          Console.WriteLine(x + " " + y);
  8.      }
  9.  }
  10.  class Program
  11.  {
  12.      static void Main(string[] args)
  13.      {
  14.          A obj1 = new A();
  15.          A obj2 = new A();
  16.          obj1.x = 1;
  17.          obj1.y = 2;
  18.          obj2 = obj1;
  19.          obj1.display();
  20.          obj2.display();
  21.      }
  22.  }

a) 1 2 0 0
b) 1 2 1 2
c) 0 0 0 0
d) Run time exception

Answer

Answer: b [Reason:] None.
Output: 1 2 1 2

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         double x = 3.14;  
  6.         int y = (int) Math.Abs(x);
  7.         Console.WriteLine(y);
  8.     }
  9. }

a) 0
b) 3
c) 3.0
d) 3.1

Answer

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

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         double x = 3.14;  
  6.         int y = (int) Math.Ceiling(x);
  7.         Console.WriteLine(y);
  8.     }
  9. }

a) 0
b) 3
c) 3.0
d) 4

Answer

Answer: d [Reason:] Ceiling(double x) returns the smallest whole number greater than or equal to variable x.
Output: 4

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         double x = 3.14;  
  6.         int y = (int) Math.Floor(x);
  7.         Console.WriteLine(y);
  8.     }
  9. }

a) 0
b) 3
c) 3.0
d) 4

Answer

Answer: b [Reason:] double Floor(double X) returns the largest whole number less than or equal to variable X. Here, the smallest whole number less than 3.14 is 3.
Output: 3

C# MCQ Set 3

1. Which mechanism among the following helps in identifying a type during the execution of a program?
a) Reflection
b) Runtime type ID
c) Both Reflection & Runtime type ID
d) None of the mentioned

Answer

Answer: b [Reason:] Runtime type ID is the mechanism that lets identify a type during the execution of a program. Using Runtime type ID we can construct and use objects at runtime.

2. Select the statement which are correct about RTTI(Runtime type identification):
a) It allows the type of an object to be determined during program execution
b) It tells what type of object is being referred to by a base class reference determined by RTTI
c) Helps in prevention of an invalid cast exception in advance
d) All of the mentioned

Answer

Answer: d [Reason:] Runtime type identification (RTTI) allows the type of an object to be determined during program execution. RTTI is useful for many reasons .For example, we can discover precisely what type of object is being referred to by a base-class reference. Another use of RTTI is to test in advance whether a cast will succeed, preventing an invalid cast exception.

3. Select the Keyword which supports the run time type identification:
a) is, as
b) as, typeof
c) Both a & b
d) Only a

Answer

Answer: c [Reason:] None.

4. What does the following code signify?
expr is type
a) Determines the type of an object
b) a simple deceleration
c) Both a & b
d) None of the mentioned

Answer

Answer: a [Reason:] The given expression determines the type of an object using the ‘is’ operator .Here, expr is an expression that describes an object whose type is being tested against type. If the type of expr is the same as, or compatible with, type, then the outcome of this operation is true. Otherwise, it is false

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

  1. class B { }
  2. class A : B { }
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         A a = new A();
  8.         B b = new B();
  9.         if (a is A) 
  10.         Console.WriteLine("a is an A");
  11.         if (b is A)
  12.         Console.WriteLine("b is an A because it is derived from A");
  13.         if (a is B)
  14.         Console.WriteLine("This won’t display -- a not derived from B");
  15.         Console.ReadLine();
  16.     }
  17. }

a) a is an A
This won’t display — a not derived from B
b) a is an A
b is an A because it is derived from A
c) b is an A because it is derived from A
This won’t display — a not derived from B
d) Both a & c

Answer

Answer: a [Reason:] We have to include the line ‘This won’t display — a not derived from B’ this is because ‘a’ is object of class ‘A’ which itself is derived from class ‘B’. So, ‘a’ is a B
Output: a is an A
This won’t display — a not derived from B

6. Which operator among the following supports the operation of conversion at runtime without generating the exceptions?
a) is
b) as
c) typeof
d) all of the mentioned

Answer

Answer: b [Reason:] By definition.

7. Which operator among the following is used to perform the operation of boxing, unboxing, reference and identity conversions?
a) is
b) as
c) typeof
d) all of the mentioned

Answer

Answer: b [Reason:] use the as operator, which has this general form:
expr as type
Here, expr is the expression being converted to type. If the conversion succeeds, then a reference to type is returned. Otherwise, a null reference is returned. The as operator can be used to perform only reference, boxing, unboxing, or identity conversions.

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

  1. class A {}
  2. class B : A {}
  3. class CheckCast 
  4. {
  5.     static void Main() 
  6.     {
  7.         A a = new A();
  8.         B b = new B();
  9.         b = a as B;
  10.         b = null;
  11.         if(b==null)
  12.         Console.WriteLine("The cast in b = (B) a is NOT allowed.");
  13.         else
  14.         Console.WriteLine("The cast in b = (B) a is allowed");
  15.     }
  16. }

a) Run time error
b) The cast in b = (B) a is NOT allowed
c) The cast in b = (B) a is allowed
d) Compile time error

Answer

Answer: b [Reason:] since a is not a B, the cast of a to B is invalid and is prevented by the if statement.
Output: The cast in b = (B) a is NOT allowed

9. Which operator among the following supplies the information about characteristics of a typeof?
a) is
b) as
c) typeof
d) none of the mentioned

Answer

Answer: c [Reason:] C# supplies the typeof operator. It retrieves a System.Type object for a given type. Using this object, we can determine the type’s characteristics.

10. What will be the output of the following code snippet?

  1. class UseTypeof 
  2. {
  3.     static void Main() 
  4.     {
  5.         Type t = typeof(StreamReader);
  6.         Console.WriteLine(t.FullName);
  7.         if(t.IsClass) Console.WriteLine("Is a class.");
  8.         if(t.IsAbstract) Console.WriteLine("Is abstract.");
  9.         else Console.WriteLine("Is concrete.");
  10.     }
  11. }

a) Is a class
Is abstract
b) Is abstract
c) System.IO.StreamReader
Is a class
Is concrete
d) Both a & c

Answer

Answer: c [Reason:] This program obtains a Type object that describes StreamReader. It then displays the fullname, and determines if it is a class and whether it is abstract.
Output: System.IO.StreamReader
Is a class
Is concrete

C# MCQ Set 4

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

  1.  static void Main(string[] args)
  2.  {
  3.      string s1 = "olleH";
  4.      string s2 = "olleh";
  5.      if (s1 == s2)
  6.      Console.WriteLine("Equal");
  7.      else
  8.      Console.WriteLine("Unequal");
  9.      if (s1.Equals(s2))
  10.      Console.WriteLine("Equal");
  11.      else
  12.      Console.WriteLine("Unequal");
  13.      Console.ReadLine();
  14.  }

a) Equal
Unequal
b) Unequal
Equal
c) Equal
Equal
d) Unequal
Unequal

Answer

Answer: d [Reason:] In the first comparison it is being checked if two strings are equal or not, but in the second comparison it is checked if two string references are equal or not. Also the length of the string and characters match is tested for the equality of strings.
Output : Unequal
Unequal

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

  1. static void Main(string[] args)
  2. {
  3.     string s1 = " Ixg";
  4.     string s2 = s1.Insert(3,"i");
  5.     string s3 = s2.Insert(5, "o");
  6.     for (int i = 0; i < s3.Length; i++)
  7.     Console.WriteLine(s3[i]);
  8.     Console.ReadLine();
  9. }

a) Ixgo
b) Ixig
c) Ixigo
d) Ixg

Answer

Answer: c [Reason:] Insert() the built in method inserts characters at specified position mentioned with index positions.
Output :Ixigo

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          char []chars = {'a', 'b', 'c'};
  6.          String s = new String(chars);
  7.          Console.WriteLine(s);
  8.          Console.ReadLine();
  9.      }
  10.  }

a) a
b) b
c) c
d) abc

Answer

Answer: d [Reason:] String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars,So s contains “abc”.
Output :abc

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          char []chars = {'a', 'b', 'c'};
  6.          String s = new String(chars);
  7.          String s1 = "abcd";
  8.          int len1 = s1.Length;
  9.          int len2 = s.Length;
  10.          Console.WriteLine(len1 + " " + len2);
  11.          Console.ReadLine();
  12.      }
  13.  }

a) 4 0
b) 3 0
c) 3 4
d) 4 3

Answer

Answer: d [Reason:] None.
Output : 4 3

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

  1.  class A
  2.  {
  3.      int i;
  4.      int j;
  5.      public A()
  6.      {
  7.          i = 1;
  8.          j = 2;
  9.      }
  10.  }
  11.  class Program
  12.  {
  13.      static void Main(string[] args)
  14.      {
  15.          A obj1 = new A();
  16. 	 Console.WriteLine(obj1.ToString());
  17.          Console.ReadLine();
  18.      }
  19.  }

a) True
b) False
c) String associated with obj1
d) Compile time error

Answer

Answer: c [Reason:] ToString() is the method of class Object, since it is the superclass of every class, every object has this method. ToString() returns the string associated with the calling object.
Output : ConsoleApplication19.A

6. Which of these constructors is used to create an empty String object?
a) String()
b) String(void)
c) String(0)
d) None of the mentioned

Answer

Answer: a [Reason:] None.

7. Which of these method of class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) Lengthof()
d) Length()

Answer

Answer: d [Reason:] Method Length() of string class is used to get the length of the object which invoked the method Length().

8. Choose the base class for string() method :
a) System.Array
b) System.char
c) System.String
d) None of the mentioned

Answer

Answer: c [Reason:] String is an alias for the predefined “System.string” class from which most of the string() methods are derived.

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          String c = "Hello i love Csharp";
  6.          Boolean var;
  7. 	 var = c.StartsWith("hello");
  8.          Console.WriteLine(var);
  9.          Console.ReadLine();
  10.      }
  11.  }

a) True
b) False
c) 1
d) Run time error

Answer

Answer: b [Reason:] StartsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.

10. What is the value returned by the function CompareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned

Answer

Answer: b [Reason:] CompareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         String s1 = "Hello i love Csharp";
  6.         StringBuilder s2 = new  StringBuilder(s1);
  7.         Console.WriteLine(s1.Equals(s2));
  8.         Console.ReadLine();
  9.     }
  10. }

a) True
b) False
c) 0
d) Compile time error

Answer

Answer: b [Reason:] Equals() compares the content of two strings. StringBuilder class supports many methods which are useful for manipulating dynamic strings.
Output :False

12. Which of these methods of class String is used to check whether a given string starts with a particular substring or not?
a) StartsWith()
b) EndsWith()
c) Starts()
d) Ends()

Answer

Answer: a [Reason:] The StartsWith() determines whether a substring exists at the beginning of the string.

C# MCQ Set 5

1. What are strings in C#?
a) a sequence of characters
b) array of characters
c) objects of built in data type
d) a reference type

Answer

Answer: c [Reason:] Generally, a string is defined as a sequence of characters but it is different in C#. In c++, string is an array of characters.In case of C#, strings are objects of the built-in string data type. Thus, string is a reference type.

2. Select the namespace in which string class is built?
a) System.Text
b) System.Net
c) System.IO
d) None of the mentioned

Answer

Answer: a [Reason:] None.

3. Select the interfaces defined by the string class?
a) IComparable
b) IComparable<string>
c) ICloneable
d) All of the mentioned

Answer

Answer: d [Reason:] None.

4. Choose the constructor type used to build strings from character array:
a) public String(value)
b) public String(char[ ] value, int startIndex, int length)
c) public String(char[ ])
d) all of the mentioned

Answer

Answer: b [Reason:] public String(char[ ] value) – This form of constructor constructs a string that contains characters in value
public String(char[ ] value, int startIndex, int length) -The second form uses length characters from value, beginning at the index specified by startIndex.

5. Select the operators used for checking the equality in strings:
a) !=
b) >
c) <
d) >=

Answer

Answer: a [Reason:] None.

6. What does the given code set specifies?

  1. public static int Compare(string strA, string strB)

a) Comparison is case and culture sensitive
b) Two strings A and B are compared with each other
c) Output is : >0 for (A > B), <0 for (A < B) else ‘0’ for(A=B)
d) All of the mentioned

Answer

Answer: d [Reason:] Compares the string referred to by strA with strB. Returns greater than zero if strA is greater than strB, less than zero if strA is less than strB, and zero if strA and strB are equal. The comparison is case and culture-sensitive.

7. Select the output for given set of code:

  1. static void Main(string[] args)
  2. {
  3.     string s1 = "Hello" + "c" + "Sharp";
  4.     Console.WriteLine(s1);
  5.     Console.ReadLine();
  6. }

a) Hello c Sharp
b) HellocSharp
c) Compile time error
d) Hello

Answer

Answer: a [Reason:] Here ‘+’ operator works as concatenation for strings.
Output : Hello c Sharp

8. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||

Answer

Answer: a [Reason:] string s1 = “Hello”+ ” I ” + “Love” + ” ComputerScience “;
Console.WriteLine(s1);
Output :Hello I Love ComputerScience.

9. What does the given code set specify?

  1. public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase)

a) Comparison begins at strA[indexA] and strB[indexB] and runs for length of characters
b) Returns output > 0 for for strA > strB else < 0 for strA < strB else if strA = str B output is 0
c) Comparison is culture sensitive and if ignore case is true, comparison ignores case differences
d) All of the mentioned

Answer

Answer: d [Reason:] Compares portions of the strings referred to by strA and strB. The comparison begins at strA[indexA] and strB[indexB] and runs for length characters. Returns greater than zero if strA is greater than strB, less than zero if strA is less than strB, and zero if strA and strB are equal. If ignoreCase is true, the comparison ignores case differences. Otherwise, case differences matter. The comparison is culture-sensitive.

10. Which string operation does the below mentioned method define?

  1. public static string Concat(string str0, string str1)

a) method returns a string
b) string str1 is concatenated to the end of str0
c) can be used to concatenate any number of strings
d) all of the mentioned

Answer

Answer: d [Reason:] This method returns a string that contains str1 concatenated to the end of str0. Another form of Concat(), shown here, concatenates three strings:
public static string Concat(string str0, string str1, string str2). Hence, any number of strings can be concatenated using this method.

11. Choose the base class for string() method :
a) System.Array
b) System.char
c) System.String
d) None of the mentioned

Answer

Answer: c [Reason:] String is an alias for the predefined “System.string” class from which most of the string() methods are derived.

12. Method used to remove white space from string?
a) Split()
b) Substring()
c) Trim()
d) TrimStart()

Answer

Answer: c [Reason:] Perfectly removes a whitespace from string whereas TrimStart() removes a string of characters from the end of the string.

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.