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. Which procedure among the following should be used to implement a ‘Has a’ or a ‘Kind of’ relationship between two entities?
a) Polymorphism
b) Inheritance
c) Templates
d) All of the mentioned

View Answer

Answer: b [Reason:] None.

2. The number of levels of inheritance are?
a) 5
b) 4
c) 3
d) 2

View Answer

Answer: b [Reason:] None.

3. What does the following code signify?

  1.  class a
  2.  {
  3.  
  4.  
  5.  
  6.  }
  7.  class b : a
  8.  {
  9.      variable declaration;
  10.      method declaration;
  11.  }

a) Declaration of a base class
b) Declaration of a sub class
c) Declaration of base class & sub class and how subclass inherits the base class
d) None of the mentioned

View Answer

Answer: c [Reason:] None.

4. In an inheritance chain through which of following, the base class and its components are accessible to the derived class?
a) Scope resolution operator(:)
b) Class visibilty modifiers (public,private etc.)
c) Dot operator (.)
d) All of the mentioned

View Answer

Answer: b [Reason:] None.

5. Select the class visibility modifiers among the following :
a) Private, protected, public, internal
b) Private, protected, public, internal, protected internal
c) Private, protected, public
d) All of the mentioned

View Answer

Answer: b [Reason:] None.

6. In Inheritance concept, which of the following members of base class are accessible to derived class members?
a) static
b) protected
c) private
d) shared

View Answer

Answer: b [Reason:] None.

7. Wrong statement about inheritance in C# .NET?
a) In inheritance chain, object construction begins from base class towards derived class
b) Inheritance cannot extend base class functionality
c) A derived class object contains all base class data
d) All of the mentioned

View Answer

Answer: b [Reason:]None.

8. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member

View Answer

Answer: d [Reason:] A class member declared protected becomes private member of subclass.

9. Which of the following functionality is facilitated by inheritance mechanism?
a) Use the existing functionality of base class
b) Override the existing functionality of base class
c) Implements new functionality in derived class
d) All of the mentioned

View Answer

Answer: d [Reason:]None.

10. Which statements among following are correct?
a) We can derive a class from a base class even if source code of base class not available
b) Multiple inheritance is different from multiple levels of inheritance
c) It is legal to make objects of one class as members of another class
d) All of the mentioned

View Answer

Answer: d [Reason:] None.

11. If base class consist of two private integers,one static integer and derived class consist of two static integers and one private integer.What would be the size of derived class object?
a) size of object depends on sizes of its non static data members
b) size of a derived class object is sum of sizes of non static data members of base class and derived class
c) size of object is calculated using sizeof() method
d) none of the mentioned

View Answer

Answer: a [Reason:] None.

12. Which is the correct way to create an object of the given class abc?
a) Declaring existing class as sealed
b) Declaring existing class as override
c) Declaring existing class as overloads
d) Declaring existing class as shadows

View Answer

Answer: a [Reason:] None.

13. Given class sample is inherited by class sample 1. Which are the correct statements about construction of object of class sample?
a) While creating the object firstly the constructor of class sample will be called followed by constructor of class sample 1
b) The constructor of only sample class will be called
c) While creating the object firstly constructor of class sample 1 will be called followed by constructor of class sample
d) The order of calling constructors depend on whether constructors in class sample and sample 1 are private or public

View Answer

Answer: c [Reason:] None.

14. Which form of inheritance is not supported directly by C# .NET?
a) Multiple inheritance
b) Multilevel inheritance
c) Single inheritance
d) Hierarchical inheritance

View Answer

Answer: a [Reason:] Supported in form of interfaces in secondary form.

15. If no access modifier for a class is specified,then class accessibility is defined as?
a) public
b) protected
c) private
d) internal

View Answer

Answer: c [Reason:] By default it is specified private in nature.

C# MCQ Set 2

1. Select the output for the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 30;
  4.      int j = 25 % 25;
  5.      if (Convert.ToBoolean(Convert.ToInt32(i = j)))
  6.      {
  7.          Console.WriteLine("In if");
  8.      }
  9.      else
  10.      {
  11.          Console.WriteLine("In else");
  12.      }
  13.      Console.WriteLine("In main");
  14.      Console.ReadLine();
  15.  }

a) In if
b) In else
c) In if
In main
d) In else
In main

View Answer

