C# MCQ Set 1
1. The process of defining two or more methods within the same class that have same name but different parameters list?
a) Method overloading
b) Method overriding
c) Encapsulation
d) None of the mentioned
Answer
Answer: a [Reason:] Two or more methods can have same name as long as their parameters declaration and definitions are different, the methods are said to be overloaded and the process is called method overloading. Method overloading is used when methods are required to perform similar tasks using different input parameters.
2. Which of these can be overloaded?
a) Constructors
b) Methods
c) Both Constructors & Methods
d) None of the mentioned
Answer
Answer: c [Reason:] None.
3. What could be the output of the following set of code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Console.WriteLine( vol(10));
-
Console.WriteLine( vol(2.5f, 5));
-
Console.WriteLine( vol( 5l, 4, 5));
-
Console.ReadLine();
-
}
-
static int vol(int x)
-
{
-
return(x * x * x);
-
}
-
static float vol(float r, int h)
-
{
-
return(3.14f * r * r * h);
-
}
-
static long vol(long l, int b, int h)
-
{
-
return(l * b * h);
-
}
-
}
a) 1000
0
100
b) 0
0
100
c) compile time error
d) 1000
98.125
100
Answer
Answer: d [Reason:] The concept of method overloading is implemented in method “vol” with same name but different definitions and parameter list which is overloaded three times and each time the return type is different for each method and hence matches the method using types of parameters .
Output: 1000
98.125
100
4. What could be the output for the set of code?
-
class overload
-
{
-
public int x;
-
int y;
-
public int add(int a)
-
{
-
x = a + 1;
-
return x;
-
}
-
public int add(int a, int b)
-
{
-
x = a + 2;
-
return x;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
overload obj = new overload();
-
overload obj1 = new overload();
-
int a = 0;
-
obj.add(6);
-
obj1.add(6, 2);
-
Console.WriteLine(obj.x);
-
Console.WriteLine(obj1.x);
-
Console.ReadLine();
-
}
-
}
a) 8
8
b) 0
2
c) 8
10
d) 7
8
Answer
Answer: d [Reason:] None.
Output: 7, 8
5. What will be the output for the set of code?
-
static void Main(string[] args)
-
{
-
int i = 5;
-
int j = 6;
-
add(ref i);
-
add(6);
-
Console.WriteLine(i);
-
Console.ReadLine();
-
}
-
static void add(ref int x)
-
{
-
x = x * x;
-
}
-
static void add(int x)
-
{
-
Console.WriteLine(x * x * x);
-
}
a) Compile time error
b) 25
0
c) 216
0
d) 216
25
Answer
Answer: d [Reason:] None.
Output: 216
25
6. What would be output for the set of code?
-
class maths
-
{
-
public int x;
-
public double y;
-
public int add(int a, int b)
-
{
-
x = a + b;
-
return x;
-
}
-
public int add(double c, double d)
-
{
-
y = c + d;
-
return (int)y;
-
}
-
public maths()
-
{
-
this.x = 0;
-
this.y = 0;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
int a = 4;
-
double b = 3.5;
-
obj.add(a, a);
-
obj.add(b, b);
-
Console.WriteLine(obj.x + " " + obj.y);
-
Console.ReadLine();
-
}
-
}
a) 4, 3.5
b) 8, 0
c) 7.5, 8
d) 8, 7
Answer
Answer: d [Reason:] None
Output: 8, 7
7. What will be output for the given set of code?
-
class maths
-
{
-
public static void fun1()
-
{
-
Console.WriteLine("method 1 :");
-
}
-
public void fun2()
-
{
-
fun1();
-
Console.WriteLine("method 2 :");
-
}
-
public void fun2(int k)
-
{
-
Console.WriteLine(k);
-
fun2();
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
maths.fun1();
-
obj.fun2(20);
-
Console.ReadLine();
-
}
-
}
a) method 1:
method 2:
20
method 1:
b) method 2:
20
method 1:
method 1:
c) method 1:
0
method 2:
method 2:
d) method 1:
20
method 1:
method 2:
Answer
Answer: d [Reason:] None.
Output : method 1:
20
method 1:
method 2:
8. What is the process of defining a method in terms of itself, that is a method that calls itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer
Answer: d [Reason:] None.
9. What will be the output for the following set of code?
-
class maths
-
{
-
public int fun1(int k)
-
{
-
k = 20;
-
return k;
-
}
-
public Single fun1(float t)
-
{
-
t = 3.4f;
-
return t;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
int i;
-
i = obj.fun1(30);
-
Console.WriteLine(i);
-
Single j;
-
j = obj.fun1(2.5f);
-
Console.WriteLine(j);
-
Console.ReadLine();
-
}
-
}
a) 30
2.5f
b) 2.5f
30
c) 20
2.5f
d) 20
3.4f
Answer
Answer: d [Reason:] None.
Output: 20
3.4f
10. What will be the output for the given set of code?
-
class maths
-
{
-
public int fun(int k, int y)
-
{
-
return k + y;
-
}
-
public int fun1(int t, float z)
-
{
-
return (t+(int)z);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
int i;
-
int b = 90;
-
int c = 100;
-
int d = 12;
-
float l = 14.78f;
-
i = obj.fun(b, c);
-
Console.WriteLine(i);
-
int j = (obj.fun1(d, l));
-
Console.WriteLine(j);
-
Console.ReadLine();
-
}
-
}
a) 190, 26.78f
b) 0, 26.78f
c) 190, 26
d) 190, 0
Answer
Answer: c [Reason:] None.
Output: 190
26
11. What will be the output for the set of code?
-
class maths
-
{
-
public int fun(int k, int y, int n)
-
{
-
Console.WriteLine(k + " " + y + " " + n);
-
return (k);
-
}
-
public int fun1(int t,float z)
-
{
-
Console.WriteLine(t + " " + z);
-
return t;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
int b = 90;
-
int c = 100;
-
int d ;
-
float l;
-
int i = obj.fun(b, c, 12);
-
int j = (obj.fun1(12, 14.78f));
-
Console.ReadLine();
-
}
-
}
a) 0, 0, 0
12, 14.78
b) 0, 0, 0
0, 0
c) 90, 100, 12
12, 14
d) 90, 100, 12
12, 14.78
Answer
Answer: d [Reason:] None.
Output: 90, 100, 12
12, 14.78
12. What will be the output for the given set of code?
-
class maths
-
{
-
public int fun(int ii)
-
{
-
return(ii > 0 ? ii :ii * -1);
-
}
-
public long fun(long ll)
-
{
-
return(ll > 0 ? ll :ll * -1);
-
}
-
public double fun( double dd)
-
{
-
return(dd > 0 ? dd :dd * -1);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
int i = -25;
-
int j ;
-
long l = -100000l ;
-
long m;
-
double d = -12.34;
-
double e;
-
j = obj.fun(i);
-
m = obj.fun(l);
-
e = obj.fun(d);
-
Console.WriteLine(j + " " + m + " " + e);
-
Console.ReadLine();
-
}
-
}
a) 1 1 1
b) 0 0 0
c) 25 100000 12.34
d) -25 -100000 -12.34
Answer
Answer: c [Reason:] None.
Output: 25 100000 12.34
C# MCQ Set 2
1. Which keyword is used to declare a base class method while performing overidding of base class methods?
a) this
b) virtual
c) override
d) extend
Answer
Answer: b [Reason:] None.
2. The process of defining a method in subclass having same name & type signature as a method in its superclass is known as?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
Answer
Answer: b [Reason:] None.
3. Which of the given modifiers can be used to prevent Method overriding?
a) Static
b) Constant
c) Sealed
d) final
Answer
Answer: c [Reason:] When an instance method declaration includes sealed modifier,the method is said to be sealed method.It means a derived class cannot override this method.
4. Select the correct statement from the following?
a) Static methods can be a virtual method
b) Abstract methods can be a virtual method
c) When overriding a method, the names and type signatures of the override method must be the same as the virtual method that is being overriden
d) We can override virtual as well as non virtual methods
Answer
Answer: c [Reason:] None.
5. Which of the following cannot be used to declare a class as a virtual?
a) Methods
b) Properties
c) Events
d) Fields
Answer
Answer: d [Reason:] None.
6. What will be the output for the given set of code?
-
class A
-
{
-
public int i;
-
public void display()
-
{
-
Console.WriteLine(i);
-
}
-
}
-
class B: A
-
{
-
public int j;
-
public void display()
-
{
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.i = 1;
-
obj.j = 2;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
a) 0
b) 2
c) 1
d) Compile time error
Answer
Answer: b [Reason:] When method display() is called using objects of class ‘B’. The method ‘display()’ for class ‘B’ is called instead of class ‘A’ as class ‘B’ is inherited by class ‘A’.
Output :2
7. What will be the output for the given set of code?
-
class A
-
{
-
public virtual void display()
-
{
-
Console.WriteLine("A");
-
}
-
}
-
class B: A
-
{
-
public override void display()
-
{
-
Console.WriteLine(" B ");
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
A obj1 = new A();
-
B obj2 = new B();
-
A r;
-
r = obj1;
-
r.display();
-
r = obj2;
-
r.display();
-
Console.ReadLine();
-
}
-
}
a) A, A
b) B, B
c) Compile time error
d) A, B
Answer
Answer: d [Reason:] The method overidding procedure has been used to produce the values from two display().
Output : A B
8. The modifier used to hide the base class methods is ?
a) Virtual
b) New
c) Override
d) Sealed
Answer
Answer: b [Reason:] Used in condition when we cannot use virtual to override a base class method. Hence, we use ‘New’ to hide the base class methods and redefine the method defined in sub class.
9. To override a method in subclass, baseclass method should be defined as?
a) Virtual
b) Abstract
c) Override
d) All of the mentioned.
Answer
Answer: d [Reason:] None.
10. What will be the output for the given set of code?
-
class a
-
{
-
public void fun()
-
{
-
Console.WriteLine("base method");
-
}
-
}
-
class b: a
-
{
-
public new void fun()
-
{
-
Console.WriteLine(" derived method ");
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
b k = new b();
-
k.fun();
-
Console.ReadLine();
-
}
-
}
a) Base method
b) Derived method
c) Code runs successfully prints nothing
d) Compile time error
Answer
Answer: b [Reason:] Use of ‘new’ modifier hides the inherited member i.e it makes only inherited member inaccessible in derived class and hence calls suitable method().
Output :derived method
C# MCQ Set 3
1. What is the output of the following set of code?
-
static void Main(string[] args)
-
{
-
int a = 5;
-
int s = 0, c = 0;
-
Mul (a, ref s, ref c);
-
Console.WriteLine(s + "t " +c);
-
Console.ReadLine();
-
}
-
static void Mul (int x, ref int ss, ref int cc)
-
{
-
ss = x * x;
-
cc = x * x * x;
-
}
a) 125 25
b) 25 125
c) Compile time error
d) 0 0
Answer
Answer: b [Reason:] The value of variable a is passed by value while value of variable s and c is passed by reference.
Output: 25 125.
2. Which of following statements are correct about functions?
a) C# allows a function to have arguments with default values
b) Redefining a method parameter in the method’s body causes an exception
c) C# allows function to have arguments with default values
d) Omitting the return type in method definition results into exception
Answer
Answer: a [Reason:] None.
3. What is output of the code?
-
static void Main(string[] args)
-
{
-
Mul();
-
m();
-
Console.ReadLine();
-
}
-
static void Mul()
-
{
-
Console.WriteLine("4");
-
}
-
static void m()
-
{
-
Console.WriteLine("3");
-
Mul();
-
}
a) 4 3 3
b) 4 4 3
c) 4 3 4
d) 3 4 4
Answer
Answer: c [Reason:] First Mul() will be executed to print the number ‘4’ after that function m() will be executed to print the number ‘3’ and at last mentioned function Mul() will be executed to print the statement 4 to return the output as 4 3 4.
Output: 4 3 4.
4. What is the output of the code ?
-
static void Main(string[] args)
-
{
-
m();
-
Console.ReadLine();
-
}
-
static void m()
-
{
-
Console.WriteLine("HI");
-
m();
-
}
a) HI HI HI
b) HI
c) Stack overflow exception
d) Compile time error
Answer
Answer: c [Reason:] Control of statement when enters for once in m() does not go out, then it executes again and again inside the block until stack overflow exception occurs.
5. When a function fun() is to receive an int, a single & a double and it is to return a decimal, then the correct way of defining this function is?
a) static fun(int i, single j, double k) { return decimal; } b) static decimal fun(int i, single, double k) { } c) decimal fun(int i, single j, double k) { } d) decimal static fun(int i, single j, double k) { }
Answer
Answer: b [Reason:] Correct way of declaration of function is defined as return_type name of function(returntype).
6. What is the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 10;
-
double d = 35.78;
-
fun(i);
-
fun(d);
-
Console.ReadLine();
-
}
-
static void fun(double d)
-
{
-
Console.WriteLine(d);
-
}
a) 35.78
10
b) 10
35.00
c) 10
35.78
d) None of the mentioned
Answer
Answer: c [Reason:] ‘int’ datatype is sub datatype of ‘double’.Hence, when first part of func() is executed it is integer part and hence when second part is executed it is double.
Output:10
35.78
7. How many values does a function return?
a) 0
b) 2
c) 1
d) any number of values
Answer
Answer: c [Reason:] A method can return only either single value or no value if no then it’s declared as void method();
8. Output for the following set of code :
-
static void Main(string[] args)
-
{
-
int y = 3;
-
y++;
-
if (y <= 5)
-
{
-
Console.WriteLine("hi");
-
Main(args);
-
}
-
Console.ReadLine();
-
}
a) hi hi
b) hi
c) Stack overflow exception
d) None of the mentioned
Answer
Answer: c [Reason:] If loop never gets over, it will execute continuously.The control never goes out of ‘if’ statement.
Output: hi
hi
.
.
.
stack overflow exception
9. Which return statement correctly returns the output:
a) public int cube(int x)
{
return (x + x);
}
b) public int cube(int x)
return (x + x);
c) public int cube(int x)
{
return x + x;
}
d) None of mentioned
Answer
Answer: a [Reason:] The correct syntax of return statement is defined within block of statements as { return(statement);}.
10. What is the output for selective code ?
-
public static void Main(string[] args)
-
{
-
p();
-
void p()
-
{
-
Console.WriteLine("hi");
-
}
-
}
a) Compile time error
b) hi
c) hi infinite times
d) None of the mentioned
Answer
Answer: a [Reason:] invalid definition of function p() inside main().
C# MCQ Set 4
1. Which of these methods of the 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 as string.Length and hence invokes the length() method.
2. Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?
a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()
Answer
Answer: a [Reason:] getBytes() stores the character in an array of bytes. It uses default character to byte conversions.
3. Which of these methods can be used to convert all characters in a String into a character array?
a) CharAt()
b) getChars()
c) TocharArray()
d) All of the mentioned
Answer
Answer: c [Reason:] None.
4. What will be the output of the given code snippet?
-
static void main(String args[])
-
{
-
char chars[] = {'x', 'y', 'z'};
-
String s = new String(chars);
-
Console.WriteLine(s);
-
}
a) x
b) xy
c) z
d) xyz
Answer
Answer: d [Reason:] String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “xyz”.
Output :xyz
5. Choose the effective stringBuilder method which helps in producing output for the given code?
-
static void Main(string[] args)
-
{
-
StringBuilder s = new StringBuilder("object");
-
s./*______*/("Oriented Language");
-
Console.WriteLine(s);
-
Console.ReadLine();
-
}
-
Output : objectOriented Language
a) Insert()
b) Add()
c) Append()
d) Join()
Answer
Answer: c [Reason:] static void Main(string[] args)
{
StringBuilder s = new StringBuilder(“object”);
s.Append(“Oriented Language”);
Console.WriteLine(s);
Console.ReadLine();
}
Output : objectOriented Language
6. What will be the output for the given code snippet?
-
static void Main(string[] args)
-
{
-
string s = " i love you";
-
Console.WriteLine(s.IndexOf('l') + " " + s.lastIndexOf('o') + " " + s.IndexOf('e'));
-
Console.ReadLine();
-
}
a) 3 5 7
b) 4 5 6
c) 3 9 6
d) 2 4 6
Answer
Answer: c [Reason:] indexof(‘l’) and lastIndexof(‘o’) are pre defined functions which are used to get the index of first and last occurrence of
the character pointed by l and c respectively in the given array.
Output : 3, 9, 6
7. Which of these methods of class String is used to extract all the characters from a String object?
a) CHARAT()
b) Remove()
c) charAt()
d) Replace()
Answer
Answer: b [Reason:] Replace() replaces all instances of a character with a new character while Remove extracts characters from the string.
8. What will be the output of the given code snippet?
-
static void Main(string[] args)
-
{
-
string c = "hello";
-
string c1 = c.Remove(1);
-
Console.WriteLine(c1);
-
Console.ReadLine();
-
}
a) ello
b) h
c) hell
d) none of the mentioned
Answer
Answer: b [Reason:] The remove() deletes characters from the string except the character which is specified with its given position.
Output : h
9. How is a string typically processed?
a) On a character by character basis
b) On a string by string basis
c) Both a & b
d) None of the mentioned
Answer
Answer: a [Reason:] None.
10. How to print on the screen?
a) Console.WriteLine(“”);
b) Console.WriteLine(“”);
c) Console.WriteLine(“”);
d) Console.WriteLine(“”);
Answer
Answer: c [Reason:] Console.WriteLine(“”);
Output :
C# MCQ Set 5
1. Which of the following string() method are used to compare two strings with each other?
a) CopyTo()
b) Copy()
c) Compare()
d) CompareTo()
Answer
Answer: b [Reason:] Creates a new string by copying one string to another.
2. 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.
3. What is output for the following set of code:
-
static void Main(string[] args)
-
{
-
string s1 = " Cshr ";
-
string s2 = s1.Insert(3 , " a ");
-
string s3 = s2.Insert(5 , " p ");
-
for (int i = 0;i < s3.Length; i++)
-
Console.WriteLine(s3[i]);
-
Console.ReadLine();
-
}
a) Cshar
b) CsharP
c) Csharp
d) Cshrap
Answer
Answer: c [Reason:] Insertion of character ‘a’ at postion ‘3’ using insert() which returns a new string with a substring inserted at a specified location.
Output: Csharp
4. Which of the following statement is correct about a string in C#.NET?
a) The System.Array class is used to represent a string
b) A string has a zero-based index
c) A number cannot be represented in the form of a string
d) A string is mutable because it can be modified once it has been created
Answer
Answer: b [Reason:] None.
5. What is output for the following set of code?
-
static void Main(string[] args)
-
{
-
string s1 = "Hello";
-
string s2 = "hello";
-
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 first comparison it is being checked either two strings are equal or not but in second comparison it is checked whether two references are equal or not.
Output: Unequal
Unequal
6. Choose Output for the following set of code :
-
static void Main(string[] args)
-
{
-
string s1 = "Hello" + " I " + "Love" + " ComputerScience ";
-
Console.WriteLine(s1);
-
Console.ReadLine();
-
}
a) HelloILoveComputerScience
b) Hello I Love ComputerScience
c) Compile time error
d) Hello
Answer
Answer: b [Reason:] Here ‘+’ defined operator works as concatenation for strings.
Output : Hello I Love ComputerScience.
7. Correct way to find if contents of two strings are equal ?
a) if (s1 = s2)
b) if (s1 != s2)
c) if (strcmp (s1 ,s2))
d) if ( s1 is s2)
Answer
Answer: c [Reason:] “==” operator used to compare length of two strings and strcmp() is the inbuilt method derived from string class.
8. Which of the following statements are correct?
a) String is value type
b) String literals can contain any character literal including escape sequences
c) The equality operators are defined to compare values of string objects as well as references
d) All of the mentioned
Answer
Answer: b [Reason:] None
9. 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);
Hello I Love ComputerScience.
10. The Method use 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.