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 of the classes provide the operation of reading from and writing to the console in C#.NET?
a) System.Array
b) System.Output
c) System.ReadLine
d) System.Console

View Answer

Answer: d [Reason:] The method for reading and writing to the console in C#.NET is provided by System.Console class.This class gives us access to the standard input,output and standard error streams.

2. Which of the given stream methods provide access to the output console by default in C#.NET?
a) Console.In
b) Console.Out
c) Console.Error
d) All of the mentioned

View Answer

Answer: b [Reason:] The standard output stream Console.Out sends output to the screen by default.

3. Which of the given stream methods provide the access to the input console in C#.NET?
a) Console.Out
b) Console.Error
c) Console.In
d) All of the mentioned

View Answer

Answer: c [Reason:] Console.In is an instance of TextReader, and we can use the methods and properties defined by TextReader to access it to read the input from the keyboard.

4. The number of input methods defined by the stream method Console.In in C#.NET is?
a) 4
b) 3
c) 2
d) 1

View Answer

Answer: b [Reason:] Two basic methods : read() and readline() and third method readkey() introduced in .NET FrameWork 2.0.

5. Select the correct methodS provided by Console.In?
a) Read(), ReadLine()
b) ReadKey(), ReadLine()
c) Read(), ReadLine(), ReadKey()
d) ReadKey(), ReadLine()

View Answer

Answer: c [Reason:] The two method Read() and ReadLine() available in .NET Framework 1.0 and Third method ReadKey() was added by .NET Framework 2.0.

6. Choose the output returned when read() reads the character from the console?
a) String
b) Char
c) Integer
d) Boolean

View Answer

Answer: c [Reason:] Read() returns the character read from the console. It returns the result. The character is returned as an int, which should be cast to char.

7. Choose the output returned when error condition is generated while read() reads from the console.
a) False
b) 0
c) -1
d) All of the mentioned

View Answer

Answer: c [Reason:] Read() returns –1 on error. This method also throws an IOException on failure.

8. Choose the object of TextReader class.
a) Console.In
b) Console.Out
c) Console.Error
d) None of the mentioned

View Answer

Answer: a [Reason:] Console.In is an instance(object) of TextReader class and we can use the methods and properties defined by TextReader to invoke the object console.in.

9. Choose the object/objects defined by the Textwriter class.
a) Console.In
b) Console
c) Console.Error
d) None of the mentioned

View Answer

Answer: c [Reason:] Console.Out and Console.Error are objects of type TextWriter class.

10. Choose the output for the given set of code:

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 10, b = 0;
  4.      int result;
  5.      Console.Out.WriteLine("This will generate an exception.");
  6.      try
  7.      {
  8.          result = a / b; // generate an exception
  9.      }
  10.      catch (DivideByZeroException exc)
  11.      {
  12.          Console.Error.WriteLine(exc.Message);
  13.      }
  14.      Console.ReadLine();
  15.  }

a) This will generate an exception
b) 0
c) Compile time error
d) This will generate an exception
Attempted to Divide by Zero

View Answer

Answer: d [Reason:] None.

11. Choose the methods provided by Console.Out and Console.Error?
a) Write
b) WriteLine
c) WriteKey
d) Write & WriteLine

View Answer

Answer: d [Reason:] None.

12. Choose the output for the following set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      Console.WriteLine("This is a Console Application:");
  4.      Console.Write("Please enter your lucky number:");
  5.      string val1 = Console.ReadLine();
  6.      int val2 = System.Convert.ToInt32(val1, 10);
  7.      val2 = val2 * val2;
  8.      Console.WriteLine("square of number is:" +val2);
  9.      Console.Read();
  10.  }

a) Compile time error
b) Runs successfully does not print anything
c) Runs successfully, ask for input and hence displays the result
d) Syntax Error

View Answer

Answer: c [Reason:] None. Output : This is a Console Application: Please enter your lucky number: 3 Square of number is : 9

C# MCQ Set 2

1. The correct way to implement a read only property add, in a math class is?
a)

  1.    class math
  2.    {
  3.        int ad;
  4.        public int add
  5.        {
  6.            get
  7.            {
  8.                return ad;
  9.            }
  10.        }
  11.  
  12.    }

b)

  1.    class math
  2.    {
  3.       public int add
  4.       {
  5.           get
  6.           {
  7.               return ad;    
  8.           }
  9.       }
  10.   }

c)

  1.    class math
  2.    {
  3.        int ad;
  4.        public int add
  5.        {
  6.            get
  7.            {
  8.                return ad;
  9.            }
  10.            set
  11.            {
  12.                ad = value; 
  13.            }
  14.        }
  15.    }

d) None of the mentioned

View Answer

Answer: a [Reason:] None.

2. The correct way to implement a write only property add in a math class?
a)

  1.    class math
  2.    {
  3.        public int add
  4.        {
  5.            set
  6.            {
  7.                 add = value;
  8.             }
  9.         }
  10.     }

b)

  1.    class math
  2.    {
  3.        int ad;
  4.        public int add
  5.        {
  6.            set
  7.            {
  8.                ad = value;
  9.            }
  10.        }
  11.    }

c)

  1.   class math
  2.   {
  3.       int ad;
  4.       public int add
  5.       {
  6.           get
  7.           {
  8.               return ad;
  9.           }
  10.           set
  11.           {
  12.               ad = value;
  13.           }
  14.       }
  15.   }

d) None of the mentioned

View Answer

Answer: b [Reason:] None.

3. Select the correct statement about properties of read and write in C#.NET?
a) A property can simultaneously be read or write only
b) A property cannot be either read only or write only
c) A write only property will only have get accessor
d) A read only property will only have get accessor

View Answer

Answer: d [Reason:] None.

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

  1.  class number
  2.  {
  3.      private int num1;
  4.      private int num2;
  5.      public int anumber
  6.      {
  7.          get
  8.          {
  9.              return num1;
  10.          }
  11.          set
  12.          {
  13.              num1 = value;
  14.          }
  15.      }
  16.      public int anumber1
  17.      {
  18.          get
  19.          {
  20.              return num2;
  21.          }
  22.          set
  23.          {
  24.              num2 = value;
  25.          }
  26.      }
  27.  }
  28.  class Program
  29.  {
  30.      public static void Main(string[] args)
  31.      {
  32.          number p = new number();
  33.          p.anumber = 20;
  34.          number k = new number();
  35.          k.anumber1 = 40; 
  36.          int m = p.anumber;
  37.          int t = k.anumber1;
  38.          int r = p.anumber + k.anumber1;
  39.          Console.WriteLine("number = " +m);
  40.          Console.WriteLine("number = " +t);
  41.          Console.WriteLine("sum = " +r);
  42.          Console.ReadLine();
  43.      }
  44.  }

a) 0
b) Compile time error
c) 60
d) None of the mentioned

View Answer

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

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

  1.   class number
  2.   {
  3.       private int num1 = 60;
  4.       private int num2 = 20;
  5.       public int anumber
  6.       {
  7.           get
  8.           {
  9.               return num1;
  10.           }
  11.           set
  12.           {
  13.               num1 = value;
  14.           }
  15.       }
  16.       public int anumber1
  17.       {
  18.           get
  19.           {
  20.               return num2;
  21.           }
  22.           set
  23.           {
  24.               num2 = value;
  25.           }
  26.       }
  27.   }
  28.   class Program
  29.   {
  30.       public static void Main(string[] args)
  31.       {
  32.           number p = new number();
  33.           number k = new number();
  34.           int m = p.anumber;
  35.           int t = k.anumber1;
  36.           int r = p.anumber * k.anumber1;
  37.           Console.WriteLine("sum = " + r);
  38.           Console.ReadLine();
  39.       }
  40.   }

a) 0
b) 120
c) 1200
d) Compile time error

View Answer

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

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

  1.  class number
  2.  {
  3.      int length = 50;
  4.      public int number1
  5.      {
  6.          get
  7.          {
  8.              return length;
  9.          }
  10.          set
  11.          {
  12.              length = value;
  13.          }
  14.      }
  15.  }
  16.  class Program
  17.  {
  18.      public static void Main(string[] args)
  19.      {
  20.          number p = new number();
  21.          p.number1 = p.number1 + 40;
  22.          int k = p.number1 * 3 / 9;
  23.          Console.WriteLine(k);
  24.          Console.ReadLine();
  25.      }
  26.  }

