Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Questions & Answers ? Accessor controls of class
C# Questions & Answers – Accessor controls of class
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
Explanation: main() method must be specified public as it called by Csharp run time system outside of the program, by default main is private in nature if no access specifier is used.
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
Explanation: None.
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
Explanation: None.
4. Which of these base class are accessible to the derived class members?
a) static
b) protected
c) private
d) shared
Explanation: None.
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
Explanation: ‘y’ is defined privately which cannot be accessed outside its scope.
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
Explanation: None.
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
Explanation: 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
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
Explanation: None.
Output :
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
Explanation: None.
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
Explanation: private members of a class cannot be inherited by a 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
Explanation: None.
Output :
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
Explanation: None.