Answer: d [Reason:] Usage of ‘=’ operator instead of ‘==’ operator .hence,the condition is not true. Output: In else In main

2. Select output for the following set of Code:

  1.  static void Main(string[] args)
  2.  {
  3.      int i;
  4.      int b = 8, a = 32;
  5.      for (i = 0; i <= 10; i++)
  6.      {
  7.          if ((a / b * 2)== 2)
  8.          {
  9.              Console.WriteLine( i + " ");
  10.              continue;
  11.          }
  12.          else if (i != 4)
  13.              Console.Write(i + " ");
  14.          else
  15.              break;
  16.     }
  17.     Console.ReadLine();
  18.  }

a) 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8
c) 0 1 2 3
d) 0 1 2 3 4

View Answer

Answer: c [Reason:] The if condition will never be fulfilled as ((a / b) * 2 == 2) is never true.Hence,only else part of condition will be executed until i! = 4 i.e i = 0,1 ,2 ,3. Output:0 1 2 3

3. Select the output for the following set of Code:

  1.  static void Main(string[] args)
  2.  {
  3.      Console.WriteLine("Enter a letter:");
  4.      char c = (char)Console.Read();
  5.      if (Char.IsDigit(c) == true)
  6.          Console.WriteLine("A number");
  7.      else if (char.IsLower(c) == true)
  8.          Console.WriteLine("A lower case letter");
  9.      else if (char.IsUpper(c) == true)
  10.          Console.WriteLine("An upper case letter");
  11.      Console.ReadLine();
  12.  }
  13.  1. Enter a letter :
  14.     a
  15.     An upper case letter
  16.  2. Enter a letter :
  17.     A
  18.     An upper case letter
  19.  3. Enter a letter :
  20.     2
  21.     A number
  22.  4. Enter a letter :
  23.     2
  24.     A lower case letter.

Which of the following conditions are true ?
a) a ,b ,c
b) b ,c ,d
c) a ,d ,b
d) b ,c

View Answer

Answer: d Output:Enter a letter : A An upper case letter Enter a letter : 2 A number

4. Select the output for the following set of code :

  1.   static void Main(string[] args)
  2.   {
  3.       int i, j;
  4.       for (i = 2; i >= 0; i--)
  5.       {
  6.           for (j = 0; j <= 2; j++)
  7.           {
  8.               if (i == j)
  9.               {
  10.                   Console.WriteLine("1");
  11.               }
  12.               else
  13.               {
  14.                   Console.WriteLine("0");
  15.               }
  16.          }
  17.          Console.WriteLine("n");
  18.          Console.ReadLine();
  19.       }
  20.   }

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

View Answer

Answer: c [Reason:] In first row for i = 2 : j = 0 == 0 as if condition fails for (i == j) i = 2 : j = 1 == 0 as again if condition fails for ( i == j) i = 2 : j = 2 == 1 as (i == j). In Second row for i = 1 : j = 0 == 0 as if condition fails for (i == j) i = 1 : j = 1 == 1 (as i==j) i = 1 : j = 2 == 0 as (i==j) not true In Third row for i = 0 : j = 0 == 1 as (i==j) true i = 0 : j = 1 == 0 as (i==j) not true. i = 0 : j = 2 == 0 . So, 0 0 1 0 1 0 1 0 0

Output: 0 0 1 0 1 0 1 0 0

5. Select the correct ‘if statement’ to be filled in the given set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int []num = {50, 65, 56, 88, 43, 52};
  4.      int even = 0, odd = 0;
  5.      for (int i = 0 ;i < num.Length ;i++)
  6.      {
  7.           /*___________________________*/
  8.      }   
  9.      Console.WriteLine("Even Numbers:" +even);
  10.      Console.WriteLine("Odd Numbers:" +odd);
  11.      Console.ReadLine();
  12.  }

a)

  1. if ((num % 2) == 0)
  2.    {
  3.        even += 1;
  4.    }
  5.    else
  6.    {
  7.        odd += 1;
  8.    }

b)

  1.  if((num * i) == 0)
  2.    {
  3.        even += 1;
  4.    }
  5.    else
  6.    {
  7.        odd += 1;
  8.    }

c)

  1. if(num[i] % 2 == 0)
  2.     {
  3.         even += 1;
  4.     }
  5.     else
  6.     {
  7.         odd += 1;
  8.     }