a) 0
b) 180
c) 30
d) Compile time error

View Answer

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

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

  1.   class number
  2.   {
  3.       int length = 60;
  4.       public int number1
  5.       {
  6.           get
  7.           {
  8.               return length;
  9.           }
  10.       }
  11.   }
  12.   class Program
  13.   {
  14.       public static void Main(string[] args)
  15.       {
  16.           number p = new number();
  17.           int l;
  18.           l = p.number1 + 40;
  19.           int k = l * 3 / 4;
  20.           Console.WriteLine(k);
  21.           Console.ReadLine();
  22.       }
  23.   }

a) 30
b) 75
c) 80
d) 0

View Answer

Answer: b [Reason:] None. Output : 75

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

  1.   class student
  2.   {
  3.       int []scores = new int[5] {23, 42, 54, 11, 65};
  4.       public int this[int index]
  5.       {
  6.           get
  7.           {
  8.               if (index < 5)
  9.               return scores[index];
  10.               else
  11.               {
  12.                   Console.WriteLine("invalid index");
  13.                   return 0;
  14.               }
  15.           }
  16.           set
  17.           {
  18.               if (index < 5)
  19.               scores[index] = value;
  20.               else
  21.               Console.WriteLine("invalid index");
  22.           }
  23.       }
  24.   }
  25.   class Program
  26.   {
  27.       public static void Main(string[] args)
  28.       {
  29.           student s = new student();
  30.           Console.WriteLine(s[4] + 8);
  31.           Console.ReadLine();
  32.       }
  33.   }

a) 73
b) 37
c) 0
d) Run time error

View Answer

Answer: a [Reason:] None. Output : 73

9. The correct way to implement the property for which property reports the error “invalid index” if user attempts to cross bounds of the array for a student class with 5 intger arrays.
a)

  1.  class student
  2.  {
  3.      int []scores = new int[5] {23, 42, 54, 11, 65};
  4.      public int this[int index]
  5.      {
  6.          get
  7.          {
  8.              if (index < 5)
  9.              return scores[index];
  10.              else
  11.              {
  12.                  Console.WriteLine("invalid index");
  13.                  return 0;
  14.              }
  15.          }
  16.          set
  17.          {
  18.              if (index < 5)
  19.              scores[index] = value;
  20.              else
  21.              Console.WriteLine("invalid index");
  22.          }
  23.      }
  24.  }

b)

  1.  class student
  2.  {
  3.      int []scores = new int[5] {23, 42, 54, 11, 65};
  4.      public int this[int index]
  5.      {
  6.          get
  7.          {
  8.              if (index < 5)
  9.              return scores[index];
  10.          }
  11.      }
  12.  }

c)

  1.  class student
  2.  {
  3.      int []scores = new int[5]{23, 42, 54, 11, 65};
  4.      public int this[int index]
  5.      {
  6.          set
  7.          {
  8.              if (index < 5)
  9.              return scores[index];
  10.              else
  11.              {
  12.                  Console.WriteLine("invalid index");
  13.                  return 0;
  14.              }
  15.          }
  16.      }
  17.  }

d) None of the mentioned

View Answer

Answer: a [Reason:] None.

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

  1.  class student
  2.  {
  3.      int []scores = new int[3] {13, 32, 24};
  4.      public int this[int index]
  5.      {
  6.          get
  7.          {
  8.              if (index < 3)
  9.              return scores[index];
  10.              else
  11.              {
  12.                  Console.WriteLine("invalid index");
  13.                  return 0;
  14.              }
  15.          }
  16.          private  set
  17.          {
  18.              if (index < 3)
  19.              scores[index] = value;
  20.              else
  21.              Console.WriteLine("invalid index");
  22.          }
  23.      }
  24.  }
  25.  class Program
  26.  {
  27.      public static void Main(string[] args)
  28.      {
  29.          student s = new student();
  30.          int[] scores1 = new int[3] {8, 19, 40};
  31.          for (int i = 0; i < 3; i++)
  32.          {
  33.              if (scores1[i] > s[i])
  34.              {
  35.                  Console.WriteLine(" scores1 had greater value :" + scores1[i]);
  36.              }
  37.              else
  38.              {
  39.                  Console.WriteLine("scores had greater value :" + s[i]);
  40.              }
  41.          }
  42.          Console.ReadLine();
  43.      }
  44.  }

