C# Multiple Choice Questions & Answers on Scope and Lifetime of Variables for Freshers

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: b
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: c

Explanation: 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: c

Explanation:Since final console statement is outside forloop. So, result will be printed in final values only.
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: b
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: c

Explanation: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: a

Explanation: 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: c

Explanation: 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: a
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: c

Explanation : 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.
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: a
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: b
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: b.

Explanation :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: c

Explanation : 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”.

Related

Multiple Choice Questions 558048489246586038

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item