d)

  1.  if(num[i] % 2 = 0)
  2.     {
  3.         even += 1;
  4.     }
  5.     else
  6.     {
  7.         odd += 1;
  8.     }

View Answer

Answer: c [Reason:] int []num = {50, 65, 56, 88, 43, 52}; int even = 0,odd = 0; for (int i = 0 ;i < num.Length ;i++) { if (num[i] % 2 == 0) { even += 1; } else { odd += 1; } } Console.WriteLine(“Even Numbers: ” +even); Console.WriteLine(“Odd Numbers: ” +odd); Console.ReadLine();

6. What is the output for the following code ?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 15, b = 10, c = 1;
  4.      if (Convert.ToBoolean(a) && (b > c))
  5.      {
  6.          Console.WriteLine("cquestionbank");
  7.      }
  8.      else
  9.      {
  10.          break;
  11.      }
  12.  }

a) cquestionbank
b) It will print nothing
c) Compile time error
d) Run time error

View Answer

Answer: c [Reason:] Keyword “break” is not part of if-else statement.This keyword is used in case of loop or switch case statement.

7. What is the output for the following code ?

  1.   static void Main(string[] args)
  2.   {  
  3.       int a = 5;
  4.       if (Convert.ToBoolean((.002f) -(0.1f)))
  5.       Console.WriteLine("Sachin Tendulkar");
  6.       else if (a == 5)
  7.       Console.WriteLine("Rahul Dravid");
  8.       else
  9.       Console.WriteLine("Ms Dhoni");
  10.       Console.ReadLine();
  11.   }

a) Rahul Dravid
b) Sachin Tendulkar
c) Ms Dhoni
d) Warning : Unreachable Code

View Answer

Answer: b [Reason:] (0.002 – 0.1f) not equivalent to zero hence it is true. So,only first if clause will execute and print:Sachin Tendulkar on console.As,first condition is always true so no else if statement will be executed. Output: Sachin Tendulkar

8. Select the output for the following set of Code :

  1.   static void Main(string[] args)
  2.   {   
  3.       int a = -1;
  4.       int b = -1;
  5.       if (Convert.ToBoolean (++a = ++b))
  6.       Console.WriteLine("a");
  7.       else
  8.       Console.WriteLine("b");
  9.       Console.ReadLine();
  10.   }

a) a
b) b
c) Compile time error
d) Code execute successfully with no output

View Answer

Answer: c [Reason:] Both a and b are constants.Illegal to assign a value to constant on left hand of ‘=’operator .Hence,it must be some variable. Output: static void Main(string[] args) { int a = -1; int b = -1; if (Convert.ToBoolean(++a == ++b)) Console.WriteLine(“a”); else Console.WriteLine(“b”); Console.ReadLine(); }

9. Select the output for the following set of Code :

  1.   static void Main(string[] args)
  2.   {
  3.       int a = 5, b = 10;
  4.       if (Convert.ToBoolean(Convert.ToInt32(0xB)))
  5.       if (Convert.ToBoolean(Convert.ToInt32(022)))
  6.       if (Convert.ToBoolean(Convert.ToInt32('xeb')))
  7.       Console.WriteLine("java");
  8.       else ;
  9.       else ;
  10.       else ;
  11.   }

a) Compile time error: Misplaced else
b) Compile time error: Undefined symbol
c) java
d) Warning: Condition is always true

View Answer

Answer: c [Reason:] oxB: hexadecimal integer constant. 022: It octal integer constant. ‘xeb’: It is hexadecimal character constant. As,zero is false and any non-zero number is true. All,constants return a non-zero value. So, all if conditions in the above program are true. Output: java.

10. Select the output for the following set of Code :

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 5, b = 10;
  4.      if (Convert.ToBoolean(Convert.ToInt32(++a)) || Convert.ToBoolean(Convert.ToInt32(++b)))
  5.      {
  6.          Console.WriteLine(a + "n" + b);
  7.      }
  8.      else
  9.      Console.WriteLine(" C# ");
  10.  }

a) 6 11
b) 6 16
c) 6 12
d) 6 10

View Answer

Answer: d [Reason:] Consider the following expression:( ++a || ++b). In this expression || is ‘Logical OR operator’. Two important properties of this operator are: Property 1: (Expression1) || (Expression2) || operator returns 0 if and only if both expressions return a zero otherwise || operator returns 1. initial value of a is 5. So ++a will be 6. Since ++a is returning a non-zero so ++b will not execute. Output : 6 10.

