Home Subjects C# Programming OOP in C#

C# Programming
OOP in C#

C# and .NET for campus placement

51 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 51
Topics in C# Programming
Q.31 Medium OOP in C#
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();
A Generic Sound
B Bark
C Compilation Error
D No Output
Correct Answer:  B. Bark
EXPLANATION

Polymorphism ensures that the overridden method in Dog class executes. The override keyword allows the derived class to provide its specific implementation.

Test
Q.32 Medium OOP in C#
In C# 9+, which feature allows you to create immutable reference types?
A enum
B record
C struct
D interface
Correct Answer:  B. record
EXPLANATION

Records (introduced in C# 9) are reference types designed for immutability with built-in equality comparison and ToString() methods.

Test
Q.33 Medium OOP in C#
A developer creates a class Library and another class Book. The Library class contains multiple Book objects. What is this relationship called?
A Inheritance
B Composition
C Polymorphism
D Abstraction
Correct Answer:  B. Composition
EXPLANATION

Composition represents a 'has-a' relationship where one class contains objects of another class. Here, Library 'has-a' Book collection.

Test
Q.34 Medium OOP in C#
Consider a BankAccount class with private balance field. How should external classes access this field safely?
A Make balance public
B Use public properties with get/set accessors
C Use friend classes
D Use reflection
Correct Answer:  B. Use public properties with get/set accessors
EXPLANATION

Properties with accessors (get/set) follow encapsulation principles, allowing controlled access while maintaining data validation logic within the property.

Test
Q.35 Medium OOP in C#
What does the 'virtual' keyword enable in C#?
A Creating abstract methods
B Method overriding in derived classes
C Preventing inheritance
D Creating static methods
Correct Answer:  B. Method overriding in derived classes
EXPLANATION

The 'virtual' keyword marks a method in the base class as overridable. Derived classes can use 'override' to provide their own implementation.

Test
Q.36 Medium OOP in C#
In C# 11, which feature enables you to define constraints on generic type parameters at compile-time?
A Generic constraints with 'where' keyword
B Runtime type checking
C Dynamic typing with 'dynamic' keyword
D Reflection-based validation
Correct Answer:  A. Generic constraints with 'where' keyword
EXPLANATION

'where' clause in generic types constrains type parameters (e.g., where T : class, where T : IComparable). C# 11 added 'allows ref struct' constraint.

Test
Q.37 Medium OOP in C#
In C#, what is the relationship between composition and inheritance in OOP design?
A Composition is always better than inheritance
B Inheritance should be preferred when there's an 'is-a' relationship; composition for 'has-a' relationship
C They are identical in functionality
D Composition can only be used with abstract classes
Correct Answer:  B. Inheritance should be preferred when there's an 'is-a' relationship; composition for 'has-a' relationship
EXPLANATION

Composition (HAS-A) and Inheritance (IS-A) serve different purposes. Composition is more flexible and often preferred over inheritance to avoid tight coupling.

Test
Q.38 Medium OOP in C#
In C#, what is the correct way to implement explicit interface implementation?
A public void InterfaceName.MethodName() { }
B private void InterfaceName.MethodName() { }
C protected void InterfaceName.MethodName() { }
D internal void InterfaceName.MethodName() { }
Correct Answer:  B. private void InterfaceName.MethodName() { }
EXPLANATION

Explicit interface implementation is always private by convention and accessed through interface reference only, not through class instance directly.

Test
Q.39 Medium OOP in C#
Which statement about record types in C# 9+ is correct?
A Records are mutable reference types by default
B Records provide built-in value-based equality and ToString() implementation
C Records cannot have methods or computed properties
D Records require explicit constructor declaration
Correct Answer:  B. Records provide built-in value-based equality and ToString() implementation
EXPLANATION

Records (C# 9+) are designed for immutable data with positional parameters, automatic value-based equality, and ToString() implementation.

Test
Q.40 Medium OOP in C#
What will be the behavior if a base class method is not marked as 'virtual' and a derived class attempts to override it?
A The derived class method hides the base class method with a warning
B Compile-time error occurs
C The override is ignored at runtime
D The method is automatically converted to virtual
Correct Answer:  A. The derived class method hides the base class method with a warning
EXPLANATION

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.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips