This set of C# Assessment Questions and Answers focuses on “Accessor controls of class”.
1. Which among these access specifiers should be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned
2. Which of these is used as default for a member of a class if no access specifier is used for it?
a) private
b) public
c) protected internal
d) protected
3. What is the process by which we can control what parts of a program can access the members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
4. Which of these base class are accessible to the derived class members?
a) static
b) protected
c) private
d) shared
5. What will be the output of the following C# code snippet?
class access
{
public int x;
private int y;
public void cal(int a, int b)
{
x = a + 1;
y = b;
}
}
class Program
{
static void Main(string[] args)
{
access obj = new access();
obj.cal(2, 3);
Console.WriteLine(obj.x + " " + obj.y);
}
}
a) 3 3
b) 2 3
c) Run time error
d) Compile time error
6. What will be the output of the following C# code snippet?
class access
{
public int x;
private int y;
public void cal(int a, int b)
{
x = a + 1;
y = b;
}
public void print()
{
Console.WriteLine(" " + y);
}
}
class Program
{
static void Main(string[] args)
{
access obj = new access();
obj.cal(2, 3);
Console.WriteLine(obj.x);
obj.print();
Console.ReadLine();
}
}
a) 2 3
b) 3 3
c) Run time error
d) Compile time error
7. What will be the output of the following C# code snippet?
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
5, 9
8. What will be the output of the following C# code snippet?
class static_out
{
public static int x;
public static int y;
public int add(int a, int b)
{
x = a + b;
y = x + b;
return 0;
}
}
class Program
{
static void Main(string[] args)
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
Console.WriteLine(static_out.x + " " + static_out.y );
Console.ReadLine();
}
}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
7, 9
9. Which of these access specifiers must be used for class so that it can be inherited by another subclass?
a) public
b) private
c) both public & private
d) none of the mentioned
10. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program
b) private members of class can only be accessed by other members of the class
c) private members of class can be inherited by a subclass, and become protected members in subclass
d) protected members of a class can be inherited by a subclass, and become private members of the subclass
11. What will be the output of the following C# code snippet?
class test
{
public int a;
public int b;
public test(int i, int j)
{
a = i;
b = j;
}
public void meth(test o)
{
o.a *= 2;
o.b /= 2;
}
}
class Program
{
static void Main(string[] args)
{
test obj = new test(10, 20);
obj.meth(obj);
Console.WriteLine(obj.a + " " + obj.b);
Console.ReadLine();
}
}
a) 20, 40
b) 40, 20
c) 20, 10
d) 10, 20
20, 10
12. Accessibility modifiers 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