C# MCQ Set 3

1. Select the correct output for the given set of code?

  1.  class sample
  2.  {
  3.      public  int i;
  4.      void display() 
  5.      {
  6.          Console.WriteLine(i);
  7.      }
  8. }    
  9. class sample1 : sample 
  10. {
  11.     public  int j;
  12.     public void display() 
  13.     {
  14.         Console.WriteLine(j);
  15.     }
  16. }    
  17. class Program
  18. {
  19.     static void Main(string[] args)
  20.     {
  21.         sample1 obj = new sample1();
  22.         obj.i = 1;
  23.         obj.j = 2;
  24.         obj.display();
  25.         Console.ReadLine();
  26.     }
  27. }

a) 1
b) 3
c) 2
d) Compile Time error

View Answer

Answer: c [Reason:] class sample & class sample1 both contain display() method, class sample1 inherits class sample, when display() method is called by object of class sample 1, display() method of class sample 1 is executed rather than that of Class sample.

2. Correct statement about C#.NET code is?

  1. class sample
  2. {
  3.     protected int index;
  4.     public sample()
  5.     {
  6.         index = 0;
  7.     }
  8. }
  9. class sample 1: sample
  10. {
  11.     public void add()
  12.     {
  13.         index += 1;
  14.     }
  15. }
  16. class Program
  17. {
  18.     static void Main(string[] args)
  19.     {
  20.         sample 1 z = new sample 1();
  21.         z . add();
  22.     }
  23. }

a) Index should be declared as protected if it is to become available in inheritance chain
b) Constructor of sample class does not get inherited in sample 1 class
c) During constructing an object referred to by z, Firstly constructor of sample class will be called followed by constructor of sample 1 class
d) All of the mentioned

View Answer

Answer: d [Reason:] None.

3. The following set of code is run on single level of inheritance. Find correct statement about the code?

  1.  class sample
  2.  {
  3.      int i = 10;
  4.      int j = 20;
  5.      public void display() 
  6.      {
  7.          Console.WriteLine("base method ");
  8.      }
  9.  }    
  10.  class sample1 : sample 
  11.  {
  12.      public  int s = 30;
  13.  }    
  14.  class Program
  15.  {
  16.      static void Main(string[] args)
  17.      {
  18.          sample1 obj = new sample1();
  19.          Console.WriteLine("{0}, {1}, {2}", obj.i,  obj.j,  obj.s);
  20.          obj.display();
  21.          Console.ReadLine();
  22.      }
  23.  }

a) 10, 20, 30
base method
b) 10, 20, 0
c) compile time error
d) base method

View Answer

Answer: c [Reason:] ‘i’ and ‘ j’ are inaccessible due to protection level.Declare them as public variable and hence will be accessed in code.

4. What will be size of the object created depicted by csharp code snippet?

  1.  class baseclass
  2.  {
  3.      private int a;
  4.      protected int b;
  5.      public int c;
  6.  }
  7.  class derived : baseclass
  8.  {
  9.      private int x;
  10.      protected int y;
  11.      public int z;
  12.  }
  13.  class Program
  14.  {
  15.      static Void Main(string[] args)
  16.      {
  17.          derived a = new derived();
  18.      }
  19.  }

a) 20 bytes
b) 12 bytes
c) 16 bytes
d) 24 bytes

View Answer

Answer: d [Reason:] Explained in fundamentals of inheritance.

5. What would be output of the following set of code?

  1. class sample
  2. {
  3.     public sample()
  4.     {
  5.         Console.WriteLine("THIS IS BASE CLASS constructor");
  6.     }
  7.  }    
  8. public class sample1 : sample 
  9. {
  10.  
  11. }    
  12. class Program
  13. {
  14.     static void Main(string[] args)
  15.     {
  16.         sample1 obj = new sample1();
  17.         Console.ReadLine();
  18.     }
  19. }

a) Code executes successfully prints nothing
b) This is base class constructor
c) Compile time error
d) None of the mentioned

View Answer

Answer: c [Reason:] Base class accessibility level is much less compared to derived class.Declare it public to get desired output.

6. Select the statement which should be added to the current set of code to get the output as 10 20 ?

  1. class baseclass
  2. {
  3.     protected int a = 20;
  4. }
  5. class derived : baseclass
  6. {
  7.     int a = 10;
  8.     public void math()
  9.     {
  10.          /* add code here */
  11.     }   
  12. }

a) Console.writeline( a + ” ” + this.a);
b) Console.Writeline( mybase.a + ” ” + a);
c) console.writeline(a + ” ” + base.a);
d) console.writeline(base.a + ” ” + a);

View Answer

Answer: c [Reason:] None.

7. Correct statement about the following C#.NET code is?

  1.  class baseclass
  2.  {
  3.      int a;
  4.      public baseclass(int a1) 
  5.      {
  6.          a = a1;
  7.          console.writeline(" a ");
  8.      }
  9.      class derivedclass : baseclass
  10.      {
  11.          public derivedclass (int a1) : base(a1)
  12.          {
  13.              console.writeline(" b ");
  14.          }
  15.      }
  16.      class program
  17.      {
  18.          static void main(string[] args)
  19.          {
  20.              derivedclass d =  new derivedclass(20);
  21.          }
  22.      }
  23.  }

a) Compile time error
b) Output : b
a
c) Output : a
b
d) The program will work correctly if we replace base(a1) with base.baseclass(a1)

View Answer

Answer: c [Reason:] None. Output : a b

8. Which statement should be added in function a() of class y to get output “i love csharp”?

  1.  class x
  2.  {
  3.      public void a()
  4.      {
  5.          console.write("bye");
  6.      }
  7.  }
  8.  class y : x
  9.  {
  10.      public void a()
  11.      {
  12.      /* add statement here */
  13.          console.writeline("  i love csharp ");
  14.      }
  15.  }
  16.  class program
  17.  {
  18.      static void main(string[] args)
  19.      {
  20.          y obj =  new obj();
  21.          obj.a();
  22.      }
  23.  }

a) x.a();
b) a();
c) base.a();
d) x::a();

View Answer

Answer: c [Reason:] None.

9. Which statements are correct?
a) If a base class consists of a member function fun() and a derived class do not have any function with this name. An object of derived class can access fun()
b) A class D can be derived from class C, which is derived from class B which in turn is derived from class A
c) If a base class and a derived class each include a member function with same name,the member function of the derived class will be called by object of derived class
d) All of the mentioned

View Answer

Answer: d [Reason:] None.

10. Select the output for the following set of code?

  1. class A 
  2. {
  3.     public int i;
  4.     protected int j;
  5. }    
  6. class B : A 
  7. {
  8.     public int j;
  9.     public void display() 
  10.     {
  11.         base.j = 3;
  12.         Console.WriteLine(i + " " + j);
  13.     }
  14. }    
  15. class Program
  16. {
  17.     static void Main(string[] args)
  18.     {
  19.         B obj = new B();
  20.         obj.i = 1;
  21.         obj.j = 2;
  22.         obj.display();     
  23.         Console.ReadLine();
  24.     }
  25. }

a) 2 1
b) 1 0
c) 0 2
d) 1 2

View Answer

Answer: d [Reason:] Both class A & B have members with same name that is j, member of class B will be called by default if no specifier is used. i contains 1 & j contains 2, printing 1 2. Output: 1, 2

11. Select the output of the following set of code?

  1.  class A
  2.  {
  3.      public int i;
  4.      private int j;
  5.  }     
  6.  class B :A 
  7.  {
  8.      void display() 
  9.      {
  10.          base.j = base.i + 1;
  11.          Console.WriteLine(base.i + "  " + base.j);
  12.      }
  13. }    
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         B obj = new B();
  19.         obj.i = 1;
  20.         obj.j = 2;
  21.         obj.display();     
  22.         Console.ReadLine();
  23.     }
  24. }

a) 1, 3
b) 2, 3
c) 1, 2
d) compile time error

View Answer

Answer: d [Reason:] Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.

12. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) base
c) this
d) None of the mentioned

View Answer

Answer: b [Reason:] Whenever a subclass needs to refer to its immediate super class, it can do so by use of the keyword base.

13. Which of these operators must be used to inherit a class?
a) :
b) &
c) ::
d) extends

View Answer

Answer: a [Reason:] class a {

} class b : a {

}

14. What is the output of the following set of code ?

  1.  using System;
  2.  public class BaseClass
  3.  {
  4.      public BaseClass()
  5.      {
  6.          Console.WriteLine("I am a base class");
  7.      }
  8.  }
  9.  public class ChildClass : BaseClass
  10.  {
  11.      public ChildClass()
  12.      {
  13.          Console.WriteLine ("I am a child class");
  14.      }
  15.  static void Main()
  16.  {
  17.      ChildClass CC = new ChildClass();
  18.  }
  19.  }