a) 0
b) Compile time error
c) Run time error
d) scores had greater value : 13
scores had greater value : 32
scores1 had greater value : 40

View Answer

Answer: d [Reason:] None. Output : scores had greater value : 13 scores had greater value : 32 scores1 had greater value : 40

C# MCQ Set 3

1. The method in which large or variable number of arguments are handled is known as:
a) Value parameters
b) Output parameters
c) Parameter arrays
d) None of the mentioned

View Answer

Answer: c [Reason:] None.

2. The modifiers used to define an array of parameters or list of arguments:
a) ref
b) out
c) param
d) var

View Answer

Answer: c [Reason:] None.

3. What will be the output for the given set of code ?

  1.  static void Main(string[] args)
  2.  {
  3.      object[] a = {" 1 ", 4.0f, " harsh "};
  4.      fun(a);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params object[] b)
  8.  {
  9.      for (int i = 0; i < b.Length - 1; i++)
  10.          Console.WriteLine(b[i] + " ");
  11.  }

a) 1 4.0 harsh
b) 1 4
c) 1 4 hars
d) 1 4 harsh

View Answer

Answer: d [Reason:] ‘a’ is declared as array of objects which is passed as a parameter to a single method fun() using variable number of parameters. Output : 1 4 harsh

4. Which of the following statements are correct?
a) C SHARP allows a function to have arguments with default values
b) C SHARP allows a function to have variable number of arguments
c) Params is used to specify the syntax for a function with variable number of arguments
d) Omitting the return value type in method definition results into an exception

View Answer

Answer: b, c [Reason:] None.

5. What will be the output of the set of code?

  1. static void Main(string[] args)
  2. {
  3.     int [] a = {1, 2, 3, 4, 5};
  4.     fun(a);
  5.     Console.ReadLine();
  6. }
  7. static void fun(params int[] b )
  8. {
  9.     int[] k = { 3, 4, 7, 8,'' };
  10.     for (int i = 0; i < b.Length; i++)
  11.     {
  12.         b[i] = b[i] + k[i] ;
  13.         Console.WriteLine( b[i] + " ");
  14.     }
  15. }

a) Compile time error
b) 3, 4, 7, 8, 5
c) 3, 4, 7, 8, 5, 1, 2, 3, 4, 5
d) 4, 6, 10, 12, 5

View Answer

Answer: d [Reason:] Passing of array parameters declared in main() and hence adding elements of array passed using param to another array k[] declared in fun() method. Output : 4, 6, 10, 12, 5

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

  1.  static void Main(string[] args)
  2.  {
  3.      int [] a = {1, 2, 3, 4, 5};
  4.      fun(a);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params int[] b )
  8.  {
  9.      for (int i = 0; i < b.Length; i++)
  10.      {
  11.          b[i] = b[i] * 5 ;
  12.          Console.WriteLine(b[i] + "");
  13.      }
  14.  }

a) 1, 2, 3, 4, 5
b) 5, 10, 15, 20, 25
c) 5, 25, 125, 625, 3125
d) 6, 12, 18, 24, 30

View Answer

Answer: b [Reason:] None. Output : 5, 10, 15, 20, 25.

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

  1.  static void Main(string[] args)
  2.  {
  3.      int[] a = { 2, 21, 34, 46, 85, 88, 90};
  4.      fun(a);
  5.      Console.WriteLine(a + " ");
  6.      Console.ReadLine();
  7.  }
  8.  static void fun(params int [] b )
  9.  {
  10.      int [] c = { 1, 2, 3, 4, 5, 6, 7};
  11.      int i ;
  12.      for (i = 0 ;i < b.Length ;i++)
  13.      if (b[i] % 2 == 0)
  14.      {
  15.          c[i] = b[i];
  16.      }
  17.      Console.WriteLine("even numbers are:");
  18.      for (i = 0 ;i <= b.Length ;i++)
  19.      {
  20.          Console.WriteLine(c[i]);
  21.      }
  22.  }

