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?
-
class sum
-
{
-
public int x;
-
private int y;
-
public void math(int a, int b)
-
{
-
x = a * 4;
-
y = b;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sum p = new sum();
-
p.math(12, 30);
-
Console.WriteLine(p.x + " " + p.y);
-
Console.ReadLine();
-
}
-
}
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?
-
class sum
-
{
-
public int x;
-
public int y;
-
public int add (int a, int b)
-
{
-
x = a + b;
-
y = x + b;
-
return 0;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sum obj1 = new sum();
-
sum obj2 = new sum();
-
int a = 2;
-
obj1.add(a, a + 1);
-
obj2.add(5, a);
-
Console.WriteLine(obj1.x + " " + obj2.y);
-
Console.ReadLine();
-
}
-
}
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?
-
class math
-
{
-
public int a,b;
-
public math(int i, int j)
-
{
-
a = i;
-
b = j;
-
}
-
public void sum(math m)
-
{
-
m.a *= 2;
-
m.b += 2;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
math t = new math(20, 10);
-
t.sum(t);
-
Console.WriteLine(t.a + " " + t.b);
-
Console.ReadLine();
-
}
-
}
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?
-
static void Main(string[] args)
-
{
-
Console.WriteLine("what is your name?");
-
char s;
-
s = Convert.ToChar(Console.ReadLine());
-
Console.WriteLine("how are you: "+s);
-
Console.Read();
-
}
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?
-
class recursion
-
{
-
int fact(int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Program
-
{
-
public static void main(String args[])
-
{
-
recursion obj = new recursion() ;
-
Console.WriteLine(obj.fact(4));
-
}
-
}
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?
-
class maths
-
{
-
int fact(int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Output
-
{
-
static void main(String args[])
-
{
-
maths obj = new maths() ;
-
Console.WriteLine(obj.fact(1));
-
}
-
}
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?
-
class maths
-
{
-
int fact(int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Output
-
{
-
static void main(String args[])
-
{
-
maths obj = new maths() ;
-
Console.WriteLine(obj.fact(4)*obj.fact(2));
-
}
-
}
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?
-
class maths
-
{
-
int fact(int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Output
-
{
-
static void main(String args[])
-
{
-
maths obj = new maths() ;
-
Console.WriteLine(obj.fact(4)*(3));
-
}
-
}
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?
-
class maths
-
{
-
public int fact(int n)
-
{
-
int result;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
Console.WriteLine(obj.fact(4));
-
Console.ReadLine();
-
}
-
}
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?
-
class maths
-
{
-
public int fact(int n)
-
{
-
int result;
-
if (n == 2)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
maths obj = new maths();
-
Console.WriteLine(obj.fact(4));
-
Console.ReadLine();
-
}
-
}
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:
-
static void Main(string[] args)
-
{
-
int a = 4;
-
int b = 5;
-
int c = 6;
-
int d = 8;
-
if (((a * b / c) + d) >= ((b * c + d ) / a))
-
{
-
Console.WriteLine("Line 1 - a is greater to b");
-
Console.WriteLine((a * b / c) + d);
-
}
-
else
-
{
-
Console.WriteLine("Line 1 - a is not greater to b");
-
Console.WriteLine((b * c + d )/ a);
-
}
-
}
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.
-
static void Main(string[] args)
-
{
-
int a = 10;
-
int b = 5;
-
int c = 12;
-
int e = 8;
-
int d;
-
d = Convert.ToInt32((a * (c - b) / e + (b + c)) <= (e * (c + a) / (b + c) + a));
-
Console.WriteLine(d);
-
if (d == 1)
-
{
-
Console.WriteLine("C# is great language!");
-
Console.WriteLine((a * (c - b) / e + (b + c)));
-
}
-
else
-
{
-
Console.WriteLine("harsh is not great language!");
-
Console.WriteLine((e * (c + a) / (b + c) + a));
-
}
-
}
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 :
-
int n = 2;
-
int p = 4;
-
int q = 5;
-
int w = 3;
-
if ( !((p * q) /n <= (q * w) + n/p ))
-
{
-
Console.WriteLine( ++p + w++ + " " + ++n);
-
Console.WriteLine("b");
-
}
-
else
-
{
-
Console.WriteLine(--p + q-- + " " + --n);
-
Console.WriteLine("a");
-
}
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 :
-
static void Main(string[] args)
-
{
-
int a = 3, b = 5, c = 1;
-
int z = ++b;
-
int y = ++c;
-
b = Convert.ToInt32((Convert.ToBoolean(z)) && (Convert.ToBoolean(y)) || Convert.ToBoolean(Convert.ToInt32(!(++a == b))));
-
a = Convert.ToInt32(Convert.ToBoolean(c) || Convert.ToBoolean(a--));
-
Console.WriteLine(++a);
-
Console.WriteLine(++b);
-
Console.WriteLine(c);
-
}
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 :
-
static void Main(string[] args)
-
{
-
int a = 4, b = 5, c = 7, u = 9;
-
int h;
-
h = (Convert.ToInt32(u < b)) + (a + b--) + 2;
-
Console.WriteLine(h);
-
Console.WriteLine(b);
-
Console.WriteLine(u < b);
-
}
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 :
-
static void Main(string[] args)
-
{
-
int m = 10, n = 5, p = 20;
-
bool b1 = m * p / n <= p * n / m ;
-
int l = p - 2 * m;
-
bool b2 = l == 0;
-
int z = Convert.ToInt32(b2);
-
int k = Convert.ToInt32(b1);
-
Console.WriteLine(k);
-
Console.WriteLine(z);
-
}
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 :
-
class method1
-
{
-
public int fun(int m)
-
{
-
return( m++ % 10);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int a = 23, b = 0, c;
-
method1 z = new method1();
-
c = z.fun (++b * --a % 2);
-
int d = (z.fun (c-- + --a));
-
Console.WriteLine(c);
-
Console.WriteLine(a++);
-
Console.WriteLine(d);
-
Console.ReadLine();
-
}
-
}
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 :
-
static void Main(string[] args)
-
{
-
int a = 8, b = 6, c = 10;
-
int d = a * c * 2 / Convert.ToInt32(Math.Pow ((c - b), 2));
-
if (d == (c = Convert.ToInt32(Math.Sqrt (a * a + b * b))) && c == 10)
-
{
-
Console.WriteLine("figure is hypotenuse");
-
}
-
else
-
{
-
Console.WriteLine("figure is square");
-
}
-
}
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.
-
class ABC
-
{
-
static int m;
-
int n;
-
void fun (int x , ref int y, out int z, int[] a)
-
{
-
int j = 10;
-
}
-
}
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 :
-
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
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 :
-
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
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 :
-
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
Answer: b [Reason:] None.
Output:0, 1, 2, 3, 4, 5
5. Correct Output for the given set of programming code is :
-
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
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
-
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
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 :
-
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
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 :
-
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
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 :
-
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
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