C# MCQ Number 00765

C# MCQ Set 1

1. Which of these is used as a default specifier for a member of the class if no access specifier is used for it?
a) private
b) public
c) public, within its own class
d) protected

Answer

Answer: a [Reason:] By definition if a class has no access specifiers,it defaults to private accessibility.

2. Which of these is used to access members of class before the object of that class is created?
a) public
b) private
c) static
d) protected

Answer

Answer: c [Reason:] None.

3. Which of these base classes are accessible to the derived class members?
a) static
b) protected
c) private
d) Shared

Answer

Answer: b [Reason:] None.

4. What is the process by which we can control parts of a program that can access the members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion

Answer

Answer: c [Reason:] By definition.

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

  1.  class sum   
  2.  {
  3.      public int x;
  4.      private int y;
  5.      public void math(int a, int b)
  6.      {
  7.          x = a * 4;
  8.          y = b;
  9.      }
  10.  }    
  11.  class Program
  12.  {
  13.      static void Main(string[] args)
  14.      {
  15.          sum p = new sum();   
  16.          p.math(12, 30);
  17.          Console.WriteLine(p.x + "  " + p.y);
  18.          Console.ReadLine();
  19.      }
  20.  }

a) 48, 30
b) 48, 0
c) 0, 0
d) Compile time error

Answer

Answer: d [Reason:] variable ‘y’ is not accessible due to its access level.
Output : Change private y to public y

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

  1.  class sum   
  2.  {
  3.      public int x;
  4.      public int y;
  5.      public  int add (int a,  int b)
  6.      {
  7.          x = a + b;
  8.          y = x + b;
  9.          return 0;
  10.      }
  11.  }    
  12.  class Program
  13.  {
  14.      static void Main(string[] args)
  15.      {
  16.          sum obj1 = new sum();
  17.          sum obj2 = new sum();   
  18.          int a = 2;
  19.          obj1.add(a,  a + 1);
  20.          obj2.add(5,  a);
  21.          Console.WriteLine(obj1.x + "  " + obj2.y);     
  22.          Console.ReadLine();
  23.      }
  24.  }

a) 6, 9
b) 5, 9
c) 9, 10
d) 3, 2

Answer

Answer: b [Reason:] Here, a = 2, a + 1 = 2 + 1 = 3.
So, a = 2, b = 3.
x = 2 + 3 = 5.
y = 5 + 3 = 8.
Similarly, a = 5, b = a + 1 = 4.
y = 5 + 4 = 9.
Output : 5, 9.

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

  1.  class math
  2.  {
  3.      public int a,b;
  4.      public math(int i,  int j)
  5.      {
  6.          a = i;
  7.          b = j;
  8.      }
  9.      public  void sum(math m)
  10.      {
  11.          m.a *= 2;
  12.          m.b += 2;
  13.      }
  14.  }    
  15.  class Program
  16.  {
  17.      static void Main(string[] args)
  18.      {
  19.          math t = new math(20,  10);
  20.          t.sum(t);
  21.          Console.WriteLine(t.a + "  " + t.b);   
  22.          Console.ReadLine();
  23.      }
  24.  }

a) 10, 20
b) 20, 10
c) 40, 12
d) 5, 40

Answer

Answer: c [Reason:] t.sum(t) sends object ‘t’ as parameter whose variables a & b are multiplied and added by 2 respectively by sum() function of class math.Hence, a & b become 40 and 12 respectively.
Output : 40, 12

8. Accessibility modifier defined in a class are?
a) public, private, protected
b) public, internal, protected internal.
c) public, private, internal, protected internal.
d) public, private, protected, internal, protected internal

Answer

Answer: d [Reason:] None.

9. Choose the statements which are false in nature:
a) The base class member functions can access public member functions of derived class
b) An object of a derived class cannot access private member of the base class
c) Private members of the base class cannot be accessed by derived class member functions or objects of derived class
d) None of the mentioned

Answer

Answer: a [Reason:] None.

10. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned

Answer

Answer: a [Reason:] By default main() is declared private if no other access specifier is used for it.

C# MCQ Set 2

1. Name the exception thrown by read() on failure.
a) InterruptedException
b) SystemException
c) SystemInputException
d) I/O Exception

Answer

Answer: d [Reason:] read() throws I/O exception on failure.

2. Which of these methods are used to read single character from the console?
a) get()
b) getline()
c) read()
d) readLine()

Answer

Answer: c [Reason:] None.

3. Which of these method used to read strings from the console?
a) get()
b) getline()
c) read()
d) readLine()

Answer

Answer: d [Reason:] None.

4. Which among the following methods are used to write characters to a string?
a) StreamWriter
b) StreamReader
c) StringWriter
d) None of the mentioned

Answer

Answer: c [Reason:] The stream class method writes characters to the string.

5. Which method in Console enables to read individual inputs directly from the keyboard in a non line buffered manner?
a) Read()
b) ReadKey()
c) ReadLine()
d) All of the mentioned

Answer

Answer: b [Reason:] The .NET Framework includes a method in Console that enables you to read individual keystrokes directly from the keyboard, in a non-line-buffered manner. This method is called ReadKey().When it is called, it waits until a key is pressed. When the key is pressed, ReadKey( ) returns the keystroke immediately.

6. What is the output returned by Console if ReadLine() stores I/O error?
a) 1
b) 0
c) False
d) I/O EXCEPTION ERROR

Answer

Answer: d [Reason:] None.

7. What would be the output for following input from the console as a character?

  1.  static void Main(string[] args)
  2.  {
  3.      Console.WriteLine("what is your name?");
  4.      char s;
  5.      s = Convert.ToChar(Console.ReadLine());
  6.      Console.WriteLine("how are you: "+s);
  7.      Console.Read();
  8.  }

a) Compile time error
b) Code run successfully prints nothing on console
c) Code runs successfully prints input on console
d) Run time error

Answer

Answer: d [Reason:] Since only a single character is required to be entered on console when a string is entered , a run time exception is being generated as we had not used Read() which reads single character but used readLine() which reads string and is converted into the char using convert.tochar().

8. Name the method/methods used to read byte streams from the file?
a) ReadByte()
b) Read
c) Readkey()
d) None of the mentioned

Answer

Answer: a [Reason:] None.

9. Which of these classes are used by Byte streams for input and output operation?
a) InputStream
b) InputOutputStream
c) Reader
d) All of the mentioned

Answer

Answer: b [Reason:] Byte stream uses InputStream and OutputStream classes for input and output operation.

10. Which of these method/methods are used to read block or array of bytes from the file?
a) Read()
b) ReadByte()
c) ReadLine()
d) Readkey()

Answer

Answer: a [Reason:] To read a block of bytes, use Read( ), which has this general form:
int Read(byte[ ] array, int offset, int count).

C# MCQ Set 3

1. What is Recursion in CSharp defined as?
a) Recursion is another form of class
b) Recursion is another process of defining a method that calls other methods repeatedly
c) Recursion is a process of defining a method that calls itself repeatedly
d) Recursion is a process of defining a method that calls other methods which in turn calls this method

Answer

Answer: c [Reason:] Recursion is the process of defining something in terms of itself. It allows us to define method that calls itself repeatedly until it meets some base case condition.

2. Which of these will happen if recursive method does not have a base case?
a) Infinite loop condition occurrence
b) System gets hanged
c) After 10000 executions program will be automatically stopped
d) None of the mentioned

Answer

Answer: a [Reason:] If a recursive method does not have a base case which is necessary to meet the end of condition then an infinite loop occurs which results in stackoverflow exception error.

3. Which of these is not a correct statement?
a) A recursive method must have a base case
b) Recursion always uses stack
c) Recursion is always managed by C# Runtime environment
d) Recursive methods are faster that programmer written loop to call the function repeatedly using a stack

Answer

Answer: c [Reason:] No matter whatever is the programming language recursion is always managed by operating system.

4. What will be the correct output for the given code snippet?

  1.  class recursion 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Program 
  13.  {
  14.      public static void main(String args[]) 
  15.      {
  16.          recursion obj = new recursion() ;
  17.          Console.WriteLine(obj.fact(4));
  18.      }
  19.  }

a) 24
b) 30
c) 120
d) 144

Answer

Answer: a [Reason:] None.

