C# MCQ Set 1
1. Which of these methods of class String is used to compare two String objects for their equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()
Answer
Answer: a [Reason:] None.
2. Which of these methods is used to compare two strings such that after comparison output returns different integer values as ( 0 for false, 1 for true)?
a) Equals ()
b) == operator
c) Compare()
d) None of the mentioned
Answer
Answer: c [Reason:] The comparison is case sensitive in nature and hence different integer values are returned for different conditions as under :
1. zero integer (0), if string s1 equal to string s2.
2. positive integer(+1) , if string s1 greater than s2.
3. Negative integer(-1) , if string s1 is less than s2.
3. Which of these methods of class String is used to check whether a substring exists at the beginning of the particular string?
a) StartsWith()
b) EndsWith()
c) Starts()
d) ends()
Answer
Answer: a [Reason:] Method startswith() of string class is used to check whether a substring exists in the beginning of string or not.
4. Which of these methods returns the string such that some characters which are specified to be removed from the end of strings are removed from string by mentioning the number of characters to be removed?
a) Trim()
b) Remove()
c) TrimEnd()
d) Split()
Answer
Answer: a [Reason:] Removes a string of characters from the end of string by mentioning the number of characters to be removed from the string.
5. What is the value returned by 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.
6. Which of these data type values is returned by equals() method of String class?
a) char
b) int
c) boolean
d) all of the mentioned
Answer
Answer: c [Reason:] equals() method of string class returns boolean value true if both the strings are equal and false if they are unequal.
7. What is output of the given set of Code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String c = "i love Csharp";
-
bool a;
-
a = c.StartsWith("I");
-
Console.WriteLine(a);
-
Console.ReadLine();
-
}
-
}
a) true
b) false
c) 0
d) 1
Answer
Answer: b [Reason:] StartsWith() method is case sensitive “i” and “I” are treated differently, hence false is stored in a.
Output: false
8. What is the output of the given set of Code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String s1 = "I love You";
-
String s2 = s1;
-
Console.WriteLine((s1 == s2) + " " + s1.Equals(s2));
-
Console.ReadLine();
-
}
-
}
a) true true
b) false false
c) true false
d) false true
Answer
Answer: a [Reason:] The ‘==’ operator tests the equality of strings and since s1 = “I love You” and also s2 = s1 .So, true is returned .Similarly,Equals() returns true
since the content of both s1 and s2 are equal in nature.
Output : true true
9. What is output of the given set of Code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String []chars = {"z", "x", "y", "z", "y"};
-
for (int i = 0; i < chars.Length; ++i)
-
for (int j = i + 1; j < chars.Length; ++j)
-
if(chars[i].CompareTo(chars[j]) == 0)
-
Console.WriteLine(chars[j]);
-
Console.ReadLine();
-
}
-
}
a) zx
b) xy
c) zy
d) yz
Answer
Answer: c [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.4
Output : z
y
10. What will be the output for the given set of code ?
-
String a = "Csharp";
-
String b = "CSHARP";
-
int c;
-
c = a.CompareTo(b);
-
Console.WriteLine(c);
a) 0
b) 1
c) -2
d) -1
Answer
Answer: d [Reason:] Negative integer -1 is returned as ‘a’ is less than ‘b’ by CompareTo() method.
Output : -1
C# MCQ Set 2
1. What will be the output of the given set of code?
-
class maths
-
{
-
public int length;
-
public int breadth;
-
public maths(int x, int y)
-
{
-
length = x;
-
breadth = y;
-
Console.WriteLine(x + y);
-
}
-
public maths(double x, int y)
-
{
-
length = (int)x;
-
breadth = y;
-
Console.WriteLine(x * y);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths m = new maths(20, 40);
-
maths k = new maths(12.0, 12);
-
Console.ReadLine();
-
}
-
}
a) 60, 24
b) 60, 0
c) 60, 144
d) 60, 144.0
Answer
Answer: c [Reason:] Matching the values passed as parameters.The respective constructors are overloaded according to the matching parameter type.
Output : 60
144
2. What will be the output of the given set of code?
-
class maths
-
{
-
public int length;
-
public int breadth;
-
public maths(int x)
-
{
-
length = x + 1;
-
}
-
public maths(int x, int y)
-
{
-
length = x + 2;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths m = new maths(6);
-
maths k = new maths(6, 2);
-
Console.WriteLine(m.length);
-
Console.WriteLine(k.length);
-
Console.ReadLine();
-
}
-
}
a) 8, 8
b) 0, 2
c) 8, 10
d) 7, 8
Answer
Answer: d [Reason:] None.
3. What will be the output of the given set of code?
-
class maths
-
{
-
public maths()
-
{
-
Console.WriteLine("constructor 1 :");
-
}
-
public maths(int x)
-
{
-
int p = 2;
-
int u;
-
u = p + x;
-
Console.WriteLine("constructor 2: " +u);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths k = new maths(4);
-
maths t = new maths();
-
Console.ReadLine();
-
}
-
}
a) constructor 1:
constructor 2: 6
b) constructor 2: 6
constructor 2: 6
c) constructor 2: 6
constructor 1:
d) none of the mentioned
Answer
Answer: c [Reason:] None.
Output: constructor 2: 6
constructor 1:
4. What will be the output of the given set of code?
-
class maths
-
{
-
int i;
-
public maths(int x)
-
{
-
i = x;
-
Console.WriteLine(" hello: ");
-
}
-
}
-
class maths1 : maths
-
{
-
public maths1(int x) :base(x)
-
{
-
Console.WriteLine("bye");
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths1 k = new maths1(12);
-
Console.ReadLine();
-
}
-
}
a) hello bye
b) 12 hello
c) bye 12
d) Compile time error
Answer
Answer: a [Reason:] None.
5. What will be the output of the given set of code?
-
class maths
-
{
-
int i;
-
public maths(int ii)
-
{
-
ii = 12;
-
int j = 12;
-
int r = ii * j;
-
Console.WriteLine(r);
-
}
-
}
-
class maths1 : maths
-
{
-
public maths1(int u) :base(u)
-
{
-
u = 13;
-
int h = 13;
-
Console.WriteLine(u + h);
-
}
-
}
-
class maths2 : maths1
-
{
-
public maths2(int k) :base(k)
-
{
-
k = 24;
-
int o = 6 ;
-
Console.WriteLine(k /o);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths2 t = new maths2(10);
-
Console.ReadLine();
-
}
-
}
a) 4, 26, 144
b) 26, 4, 144
c) 144, 26, 4
d) 0, 0, 0
Answer
Answer: c [Reason:] None.
6. Which keyword is used to refer baseclass constructor to subclass constructor?
a) This
b) Static
c) Base
d) Extend
Answer
Answer: c [Reason:] None.
7. When we call a constructor method among different given constructors. We match the suitable constructor by matching the name of constructor first , then the number and then the type of parameters to decide which constructor is to be overloaded.The process is also known as?
a) Method overriding
b) Inheritance
c) Polymorphism
d) Encapsulation
Answer
Answer: c [Reason:] None.
8. Correct statement about constructor overloading in C# is?
a) Overloaded constructors have the same name as the class
b) Overloaded constructors cannot use optional arguments
c) Overloaded constructors can have different type of number of arguments as well as differ in number of arguments
d) All of the mentioned
Answer
Answer: c [Reason:] By definition of overloaded constructors.
9. What will be the output of the given set of code?
-
class maths
-
{
-
static maths()
-
{
-
int s = 8;
-
Console.WriteLine(s);
-
}
-
public maths(int f)
-
{
-
int h = 10;
-
Console.WriteLine(h);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths p = new maths(0);
-
Console.ReadLine();
-
}
-
}
a) 10, 10
b) 0, 10
c) 8, 10
d) 8, 8
Answer
Answer: c [Reason:] None.
Output: 8, 10
10. What will be the output of the given set of code?
-
class maths
-
{
-
int i;
-
public maths(int ii)
-
{
-
ii = -25;
-
int g;
-
g = ii > 0 ? ii : ii * -1;
-
Console.WriteLine(g);
-
}
-
}
-
class maths1 :maths
-
{
-
public maths1(int ll) :base(ll)
-
{
-
ll = -1000;
-
Console.WriteLine((ll > 0 ? ll : ll * -1));
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths1 p = new maths1(6);
-
Console.ReadLine();
-
}
-
}
a) -25
-1000
b) -1000
-25
c) 25
1000
d) None of the mentioned
Answer
Answer: c [Reason:] None.
Output : 25, 1000
C# MCQ Set 3
1. Number of constructors a class can define is ?
a) 1
b) 2
c) Any number
d) None of the mentioned
Answer
Answer: c [Reason:] A constructor is a simple method which has the same name as the class and hence used to create object of a class. C# class can define any number of constructors. Every class contains a default constructor.
2. Correct way of defining constructor of the given class as and when objects of classes are created is:
maths s1 = new maths();
maths s2 = new maths(5, 5.4f);
a) public maths(int pp, single tt) { p = pp; t = tt; } b) sample s; c) public sample() { p = 0; t = 0.0f; } public sample(int pp, single tt) { p = pp; t = tt; } d) s = new sample();
Answer
Answer: a [Reason:] None.
3. Correct way to define object of sample class in which code will work correctly is:
-
class abc
-
{
-
int i;
-
float k;
-
public abc(int ii, float kk)
-
{
-
i = ii;
-
k = kk;
-
}
-
}
a) abc s1 = new abc(1);
b) abc s1 = new abc();
c) abc s2 = new abc(1.4f);
d) abc s2 = new abc(1, 1.4f);
Answer
Answer: d [Reason:] Return types of parameters of object of class matches with defined constructor arguments types.
4. Correct statement about constructors in C#.NET is ?
a) Constructors can be overloaded
b) Constructors are never called explicitly
c) Constructors have same name as name of the class
d) All of the mentioned
Answer
Answer: d [Reason:] None.
5. Which among the following is the correct statement :
Constructors are used to
a) initialize the objects
b) construct the data members
c) initialize the objects & construct the data members
d) None of the mentioned
Answer
Answer: a [Reason:] Once the object is declared means, the constructor is also declared by default.
6. Can the method add() be overloaded in the following ways in C#?
public int add() { }
public float add(){ }
a) True
b) False
c) None of the mentioned.
Answer
Answer: b [Reason:]C# provides feature of method overloading which means methods with same name but different types and arguments.
7. Which of the following statements is correct about constructors in C#.NET?
a) A constructor cannot be declared as private
b) A constructor cannot be overloaded
c) A constructor can be a static constructor
d) None of the mentioned
Answer
Answer: c [Reason:] Static constructor is a constructor which can be called before any object of class is created or any static method is invoked.Static constructor is implicitly called by .net CLR.
8. What is output of the following section of code ?
-
class abc
-
{
-
public static void a()
-
{
-
console.writeline("first method");
-
}
-
public void b()
-
{
-
a();
-
console.writeline("second method");
-
}
-
public void b(int i)
-
{
-
console.writeline(i);
-
b();
-
}
-
}
-
class program
-
{
-
static void main()
-
{
-
abc k = new abc();
-
abc.a();
-
k.b(20);
-
}
-
}
a) second method
20
second method
first method
b) first method
20
first method
second method
c) first method
20
d) second method
20
first method
Answer
Answer: b [Reason:] The class ‘abc’ first calls method()’a’ then object of class ‘k’ when calls method ‘b’ .First of all, the method ‘a’ will be executed and then executes second statement.
Output :first method
20
first method
second method
9. What is the return type of constructors?
a) int
b) float
c) void
d) none of the mentioned
Answer
Answer: d [Reason:] Constructors do not have any return type not even void included in it.
10. Which method has the same name as that of its class?
a) delete
b) class
c) constructor
d) none of mentioned
Answer
Answer: c [Reason:] By definition.
C# MCQ Set 4
1. Which operator among the following signifies the destructor operator?
a) ::
b) :
c) ~
d) &
Answer
Answer: c [Reason:] None.
2. The method called by clients of a class to explicitly release any resources like network,connection,open files etc.When the object is no longer required?
a) Finalize()
b) End()
c) Dispose()
d) Close()
Answer
Answer: c [Reason:] Dispose() is only method called by clients of a class to explicitly release any resource like network connection,open files etc.when object is no longer required.Hence,Dispose() provides programmer with such programming control.
3. Name a method which has the same name as that of class and which is used to destroy objects also called automatically when application is finally on process of being getting terminated.
a) Constructor
b) Finalize()
c) Destructor
d) End
Answer
Answer: c [Reason:] Definition of destructor.
4. Which of the following statements are correct?
a) There is one garbage collector per program running in memory
b) There is one common garbage collector for all programs
c) To garbage collect an object set all references to it as null
d) Both b & c
Answer
Answer: d [Reason:] None.
5. Operator used to free the memory when memory is allocated ?
a) new
b) free
c) delete
d) none of the mentioned
Answer
Answer: c [Reason:] ‘New’ is used to allocate memory in the constructors.Hence,we should use ‘delete’ to free that memory.
6. Select wrong statement about destructor in C#?
a) A class can have one destructor only
b) Destructors cannot be inherited or overloaded
c) Destructors can have modifiers or parameters
d) All of the mentioned
Answer
Answer: c [Reason:] None.
7. Output from following set of code ?
-
class sample
-
{
-
int i;
-
double k;
-
public sample (int ii, double kk)
-
{
-
i = ii;
-
k = kk;
-
double j = (i) + (k);
-
Console.WriteLine(j);
-
}
-
~sample()
-
{
-
double j = i - k;
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample s = new sample(8, 2.5);
-
Console.ReadLine();
-
}
-
}
a) 0 0
b) 10.5 0
c) Compile time error
d) 10.5 5.5
Answer
Answer: d [Reason:] First constructor ‘sample’ is called and hence then destructor ‘~sample’ is evaluated.
Output : 10.5, 5.5
8. What is the return type of destructor ?
a) int
b) void
c) float
d) none of the mentioned
Answer
Answer: d [Reason:] Destructors do not have any return type not even void.
9. What is output for the following set of Code ?
-
class box
-
{
-
public int volume;
-
int width;
-
int height;
-
int length;
-
public box ( int w, int h, int l)
-
{
-
width = w;
-
height = h;
-
length = l;
-
}
-
public ~box()
-
{
-
volume = width * length * height;
-
-
}
-
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
box b = new box(4, 5, 9);
-
Console.WriteLine(b.volume);
-
Console.ReadLine();
-
}
-
-
}
a) 0
b) 180
c) Compile time error
d) None of the mentioned
Answer
Answer: c [Reason:] We cannot use any kind of modifier with destructor.
10. Output from selective set of code is :
-
class box
-
{
-
public int volume;
-
int width;
-
int height;
-
int length;
-
public box ( int w, int h, int l)
-
{
-
this.width = w;
-
this.height = h;
-
this.length = l;
-
}
-
~ box()
-
{
-
volume = this.width * this.length * this.height;
-
console.writeline(volume);
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
box b = new box(4, 5, 9);
-
Console.ReadLine();
-
}
-
}
a) 0
b) Code executes successfully but prints nothing
c) Compile time error
d) 180
Answer
Answer: d [Reason:] None.
Output: 180.
C# MCQ Set 5
1. Select output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 1, j = 2, k = 3;
-
do
-
{
-
Console.WriteLine((Convert.ToBoolean(Convert.ToInt32(i++))) && (Convert.ToBoolean(Convert.ToInt32(++j))));
-
}while (i <= 3);
-
Console.ReadLine();
-
}
a) 0 0 0
b) True True True
c) 1 1 1
d) False False False
Answer
Answer: b [Reason:] 1 AND 1 = True.Similarly , non zero number || non zero number = True.
Output:True True True.
2. Select output for the following set of code :
-
static void Main(string[] args)
-
{
-
float i = 1.0f, j = 0.05f;
-
do
-
{
-
Console.WriteLine(i++ - ++j);
-
}while (i < 2.0f && j <= 2.0f);
-
Console.ReadLine();
-
}
a) 0.05
b) -0.05
c) 0.95
d) -0.04999995
Answer
Answer: d [Reason:] None.
Output : -0.04999995
3. Select the output for the following code :
-
static void Main(string[] args)
-
{
-
int i = 1, j = 5;
-
do
-
{
-
Console.WriteLine(i = i++ * j);
-
}while (i <= 10);
-
Console.ReadLine();
-
}
a) 5 10 15 20 25 30 35 40 45 50
b) 5 25
c) 5 11 16 21 26 31 36 41 46 51
d) 5 30
Answer
Answer: b [Reason:] For first step of loop i = 1 .So, i++ * j = 1 * 5 = 5 .For second step of loop i = 5 ,j = 5 .So, i++ * j = 25.As, i = 25 hence , 25 >=10 loop condition breaks.
Output: 5 25.
4. For the incomplete program below, which of the code fragment will not result in an infinite loop:
-
static void Main(string[] args)
-
{
-
int i = 1234 ,j = 0;
-
/*ADD CODE HERE */
-
Console.WriteLine(j);
-
}
a)
do { j = j + (i % 10); }while ((i = i / 10)!= 0);
b)
do { j = j + (i % 10); }while ((i / 10)!= 0);
c)
do { j = j + (i % 10); }while ((i % 10)!= 0);
d)
do { j = j + (i % 10); }while ((i/10 == 0)!= 0);
Answer
Answer: a
Output :
static void Main(string[] args) { int i = 1234,j = 0; do { j = j +( i % 10); }while ((i = i / 10)!= 0); Console.WriteLine(j);
}
5. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
long x;
-
x = Convert.ToInt32(Console.ReadLine());
-
do
-
{
-
Console.WriteLine(x % 10);
-
}while ((x = x / 10) != 0);
-
Console.ReadLine();
-
}
-
enter x = 1234.
a) number of digits present in x
b) prints ‘1’
c) prints reverse of x
d) prints sum of digits of ‘x’
Answer
Answer: c [Reason:] Reverse of digits using while loop statements.
Output: 4321.
6. Select output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i, s = 0, a = 1, d;
-
i = Convert.ToInt32(Console.ReadLine());
-
do
-
{
-
d = i % (2 * 4);
-
s = s + d * a;
-
}while ((Convert.ToInt32(i = i / (2 * 4))) != 0 && (Convert.ToBoolean(Convert.ToInt32((a) = (a * 10)))));
-
Console.WriteLine(s);
-
Console.ReadLine();
-
}
-
enter i = 342.
a) It finds binary equivalent of i
b) It finds octal equivalent of i
c) It finds sum of digits of i
d) It finds reverse of i
Answer
Answer: b [Reason:] None.
Output : i = 342.
s = 526.
7. Correct syntax for do while loop is :
a) do; { statement; }while (condition); b) do(condition) { statement; }while; c) do { statement; }while (condition) d) do { statement; }while (condition);
Answer
Answer: d [Reason:] By definition
Output:do
{
statement;
}while (condition);
8. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int x = 10;
-
do
-
{
-
Console.WriteLine( x++);
-
}
-
while(Convert.ToBoolean(5) && Convert.ToBoolean(4) && Convert.ToBoolean(3) && Convert.ToBoolean(2) && Convert.ToBoolean(1) && Convert.ToBoolean(0));
-
Console.ReadLine();
-
}
a) 13
b) 15
c) 11
d) 10
Answer
Answer: d [Reason:] Here in do while condition ‘&&’ i.e ‘AND’operator return ‘0’ i.e false.So, as condition is false so program comes out of the loop.
Output : 10.
9. Select output for the following set of code :
-
static void Main(string[] args)
-
{
-
int x;
-
for (x = 10; x <= 15; x++)
-
while (Convert.ToBoolean(Convert.ToInt32(x)))
-
{
-
do
-
{
-
Console.WriteLine(1);
-
if (Convert.ToBoolean(x >> 1))
-
continue;
-
}while (Convert.ToBoolean(0));
-
break;
-
}
-
Console.ReadLine();
-
}
a) 0 0 0….infinite times
b) 1 1 1….infinite times
c) 1 1 1 1 1 1
d) System outofflow exception error
Answer
Answer: c [Reason:] The execution of for loop is done for six consecutive times.
Output : 1 1 1 1 1 1
10. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int x = 0;
-
do
-
{
-
x++;
-
if (x == 5)
-
{
-
x++;
-
continue;
-
break;
-
}
-
Console.WriteLine(x + " ");
-
}
-
}while (x < 10);
a) 1 2 3 4 5
b) 10
c) 5 6 7 8 9 10
d) 1 2 3 4 5 6 7 8 9 10
Answer
Answer: d [Reason:] The condition will print the numbers from 1 to 10 when x == 5 and when x does not satisfy if condition until x < 10.
Output: 1 2 3 4 5 6 7 8 9 10 .
11. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int x;
-
for (x = 1; x <= 3; x++)
-
{
-
int j = 1;
-
do
-
{
-
j++;
-
}while (x % j == 2);
-
Console.WriteLine(x + " " + j);
-
}
-
Console.ReadLine();
-
}
a) 1 12 1 3 1
b) 1 12 13 1
c) 12 22 32
d) 11 21 31
Answer
Answer: c [Reason:] None.
Output : 12 22 32.