C# Questions & Answers ? Scope and Lifetime of Variables
Posted by Superadmin on August 18 2022 04:40:30

C# Questions & Answers – Scope and Lifetime of Variables

 

 

 

This section of our 1000+ C# multiple choice questions focuses on scope and lifetime of variables in C# Programming Language.

 

 

 

 

1. Choose the correct type of variable scope for the following C# 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.   }

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
Explanation: By definition of scope of variables.
 

 

 

 

 

 

2. What will be the output of the following C# code?

  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. What will be the output of the following C# code?

  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.
Output:

 

 5

 

 

 

 

 

4. What will be the output of the following C# code?

  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
Explanation: None.
Output:

 

0, 1, 2, 3, 4, 5

 

 

 

 

 

5. What will be the output of the following C# code?

  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:
i. Region of code within which variable value is valid and hence can be accessed.
ii. No, relation with region where variable is declared its value is valid in entire scope.
a) i
b) ii
c) i, ii
d) None of the mentioned

Answer: a
Explanation: Scope of variable is the area or region within which variable is declared and hence initialized 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. What will be the output of the following C# 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) <data type><var_name>;
c) <var_name><data type>;
d) <var_name> = <value>;

Answer: a
Explanation: By definition.

 

 

 

 

 

9. What will be the output of the following C# 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 increment 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: a
Explanation: By definition.

 

 

 

 

 

 

11. Select differences between reference type and value type:
i. Memory allocated to ‘Value type’ is from heap and reference type is from ‘System. ValueType’
ii. Memory allocated to ‘Value type’ is from ‘System. ValueType’ and reference type is from ‘Heap’
iii. 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) i, iii
b) ii, iii
c) i, ii, iii
d) i

Answer: b
Explanation: By definition.

 

 

 

 

 

 

 

12. What will be the output of the following C# code?

  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. What will be the output of the following C# code?

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

 

546, 70