C# MCQ Set 1
1. Why are generics used?
a) Generics make code more fast
b) Generics make code more optimised and readable
c) Generics add stability to your code by making more of your bugs detectable at compile time
d) Generics add stability to your code by making more of your bugs detectable at run time
Answer
Answer: c [Reason:] Generics add stability to your code by making more of your bugs detectable at compile time.
2. Which of these type parameters is used for generic methods to return and accept any type of object?
a) K
b) N
c) T
d) V
Answer
Answer: c [Reason:] T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.
3. Which of these is an correct way of defining generic method?
a) name(T1, T2, …, Tn) { /* … */ }
b) public name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) name{T1, T2, …, Tn} { /* … */ }
Answer
Answer: b [Reason:] The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method’s return type. For static generic methods, the type parameter section must appear before the method’s return type.
4. What will be the output of the given code snippet?
-
public class Generic<T>
-
{
-
Stack<T> stk = new Stack<T>();
-
public void push(T obj)
-
{
-
stk.Push(obj);
-
}
-
public T pop()
-
{
-
T obj = stk.Pop();
-
return obj;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<int> g = new Generic<int>();
-
g.push("Csharp");
-
Console.WriteLine(g.pop());
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) Csharp
c) 0
d) Run time error
Answer
Answer: b
Output : Csharp
5. What will be the output of the given code snippet?
-
public class Generic<T>
-
{
-
Stack<T> stk = new Stack<T>();
-
public void push(T obj)
-
{
-
stk.Push(obj);
-
}
-
public T pop()
-
{
-
T obj = stk.Pop();
-
return obj;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<string> g = new Generic<string>();
-
g.push(30);
-
Console.WriteLine(g.pop());
-
Console.ReadLine();
-
}
-
}
a) 0
b) 30
c) Runtime Error
d) Compile time Error
Answer
Answer: b
Output : 30
6. What does the following code block define?
-
class Gen<T> {
-
T ob;
-
}
a) Generics class decleration
b) Decleration of variable
c) A simple class decleration
d) Both a & b
Answer
Answer: d [Reason:] class Gen
7. What will be the output of given code snippet?
-
public class Generic<T>
-
{
-
Stack<T> stk = new Stack<T>();
-
public void push(T obj)
-
{
-
stk.Push(obj);
-
}
-
public T pop()
-
{
-
T obj = stk.Pop();
-
return obj;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<string> g = new Generic<string>();
-
g.push("C++");
-
Console.WriteLine(g.pop() + " ");
-
Generic<int> g1 = new Generic<int>();
-
g1.push(20);
-
Console.WriteLine(g1.pop());
-
Console.ReadLine();
-
}
-
}
a) C++
b) 20
c) C++
20
d) 0
Answer
Answer: c
Output : C++
20
8. Select the type argument of open constructed type?
a) Gen<int>
b) Gen<T>
c) Gen<>
d) None of the mentioned
Answer
Answer: c [Reason:] A generic type, such as Gen
9. Choose the correct way to call subroutine fun() of the sample class?
-
class a
-
{
-
public void x(int p, double k)
-
{
-
Console.WriteLine("k : csharp!");
-
}
-
}
a) delegate void del(int i);
x s = new x();
del d = new del(ref s.x);
d(8, 2.2f);
b) delegate void del(int p, double k);
del d;
x s = new x();
d = new del(ref s.x);
d(8, 2.2f);
c) x s = new x();
delegate void d = new del(ref x);
d(8, 2.2f);
d) all of the mentioned
Answer
Answer: b [Reason:] None.
10. What does the following code set defines?
-
public Gen(T o) {
-
ob = o;
-
}
a) Generics class decleration
b) Decleration of variable
c) Generic constructor decleration
d) All of the mentioned
Answer
Answer: c [Reason:] None.
C# MCQ Set 2
1. What does URL stand for?
a) Uniform Resource Locator
b) Uniform Resource Latch
c) Universal Resource Locator
d) Universal Resource Latch
Answer
Answer: a [Reason:] None.
2. Which of these exceptions is thrown by the URL class’s constructors?
a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) URLNotFoundException
Answer
Answer: c [Reason:] None.
3. What does the following form define?
Protocol://HostName/FilePath?Query
a) Protocol specifies the protocol being used, such as HTTP
b) HostName identifies a specific server, such as mhprofessional.com or www.google.com
c) FilePath specifies the path to a specific file
d) All of the mentioned
Answer
Answer: d [Reason:] By definition.
4. Which of these classes is used to encapsulate IP address and DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
Answer
Answer: c [Reason:] InetAddress class encapsulates both IP address and DNS, we can interact with this class by using the name of an IP host.
5. Which of these is a standard for communicating multimedia content over email?
a) http
b) https
c) Mime
d) httpd
Answer
Answer: c [Reason:] MIME is an internet standard for communicating multimedia content over email. The HTTP protocol uses and extends the notion of MIME headers to pass attribute pairs between HTTP client and server.
6. What does the following method specify?
public static WebRequest Create(string requestUriString)
a) Creates a WebRequest object for the URI specified by the string passed by requestUriString
b) The object returned will implement the protocol specified by the prefix of the URI
c) The object will be an instance of the class that inherits WebRequest
d) All of the mentioned
Answer
Answer: d [Reason:] Creates a WebRequest object for the URI specified by the string passed by requestUriString. The object returned will
implement the protocol specified by the prefix of the URI.Thus, the object will be an instance of a class that inherits WebRequest. A NotSupportedException is thrown if the requested protocol is not available. A UriFormatException is thrown if the URI format is invalid.
C# MCQ Set 3
1. Choose the symbol which begins a preprocessor directive in C#.NET?
a) #
b) **
c) *
d) &
Answer
Answer: a [Reason:] #define, #elif, #else etc.
2. What is meant by preprocessor directive in C#.NET?
a) a form of command which are interpreted by the compiler
b) a form of macros like in c and c++ not exactly same to them , separately designed for C#.NET
c) always begins with a ‘#’ character occupies separate line of source of code
d) all of the mentioned
Answer
Answer: d [Reason:] Preprocessor directives are commands that are interpreted by the compiler and affect the output or behavior of the build process. The C# compiler does not have a separate preprocessor, like C and C++ we cannot use these directives to create macros. Preprocessing directives are top lines in our program that start with ‘#’. The ‘#’ is followed by an identifier that is the directive name.
3. What is meant by preprocessor directive #define?
a) defines a character sequence
b) helps in determining existence and non existence of a symbol
c) can be used to create function like macros as in C/C++
d) all of the mentioned
Answer
Answer: a [Reason:] The #define directive defines a character sequence called a symbol. The existence or nonexistence of a symbol can be determined by #if or #elif and is used to control compilation. #define which supports creation of function like macros in c/c++ does not support the same in C#.
4. Select the defined preprocessor in C#.NET?
a) #define
b) #elif
c) #else
d) All of the mentioned
Answer
Answer: d [Reason:] None.
5. What does preprocessor directive #if and #endif explains?
a) Enables compilation of sequence of code on condition basis
b) Express results into true or false on evaluation of condition
c) If expression following #if is true then code that is between #if and #endif is compiled otherwise skipped
d) All of the mentioned
Answer
Answer: d [Reason:] The #if and #endif directives enable conditional compilation of a sequence of code based upon whether an expression involving one or more symbols evaluates to true. A symbol is true if it has been defined. It is false otherwise.If the expression following #if is true, the code that is between it and #endif is compiled.Otherwise, the intervening code is skipped. The #endif directive marks the end of an #if block.
6. What will be the output of following code snippet?
-
#define pi
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace ConsoleApplication13
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
#if (!pi)
-
Console.WriteLine("i");
-
#else
-
Console.WriteLine("pi not define");
-
#endif
-
Console.WriteLine("ok");
-
Console.ReadLine();
-
}
-
}
-
}
a) i
pi not define
b) pi not define
ok
c) i
ok
d) ok
Answer
Answer: b [Reason:] The defined symbol ‘pi’ when compared as per ‘if’ condition, hence the outcome is false which results in skip of statement and hence executes statement after #else and finally the end statement after #endif.
Output: pi not define
ok
7. What will be the output of following code snippet?
-
#define DEBUG
-
#define MYTEST
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace ConsoleApplication13
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
#if (DEBUG && !MYTEST)
-
Console.WriteLine("DEBUG is defined");
-
#elif (!DEBUG && MYTEST)
-
Console.WriteLine("MYTEST is defined");
-
#elif (DEBUG && MYTEST)
-
Console.WriteLine("DEBUG and MYTEST are defined");
-
#else
-
Console.WriteLine("DEBUG and MYTEST are not defined");
-
#endif
-
Console.ReadLine();
-
}
-
}
-
}
a) DEBUG is defined
MYTEST is defined
b) MYTEST is defined
DEBUG and MYTEST are defined
c) DEBUG and MYTEST are not defined
MYTEST is defined
d) DEBUG and MYTEST are defined
Answer
Answer: d [Reason:] None.
8. What will be the output of the following code snippet?
-
#define DEBUG
-
#undef DEBUG
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace ConsoleApplication13
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
#if (DEBUG)
-
Console.WriteLine("DEBUG is defined");
-
#elif (!DEBUG && MYTEST)
-
Console.WriteLine("MYTEST is defined");
-
#elif (DEBUG && MYTEST)
-
Console.WriteLine("DEBUG and MYTEST are defined");
-
#else
-
Console.WriteLine("DEBUG and MYTEST are not defined");
-
#endif
-
Console.ReadLine();
-
}
-
}
-
}
a) DEBUG is defined
DEBUG and MYTEST are not defined
b) DEBUG and MYTEST are not defined
c) MYTEST is defined
DEBUG and MYTEST are not defined
d) DEBUG is defined
Answer
Answer: b [Reason:] #undef lets to undefine a symbol such that by using the symbol as the expression in a #if directive, the expression will evaluate to false i.e the symbol will be undefined in nature.
Output: DEBUG and MYTEST are not defined
9. Which preprocessor directive among the following forces the compiler to stop the compilation?
a) #warning
b) #endregion
c) #undef
d) #error
Answer
Answer: d [Reason:] The #error directive forces the compiler to stop compilation. It is used for debugging. The general form of the #error directive is #error error-message. When the #error directive is encountered, the error message is displayed.
10. Which among the following is not a preprocessor directive?
a) #ifdef
b) #pragma
c) #Or
d) #undef
Answer
Answer: c [Reason:] None.
C# MCQ Set 4
1. What will be the output of the following set of code?
-
{
-
int sum = 10;
-
try
-
{
-
int i;
-
for (i = -1; i < 3; ++i)
-
sum = (sum / i);
-
}
-
catch (ArithmeticException e)
-
{
-
Console.WriteLine("0");
-
}
-
Console.WriteLine(sum);
-
Console.ReadLine();
-
}
a) 0
b) 0 5
c) 0 -10
d) Compile time error
Answer
Answer: c [Reason:] Value of variable sum is printed as sum and is defined outside try & catch block. If defined inside the try block then sum would be undefined for execution.
Output : 0 -10
2. What will be the output of the following set of code?
-
{
-
try
-
{
-
int []a = {1, 2, 3, 4, 5};
-
for (int i = 0; i < 5; ++i)
-
Console.WriteLine(a[i]);
-
int x = (1 / Convert.ToInt32(0));
-
}
-
catch(IndexOutOfRangeException e)
-
{
-
Console.WriteLine("A");
-
}
-
catch(ArithmeticException e)
-
{
-
Console.WriteLine("B");
-
}
-
Console.ReadLine();
-
}
a) 1234
b) 12345
c) Run time error
d) 12345B
Answer
Answer: d [Reason:] Due to occurrence of arithmetic exception here ‘B’ is printed after 12345.
Output : 12345B
3. What will be the output of given code snippet?
-
{
-
try
-
{
-
int []a = {1, 2, 3, 4, 5};
-
for (int i = 0; i < 7; ++i)
-
Console.WriteLine(a[i]);
-
}
-
catch(IndexOutOfRangeException e)
-
{
-
Console.WriteLine("0");
-
}
-
Console.ReadLine();
-
}
a) 12345
b) 123450
c) 1234500
d) Compile time error
Answer
Answer: b [Reason:] When array index goes out of bound then IndexOutOfBoundsException exception is thrown by the system.
Output : 123450
4. What would be the output of following code snippet?
-
{
-
try
-
{
-
int a, b;
-
b = 0;
-
a = 10 / b;
-
Console.WriteLine("A");
-
}
-
catch(ArithmeticException e)
-
{
-
Console.WriteLine("B");
-
}
-
Console.ReadLine();
-
}
a) A
b) B
c) Compile time error
d) Run time error
Answer
Answer: b [Reason:] Since b = 0 since a = 10 / 0 so, arithmetic exception is caught and hence statement in catch block is executed.
Output : B
5. What would be the output of following code snippet?
-
{
-
try
-
{
-
int i, sum;
-
sum = 10;
-
for (i = -1 ;i < 3 ;++i)
-
{
-
sum = (sum / i);
-
Console.WriteLine(i);
-
}
-
}
-
catch(ArithmeticException e)
-
{
-
Console.WriteLine("0");
-
}
-
Console.ReadLine();
-
}
a) -1
b) 0
c) -1 0
d) -1 0 -1
Answer
Answer: c [Reason:] None.
Output : -1 0
6. What would be the output of following code snippet?
-
{
-
try
-
{
-
int a, b;
-
b = 0;
-
a = 5 / b;
-
Console.WriteLine("A");
-
}
-
catch(ArithmeticException e)
-
{
-
Console.WriteLine("B");
-
}
-
finally
-
{
-
Console.WriteLine("C");
-
}
-
Console.ReadLine();
-
}
a) A
b) B
c) B C
d) Run time error
Answer
Answer: c [Reason:] finally keyword is used to execute before catch and try block is executed.
Output : B C
7. What would be the output of given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int i;
-
int v = 40;
-
int[] x = new int[5];
-
try
-
{
-
Console.WriteLine(" Enter the number: ");
-
index = Convert.ToInt32(Console.ReadLine());
-
x[index] = v;
-
}
-
catch(Exception e)
-
{
-
Console.WriteLine("Exception occured");
-
}
-
Console.WriteLine("Program executed");
-
}
-
}
a) Exception occured
b) Program executed
c) Exception occured
Program executed
d) Program executed
Exception occured
Answer
Answer: c [Reason:] None.
Output : Exception occured
Program executed
8. When no exception is thrown at runtime then who will catch it?
a) CLR
b) Operating System
c) Loader
d) Compiler
Answer
Answer: a [Reason:] None.
9. What would be the output of given code snippet?
-
public static void Main(string[] args)
-
{
-
try
-
{
-
int a, b, c = 5;
-
b = 0;
-
a = c / b;
-
Console.WriteLine("A");
-
}
-
catch (ArithmeticException e)
-
{
-
int c = 5;
-
int i = 10;
-
int z = 2 * c - i;
-
Console.WriteLine("B");
-
Console.WriteLine(z);
-
}
-
Console.ReadLine();
-
}
a) Compile time error
b) Run time error
c) B 0
d) B
Answer
Answer: c [Reason:] The catch block is called, as the exception is caught by the same block and hence statements are executed consecutively.
Output : B 0
10. Choose the correct statement which makes exception handling work in C#.NET?
a) .Net runtime makes search for the exception handler where exception occurs
b) If no exception is matched, exception handler goes up the stack and hence finds the match there
c) If no match is found at the highest level of stack call, then unhandledException is generated and hence termination of program occurs
d) All of the mentioned
Answer
Answer: d [Reason:] By definition of exceptionhandling mechanism in C#.NET.
C# MCQ Set 5
1. The capability of an object in Csharp to take number of different forms and hence display behaviour as according is known as:
a) Encapsulation
b) Polymorphism
c) Abstraction
d) None of the mentioned
Answer
Answer: b [Reason:] None.
2. Select the output for the given set of code?
-
public class sample
-
{
-
public static int x = 100;
-
public static int y = 150;
-
-
}
-
public class newspaper :sample
-
{
-
new public static int x = 1000;
-
static void Main(string[] args)
-
{
-
console.writeline(sample.x + " " + sample.y + " " + x);
-
}
-
}
a) 100 150 1000
b) 1000 150 1000
c) 100 150 1000
d) 100 150 100
Answer
Answer: c [Reason:] sample.x = 100
sample.y = 150
varhiable within scope of main() is x = 1000
Output : 100 150 1000
3. Which of the following keyword is used to change data and behavior of a base class by replacing a member of the base class with a new derived member?
a) Overloads
b) Overrides
c) new
d) base
Answer
Answer: c [Reason:] None.
4. Correct way to overload +operator?
a) public sample operator + ( sample a, sample b)
b) public abstract operator + (sample a,sample b)
c) public static sample operator + (sample a, sample b)
d) All of the mentioned
Answer
Answer: d [Reason:] None.
5. Correct statement about C# code is?
-
public class maths
-
{
-
public int x;
-
public virtual void a()
-
{
-
-
}
-
-
}
-
public class subject : maths
-
{
-
new public void a()
-
{
-
-
}
-
-
}
a) The subject class version of a() method gets called using sample class reference which holds subject class object
b) subject class hides a() method of base class
c) The code replaces the subject class version of a() with its math class version
d) None of the mentioned
Answer
Answer: d [Reason:] None.
6. Select the sequence of execution of function f1(), f2() & f3() in C# .NET CODE?
-
class base
-
{
-
public void f1() {}
-
public virtual void f2() {}
-
public virtual void f3() {}
-
}
-
class derived :base
-
{
-
new public void f1() {}
-
public override void f2() {}
-
public new void f3() {}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
baseclass b = new derived();
-
b.f1 ();
-
b.f2 ();
-
b.f3 ();
-
}
-
}
a) f1() of derived class get executed
f2() of derived class get executed
f3() of base class get executed
b) f1() of base class get executed
f2() of derived class get executed
f3() of base class get executed
c) f1() of base class get executed
f2() of derived class get executed
f3() of derived class get executed
d) f1() of derived class get executed
f2() of base class get executed
f3() of base class get executed
Answer
Answer: b [Reason:]None.
7. Which of the following statements is correct?
a) Each derived class does not have its own version of a virtual method
b) If a derived class does not have its own version of virtual method then one in base class is used
c) By default methods are virtual
d) All of the mentioned
Answer
Answer: c [Reason:] None.
8. Correct code to be added for overloaded operator – for C# .net code given below is?
-
class csharp
-
{
-
int x, y, z;
-
public csharp()
-
{
-
-
}
-
public csharp(int a ,int b ,int c)
-
{
-
x = a;
-
y = b;
-
z = c;
-
}
-
Add correct set of code here
-
public void display()
-
{
-
console.writeline(x + " " + y + " " + z);
-
}
-
class program
-
{
-
static void Main(String[] args)
-
{
-
csharp s1 = new csharp(5 ,6 ,8);
-
csharp s3 = new csharp();
-
s3 = - s1;
-
s3.display();
-
}
-
}
-
}
a)
-
public static csharp operator -(csharp s1)
-
{
-
csharp t = new csharp();
-
t.x = s1.x;
-
t.y = s1.y;
-
t.z = -s1.z;
-
return t;
-
}
b)
-
public static csharp operator -(csharp s1)
-
{
-
csharp t = new csharp();
-
t.x = s1.x;
-
t.y = s1.y;
-
t.z = s1.z;
-
return t;
-
}
c)
-
public static csharp operator -(csharp s1)
-
{
-
csharp t = new csharp();
-
t.x = -s1.x;
-
t.y = -s1.y;
-
t.z = -s1.z;
-
return t;
-
}
d) None of the mentioned
Answer
Answer: c [Reason:] None.
9. Selecting appropriate method out of number of overloaded methods by matching arguments in terms of number ,type and order and binding that selected method to object at compile time is called?
a) Static binding
b) Static Linking
c) Compile time polymorphism
d) All of the mentioned
Answer
Answer: d [Reason:] None.
10. Wrong statement about run time polymorphism is?
a) The overridden base method should be virtual,abstract or override
b) An abstract method is implicitly a virtual method
c) An abstract inherited property cannot be overridden in a derived class
d) Both override method and virtual method must have same access level modifier
Answer
Answer: c [Reason:] None.