5. What will be the correct output the for the given code snippet?

  1.  class maths 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Output 
  13.  {
  14.      static void main(String args[]) 
  15.      {
  16.          maths obj = new maths() ;
  17.          Console.WriteLine(obj.fact(1));
  18.      }
  19.  }

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

Answer

Answer: c [Reason:] fact() calculates recursively the factorial of a number when n turns to be 1, base case is executed consecutively and hence 1 is returned.
Output: 1

6. What will be the correct output for the given code snippet?

  1. class maths 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Output 
  13.  {
  14.      static void main(String args[]) 
  15.      {
  16.          maths obj = new maths() ;
  17.          Console.WriteLine(obj.fact(4)*obj.fact(2));
  18.      }
  19.  }

a) 64
b) 60
c) 120
d) 48

Answer

Answer: d [Reason:] 4! = 4*3*2*1 & 2! = 2*1 .So, 24*2 = 48.
Output : 48

7. What will be the correct output for the given code snippet?

  1.  class maths 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Output 
  13.  {
  14.      static void main(String args[]) 
  15.      {
  16.          maths obj = new maths() ;
  17.          Console.WriteLine(obj.fact(4)*(3));
  18.      }
  19.  }

a) 64
b) 60
c) 72
d) 84

Answer

Answer: c [Reason:] 4! = 4 * 3 *2 * 1 = 24 * 3 = 72.Not factorial of 3 but just multiply the number with 3.
Output : 72

8. Which of these data types is used by operating system to manage the Recursion in Csharp?
a) Array
b) Queue
c) Tree
d) Stack

Answer

Answer: d [Reason:] None.

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

  1.  class maths
  2.  {
  3.      public int fact(int n)
  4.      {
  5.          int result;
  6.          result = fact(n - 1) * n;
  7.          return result;
  8.      }
  9.  } 
  10.  class Program
  11.  {
  12.      static void Main(string[] args)
  13.      {            
  14.          maths obj = new maths();
  15.          Console.WriteLine(obj.fact(4));
  16.          Console.ReadLine();
  17.      }
  18.  }

a) 24
b) 30
c) Compile time error
d) Runtime Error

Answer

Answer: d [Reason:] Absence of base case condition. So absence of limit or end of for execution of a loop and hence results in stackoverflow exception error.

10. What will be the correct output for the given code snippet?

  1.  class maths
  2.  {
  3.      public int fact(int n)
  4.      {
  5.          int result;
  6.          if (n == 2)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Program
  13.  {
  14.      static void Main(string[] args)
  15.      {            
  16.          maths obj = new maths();
  17.          Console.WriteLine(obj.fact(4));
  18.          Console.ReadLine();
  19.      }
  20.  }

a) 24
b) 0
c) 12
d) 1

Answer

Answer: c [Reason:] fact() calculates factorial of number ‘4’ but this time base case condition is executed upto 2 only.As soon as n reaches 2 it returns 2.

C# MCQ Set 4

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

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 4;
  4.      int b = 5;
  5.      int c = 6;
  6.      int d = 8;
  7.      if (((a * b / c) + d) >= ((b * c + d ) / a))
  8.      {
  9.          Console.WriteLine("Line 1 - a is greater to b");
  10.          Console.WriteLine((a * b / c) + d);
  11.      }
  12.      else
  13.      {
  14.          Console.WriteLine("Line 1 - a is not greater to b");
  15.          Console.WriteLine((b * c + d )/ a);
  16.      }
  17.  }

a) “Line 1 – a is greater to b”
11
b) “Line 1 – a is not greater to b”
9
c) Both are equal
d) None of the mentioned

Answer

Answer: a [Reason:] Now, here in ‘if’ condition both conditions of parenthesis and hence evaluating operators based on parenthesis are tested.
for expression : ((a * b / c) + d)
Step 1 : (a*b/c) (Evaluating as 4*5/6 = 3)
Step 2 : ( (a*b/c) + d ) (Evaluating (3 + 8 = 11))
Result : 11
for expression : (b * c + d )/ a
Step 1 : (b*c + d) (Evaluating as 5*6 +8 = 38)
Step 2: (b*c + d) / a (Evaluating as 38 / 4 = 9)
Result : 9
The relational operator “>=” between both expressions check for largest figure and hence consecutively executes the if condition.
Output : Line 1 – a is greater to b.
11

