What will be the output of the following code?
class A { public virtual void Show() { Console.WriteLine("A"); } }
class B : A { public override void Show() { Console.WriteLine("B"); } }
A obj = new B();
obj.Show();
Answer: B
Due to polymorphism, the overridden method in class B is called, not the virtual method in class A. Output is 'B'.
Q.2Medium
What is the difference between 'new' keyword and 'override' keyword in C#?
Answer: A
'new' keyword hides the base class method (method hiding), while 'override' provides true polymorphic behavior by replacing the method.
Q.3Medium
In C#, which interface is used to implement custom iteration logic?
Answer: A
IEnumerable interface in C# is used to provide iteration capability through the GetEnumerator() method.
Q.4Medium
Which of the following is true about properties in C#?
Answer: C
Properties in C# allow controlled access to fields with validation logic while maintaining clean, intuitive syntax like field access.
Q.5Medium
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.
Advertisement
Q.6Medium
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.7Medium
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.8Medium
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.9Medium
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.
Q.10Medium
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.11Medium
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.16Medium
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.17Medium
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.18Medium
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.19Medium
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.20Medium
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.