a) Compile time error
b) 2, 21, 34, 4, 6, 46, 88, 90
c) 2, 4, 34, 46, 6, 88, 90
d) 2, 34, 46, 88, 90

View Answer

Answer: d [Reason:] None.

8. Select the correct declaration of the defining array of parameters:
a) void func(int[] x)
{

}
b) void func(int x)
{

}
c) void func(param int[])
{

}
d) void fun(param int[] x)
{

}

View Answer

Answer: d [Reason:] None.

9. What will be the output of the given set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      int[] x = { 80, 82, 65, 72, 83, 67 };
  4.      fun(x);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params int [] b )
  8.  {
  9.      int i;
  10.      for (i = 5; i >=0 ; i--)
  11.      {
  12.          Console.WriteLine(Convert.ToChar(b[i]));
  13.      }
  14.  }

a) 67 83 72 65 82 80
b) P R A H S C
c) C S H A R P
d) 80 82 65 72 83 67

View Answer

Answer: c [Reason:] None.

10. What will be the output of the given set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      int[] x = {65, 66, 67, 68, 69, 70};
  4.      fun(x);
  5.      Console.ReadLine();
  6.  }
  7.  static void fun(params int[] b )
  8.  {
  9.      int i;
  10.      for (i = 5; i > 0 ; i--)
  11.      {
  12.          b[i] = b[i] + 32;
  13.          Console.WriteLine(Convert.ToChar(b[i]));
  14.      }
  15.  }

a) A, B, C, D, E, F
b) F, E, D, C, B, A
c) f, e, d, c, b
d) b, c, d, e, f

View Answer

Answer: c [Reason:] None.

C# MCQ Set 4

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

  1. class UnsafeCode
  2. {
  3.     struct MyStruct
  4.     {
  5.         public int a;
  6.         public int b;
  7.         public int Sum() 
  8.        { 
  9.            return a * b; 
  10.        }
  11.    }
  12.    unsafe static void Main()
  13.    {
  14.        MyStruct o = new MyStruct();
  15.        MyStruct* p; 
  16.        p = &o;
  17.        p->a = 10; 
  18.        p->b = 20; 
  19.        Console.WriteLine("Value is " + p->Sum());
  20.        Console.ReadLine();
  21.    }
  22. }

a) Compile time error
b) Run time error
c) 200
d) 30

View Answer

Answer: c [Reason:] A pointer can point to an object of a structure type as long as the structure does not contain reference types. When we access a member of a structure through a pointer, we must use the arrow operator, which is –>, rather than the dot (.) operator. Output : 200

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

  1. class UnsafeCode
  2. {
  3.     struct MyStruct
  4.     {
  5.         public int a;
  6.         public int b;
  7.         public int Sum() 
  8.        { 
  9.            return a / b; 
  10.        }
  11.    }
  12.    unsafe static void Main()
  13.    {
  14.        MyStruct o = new MyStruct();
  15.        MyStruct* p; 
  16.        p = &o;
  17.        p->a = 60; 
  18.        p->b = 15; 
  19.        int c = 30;
  20.        Console.WriteLine("Value is : " + p->Sum()*c);
  21.        Console.ReadLine();
  22.    }
  23. }

a) Compile time error
b) 120
c) Run time error
d) 4

View Answer

Answer: b [Reason:] None. Output :120

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

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int[] nums = new int[10];
  6.         fixed (int* p = &nums[0], p2 = nums)
  7.         {
  8.             if (p == p2)
  9.             Console.WriteLine("p and p2 point to same address.");
  10.             Console.ReadLine();
  11.         }
  12.     }
  13. }

a) Run time error
b) Compile time error
c) p and p2 point to the same address
d) Only b

View Answer

Answer: c [Reason:] None. Output: p and p2 point to same address

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

  1. class UnsafeCode
  2. {
  3.     static void Main()
  4.     {
  5.         int? count = null;
  6.         int? result = null;
  7.         int incr = 10;
  8.         result = count + incr;
  9.         if (result.HasValue)
  10.             Console.WriteLine("result has this value: " + result.Value);
  11.         else
  12.             Console.WriteLine("result has no value");
  13.         Console.ReadLine();
  14.     }
  15. }

a) Run time error
b) 0
c) Result has no value
d) Compile time error

View Answer

Answer: c [Reason:] A nullable object can be used in expressions that are valid for its underlying type.When non-nullable and nullable types are mixed in an operation, the outcome is a nullable value. Output: result has no value

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

  1. class UnsafeCode
  2. {
  3.     static void Main()
  4.     {
  5.         int count = 100;
  6.         int? result = null;
  7.         int incr = 10;
  8.         result = count + incr;
  9.         if (result.HasValue)
  10.             Console.WriteLine("result has this value: " + result.Value);
  11.         else
  12.             Console.WriteLine("result has no value");
  13.         Console.ReadLine();
  14.     }
  15. }

a) Run time error
b) 110
c) Result has no value
d) Compile time error

View Answer

Answer: b [Reason:] None. Output: result has this value : 110

6. Choose the statement which defines the Nullable type Correctly:
a) A special version of a value type that is represented by a structure
b) A nullable type can also store the value null
c) Nullable types are objects of System.Nullable, where T must be a non nullable value type
d) All of the mentioned

View Answer

Answer: d [Reason:] A nullable type is a special version of the value type that is represented by a structure. In addition to the values defined by the underlying type, a nullable type can also store the value null. Thus, a nullable type has the same range and characteristics as its underlying type. It simply adds the ability to represent a value which indicates that a variable of that type is unassigned. Nullable types are objects of System.Nullable, where T must be a nonnullable value type.

7. What does the following code depicts?
1. System.Nullable count;
2. bool? done;
a) Code 1 declares the objects of nullable of type Nullable defined in the System namespace
b) Code 2 declares a nullable type in much shorter and in more commonly used way using ‘?’
c) Both a & b
d) None of the mentioned

View Answer

Answer: c [Reason:] None.

8. Which operator is commonly used to find the size of the type of C#?
a) size()
b) sizeof(type)
c) both a & b
d) none of the mentioned

View Answer

Answer: b [Reason:] None.

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

  1.  unsafe struct FixedBankRecord
  2.  {
  3.      public fixed byte Name[80]; 
  4.      public double Balance;
  5.      public long ID;
  6.  }
  7.  class UnsafeCode
  8.  {
  9.      unsafe static void Main()
  10.      {
  11.          Console.WriteLine("Size of FixedBankRecord is " + sizeof(FixedBankRecord));
  12.          Console.ReadLine();
  13.      }
  14.  }

a) Run time error
b) 80
c) 96
d) Compile time error

View Answer

Answer: c [Reason:] The purpose of a fixed-size buffer is to allow the creation of a struct in which the array of elements that make up the buffer are contained within the struct.By using a fixed-size buffer, we let the entire array to be contained within the struct.The overall size of FixedBankRecord is 96, which is the sum of its members. Output : 96

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

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int* ptrs = stackalloc int[3];
  6.         ptrs[0] = 1;
  7.         ptrs[1] = 2;
  8.         ptrs[2] = 3;
  9.         for (int i = 2; i >=0; i--)
  10.         Console.WriteLine(ptrs[i]);
  11.         Console.ReadLine();
  12.     }
  13. }

a) 3 2 1
b) 1 2 3
c) None of the mentioned
d) Run time error

View Answer

Answer: a [Reason:] Allocates memory from the stack by using stackalloc.Here, ptrs is a pointer that receives the address of the memory that is large enough to hold size of number of objects of type ‘int’.Here, type ‘int’ is a non reference type.Finally, stackalloc can be used only in an unsafe context. Output : 3 2 1

C# MCQ Set 5

1. What is the need for ‘Conversion of data type’ in C#?
a) To store a value of one data type into a variable of another data type
b) To get desired data
c) To prevent situations of run time error during change or conversion of data type
d) None of the mentioned

View Answer

Answer: c [Reason:] By Definition.

2. Types of ‘Data Conversion’ in C#?
a) Implicit Conversion
b) Explicit Conversion
c) Implicit Conversion and Explicit Conversion
d) None of the mentioned

