In C#, what is the default access modifier for class members?
Answer: B
The default access modifier for class members in C# is 'private', which restricts access to within the class only.
Q.2Easy
Which keyword is used to create a derived class in C#?
Answer: C
In C#, the colon (:) is used to indicate inheritance. For example, 'class Child : Parent' creates a derived class.
Q.3Easy
In C#, can a class inherit from multiple classes?
Answer: B
C# does not support multiple class inheritance to avoid the diamond problem. However, a class can implement multiple interfaces.
Q.4Easy
Which of the following correctly demonstrates an abstract class with an abstract method in C#?
Answer: A
An abstract class uses the 'abstract' keyword, and abstract methods must be declared without implementation inside abstract classes.
Q.5Easy
Which access modifier allows a member to be accessed only within the same assembly?
Answer: B
'internal' access modifier restricts member access to the same assembly only. This is useful for creating assembly-wide internal APIs.
Advertisement
Q.6Easy
What is the output of using 'nameof()' operator on a property in C#?
Answer: B
'nameof()' operator (C# 6+) returns the name of a variable, type, or member as a string literal, useful for logging and validation.
Q.7Easy
What happens when you mark a class with the 'sealed' modifier?
Answer: B
'sealed' keyword prevents a class from being inherited. This is useful for security and performance optimization when you want to prevent further derivation.
Q.8Easy
What does the 'base' keyword do in a derived class method in C#?
Answer: B
'base' keyword is used to access base class members, particularly useful in constructors to call base class constructors or override base methods.
Q.9Easy
Which of the following demonstrates proper encapsulation in C#?
Answer: B
Proper encapsulation uses private fields with public properties, providing controlled access and allowing for future validation logic.
Q.10Easy
What is the output behavior of ToString() method when not overridden in a custom class?
Answer: B
Default ToString() (inherited from object) returns the fully qualified namespace and class name. Custom implementations are recommended for meaningful string representation.
Q.11Easy
In C#, which access modifier allows a member to be accessed only within the same class?
Answer: A
The 'private' access modifier restricts access to members within the same class only. It is the most restrictive access level in C#.
Q.12Easy
What is the primary purpose of using interfaces in C#?
Answer: B
Interfaces define a contract that classes must implement, ensuring consistent method signatures across different implementations. They enable polymorphism and loose coupling.
Q.13Easy
In C# inheritance, which keyword allows accessing members of the parent class?
Answer: B
The 'base' keyword is used to access members (properties, methods) of the parent class from within derived class methods, particularly in constructors and method overriding.
Q.14Easy
Which of the following best describes encapsulation in C#?
Answer: A
Encapsulation is the bundling of data (variables) and methods (functions) into a single unit (class) while hiding the internal details. Access modifiers like private, protected, and public control visibility.
Q.15Easy
What is the purpose of the 'base' keyword in C#?
Answer: B
The 'base' keyword allows a derived class to access members (methods, properties, constructors) of the parent class. It's commonly used to call the parent constructor or override methods while keeping parent functionality.
Q.16Easy
What is the output of this C# code?
public class Employee
{
private string name = "John";
public Employee(string n) { name = n; }
}
Employee emp = new Employee("Alice");
Console.WriteLine(emp.name);
Answer: C
The 'name' field is declared as 'private', so it cannot be accessed outside the class. Attempting to access emp.name from outside the class results in a compilation error.
Q.17Easy
Which of the following best describes the 'this' keyword in C#?
Answer: B
'this' keyword represents the current instance of the class. It is used to access instance members, differentiate between local and instance variables, and pass the current object as a parameter.
Q.18Easy
What is the primary purpose of encapsulation in OOP?
Answer: B
Encapsulation bundles data and methods together, hiding internal implementation and controlling access through public interfaces, improving maintainability and security.
Q.19Easy
Which keyword is used to create a class that cannot be instantiated in C#?
Answer: B
The 'abstract' keyword prevents instantiation of a class and forces derived classes to implement abstract members, establishing a contract for subclasses.
Q.20Easy
In C#, what is the correct syntax to implement multiple interfaces in a class?
Answer: A
C# uses colon (:) to implement interfaces, and multiple interfaces are separated by commas. The 'implements' keyword is used in Java, not C#.