2. Check for given code whether the given relation operator works according to the if condition or not.

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 10;
  4.      int b = 5;
  5.      int c = 12; 
  6.      int e = 8;
  7.      int d;
  8.      d = Convert.ToInt32((a * (c - b) / e + (b + c)) <= (e * (c + a) / (b + c) + a));
  9.      Console.WriteLine(d);
  10.      if (d == 1)
  11.      {
  12.          Console.WriteLine("C# is great language!");
  13.          Console.WriteLine((a * (c - b) / e + (b + c)));
  14.      }
  15.      else
  16.      {
  17.          Console.WriteLine("harsh is not great language!");
  18.          Console.WriteLine((e * (c + a) / (b + c) + a));
  19.      }
  20.  }

a) 0
C# is great!
20
b) 0
C# is not great!
25
c) 0
C# is great!
25
d) 0
C# is not great!
20

Answer

Answer: d [Reason:] The expression (a * (c – b) / e + (b + c)) on evaluation parenthesis by parenthesis gives result mathematically as 25.Similarly, (e * (c + a) / (b + c) + a) on evaluation parenthesis by parenthesis gives mathematically result as 20.Relational operator now checks for condition as in if condition as (25 < 20 ) which is false. So, a false bit in form of ‘0’ is assigned to d. Now, in if condition (d != 1) as d = 0. So, condition after else is evaluated.
Output :0.
C# is not great!.
20.

3. Which of the following is/are not Relational operators in C#.NET ?
a) >=
b) <>=
c) Not
d) <=

Answer

Answer: b [Reason:] By definition.

4. The relevant output for the following set of code is :

  1.     int n = 2;
  2.     int p = 4;
  3.     int q = 5;
  4.     int w = 3;
  5.     if ( !((p * q) /n <= (q * w) + n/p ))
  6.     {
  7.         Console.WriteLine( ++p + w++ + " " + ++n);
  8.         Console.WriteLine("b");
  9.     }
  10.     else
  11.     {
  12.         Console.WriteLine(--p + q-- + " " + --n);
  13.         Console.WriteLine("a");
  14.     }

a) 6 2
b
b) 8 1
a
c) 6 1
a
d) 8 1
b

Answer

Answer: b [Reason:] After evaluation of the test expression (!((p*q)/n <= (q*w)+n/p )) .The use of logical operator(!) turns false(0) result to bit ‘1’ and hence the condition evaluated by ‘if’ loop is after else as :
–p = 3
q–= 5
–p + q– = 8 where now value of ‘q’is 4.
–n = 2 – 1 =1.
So,values after evaluations are: 8 1.
a.
Output : 8 1
a

5. Select the relevant output for the set of code :
m = 5;
int y;
1. y = m++;
2. y = ++m;
a) y = 5, m = 6 ; y = 5, m = 5
b) y = 6, m = 6; y = 7, m = 6
c) y = 5, m = 6; y = 7, m = 7
d) y = 5, m = 6; y = 7, m = 8

Answer

Answer: c [Reason:] step 1 : m = 5, y = m++ i.e y =5 ,m =6.
step 2 : y = ++m , Since m = 6 .So, m = 7 on ++m and hence y = 7.
Output : y = 5, m = 6; y =7 , m = 7.

6. Predict the output for the follwing set of code :

  1.   static void Main(string[] args)
  2.   {
  3.       int a = 3, b = 5, c = 1;
  4.       int z = ++b;
  5.       int y = ++c;
  6.       b = Convert.ToInt32((Convert.ToBoolean(z)) && (Convert.ToBoolean(y)) || Convert.ToBoolean(Convert.ToInt32(!(++a == b))));
  7.       a = Convert.ToInt32(Convert.ToBoolean(c) || Convert.ToBoolean(a--));
  8.       Console.WriteLine(++a);
  9.       Console.WriteLine(++b);
  10.       Console.WriteLine(c);
  11.   }

a) 2 ,2 ,1
b) 2 ,3 ,2
c) 2 ,2 ,2
d) 2 ,0 ,9

Answer