View Answer

Answer: b [Reason:] By Definition.

3.’Implicit Conversion’ follows the order of conversion as per compatibility of datatype as :
a) float < char < int
b) char < int < float
c) int < char < float
d) float < int < char

View Answer

Answer: b [Reason:] None.

4. For the given set of code select the relevant solution for conversion of data type.

  1.  static void Main(string[] args)
  2.  {
  3.      int num1 = 20000;
  4.      int num2 = 50000;
  5.      long total;
  6.      total = num1 + num2;
  7.      Console.WriteLine("Total is : " +total);
  8.      Console.ReadLine();
  9.  }

a) Compiler will generate runtime error
b) Conversion is implicit type, no error generation
c) Specifying datatype for conversion externally will solve the problem
d) None of the mentioned

View Answer

Answer: b [Reason:] Since,conversion of datatype is implicit type as ‘int’ is a subset of ‘longtype’ hence no need to explicitly convert data from one type to another.Compiler will automatically do conversion. Output : Total is : 70000.

5. Subset of ‘int’ datatype is :
a) long ,ulong, ushort
b) long, ulong, uint
c) long, float, double
d) long, float, ushort

View Answer

Answer: c [Reason:] By definition.

6. Type of Conversion in which compiler is unable to convert the datatype implicitly is ?
a) ushort to long
b) int to uint
c) ushort to long
d) byte to decimal

View Answer

Answer:b [Reason:] ‘int’ is 32 bit signed integer whereas ‘uint’ is 32 bit unsigned integer .Range of int is larger than uint.So,compiler cannot implicitly convert from larger datatype to smaller datatype.

7. Disadvantages of Explicit Conversion are ?
a) Makes program memory heavier
b) Results in loss of data
c) Potentially Unsafe
d) None of the mentioned

View Answer

Answer: b [Reason:] By definition.

8. For the given set of code, is conversion possible?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 76;
  4.      char b;
  5.      b = (char)a;
  6.      Console.WriteLine(b);
  7.      Console.ReadLine();
  8.  }

a) Compiler will generate runtime error
b) Conversion is explicit type
c) Compiler will urge for conversion from ‘integer’ to ‘character’ datatype
d) None of the mentioned

View Answer

Answer: b [Reason:] Since, given conversion is of explicit type as one datatype is in integer and other is in ‘char’.Compiler is needed to make a clear distinction between both type of datatypes and hence,explicitly one need to specify datatype as compiler is unable to make automatic conversion. Output : L.

9. Predict the relevant output for the given set of code.

  1. static void Main(string[] args)
  2. {
  3.     float sum;
  4.     int i;
  5.     sum = 0.0F;
  6.     for (i = 1; i <= 10; i++)
  7.     {
  8.         sum = sum + 1 /(float)i;
  9.     }
  10.     Console.WriteLine("sum =" +sum);
  11.     Console.ReadLine();
  12. }

a) 2.000
b) 2.910
c) 2.928
d) 3.000

View Answer

Answer: c [Reason:] None. Output : sum = 2.928698.

10) Which of the conversions are valid for the given set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 22;
  4.      long b = 44;
  5.      double c = 1.406;
  6.      b = a;
  7.      c = a;
  8.      a = b;
  9.      b = c;
  10.  }

a) c = a, b = c
b) a = c, b = a
c) b = a, c = a
d) All of the mentioned

View Answer

Answer: a [Reason:] Conversion of data type from ‘int’ to ‘double’ is implicit in nature for ‘c = a’ as int is subset of double but same is not applicable for ‘b = c’ as ‘c’ had wider scope of data range then ‘b’ so explicit conversion is needed. Output : Error 1 :Cannot implicitly convert type ‘long’ to ‘int’. An explicit conversion exists (are you missing a cast?). Error 2 :Cannot implicitly convert type ‘double’ to ‘long’. An explicit conversion exists (are you missing a cast?).

Correct solution :

 static void Main(string[] args)
 {
     int a = 22;
     long b = 44;
     double c = 1.406;
     b = a;
     c = a;
     a = (int)b;
     b = (long)c;
 }