Home Subjects C# Programming OOP in C#

C# Programming
OOP in C#

C# and .NET for campus placement

100 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 100
Topics in C# Programming
Q.1 Easy OOP in C#
Consider a payment processing system where PaymentProcessor is an abstract class with an abstract method ProcessPayment(). CreditCardProcessor and UPIProcessor are derived classes. What will happen if you try to instantiate PaymentProcessor directly in C#?
A It will compile successfully and create an instance with default implementations
B Compiler error - cannot instantiate an abstract class
C Runtime error - abstract class cannot be instantiated at runtime
D It will compile but throw an error only if ProcessPayment() is called
Correct Answer:  B. Compiler error - cannot instantiate an abstract class
EXPLANATION

Abstract classes in C# cannot be instantiated directly. The compiler enforces this rule at compile-time. Abstract classes serve as blueprints and must be inherited by concrete classes that provide implementations for all abstract members. This ensures proper design and forces derived classes to implement required functionality.

Test
Q.2 Medium OOP in C#
In a real-time inventory management system, a Product class needs to track stock levels that should not be modified directly from outside the class. Which OOP principle should be applied here, and how?
A Encapsulation - using private fields with public properties to control access
B Inheritance - creating a base Product class with derived classes for each product type
C Polymorphism - overriding methods in derived classes to change behavior
D Abstraction - using abstract classes to hide implementation details
Correct Answer:  A. Encapsulation - using private fields with public properties to control access
EXPLANATION

Encapsulation is the principle of bundling data and methods together while hiding internal details. Using private fields with public properties (getters/setters) allows controlled access to the stock level, preventing unauthorized direct modifications. This is the standard practice in inventory systems.

Test
Q.3 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.4 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.5 Easy OOP in C#
Which statement is TRUE about abstract methods in C#?
A Abstract methods must be implemented in the base class
B Abstract methods cannot have any body and must be implemented in derived classes
C Abstract methods are only for static classes
D Abstract methods can be private
Correct Answer:  B. Abstract methods cannot have any body and must be implemented in derived classes
EXPLANATION

Abstract methods have no implementation in the abstract class and force derived classes to provide their own implementation. They cannot be private as they must be overridable.

Test
Q.6 Medium OOP in C#
In a logistics system, both Truck and Bicycle need to calculate delivery cost. Instead of duplicating code, you decide to use an interface. What advantage does this provide?
A It enforces implementation of delivery cost calculation in both classes
B It automatically calculates delivery cost
C It restricts inheritance to one parent class only
D It provides default implementation for all methods
Correct Answer:  A. It enforces implementation of delivery cost calculation in both classes
EXPLANATION

Interfaces establish a contract that forces implementing classes to provide their own logic for calculating delivery costs, promoting code consistency without duplicating implementation.

Test
Q.7 Medium OOP in C#
In a banking application, you need to create a base class that cannot be instantiated but defines common properties for SavingsAccount and CheckingAccount. Which should you use?
A sealed class
B interface
C abstract class
D static class
Correct Answer:  C. abstract class
EXPLANATION

An abstract class is ideal when you want to provide common functionality and properties to derived classes while preventing direct instantiation. It can have both abstract and concrete members.

Test
Q.8 Medium OOP in C#
Which of the following correctly demonstrates composition in C#?
A class Car : Vehicle { }
B class Car { private Engine engine; }
C class Car : Vehicle, IVehicle { }
D abstract class Car { }
Correct Answer:  B. class Car { private Engine engine; }
EXPLANATION

Composition is represented by having an object of one class as a member variable of another class, allowing code reuse through object containment rather than inheritance.

Test
Q.9 Medium OOP in C#
What will be the result of the following C# code?

class Animal { }
class Dog : Animal { }
Dog dog = new Animal(); // Line 1
A Code compiles and runs successfully
B Compilation error: Cannot convert type 'Animal' to 'Dog'
C Runtime error
D Code runs but throws InvalidCastException
Correct Answer:  B. Compilation error: Cannot convert type 'Animal' to 'Dog'
EXPLANATION

You cannot assign a base class instance to a derived class reference without explicit casting. This violates type safety rules in C#.

Test
Q.10 Medium OOP in C#
Which keyword in C# is used to prevent a class from being inherited?
A abstract
B virtual
C sealed
D static
Correct Answer:  C. sealed
EXPLANATION

The 'sealed' keyword prevents a class from being used as a base class for inheritance, ensuring that derived classes cannot override the sealed class.

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