a) I am a base class
I am a child class
b) I am a child class
I am a base class
c) compile time error
d) None of the mentioned

View Answer

Answer: a [Reason:] This is because base classes are automatically instantiated before derived classes. Notice the output, The BaseClass constructor is executed before the ChildClass constructor. Output: I am a base class I am a child class

C# MCQ Set 4

1. How many Bytes are stored by ‘Long’ Datatype in C# .net?
a) 8
b) 4
c) 2
d) 1

View Answer

Answer: a [Reason:] ‘Long’ is the datatype keyword used for storing data of unlimited length so by definition its size is always maximum i.e 8.

2. Choose “.NET class” name from which datatype “UInt” is derived ?
a) System.Int16
b) System.UInt32
c) System.UInt64
d) System.UInt16

View Answer

Answer: b [Reason:] By Defination class assigned to a) System.Int16 = short. b) System.UInt32 = UInt. c) System.UInt64 = ULong. d) System.UInt16 = UShort.

3. Correct Declaration of Values to variables ‘a’ and ‘b’?
a) int a = 32, b = 40.6;
b) int a = 42; b = 40;
c) int a = 32; int b = 40;
d) int a = b = 42;

View Answer

Answer: c [Reason:] a) Although,declaration of ‘b’ and ‘a’ are correct but initialization of value to ‘b’ should be ‘int’ datatype not float. b) Missing declaration type of ‘b’. c) correctly declared datatypes ‘a’ and ‘b’. d) ‘b’ can’t be assigned values before declaration.

4. Select error in the given program :

  1.  Static Void Main(String[] args)
  2.  {
  3.      const int m = 100;
  4.      int n = 10;
  5.      const int k = n / 5 * 100 * n ;
  6.      Console.WriteLine(m * k);
  7.      Console.ReadLine();
  8.  }

a) ‘k’ should not be declared constant
b) Expression assigned to ‘k’ should be constant in nature
c) Expression (m * k) is invalid
d) ‘m ‘ is declared in invalid format

View Answer

Answer: b [Reason:]’k’ should be declared as const int k = 10/5 * 100*10 i.e only constant values should be assigned to a constant. Output :Error 1 – The expression being assigned to ‘k’ must be constant.

5. Arrange the following datatype in order of increasing magnitude sbyte, short, long, int.
a) long < short < int < sbyte
b) sbyte < short < int < long
c) short < sbyte < int < long
d) short < int < sbyte < long

View Answer

Answer: b [Reason:] By definition.

6. Which datatype should be more preferred for storing a simple number like 35 to improve execution speed of a program?
a) sbyte
b) short
c) int
d) long

View Answer

Answer: a [Reason:] Wider datatype like int,long takes more time for manipulation of a program.

7. Which Conversion function of ‘Convert.TOInt32()’ and ‘Int32.Parse()’ is efficient?
1) Int32.Parse() is only used for strings and throws argument exception for null string
2) Convert.Int32() used for datatypes and returns directly ‘0’ for null string
a) 2
b) Both 1,2
c) 1
d) None of the mentioned

View Answer

Answer: a [Reason:] Convenient for every datatype so mostly preferred.

8. Correct way to assign values to variable ‘c’ when int a=12, float b=3.5,int c;
a) c = a + b;
b) c = a + int(float(b));
c) c = a + convert.ToInt32(b);
d) c = int(a + b);

View Answer

Answer: c [Reason:] None.

9. Correct Set of Code for given data ‘a’ and ‘b’ to print output for ‘c’ as 74 ?
a)

  1.    int a = 12;
  2.    float b = 6.2f;
  3.    int c;
  4.    c = a / b + a * b;
  5.    Console.WriteLine(c);

b)

  1.    int a = 12;
  2.    float b = 6.2f;
  3.    int c;
  4.    c = a / convert.ToInt32(b) + a * b;
  5.    Console.WriteLine(c);

c)

  1.    int a = 12;
  2.    float b = 6.2f;
  3.    int c;
  4.    c = a / convert.ToInt32(b) + a * convert.ToInt32(b);
  5.    Console.WriteLine(c);

d)

  1.     int a = 12;
  2.     float b = 6.2f;
  3.     int c;
  4.     c = convert.ToInt32(a / b + a * b);
  5.     Console.WriteLine(c);

View Answer

Answer: c [Reason:] Usage of typecasting operation. Seperately check each expression taking typecast operations in concern. Output : 74.

10. Does the output remain same or different for both cases?
1)

  1.   char l ='k';
  2.   float b = 19.0f;
  3.   int c;
  4.   c = (l / convert.ToInt32(b));
  5.   Console.Writeline(c);

2)

  1.    char l ='k';
  2.    float b = 19.0f;
  3.    int c;
  4.    c = Convert.ToInt32(l / b);
  5.    console.writeline(c);

a) Yes
b) No

View Answer

Answer: No [Reason:] Output – 1) 5. 2) 6.

11. Default Type of number without decimal is ?
a) Long Int
b) Unsigned Long
c) Int
d) Unsigned Int

View Answer

Answer: c [Reason:] By definition.

12. Correct output for code is?

  1.  static void Main(string[] args)
  2.  {
  3.      float a = 10.553f;
  4.      long b = 12L;
  5.      int  c;
  6.      c = Convert.ToInt32(a + b);
  7.      Console.WriteLine(c);
  8.  }

a) 23.453
b) 22
c) 23
d) 22.453

View Answer

Answer: c [Reason:] The two datatype ‘float’ and ‘long’ after arithmetic operation completely converted to nearest whole number 23. Output : 23.

C# MCQ Set 5

1. Correct statement about c#.NET code snippet given below is?

  1. interface a1
  2. {
  3.     void f1();
  4.     int f2();
  5. }
  6. class a :a1
  7. {
  8.     void f1()
  9.     {
  10.     }
  11.     int a1.f2()
  12.     {
  13.     }
  14. }

a) class a is an abstract class
b) A method table would not be created for class a
c) The definition of f1() in class a should be void a1.f1()
d) None of the mentioned

View Answer

Answer: c [Reason:] None.

2. Choose the wrong statement about ‘INTERFACE’ in C#.NET?
a) An explicitly implemented member could be accessed from an instance of the interface
b) Interfaces are declared public automatically
c) An interface could not contain signature of the indexer
d) None of the mentioned

View Answer

Answer: c [Reason:] None.

3. Choose the correct statement about following code snippet given below:

  1.  interface a1
  2.  {
  3.      void f1();
  4.      void f2();
  5.  }
  6.  class a :a1
  7.  { 
  8.      private int i;
  9.      void a1.f1()
  10.      {
  11.      }
  12.  }

a) Class a could not have an instance data
b) Class a is an abstract class
c) Class a fully implements the interface a1
d) None of the mentioned

View Answer

Answer: b [Reason:] None.

4. Choose the correct statement about the following code snippet in C#.NET:

  1.  interface abc
  2.  {
  3.      String FirstName
  4.      {
  5.          get;
  6.          set;
  7.      }
  8.     String LastName
  9.     {
  10.         get;
  11.         set;
  12.     }
  13.     void print();
  14.     void stock();
  15.     int fun();
  16. }

a) Functions should be declared inside an interface
b) It is workable code
c) Properties cannot be declared inside an interface
d) None of the mentioned

View Answer

Answer: b [Reason:] None.

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

  1.  interface calc
  2.  {
  3.      void cal(int i);
  4.  }
  5.  public  class maths :calc 
  6.  {
  7.      public int x;
  8.      public void cal(int i) 
  9.      {
  10.          x = i * i;            
  11.      }
  12.  }
  13.  class Program
  14.  {
  15.      public static void Main(string[] args)
  16.      {            
  17.          display arr = new display();
  18.          arr.x = 0;      
  19.          arr.cal(2);
  20.          Console.WriteLine(arr.x);
  21.          Console.ReadLine();
  22.      }
  23.  }

a) 0
b) 2
c) 4
d) None of the mentioned

View Answer

