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.
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
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
0, 1, 2, 3, 4
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
5
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
0, 1, 2, 3, 4, 5
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
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
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
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>;
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
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
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
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
456, 123
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
546, 70