C# multiple choice questions on scope and lifetime of variables in C# Programming Language.
1. Choose the correct type of variable scope for the following C# defined variables.
-
class ABC
-
{
-
static int m;
-
int n;
-
void fun (int x , ref int y, out int z, int[] a)
-
{
-
int j = 10;
-
}
-
}
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
Clarification: By definition of scope of variables.
2. What will be the output of the following C# code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int i ;
-
for (i = 0; i < 5; i++)
-
{
-
Console.WriteLine(i);
-
}
-
Console.ReadLine();
-
}
-
}
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
Clarification: 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:
3. What will be the output of the following C# code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int i;
-
for ( i = 0; i < 5; i++)
-
{
-
}
-
Console. WriteLine(i);
-
Console. ReadLine();
-
}
-
}
a) 0, 1, 2, 3, 4, 5
b) 0, 1, 2, 3, 4
c) 5
d) 4
Answer: c
Clarification: Since final console statement is outside forloop. So, result will be printed in final values only.
Output:
4. What will be the output of the following C# code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int i ;
-
for ( i = 0; i < 5; i++)
-
{
-
int j = 0;
-
j += i;
-
Console. WriteLine(j);
-
}
-
Console. WriteLine(i);
-
Console. ReadLine();
-
}
-
}
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
Clarification: None.
Output:
5. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i ;
-
for (i = 0; i < 5; i++)
-
{
-
int j = 0;
-
j += i;
-
Console. WriteLine(j);
-
}
-
Console. WriteLine( i * j);
-
Console. ReadLine();
-
}
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
Clarification: 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
Clarification: 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?
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
int i = 100;
-
for (a = 0; a < 5; a++)
-
{
-
int i = 200;
-
Console. WriteLine(a * i);
-
}
-
Console. ReadLine();
-
}
-
}
a) 5, 10, 15, 20
b) 0, 5, 10, 20
c) Compile time error
d) 0, 1, 2, 3, 4
Answer: c
Clarification: 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) = ;
b) ;
c) ;
d) = ;
Answer: a
Clarification: By definition.
9. What will be the output of the following C# code?
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
int i, j;
-
i = (j = 5) + 10;
-
Console. WriteLine(i);
-
Console. WriteLine(j);
-
Console. ReadLine();
-
}
-
}
a) 15, 15
b) 10, 5
c) 15, 5
d) 10, 15
Answer: c
Clarification: 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:
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
Clarification: 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
Clarification: By definition.
12. What will be the output of the following C# code?
-
public static void Main(string[] args)
-
{
-
int i = 123;
-
object o = i;
-
i = 456;
-
System. Console. WriteLine("The value-type value = {0}", i);
-
System. Console. WriteLine("The object-type value = {0}", o);
-
Console. ReadLine();
-
}
a) 123, 123
b) 456, 123
c) 456, 456
d) 123, 456
Answer: b
Clarification: 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:
13. What will be the output of the following C# code?
-
public static void Main(string[] args)
-
{
-
int i = 546;
-
object o = i;
-
int n =(int) o;
-
o = 70;
-
System. Console. WriteLine("The value-type value = {0}", n);
-
System. Console. WriteLine("The object-type value = {0}", o);
-
Console. ReadLine();
-
}
a) 546, 0
b) 546, 546
c) 546, 70
d) 70, 546
Answer: c
Clarification: 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: