C# MCQ Set 1
1. Which among these access specifiers should be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned
Answer
Answer: b [Reason:] main() method must be specified public as it called by Csharp run time system outside of the program,by default main is private in nature if no access specifier is used.
2. Which of these is used as default for a member of a class if no access specifier is used for it?
a) private
b) public
c) protected internal
d) protected
Answer
Answer: a [Reason:] None.
3. What is the process by which we can control what parts of a program can access the members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer
Answer: c [Reason:] None.
4. Which of these base class are accessible to the derived class members?
a) static
b) protected
c) private
d) shared
Answer
Answer: b [Reason:] None.
5. What will be the output of the given code snippet?
-
class access
-
{
-
public int x;
-
private int y;
-
public void cal(int a, int b)
-
{
-
x = a + 1;
-
y = b;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
access obj = new access();
-
obj.cal(2, 3);
-
Console.WriteLine(obj.x + " " + obj.y);
-
}
-
}
a) 3 3
b) 2 3
c) Run time error
d) Compile time error
Answer
Answer: d [Reason:] ‘y’ is defined privately which cannot be accessed outside its scope.
6. What will be the output of the given code snippet?
-
class access
-
{
-
public int x;
-
private int y;
-
public void cal(int a, int b)
-
{
-
x = a + 1;
-
y = b;
-
}
-
public void print()
-
{
-
Console.WriteLine(" " + y);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
access obj = new access();
-
obj.cal(2, 3);
-
Console.WriteLine(obj.x);
-
obj.print();
-
Console.ReadLine();
-
}
-
}
a) 2 3
b) 3 3
c) Run time error
d) Compile time error
Answer
Answer: b [Reason:] None.
7. What will be the output of the following set of code?
-
class sum
-
{
-
public int x;
-
public int y;
-
public int add (int a, int b)
-
{
-
x = a + b;
-
y = x + b;
-
return 0;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sum obj1 = new sum();
-
sum obj2 = new sum();
-
int a = 2;
-
obj1.add(a, a + 1);
-
obj2.add(5, a);
-
Console.WriteLine(obj1.x + " " + obj2.y);
-
Console.ReadLine();
-
}
-
}
a) 6, 9
b) 5, 9
c) 9, 10
d) 3, 2
Answer
Answer: b [Reason:] Here, a = 2, a + 1 = 2 + 1 = 3.
So, a = 2, b = 3.
x = 2 + 3 = 5.
y = 5 + 3 = 8.
Similarly, a = 5, b = a + 1 = 4.
y = 5 + 4 = 9.
Output : 5, 9.
8. What will be the output of the following set of code?
-
class static_out
-
{
-
public static int x;
-
public static int y;
-
public int add(int a, int b)
-
{
-
x = a + b;
-
y = x + b;
-
return 0;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
static_out obj1 = new static_out();
-
static_out obj2 = new static_out();
-
int a = 2;
-
obj1.add(a, a + 1);
-
obj2.add(5, a);
-
Console.WriteLine(static_out.x + " " + static_out.y );
-
Console.ReadLine();
-
}
-
}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
Answer
Answer: c [Reason:] None.
Output : 7, 9
9. Which of these access specifiers must be used for class so that it can be inherited by another sub class?
a) public
b) private
c) both public & private
d) none of the mentioned
Answer
Answer: a [Reason:] None.
10. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program
b) private members of class can only be accessed by other members of the class
c) private members of class can be inherited by a sub class, and become protected members in sub class
d) protected members of a class can be inherited by a sub class, and become private members of the sub class
Answer
Answer: c [Reason:] private members of a class cannot be inherited by a sub class.
11. What will be the output of code snippet?
-
class test
-
{
-
public int a;
-
public int b;
-
public test(int i, int j)
-
{
-
a = i;
-
b = j;
-
}
-
public void meth(test o)
-
{
-
o.a *= 2;
-
o.b /= 2;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
test obj = new test(10, 20);
-
obj.meth(obj);
-
Console.WriteLine(obj.a + " " + obj.b);
-
Console.ReadLine();
-
}
-
}
a) 20, 40
b) 40, 20
c) 20, 10
d) 10, 20
Answer
Answer: c [Reason:] None.
Output :20, 10
12. Accessibility modifiers defined in a class are?
a) public, private, protected
b) public, internal, protected internal.
c) public, private, internal, protected internal.
d) public, private, protected, internal, protected internal
Answer
Answer: d [Reason:] None.
C# MCQ Set 2
1. Which of these methods of class String is used to extract a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Answer
Answer: b [Reason:] None.
2. What is the output for the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String s1 = "one";
-
String s2 = string.Concat(s1 + " " + "two");
-
Console.WriteLine(s2);
-
Console.ReadLine();
-
}
-
}
a) one
b) two
c) one two
d) two one
Answer
Answer: c [Reason:] Two strings can be concatenated using Concat() method.
Output: one two
3. Which of these methods of class String is used to remove leading and trailing whitespaces?
a) startsWith()
b) TrimEnd()
c) Trim()
d) TrimStart()
Answer
Answer: c [Reason:] Removes white space from the string.
4. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String c = " Hello World ";
-
String s = c.Trim();
-
Console.WriteLine("""+s+""");
-
Console.ReadLine();
-
}
-
}
a) ” Hello World ”
b) “HelloWorld”
c) “Hello World”
d) “Hello”
Answer
Answer: c [Reason:] Trim() method is used to remove leading and trailing whitespaces in a string.
Output: “Hello World”
5. What will be the output of the code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String s1 = "CSHARP";
-
String s2 = s1.Replace('H','L');
-
Console.WriteLine(s2);
-
Console.ReadLine();
-
}
-
}
a) CSHAP
b) CSHP
c) CSHALP
d) CSHP
Answer
Answer: c [Reason:] Replace() method replaces all occurrences of a single character in invoking strings with another character. s1.Replace(‘H’,’L’) replaces every occurrence of ‘H’ in CSHARP by ‘L’, giving CSHALP.
Output: CSHALP
6. What will be the output of the code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String s1 = "Hello World";
-
String s2 = s1.Substring(0, 4);
-
Console.WriteLine(s2);
-
Console.ReadLine();
-
}
-
}
a) Hello
b) Hell
c) H
d) Hello World
Answer
Answer: b [Reason:] None.
Output: Hell
7. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
String s = "Hello World";
-
int i = s.IndexOf('o');
-
int j = s.LastIndexOf('l');
-
Console.WriteLine(i + " " + j);
-
Console.ReadLine();
-
}
-
}
a) 9 5
b) 4 9
c) 9 0
d) 9 4
Answer
Answer: b [Reason:] None.
Output: 4 9
8. What will be the output of the given code snippet?
-
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
9. What will be the output of the given code snippet?
-
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 a value greater than zero if the invoking string is greater than the string compared to 4
Output: z
y
10. What will be the output of the code snippet?
-
static void main(String args[])
-
{
-
char chars[] = {'a', 'b', 'c'};
-
String s = new String(chars);
-
Console.WriteLine(s);
-
}
a) a
b) b
c) ab
d) abc
Answer
Answer = d [Reason:] None.
Output: abc
11. What will be the output of 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 function 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
C# MCQ Set 3
1. Which of these clauses will be executed even if no exceptions are found?
a) throws
b) finally
c) throw
d) catch
Answer
Answer: b [Reason:] finally keyword is used to define a set of instructions that will be executed irrespective of whether the exception is found or not.
2. A single try block must be followed by which of these?
a) finally
b) catch
c) Both finally & catch
d) None of the mentioned
Answer
Answer: c [Reason:] Try block can be followed by any of finally or catch block, try block checks for exceptions and work is performed by finally and catch block as per the exception.
3. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
Answer
Answer: a [Reason:] None.
4. Which of these exceptions will occur if we try to access the index of an array beyond its length?
a) ArithmeticException
b) ArrayException
c) ArrayArguementException
d) IndexOutOfRangeException
Answer
Answer: d [Reason:] IndexOutOfRangeException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.
5. What will be the output of the given code snippet?
-
class program
-
{
-
public static void Main(string[] args)
-
{
-
try
-
{
-
int a = args.Length;
-
int b = 1 / a;
-
Console.WriteLine(a);
-
}
-
catch (ArithmeticException e)
-
{
-
Console.WriteLine("1");
-
}
-
Console.ReadLine();
-
}
-
}
a) 0
b) 1
c) Compile time error
d) Runtime error
Answer
Answer: b [Reason:] None.
6. What will be the output of given code snippet?
-
class program
-
{
-
public static void Main(string[] args)
-
{
-
try
-
{
-
throw new NullReferenceException("C");
-
Console.WriteLine("A");
-
}
-
catch (ArithmeticException e)
-
{
-
Console.WriteLine("B");
-
}
-
Console.ReadLine();
-
}
-
}
a) A
b) B
c) Compile time error
d) Runtime error
Answer
Answer: d [Reason:] Try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
7. What will be the output of the given code snippet?
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
try
-
{
-
int a = 1;
-
int b = 10 / a;
-
try
-
{
-
if (a == 1)
-
a = a / a - a;
-
if (a == 2)
-
{
-
int[] c = { 1 };
-
c[8] = 9;
-
}
-
}
-
finally
-
{
-
Console.WriteLine("A");
-
}
-
}
-
catch (IndexOutOfRangeException e)
-
{
-
Console.WriteLine("B");
-
}
-
Console.ReadLine();
-
}
-
}
a) A
b) B
c) AB
d) BA
Answer
Answer: a [Reason:] The inner try block does not have a catch which can tackle IndexOutOfRangeException hence finally is executed which prints ‘A’. The outer try block does have catch for IndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
8. What will be the output of the given code snippet?
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
try
-
{
-
int a = args.Length;
-
int b = 10 / a;
-
Console.WriteLine(a);
-
try
-
{
-
if (a == 1)
-
a = a / a - a;
-
if (a == 2)
-
{
-
int[] c = { 1 };
-
c[8] = 9;
-
}
-
}
-
catch (IndexOutOfRangeException e)
-
{
-
Console.WriteLine("TypeA");
-
}
-
}
-
catch (ArithmeticException e)
-
{
-
Console.WriteLine("TypeB");
-
}
-
Console.ReadLine();
-
}
-
}
a) TypeA
b) TypeB
c) 0TypeA
d) Compile time error
Answer
Answer: b [Reason:] None.
9. Which of the following keywords is used by the calling function to guard against the exception that is thrown by called function?
a) try
b) throw
c) throws
d) catch
Answer
Answer: c [Reason:] If a method is capable of causing an exception that it does not handle. It must specify this behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.
10. Which of these classes is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw
Answer
Answer: c [Reason:] None.
C# MCQ Set 4
1. Which of these keywords are used to implement synchronization?
a) synchronize
b) syn
c) synch
d) synchronized
Answer
Answer: d [Reason:] None.
2. Which keyword is used for using the synchronization features defined by the Monitor class?
a) lock
b) synchronized
c) monitor
d) locked
Answer
Answer: a [Reason:] The C# keyword lock is really just shorthand for using the synchronization features defined by the Monitor class, which is defined in the System.Threading namespace.
3. What is synchronization in reference to a thread?
a) Its a process of handling situations when two or more threads need access to a shared resource
b) Its a process by which many thread are able to access same shared resource simultaneously
c) Its a process by which a method is able to access many different threads simultaneously
d) Its a method that allow to many threads to access any information the require
Answer
Answer: a [Reason:] When two or more threads need to access the same shared resource, they need some way to ensure that the resource will be used by only one thread at a time, the process by which this is achieved is called synchronization.
4. Which method is called when a thread is blocked from running temporarily?
a) Pulse()
b) PulseAll()
c) Wait()
d) Both Pulse() & Wait()
Answer
Answer: c [Reason:] When a thread is temporarily blocked from running, it calls Wait( ). This causes the thread to go to sleep and the lock for that object to be released, allowing another thread to acquire the lock.
5. What kind of exception is being thrown if Wait(),Pulse() or PulseAll() iscalled from code that is not within synchronized code?
a) System I/O Exception
b) DivideByZero Exception
c) SynchronizationLockException
d) All of the mentioned
Answer
Answer: c [Reason:] A SynchronizationLockException will be thrown if Wait(), Pulse(), or PulseAll() is called from code that is not within synchronized code, such as a lock block.
6. What is mutex?
a) a mutually exclusive synchronization object
b) can be acquired by more than one thread at a time
c) helps in sharing of resource which can be used by one thread
d) all of the mentioned
Answer
Answer: a [Reason:] A mutex is a mutually exclusive synchronization object. This means it can be acquired by one and only one thread at a time. The mutex is designed for those situations in which a shared resource can be used by only one thread at a time.
7. What is Semaphore?
a) Grant more than one thread access to a shared resource at the same time
b) Useful when a collection of resources is being synchronized
c) Make use of a counter to control access to a shared resource
d) All of the mentioned
Answer
Answer: d [Reason:] A semaphore is similar to a mutex except that it can grant more than one thread access to a shared resource at the same time. Thus, the semaphore is useful when a collection of resources is being synchronized. A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, access is denied.
8. Which method is used to abort thread prior to it’s normal execution?
a) sleep()
b) terminate()
c) suspend()
d) Abort()
Answer
Answer: d [Reason:] To terminate a thread prior to its normal conclusion, use Thread.Abort( ). Its simplest form is shown here:
public void Abort()
Abort() causes a ThreadAbortException to be thrown to the thread on which Abort() is called. This exception causes the thread to terminate.
9. Which of these statements is incorrect?
a) By multithreading CPU’s idle time is minimized, and we can take maximum use of it
b) By multitasking CPU’s idle time is minimized, and we can take maximum use of it
c) Two thread in Csharp can have same priority
d) A thread can exist only in two states, running and blocked
Answer
Answer: d [Reason:] Thread exist in several states, a thread can be running, suspended, blocked, terminated & ready to run.
10. What is multithreaded programming?
a) It’s a process in which two different processes run simultaneously
b) It’s a process in which two or more parts of same process run simultaneously
c) Its a process in which many different process are able to access same information
d) Its a process in which a single process can access information from many sources
Answer
Answer: b [Reason:] Multithreaded programming a process in which two or more parts of same process run simultaneously.
C# MCQ Set 5
1. Select the relevant output for the following set of code :
-
static void Main(string[] args)
-
{
-
byte varA = 10;
-
byte varB = 20;
-
long result = varA & varB;
-
Console.WriteLine("{0} AND {1} Result :{2}", varA, varB, result);
-
varA = 10;
-
varB = 10;
-
result = varA & varB;
-
Console.WriteLine("{0} AND {1} Result : {2}", varA, varB, result);
-
Console.ReadLine();
-
}
a) 0, 20
b) 10, 10
c) 0, 10
d) 0, 0
Answer
Answer: c [Reason:] When ‘OR’ operations is done on the binary values following are the results of OR.
‘OR’ means addition(+) operation.
0 (false) + 0(false) = 0 (false)
1 (True) + 0(false) = 1 (True)
0(false) + 1(True) = 1 (True)
1(True) + 1(True) = 1 (True)
When using OR operation it gives FALSE only when both the values are FALSE. In all other cases ‘OR’ operation gives ‘true’.
Output : 10 AND 20 Result :0.
10 AND 10 Result :10.
2. Select the relevant output for the following set of code :
-
public static void Main()
-
{
-
byte varA = 10;
-
byte varB = 20;
-
long result = varA | varB;
-
Console.WriteLine("{0} OR {1} Result :{2}", varA, varB, result);
-
varA = 10;
-
varB = 10;
-
result = varA | varB;
-
Console.WriteLine("{0} OR {1} Result : {2}", varA, varB, result);
-
}
a) 20, 10
b) 30, 10
c) 10, 20
d) 10, 10
Answer
Answer: b [Reason:] There are two kinds of Shift operations “Right Shift” and “Left Shift”. Right Shift operation is used for shifting the bit positions towards right side.Left Shift operation is used for shifting the bit positions towards left side. When Right Shift operations are done on a binary value the bits are shifted one position towards the right.
Output :10 OR 20 Result :30.
10 OR 10 Result :10.
3. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
byte b1 = 0 * AB;
-
byte b2 = 0 * 99;
-
byte temp;
-
temp = (byte) ~b2;
-
Console.Write( temp + " ");
-
temp = (byte) (b1 << b2);
-
Console.Write(temp + " ");
-
temp = (byte)(b2 >> 2);
-
Console.WriteLine(temp);
-
Console.ReadLine();
-
}
a) 101 0 34
b) 103 2 38
c) 102 0 38
d) 101 1 35
Answer
Answer: c [Reason:] None.
Output:102 0 38.
4. Which of the following options is not a Bitwise Operator in C#?
a) &, |
b) ^, ~
c) <<, >>
d) +=, -=
Answer
Answer: d [Reason:] +=, -= are Assignment Operators in C#.
5. Select the output for the following set of Code:
-
bool a = true;
-
bool b = false;
-
a |= b;
-
Console.WriteLine(a);
-
Console.ReadLine();
a) 0
b) 1
c) True
d) False
Answer
Answer: c [Reason:] ‘bools’ are single bits, and so a bit-wise OR is the same as a logical OR.
Output : True.
6. Select the relevant code set to fill up the blank for the following program :
-
static void Main(string[] args)
-
{
-
int x = 10, y = 20;
-
int res;
-
/*_______________*/
-
Console.WriteLine(res);
-
}
a) x % y == 0 ? (x == y ? (x += 2):(y = x + y)):y = y*10;
b) x % y == 0 ? y += 10:(x += 10);
c) x % y == 0 ? return(x) : return (y);
d) All of the mentioned.
Answer
Answer: b [Reason:] None.
Output : {
int x = 10, y = 20;
int res;
x % y == 0 ? y += 10:(x += 10);
Console.WriteLine(res);
}
7. Select the output for the following set of code:
-
static void Main(string[] args)
-
{
-
int y = 5;
-
int x;
-
int k = (!(Convert.ToInt32(y) > 10))? x = y + 3 : x = y + 10;
-
Console.WriteLine(x);
-
Console.WriteLine(y);
-
Console.ReadLine();
-
}
a) 5, 8
b) 10, 4
c) 8, 5
d) 11, 8
Answer
Answer: c [Reason:] Since condition y > 10 is false and !(false) = true .So, first statement x = y + 3 is executed which is x = 8 with y = 5.
Output: 8, 5.
8. Which among the following is a conditional operator ?
a) ‘:?’
b) ?;
c) ?:
d) ??
Answer
Answer: c [Reason:] By definition.
9. Select the Output for the following set of code :
-
public static void Main(string[] args)
-
{
-
int a = 4;
-
int c = 2;
-
bool b = (a % c == 0 ? true : false);
-
Console.WriteLine(b.ToString());
-
if (a/c == 2)
-
{
-
Console.WriteLine("true");
-
}
-
else
-
{
-
Console.WriteLine("false");
-
}
-
Console.ReadLine();
-
}
a) True
False
b) False
True
c) True
True
d) False
False
Answer
Answer: c [Reason:] a % c == 0 condition is true as (4 % 2 == 0). So, b is evaluated as true.Now (a/c == 2) which means if condition is also true hence it is evaluated as true.
Output: True
True
10. Arrange the operators in the increasing order as defined in C#:
!=, ?:, &, ++, &&
a) ?: < && < != < & < ++
b) ?: < && < != < ++ < &
c) ?: < && < & <!= < ++
d) ?: < && < != < & < ++
Answer
Answer: c [Reason:] By definition.