C# Programming Questions and Answers Part-20

1. 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: d
Explanation: public, private, protected, internal, protected internal

2. 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: a
Explanation: The base class member functions can access public member functions of derived class

3. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned

Answer: a
Explanation: By default main() is declared private if no other access specifier is used for it.

4. What will be the output of the following C# code?
class Program
{
static void Main(string[] args)
{
int i = 5;
int j;
method1(ref i);
method2(out j);
Console.writeline(i + " " + j);
}
static void method1(ref int x)
{
x = x + x;
}
static void method2(out int x)
{
x = 6;
x = x * x;
}
}
a) 36, 10
b) 10, 36
c) 0, 0
d) 36, 0

Answer: b
Explanation: Variable ‘i’ is passed as reference parameter declared with ‘ref’ modifier and variable ‘j’ is passed as a output parameter declared with ‘out’ keyword. Reference parameter used to pass value by reference is the same with out parameter.
Output :
10, 36

5. Statements about ‘ref’ keyword used in C#.NET are?
a) The ref keyword causes arguments to be passed by reference
b) While using ‘ref’ keyword any changes made to the parameter in the method will be reflected in the variable when control is passed back to the calling method
c) Ref usage eliminates overhead of copying large data items
d) All of the mentioned

Answer: d
Explanation: All of the mentioned

6. Which method does following C# code explains?
static void Main(string[] args)
{
int a = 10, b = 20;
method(ref a, ref b);
console.writeline(a + " " + b);
}
static void swap(ref int i, ref int j)
{
int t;
t = i;
i = j;
j = t;
}
a) Call by reference
b) Call by value
c) Output parameter
d) parameter arrays

Answer: a
Explanation: The following set of code explains swapping of numbers by reference parameters which makes usage of call by reference process.

7. What will be the output of the following C# code?
static void main(string[] args)
{
int []arr = new int[]{ 1, 2, 3, 4, 5};
fun (ref arr);
for (int i = 0; i < arr.Length ; i++)
Console.WriteLine( arr[i] + " ");
}
static void fun(ref int[]a)
{
a = new int[6];
a[3] = 32;
a[1] = 24;
}
a) 0, 0, 32, 0, 0, 0
b) 0, 24, 0, 32, 0, 0
c) 24, 0, 32, 0, 0, 0
d) 0, 0, 32, 0, 0, 0

Answer: b
Explanation: index positions which are assigned the new values are passed as a reference parameter and hence rest positions are filled with zero values.
Output :
0 24 0 32 0 0

8. What will be the output of the following C# code?
static void Main(string[] args)
{
int a = 5;
int b = 0, c = 0;
method (a, ref b, ref c);
Console.WriteLine(b + " " + c);
Console.ReadLine();
}
static int method(int x, int p, ref int k)
{
p = x + x * x;
k = x * x + p;
return 0;
}
a) 30, 55
b) 55, 30
c) Compile time error
d) 0, 0

Answer: c
Explanation: Error occurrence as mismatch in parameter of method() definition. Keyword ‘ref’ should be used with parameter ‘p’ as ref int p.

9. Keyword used to define call by reference parameter in C# .NET?
a) &
b) out
c) ref
d) &&

Answer: c
Explanation: ref

10. Which statement is/are correct?
a) An argument passed to a ref parameter need not be initialized first
b) Variables passed as out arguments need to be initialized prior to being passed
c) To use a ref parameter, only the calling method must explicitly use the ref keyword
d) None of the mentioned

Answer: d
Explanation: None of the Above