Home Subjects C# Programming OOP in C#

C# Programming
OOP in C#

C# and .NET for campus placement

25 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 25
Topics in C# Programming
Q.1 Hard OOP in C#
In a healthcare system, Doctor is a derived class from Employee. If you want to ensure that Doctor's constructor calls Employee's constructor before executing, which approach is correct?
A Use the 'base' keyword in Doctor's constructor initialization list
B Manually call Employee's constructor inside Doctor's constructor body
C Use the 'this' keyword to call parent constructor
D This is automatic and requires no explicit code
Correct Answer:  A. Use the 'base' keyword in Doctor's constructor initialization list
EXPLANATION

The 'base' keyword in the constructor initialization list ensures the parent class constructor is called before the derived class constructor body executes, maintaining proper initialization order.

Test
Q.2 Hard OOP in C#
Consider a scenario where class X implements interfaces IA and IB, and both interfaces have a method named Process(). How should X implement this?
A X must implement Process() twice with different names
B X can implement Process() once, satisfying both interfaces using explicit interface implementation if needed
C This will cause a compilation error and is not allowed
D X must choose which interface's Process() to implement
Correct Answer:  B. X can implement Process() once, satisfying both interfaces using explicit interface implementation if needed
EXPLANATION

C# allows one implementation to satisfy multiple interfaces when they have the same method signature. Explicit interface implementation can be used if methods need different behaviors.

Test
Q.3 Hard OOP in C#
In a real estate system, consider Property (base) with House and Apartment (derived). If Property has a sealed method CalculateTax(), what happens in House class?
A House can override CalculateTax()
B House cannot override CalculateTax() and must inherit it as-is
C House must reimplement CalculateTax() in a new virtual method
D Compilation error occurs
Correct Answer:  B. House cannot override CalculateTax() and must inherit it as-is
EXPLANATION

The sealed keyword on a virtual method prevents overriding in derived classes, ensuring that implementation is locked in the current class.

Test
Q.4 Hard OOP in C#
A financial system implements IEquatable for comparing accounts. What is the primary benefit of implementing this interface?
A It allows sorting accounts
B It provides type-safe equality comparison and optimizes collections
C It makes accounts serializable
D It allows accounts to be used as dictionary keys
Correct Answer:  B. It provides type-safe equality comparison and optimizes collections
EXPLANATION

IEquatable<T> provides type-safe equality comparison, improves performance by avoiding boxing, and is essential for proper behavior in collections like Dictionary.

Test
Q.5 Hard OOP in C#
In a complex system where a class needs to inherit from multiple sources of functionality while maintaining a single base type, which C# feature is most appropriate?
A Multiple inheritance
B Interface implementation combined with single inheritance
C Only interfaces without classes
D Static classes
Correct Answer:  B. Interface implementation combined with single inheritance
EXPLANATION

C# doesn't support multiple inheritance of classes, but allows one base class and multiple interfaces, providing flexibility while maintaining hierarchy.

Test
Q.6 Hard OOP in C#
A gaming application needs to implement different weapons (Sword, Gun, Bow) with different attack methods. Which approach is most maintainable?
A Create separate classes for each weapon inheriting from base Weapon class
B Use a single class with if-else statements for each weapon type
C Use global functions for each weapon
D Use only static methods
Correct Answer:  A. Create separate classes for each weapon inheriting from base Weapon class
EXPLANATION

Inheritance and polymorphism provide better maintainability, scalability, and follow SOLID principles compared to conditional logic.

Test
Q.7 Hard OOP in C#
In a hospital management system, Doctor and Nurse classes need to perform a common Logout() operation but have different Login() procedures. What is the best OOP approach?
A Create a base class with virtual Logout() and abstract Login()
B Create separate classes with no inheritance
C Create an interface with both methods
D Use static methods for common operations
Correct Answer:  A. Create a base class with virtual Logout() and abstract Login()
EXPLANATION

A base class with virtual Logout() allows common implementation, while abstract Login() forces derived classes to provide their own implementation. This combines code reuse with flexibility.

Test
Q.8 Hard OOP in C#
A banking application uses inheritance where Account is the parent class and SavingsAccount, CurrentAccount are derived classes. Each has different withdrawal rules. Which design principle is being followed?
A Single Responsibility Principle (SRP)
B Open/Closed Principle (OCP)
C Liskov Substitution Principle (LSP)
D Dependency Inversion Principle (DIP)
Correct Answer:  B. Open/Closed Principle (OCP)
EXPLANATION

Open/Closed Principle states that classes should be open for extension (SavingsAccount, CurrentAccount extend Account) but closed for modification (Account class rules remain unchanged). This is a classic example of OCP.

Test
Q.9 Hard OOP in C#
What will happen if you try to override a non-virtual method in a derived class in C#?
A Compilation error - cannot override non-virtual methods
B The method will be hidden using the 'new' keyword implicitly
C Runtime error will occur
D Compilation warning, but it will compile and run
Correct Answer:  B. The method will be hidden using the 'new' keyword implicitly
EXPLANATION

In C#, if you attempt to override a non-virtual method without using 'new' keyword, the compiler issues a warning but the method is hidden (not overridden). Using 'new' keyword explicitly hides the base method.

Test
Q.10 Hard OOP in C#
A logging system needs to handle multiple logger types (File, Database, Console) interchangeably. Which design pattern combined with polymorphism best suits this?
A Singleton with inheritance
B Strategy pattern with interface implementation
C Factory pattern only
D Observer pattern exclusively
Correct Answer:  B. Strategy pattern with interface implementation
EXPLANATION

Strategy pattern defines a family of algorithms (logger types) and makes them interchangeable through interface polymorphism, allowing runtime selection.

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