Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

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

View 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

View 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} { /* … */ }

View 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?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<int> g = new Generic<int>();
  19.         g.push("Csharp");
  20.         Console.WriteLine(g.pop());
  21.         Console.ReadLine();
  22.     }
  23. }

a) Compile time error
b) Csharp
c) 0
d) Run time error

View Answer

Answer: b Output : Csharp

5. What will be the output of the given code snippet?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<string> g = new Generic<string>();
  19.         g.push(30);
  20.         Console.WriteLine(g.pop());
  21.         Console.ReadLine();
  22.     }
  23. }

a) 0
b) 30
c) Runtime Error
d) Compile time Error

View Answer

Answer: b Output : 30

6. What does the following code block define?

  1.  class Gen<T> {  
  2.                   T ob;    
  3.               }

a) Generics class decleration
b) Decleration of variable
c) A simple class decleration
d) Both a & b

View Answer

Answer: d [Reason:] class Gen This defines the generics declaration where ‘T’ is the name of type parameter.This parameter is used as a placeholder for the actual type that will be specified when a Gen object is created.Gen is a generic class . T is used to declare a variable called ‘ob’.

7. What will be the output of given code snippet?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<string> g = new Generic<string>();
  19.         g.push("C++");
  20.         Console.WriteLine(g.pop() + " ");
  21.         Generic<int> g1 = new Generic<int>();
  22.         g1.push(20);
  23.         Console.WriteLine(g1.pop());
  24.         Console.ReadLine();
  25.     }
  26. }

a) C++
b) 20
c) C++
20
d) 0

View 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

View Answer

Answer: c [Reason:] A generic type, such as Gen, is an abstraction.In C# terminology, a construct such as Gen is called an open constructed type, because the type parameter T (rather than an actual type, such as int) is specified.

9. Choose the correct way to call subroutine fun() of the sample class?

  1. class a
  2. {
  3.     public void x(int p, double k)
  4.     {
  5.         Console.WriteLine("k : csharp!");
  6.     }
  7. }

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

View Answer

Answer: b [Reason:] None.

10. What does the following code set defines?

  1.  public Gen(T o) {
  2.                      ob = o; 
  3.                  }

a) Generics class decleration
b) Decleration of variable
c) Generic constructor decleration
d) All of the mentioned

View 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

View 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

View 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

View 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

View 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

View 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

View 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) &

View 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

View 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

View 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

View 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

View 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?

  1.  #define pi 
  2.  using System;
  3.  using System.Collections.Generic;
  4.  using System.Linq;
  5.  using System.Text;
  6.  using System.Threading.Tasks;
  7.  
  8.  namespace ConsoleApplication13
  9. {
  10.     class Program
  11.     {   
  12.         static void Main(string[] args)
  13.         {
  14.             #if (!pi) 
  15.             Console.WriteLine("i");
  16.             #else 
  17.             Console.WriteLine("pi not define");
  18.             #endif
  19.             Console.WriteLine("ok");
  20.             Console.ReadLine();
  21.        }
  22.    }
  23. }

a) i
pi not define
b) pi not define
ok
c) i
ok
d) ok

View 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?

  1. #define DEBUG 
  2. #define MYTEST
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApplication13
  10. {
  11.     class Program
  12.     {   
  13.         static void Main(string[] args)
  14.         {
  15.             #if (DEBUG && !MYTEST)
  16.             Console.WriteLine("DEBUG is defined");
  17.             #elif (!DEBUG && MYTEST)
  18.             Console.WriteLine("MYTEST is defined");
  19.             #elif (DEBUG && MYTEST)
  20.             Console.WriteLine("DEBUG and MYTEST are defined");
  21.             #else
  22.             Console.WriteLine("DEBUG and MYTEST are not defined");
  23.             #endif
  24.             Console.ReadLine();
  25.         }
  26.     }
  27. }

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

View Answer

Answer: d [Reason:] None.

8. What will be the output of the following code snippet?

  1. #define DEBUG 
  2. #undef DEBUG
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApplication13
  10. {
  11.     class Program
  12.     {   
  13.         static void Main(string[] args)
  14.         {
  15.             #if (DEBUG)
  16.             Console.WriteLine("DEBUG is defined");
  17.             #elif (!DEBUG && MYTEST)
  18.             Console.WriteLine("MYTEST is defined");
  19.             #elif (DEBUG && MYTEST)
  20.             Console.WriteLine("DEBUG and MYTEST are defined");
  21.             #else
  22.             Console.WriteLine("DEBUG and MYTEST are not defined");
  23.             #endif
  24.             Console.ReadLine();
  25.        }
  26.    }
  27. }

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

View 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

View 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

View Answer

Answer: c [Reason:] None.

C# MCQ Set 4

1. What will be the output of the following set of code?

  1.   {
  2.       int sum = 10;
  3.       try
  4.       {
  5.           int i;
  6.           for (i = -1; i < 3; ++i)
  7.           sum = (sum / i);
  8.       }
  9.       catch (ArithmeticException e)
  10.       {
  11.           Console.WriteLine("0");
  12.       }
  13.       Console.WriteLine(sum);
  14.       Console.ReadLine();
  15.   }

a) 0
b) 0 5
c) 0 -10
d) Compile time error

View 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?

  1. {
  2.     try 
  3.     {
  4.         int []a = {1, 2, 3, 4, 5};
  5.         for (int i = 0; i < 5; ++i) 
  6.         Console.WriteLine(a[i]);
  7.         int x = (1 / Convert.ToInt32(0));
  8.     }
  9.     catch(IndexOutOfRangeException e) 
  10.     {
  11.         Console.WriteLine("A");        	
  12.     }
  13.     catch(ArithmeticException e) 
  14.     {     	
  15.         Console.WriteLine("B");
  16.     }
  17.     Console.ReadLine();
  18. }

a) 1234
b) 12345
c) Run time error
d) 12345B

View 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?

  1. {
  2.     try 
  3.     {
  4.         int []a = {1, 2, 3, 4, 5};
  5.         for (int i = 0; i < 7; ++i) 
  6.         Console.WriteLine(a[i]);
  7.     }
  8.     catch(IndexOutOfRangeException e) 
  9.     {
  10.         Console.WriteLine("0");        	
  11.     }
  12.     Console.ReadLine();
  13. }

a) 12345
b) 123450
c) 1234500
d) Compile time error

View 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?

  1.  {
  2.      try 
  3.      {
  4.          int a, b;
  5.          b = 0;
  6.          a = 10 / b;
  7.          Console.WriteLine("A");
  8.      }
  9.      catch(ArithmeticException e) 
  10.      {
  11.          Console.WriteLine("B");        	
  12.      }
  13.      Console.ReadLine();
  14.  }

a) A
b) B
c) Compile time error
d) Run time error

View 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?

  1.  {
  2.      try 
  3.      {
  4.          int i, sum;
  5.          sum = 10;
  6.          for (i = -1 ;i < 3 ;++i) 
  7.          {
  8.              sum = (sum / i);
  9.              Console.WriteLine(i);
  10.          }
  11.      }
  12.      catch(ArithmeticException e) 
  13.      {
  14.          Console.WriteLine("0");
  15.      }
  16.      Console.ReadLine();
  17.  }

a) -1
b) 0
c) -1 0
d) -1 0 -1

View Answer

Answer: c [Reason:] None. Output : -1 0

6. What would be the output of following code snippet?

  1.  {
  2.      try 
  3.      {
  4.          int a, b;
  5.          b = 0;
  6.          a = 5 / b;
  7.          Console.WriteLine("A");
  8.      }
  9.      catch(ArithmeticException e) 
  10.      {
  11.          Console.WriteLine("B");
  12.      }
  13.      finally
  14.      {
  15.          Console.WriteLine("C");
  16.      }
  17.      Console.ReadLine();
  18.  }

a) A
b) B
c) B C
d) Run time error

View 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?

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int i;
  6.          int v = 40;
  7.          int[] x = new int[5];
  8.          try
  9.          {
  10.              Console.WriteLine(" Enter the number: ");
  11.              index = Convert.ToInt32(Console.ReadLine());
  12.              x[index] = v;
  13.          }
  14.          catch(Exception e)
  15.          {
  16.              Console.WriteLine("Exception occured");
  17.          }
  18.          Console.WriteLine("Program executed");
  19.      }
  20.  }