Answer: c [Reason:] z = 6 as ++b.
y = 2 as ++c.
6 && 2 = 1
(++a == b ) which is false as 4!=6. Now, !(false) = true i.e 1.
So, 1 || 1 = 1. So, b = 1.
Similarly, c = 2 and a = 4.Now, 2 || 4 = 1.
So, a = 1.
Hence ++a = 2,++b = 2, c = 2.
Output : 2, 2, 2

7. Select the output for the relevant code set :

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 4, b = 5, c = 7, u = 9;
  4.      int h;
  5.      h = (Convert.ToInt32(u < b)) + (a + b--) + 2;
  6.      Console.WriteLine(h);
  7.      Console.WriteLine(b);
  8.      Console.WriteLine(u < b);
  9.  }

a) 12, 5, 0
b) 11, 4, False
c) 11, 5, 0
d) 12, 4, False

Answer

Answer: b [Reason:] Step 1: Convert.ToInt32(u < b)(Evaluate result as 9 < 5 which is false in nature.So, solution is converted from ‘false’ to ‘0’).
Step 2: (a + b–) evaluated as 4 + 5 = 9 + 2 =11.
Step 3: u < b evaluated as ‘False’ without being converted to ‘0’.
Output : 11
4
False.

8. Select the suitable output for the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int m = 10, n = 5, p = 20;
  4.      bool b1 =  m * p / n <= p * n / m ;
  5.      int l = p - 2 * m;
  6.      bool b2 = l == 0;
  7.      int z = Convert.ToInt32(b2);
  8.      int k = Convert.ToInt32(b1);
  9.      Console.WriteLine(k);
  10.      Console.WriteLine(z);
  11.  }

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

Answer

Answer : c [Reason:] Solving the expression for b1 tests the condition for either true or false result in ‘0’. Similarly, for ‘b2’ ‘l’ on solving gives ‘0’. So, condition is true for bool b2 as 0 == 0 . Hence, k = 0 and z = 1.
Output : 0 1.

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

  1.  class method1
  2.  {
  3.      public int fun(int m)
  4.      {
  5.          return( m++ % 10);
  6.      }
  7.  }
  8.  class Program
  9.  {
  10.      static void Main(string[] args)
  11.      {
  12.          int a = 23, b = 0, c;
  13.          method1 z = new method1();
  14.          c = z.fun (++b * --a % 2);
  15.          int d = (z.fun (c-- + --a));
  16.          Console.WriteLine(c);
  17.          Console.WriteLine(a++);
  18.          Console.WriteLine(d);
  19.          Console.ReadLine();
  20.      }
  21.  }

a) -1, 22, 0
b) -1, 21, 1
c) 0, 22, 1
d) 0, 22, 0

Answer

Answer : b [Reason:] Here, for first value of c, ++b = 1 and 1 * (22%2) = 0 . c = 0 . Now c — = 0 and — a = 22 – 1 =21.Now, c — is the first condition executed and then decremented So, c = -1.Similarly, a++ = 21. Now, as we can see from options we are confirmed over value of c = -1, a = 21. So, we can easily know that d = 1.
Output:-1 21 1

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

  1.   static void Main(string[] args)
  2.   {
  3.       int a = 8, b = 6, c = 10;
  4.       int d = a * c * 2 / Convert.ToInt32(Math.Pow ((c - b), 2));
  5.       if (d == (c = Convert.ToInt32(Math.Sqrt (a * a + b * b))) && c == 10)
  6.       {
  7.           Console.WriteLine("figure is hypotenuse");
  8.       }
  9.       else
  10.       {
  11.          Console.WriteLine("figure is square");
  12.       }
  13.   }

a) Figure is square
b) Figure is hypotenuse
c) False
d) None of the mentioned

Answer

Answer : a [Reason:] Solving the expression for ‘c’ we get c==10 in if first condition as (c == Convert.ToInt32(Math.Sqrt(a * a + b * b))). The logical condition when d == (c = 10) suits here . Similarly, going for second condition where c ==10 as ‘&&’ operator exists between both given condition and at last both are evaluated to true as c == 10. So, only first statement is executed.
Output :Figure is square

C# MCQ Set 5

1. Choose the correct type of variable scope for the given defined variables.

  1.   class ABC
  2.   {
  3.       static int m;
  4.       int n;
  5.       void fun (int x , ref int y, out int z, int[] a)
  6.       {
  7.          int j = 10;
  8.       }
  9.   }

Scope declaration:
a) m = static variable, n = local variable, x = output parameter, y = reference parameter, j = instance variable, z =output parameter, a[0]= array element
b) m = static variable, n = instance variable, x = value parameter, y = reference parameter, j = local variable, z =output parameter , a[0] = array element
c) m = static variable, n = instance variable, x = reference parameter, y = value parameter, j = local variable, z =output parameter, a[0] = array element
d) m = local variable, n = instance variable, x = reference parameter, y = value parameter, j = static variable, z =output parameter, a[0] = array element

Answer

Answer: b [Reason:] By definition of scope of variables.

2. Correct Output for the given set of programming code is :

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         int  i ;
  6.         for (i = 0; i < 5; i++)
  7.         {
  8.             Console.WriteLine(i);
  9.         }
  10.         Console.ReadLine();
  11.     }
  12. }

a) 0, 1, 2, 3, 4, 5
b) 0, 1, 2, 3
c) 0, 1, 2, 3, 4
d) 0, 0, 0, 0, 0

Answer

Answer: c [Reason:] Scope of ‘i’ is alive within block in which it is declared. So, change in value of i within for loop is reserved until condition of for loop is executing.
Output:0, 1, 2, 3, 4

3. Correct Output for the given set of programming code is :

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int i;
  6.          for ( i = 0; i < 5; i++)
  7.          {
  8.  
  9.          }
  10.          Console. WriteLine(i);
  11.          Console. ReadLine();
  12.      }
  13. }

a) 0, 1, 2, 3, 4, 5
b) 0, 1, 2, 3, 4
c) 5
d) 4

Answer

Answer: c [Reason:] Since final console statement is outside forloop. So, result will be printed in final values only.
Output: 5

4. Correct Output for the given set of programming code is :

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int i ;
  6.          for ( i = 0; i < 5; i++)
  7.          {
  8.              int j = 0;
  9.              j += i; 
  10.              Console. WriteLine(j);
  11.          }
  12.          Console. WriteLine(i);
  13.          Console. ReadLine();
  14.      }
  15.  }

a) 0, 1, 2, 3, 4, 5, 6
b) 0, 1, 2, 3, 4, 5
c) 0, 1, 2, 3, 4
d) 0, 1, 2, 3

Answer

Answer: b [Reason:] None.
Output:0, 1, 2, 3, 4, 5

5. Correct Output for the given set of programming code is :

  1.   static void Main(string[] args)
  2.   {
  3.       int i ;
  4.       for (i = 0; i < 5; i++)
  5.       {
  6.           int j = 0;
  7.           j += i; 
  8.           Console. WriteLine(j);
  9.       }
  10.       Console. WriteLine( i * j);
  11.       Console. ReadLine();
  12.   }

a) 0, 1, 6, 18, 40
b) 0, 1, 5, 20, 30
c) Compile time error
d) 0, 1, 2, 3, 4, 5

Answer

Answer: c [Reason:] The scope of j is local in nature it cannot be extended outside the block in which it is defined.

6. Scope of variable is related to definition of variable as:
1. Region of code within which variable value is valid and hence can be accessed.
2. No, relation with region where variable is declared its value is valid in entire scope.
a) a
b) b
c) a, b
d) None of the mentioned

Answer

Answer: a [Reason:] Scope of variable is the area or region within which variable is declared and hence intialized values of different kind. Based, on which operations of different kinds are carried out on that variable declared within that scope. Its value is preserved until and unless scope of that block ({ })is not expired because as soon as scope gets over. Hence, variable value gets expired. Hence, it’s inaccessible after it.

7. Select the correct output for the following programming code

  1.  class Program
  2.  {
  3.      public static void Main(string[] args)
  4.      {
  5.          int i = 100;
  6.          for (a = 0; a < 5; a++)
  7.          {
  8.              int i = 200;
  9.              Console. WriteLine(a * i);
  10.          }
  11.          Console. ReadLine();
  12.      }
  13.  }

