C# MCQ Set 1
1. What will be the output of the given code?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
int m = 10;
-
int *mptr = &m;
-
int **ptr = &mptr;
-
int n = 20;
-
int *nptr = &n;
-
int **prt = &nptr;
-
m = **prt + *nptr;
-
n = *mptr* **prt;
-
Console.WriteLine(n + " " + m);
-
Console.ReadLine();
-
}
-
}
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?
-
unsafe static void Main()
-
{
-
int a = 5;
-
int b = 5;
-
int c = 5;
-
int*[] ptr = new int* [3];
-
ptr[0] = &a;
-
ptr[1] = &b;
-
ptr[2] = &c;
-
for (a = 0; a < 3; a++)
-
{
-
c += *ptr[a];
-
Console.WriteLine(c);
-
}
-
Console.ReadLine();
-
}
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?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
int* ptrs = stackalloc int[3];
-
ptrs[0] = 1;
-
ptrs[1] = 2;
-
ptrs[2] = 3;
-
for (int i = 2; i >= 0; --i)
-
{
-
ptrs[i] = ptrs[i]* 3;
-
ptrs[i] = ptrs[i] + 4;
-
Console.WriteLine(ptrs[i]);
-
}
-
Console.ReadLine();
-
}
-
}
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?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
char[] arr = { 'A', 'B', 'C', 'D', 'E' };
-
fixed (char* P = arr)
-
{
-
int i;
-
for (i = 0 ;i < 5 ;i++)
-
if (*P % 2 == 0)
-
++*P;
-
else
-
(*P)++;
-
Console.WriteLine(arr);
-
}
-
Console.ReadLine();
-
}
-
}
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?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
int[] nums = new int[10];
-
Console.WriteLine("Index pointer like array.");
-
fixed (int* p = nums)
-
{
-
for (int i = 0 ;i <10 ;i++)
-
p[i] = i;
-
for (int i = 10 ;i >0 ;i--)
-
Console.WriteLine("p[{0}]: {1} ", i, p[i]);
-
Console.ReadLine();
-
}
-
}
-
}
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?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
string str = "this is a test";
-
-
fixed (char* p = str)
-
{
-
for (int i = str.Length-1 ;p[i] != 0 ;i--)
-
Console.Write(p[i]);
-
}
-
Console.ReadLine();
-
}
-
}
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?
-
class UnsafeCode
-
{
-
unsafe static void Main()
-
{
-
int* p ;
-
int ch = 13*5;
-
p = &(ch);
-
Console.WriteLine(Convert.ToChar(*p));
-
if (*p == 'A')
-
Console.WriteLine(Convert.ToBoolean(1));
-
else
-
Console.WriteLine(Convert.ToBoolean(0));
-
Console.ReadLine();
-
}
-
}
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?
-
public class A
-
{
-
public int x;
-
public int y;
-
public void display()
-
{
-
Console.WriteLine(x + " " + y);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
A obj1 = new A();
-
A obj2 = new A();
-
obj1.x = 1;
-
obj1.y = 2;
-
obj2 = obj1;
-
obj1.display();
-
obj2.display();
-
}
-
}
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?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
double x = 3.14;
-
int y = (int) Math.Abs(x);
-
Console.WriteLine(y);
-
}
-
}
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?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
double x = 3.14;
-
int y = (int) Math.Ceiling(x);
-
Console.WriteLine(y);
-
}
-
}
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?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
double x = 3.14;
-
int y = (int) Math.Floor(x);
-
Console.WriteLine(y);
-
}
-
}
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?
-
class B { }
-
class A : B { }
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
A a = new A();
-
B b = new B();
-
if (a is A)
-
Console.WriteLine("a is an A");
-
if (b is A)
-
Console.WriteLine("b is an A because it is derived from A");
-
if (a is B)
-
Console.WriteLine("This won’t display -- a not derived from B");
-
Console.ReadLine();
-
}
-
}
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?
-
class A {}
-
class B : A {}
-
class CheckCast
-
{
-
static void Main()
-
{
-
A a = new A();
-
B b = new B();
-
b = a as B;
-
b = null;
-
if(b==null)
-
Console.WriteLine("The cast in b = (B) a is NOT allowed.");
-
else
-
Console.WriteLine("The cast in b = (B) a is allowed");
-
}
-
}
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?
-
class UseTypeof
-
{
-
static void Main()
-
{
-
Type t = typeof(StreamReader);
-
Console.WriteLine(t.FullName);
-
if(t.IsClass) Console.WriteLine("Is a class.");
-
if(t.IsAbstract) Console.WriteLine("Is abstract.");
-
else Console.WriteLine("Is concrete.");
-
}
-
}
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?
-
static void Main(string[] args)
-
{
-
string s1 = "olleH";
-
string s2 = "olleh";
-
if (s1 == s2)
-
Console.WriteLine("Equal");
-
else
-
Console.WriteLine("Unequal");
-
if (s1.Equals(s2))
-
Console.WriteLine("Equal");
-
else
-
Console.WriteLine("Unequal");
-
Console.ReadLine();
-
}
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?
-
static void Main(string[] args)
-
{
-
string s1 = " Ixg";
-
string s2 = s1.Insert(3,"i");
-
string s3 = s2.Insert(5, "o");
-
for (int i = 0; i < s3.Length; i++)
-
Console.WriteLine(s3[i]);
-
Console.ReadLine();
-
}
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?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
char []chars = {'a', 'b', 'c'};
-
String s = new String(chars);
-
Console.WriteLine(s);
-
Console.ReadLine();
-
}
-
}
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?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
char []chars = {'a', 'b', 'c'};
-
String s = new String(chars);
-
String s1 = "abcd";
-
int len1 = s1.Length;
-
int len2 = s.Length;
-
Console.WriteLine(len1 + " " + len2);
-
Console.ReadLine();
-
}
-
}
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?
-
class A
-
{
-
int i;
-
int j;
-
public A()
-
{
-
i = 1;
-
j = 2;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
A obj1 = new A();
-
Console.WriteLine(obj1.ToString());
-
Console.ReadLine();
-
}
-
}
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?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String c = "Hello i love Csharp";
-
Boolean var;
-
var = c.StartsWith("hello");
-
Console.WriteLine(var);
-
Console.ReadLine();
-
}
-
}
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?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String s1 = "Hello i love Csharp";
-
StringBuilder s2 = new StringBuilder(s1);
-
Console.WriteLine(s1.Equals(s2));
-
Console.ReadLine();
-
}
-
}
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?
-
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:
-
static void Main(string[] args)
-
{
-
string s1 = "Hello" + "c" + "Sharp";
-
Console.WriteLine(s1);
-
Console.ReadLine();
-
}
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?
-
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?
-
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.