What is the purpose of the 'sealed' keyword in C#?
Answer: A
The 'sealed' keyword prevents a class from being inherited or a method from being overridden in derived classes.
Q.62Medium
In C#, what is the difference between a class and a struct?
Answer: A
Classes are reference types stored on heap, structs are value types stored on stack. This affects memory management and behavior.
Q.63Medium
Which of the following represents proper use of constructor chaining in C#?
Answer: B
Constructor chaining uses 'this()' to call another constructor in the same class. Option B shows proper syntax for calling a parameterless constructor.
Q.64Medium
In C# 2024-25, which feature allows defining read-only properties with automatic backing fields?
Answer: A
C# auto-properties with 'init' accessor allow creating read-only properties that can only be set during initialization, improving encapsulation.
Q.65Medium
In C# 11, which feature allows you to define required members in a class?
Answer: A
C# 11 introduced the 'required' keyword that enforces callers to initialize specific members during object instantiation.
Advertisement
Q.66Medium
What is the output when you access a property marked with 'init' accessor after object initialization in C#?
Answer: B
The 'init' accessor (introduced in C# 9) allows setting the property only during initialization. Any modification attempt post-initialization causes a compile-time error.
Q.67Medium
What is the primary difference between method overloading and method overriding in C#?
Answer: A
Overloading creates multiple methods with the same name but different parameters. Overriding replaces a base class method in a derived class.
Q.68Medium
What will be the behavior if a base class method is not marked as 'virtual' and a derived class attempts to override it?
Answer: A
If you override a non-virtual method, C# will hide it and show a compiler warning. Use 'new' keyword to suppress the warning and explicitly hide the base method.
Q.69Medium
Which statement about record types in C# 9+ is correct?
Answer: B
Records (C# 9+) are designed for immutable data with positional parameters, automatic value-based equality, and ToString() implementation.
Q.70Medium
In C#, what is the correct way to implement explicit interface implementation?
Answer: B
Explicit interface implementation is always private by convention and accessed through interface reference only, not through class instance directly.
Q.71Medium
In C#, what is the relationship between composition and inheritance in OOP design?
Answer: B
Composition (HAS-A) and Inheritance (IS-A) serve different purposes. Composition is more flexible and often preferred over inheritance to avoid tight coupling.
Q.72Medium
In C# 11, which feature enables you to define constraints on generic type parameters at compile-time?
Answer: A
'where' clause in generic types constrains type parameters (e.g., where T : class, where T : IComparable). C# 11 added 'allows ref struct' constraint.
Q.73Medium
What does the 'virtual' keyword enable in C#?
Answer: B
The 'virtual' keyword marks a method in the base class as overridable. Derived classes can use 'override' to provide their own implementation.
Q.74Medium
Consider a BankAccount class with private balance field. How should external classes access this field safely?
Answer: B
Properties with accessors (get/set) follow encapsulation principles, allowing controlled access while maintaining data validation logic within the property.
Q.75Medium
A developer creates a class Library and another class Book. The Library class contains multiple Book objects. What is this relationship called?
Answer: B
Composition represents a 'has-a' relationship where one class contains objects of another class. Here, Library 'has-a' Book collection.
Q.76Medium
In C# 9+, which feature allows you to create immutable reference types?
Answer: B
Records (introduced in C# 9) are reference types designed for immutability with built-in equality comparison and ToString() methods.
Q.77Medium
What is the output of the following code?
class Animal { public virtual void Sound() { Console.WriteLine("Generic Sound"); } }
class Dog : Animal { public override void Sound() { Console.WriteLine("Bark"); } }
Dog dog = new Dog();
dog.Sound();
Answer: B
Polymorphism ensures that the overridden method in Dog class executes. The override keyword allows the derived class to provide its specific implementation.
Q.78Medium
An abstract class Shape has an abstract method CalculateArea(). Which statement is true?
Answer: B
Abstract classes cannot be instantiated. Derived classes must provide concrete implementations of all abstract methods, enforcing a contract.
Q.79Medium
In C#, what is the difference between 'is' and 'as' operators?
Answer: A
The 'is' operator returns a boolean indicating type compatibility. The 'as' operator performs type conversion and returns null if the conversion fails (no exception).
Q.80Medium
In C# 13+ (projected features), nullable reference types help prevent which type of error?
Answer: B
Nullable reference types (enabled from C# 8) provide compile-time checks for null references, helping prevent NullReferenceException at runtime.