a) 5, 10, 15, 20
b) 0, 5, 10, 20
c) Compile time error
d) 0, 1, 2, 3, 4

Answer

Answer: c [Reason:] The compiler cannot interpret between variable ‘i’ declared as an instance variable outside for loop block and variable ‘i’ declared as a local variable inside the for loop context. The instance variable ‘id’ defined before the for loop is still in scope inside for loop and hence goes out of scope only when main() is finished executing. The local variable ‘i’ declared inside for loop had scope limited within blocks({ }) in which it is declared and hence creates name conflict with instance variable ‘i’ so, compiler is unable to distinguish between both. When instance variable ‘i’ is removed away. The program runs accurately producing the output as “0, 200, 400, 600, 800”, this explains the concept of scope deceleration.

8. Syntax for declaration and initialization of data variable is :

 a) <data type><var_name> = <Value>;
 b) <datatype><var_name>;
 c) <var_name><data type>;
 d) <var_name> = <value>;
Answer

Answer: a [Reason:] By definition.

9. Select the correct output for the following set of code :

  1.  class Program
  2.  {
  3.      public static void Main(string[] args)
  4.      {
  5.          int i, j;
  6.          i = (j = 5) + 10;
  7.          Console. WriteLine(i);
  8.          Console. WriteLine(j);
  9.          Console. ReadLine();
  10.      }
  11.  }

a) 15, 15
b) 10, 5
c) 15, 5
d) 10, 15

Answer

Answer: c [Reason:] j=’5′ will return value of 5 stored it in variable ‘j’ but value assigned to variable ‘i’ will be first value of ‘j’ and hence incremented a value of ’10’ in that value of ‘j’ i. e 15.
Output:15, 5

10. Choose effective differences between ‘Boxing’ and ‘Unboxing’.
a) ‘Boxing’ is the process of converting a value type to the reference type and ‘Unboxing’ is the process of converting reference to value type
b) ‘Boxing’ is the process of converting a reference type to value type and ‘Unboxing’ is the process of converting value type to reference type
c) In ‘Boxing’ we need explicit conversion and in ‘Unboxing’ we need implicit conversion
d) Both ‘Boxing’ and ‘Unboxing’ we need implicit conversion

Answer

Answer: a [Reason:] By definition.

11. Select differences between reference type and value type :
1. Memory allocated to ‘Value type’ is from heap and reference type is from ‘System. ValueType’
2. Memory allocated to ‘Value type’ is from ‘System. ValueType’ and reference type is from ‘Heap’
3. Structures, enumerated types derived from ‘System. ValueType’ are created on stack, hence known as ValueType and all ‘classes’ are reference type because values are stored on heap
a) 1, 3
b) 2, 3
c) 1, 2, 3
d) 1

Answer

Answer: b [Reason:] By definition.

12. Correct output for the following set of code is :

  1.     public static void Main(string[] args)
  2.     {
  3.         int i = 123;
  4.         object o = i;
  5.         i = 456;
  6.         System. Console. WriteLine("The value-type value = {0}", i);
  7.         System. Console. WriteLine("The object-type value = {0}", o);
  8.         Console. ReadLine();
  9.     }

a) 123, 123
b) 456, 123
c) 456, 456
d) 123, 456

Answer

Answer: b [Reason:] The concept of boxing is implemented here. The variable ‘i’ of ‘int’ type is boxed using variable ‘o’ of object type and hence value is stored inside it and is initialized to the object variable ‘o’. Next, variable ‘i’ is again initialized with some value overriding it’s previous stored value.
Output:456, 123

13. Correct output for the following set of code is :

  1.   public static void Main(string[] args)
  2.   {
  3.       int i = 546;
  4.       object o = i;
  5.       int n =(int) o;
  6.       o = 70;
  7.       System. Console. WriteLine("The value-type value = {0}", n);
  8.       System. Console. WriteLine("The object-type value = {0}", o);
  9.       Console. ReadLine();
  10.   }

a) 546, 0
b) 546, 546
c) 546, 70
d) 70, 546

Answer

Answer: c [Reason:] The concept of ‘unboxing’ is implemented here . To ‘unbox’ an object back to value type, we have to do it explicitly as “int n = (int) o”.
Output:546, 70

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.