a) Exception occured
b) Program executed
c) Exception occured
Program executed
d) Program executed
Exception occured

View 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

View Answer

Answer: a [Reason:] None.

9. What would be the output of given code snippet?

  1.  public  static void Main(string[] args)
  2.  {
  3.      try
  4.      {
  5.          int a, b, c = 5;
  6.          b = 0;
  7.          a = c / b;
  8.          Console.WriteLine("A");
  9.      }
  10.      catch (ArithmeticException e)
  11.      {
  12.          int c = 5;
  13.          int i = 10;
  14.          int z = 2 * c - i;
  15.          Console.WriteLine("B");
  16.          Console.WriteLine(z);
  17.      }
  18.      Console.ReadLine();
  19.  }

a) Compile time error
b) Run time error
c) B 0
d) B

View 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

View 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

View Answer

Answer: b [Reason:] None.

2. Select the output for the given set of code?

  1.  public class sample
  2.  {
  3.      public static int x = 100;
  4.      public static int y = 150;
  5.  
  6.  }
  7.  public class newspaper :sample
  8.  {
  9.      new public static int x = 1000;
  10.      static void Main(string[] args)
  11.      {
  12.          console.writeline(sample.x + "  " + sample.y + "  " + x);
  13.      }
  14.  }

a) 100 150 1000
b) 1000 150 1000
c) 100 150 1000
d) 100 150 100

View 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

View 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

View Answer

Answer: d [Reason:] None.

5. Correct statement about C# code is?

  1.  public class maths
  2.  {
  3.      public int x;
  4.      public virtual void a()
  5.      {
  6.  
  7.      }
  8.  
  9.  }
  10.  public class subject : maths
  11.  {
  12.      new public void a()
  13.      {
  14.  
  15.      }
  16.  
  17.  }

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

View Answer

Answer: d [Reason:] None.

6. Select the sequence of execution of function f1(), f2() & f3() in C# .NET CODE?

  1.  class base
  2.  {
  3.      public void f1() {}
  4.      public virtual void f2() {}
  5.      public virtual  void f3() {}
  6.  }
  7.  class derived :base
  8.  {
  9.      new public void f1() {}
  10.      public override void f2() {}
  11.      public new void f3() {}
  12.  } 
  13.  class Program
  14.  {
  15.      static void Main(string[] args)
  16.      {
  17.          baseclass b = new derived();
  18.          b.f1 ();
  19.          b.f2 ();
  20.          b.f3 ();
  21.      }
  22.  }

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

View 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

View Answer

Answer: c [Reason:] None.

8. Correct code to be added for overloaded operator – for C# .net code given below is?

  1.  class csharp
  2.  {
  3.      int x, y, z;
  4.      public csharp()
  5.      {
  6.  
  7.      }
  8.      public csharp(int a ,int b ,int c)
  9.      {
  10.          x = a;
  11.          y = b;
  12.          z = c;
  13.      }
  14.      Add correct set of code here
  15.     public void display()
  16.     {
  17.         console.writeline(x + "  " + y + "  " + z);
  18.     }
  19.     class program
  20.     {
  21.         static void Main(String[] args)
  22.         {
  23.             csharp s1 = new csharp(5 ,6 ,8);
  24.             csharp s3 = new csharp();
  25.             s3 = - s1;
  26.             s3.display();
  27.         }
  28.     }
  29. }

a)

  1.  public static csharp operator -(csharp s1)
  2.     {
  3.         csharp t = new csharp();
  4.         t.x = s1.x;
  5.         t.y = s1.y;
  6.         t.z = -s1.z;
  7.         return t;
  8.     }

b)

  1.  public static csharp operator -(csharp s1)
  2.     {
  3.         csharp t = new csharp();
  4.         t.x = s1.x;
  5.         t.y = s1.y;
  6.         t.z = s1.z;
  7.         return t;
  8.     }

c)

  1. public static csharp operator -(csharp s1)
  2.     {
  3.         csharp t = new csharp();
  4.         t.x = -s1.x;
  5.         t.y = -s1.y;
  6.         t.z = -s1.z;
  7.         return t;
  8.     }

d) None of the mentioned

View 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

View 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

View Answer

Answer: c [Reason:] None.