Answer: c [Reason:] None. Output : 4

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

  1.  interface calc
  2.  {
  3.      void cal(int i);
  4.  }
  5.  class displayA :calc 
  6.  {
  7.      public int x;
  8.      public void cal(int i) 
  9.      {
  10.          x = i * i;            
  11.      }
  12.  }
  13.  class displayB :calc
  14.  {
  15.      public int x;
  16.      public void cal(int i)
  17.      {
  18.          x = i / i;
  19.      }
  20.  }
  21.  class Program
  22.  {
  23.      public static void Main(string[] args)
  24.      {            
  25.          displayA arr1 = new displayA();
  26.          displayB arr2 = new displayB();
  27.          arr1.x = 0;
  28.          arr2.x = 0;
  29.          arr1.cal(2);
  30.          arr2.cal(2);
  31.          Console.WriteLine(arr1.x + " " + arr2.x);
  32.          Console.ReadLine();
  33.      }
  34.  }

a) 0 0
b) 2 2
c) 4 1
d) 1 4

View Answer

Answer: c [Reason:] class displayA executes the interface calculate by doubling the value of item . Similarly class displayB implements the interface by dividing item by item.So, variable x of class displayA stores 4 and variable x of class displayB stores 1. Output : 4, 1

7. Choose the correct output of the given code snippet?

  1. interface i1
  2. {
  3.     void fun();
  4. }
  5. interface i2
  6. {
  7.     void fun();
  8. }
  9. public class maths :i1, i2
  10. {
  11.     void i1.fun()
  12.     {
  13.         Console.WriteLine("i1.fun");
  14.     }
  15.     void i2.fun()
  16.     {
  17.         Console.WriteLine("i2.fun");
  18.     }
  19. }
  20. class Program
  21. {
  22.     static void Main(string[] args)
  23.     {
  24.         Sample obj = new Sample();
  25.         i1 i = (i1) obj;
  26.         i.fun();
  27.         i2 ii = (i2) obj;
  28.         ii.fun();
  29.     }
  30. }

a) i1.fun
b) i2.fun
i1.fun
c) 0
d) i1.fun
i2.fun

View Answer

Answer: d [Reason:] None.

8. Correct way to implement the interface given below?

  1.   interface abc
  2.    {
  3.        string name
  4.        {
  5.            get;
  6.            set;
  7.        }
  8.    }

a)

   class emp :employee
   {
       private string str;
       public string firstname;
       {
           get
           {
               return str;
           }
           set
           {
               str = value;
           }
       }
   }

b)

  class emp :implements person
  {
      private string str;
      public string firstname
      {
          get
          {
              return str;
          }
          set
          {
              str = value;
          }
      }
  }

c)

 class emp: implements person
 {
     private string str;
     public string person.firstname
     {
         get
         {
             return str;
         }
         set
         {
             str = value;
         }
     }
 }

d) None of the mentioned

View Answer

Answer: a [Reason:] None.

9. Choose the correct output of the following code snippet?

  1.  interface i1
  2.  {
  3.      void f1();
  4.  }
  5.  interface i2 :i1
  6.  {
  7.      void f2();
  8.  }
  9.  public class maths :i2
  10.  {
  11.      public void f2()
  12.      {
  13.          Console.WriteLine("fun2");
  14.      }
  15.      public void f1()
  16.      {
  17.          Console.WriteLine("fun1");
  18.      }
  19.  }
  20.  class Program
  21.  {
  22.      static Void Main()
  23.      {
  24.          maths m = new maths();
  25.          m.f1();
  26.          m.f2();
  27.      }
  28.  }

a) fun2
b) fun1
c) fun1
fun2
d) fun2
fun1

View Answer

Answer: c [Reason:] None.

10. In order to avoid ambiguity among an interface derived from two base interfaces with same method name(and signature), the right code among the following given codes is?
a)

  1.  interface i1
  2.  {
  3.      void m1();
  4.  }
  5.  interface i2
  6.  {
  7.      void m1();
  8.  }
  9.  interface i3 :i1, i2
  10.  {
  11.  }
  12.  class c3 :i3
  13.  {
  14.      void i1.m1()
  15.      {
  16.      }
  17.      void i1.m1()
  18.      {
  19.      }
  20.  }

b)

 interface i1
 {
     void m1();
 }
     interface i2
     {
         void m1();
     }
     interface i3 :i1, i2
     {
     }
     class c3 :i3
     {
         void i1.i2.m1()
         {
         }
     }

c)

  interface i1
  {
      void m1();
  }
  interface i2
  {
      void m1();
  }
  interface i3 :i1, i2
  {
  }
  class c3 :i3
  {
      void i1.m1()
      {
      }
      void i2.m1()
      {
      }
  }

d) All of the mentioned

View Answer

Answer: c [Reason:] None.