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 1–10 of 51
Topics in C# Programming
Q.1 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.2 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.3 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.4 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.5 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.6 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
Q.7 Medium OOP in C#
What is the output of the following C# code?

class Base { public virtual void Display() { Console.WriteLine("Base"); } }
class Derived : Base { public override void Display() { Console.WriteLine("Derived"); } }
Base obj = new Derived();
obj.Display();
A Base
B Derived
C Compilation error
D Runtime error
Correct Answer:  B. Derived
EXPLANATION

Due to polymorphism and method overriding, the Derived class's Display() method is called even though the reference type is Base. This demonstrates runtime polymorphism.

Test
Q.8 Medium OOP in C#
Which statement about abstract classes in C# is INCORRECT?
A Abstract classes cannot be instantiated
B Abstract classes can contain abstract methods
C Abstract classes can have concrete methods with implementation
D Abstract classes cannot have any properties
Correct Answer:  D. Abstract classes cannot have any properties
EXPLANATION

Abstract classes can have properties, fields, concrete methods, and abstract members. The statement that they cannot have properties is incorrect.

Test
Q.9 Medium OOP in C#
In a company payroll system, Employee is the base class with CalculateSalary() as virtual method. Manager and Intern classes override it differently. Why is this design preferred?
A It increases code size
B It provides flexibility to implement different salary calculations while maintaining a common interface
C It makes the code harder to understand
D It prevents inheritance
Correct Answer:  B. It provides flexibility to implement different salary calculations while maintaining a common interface
EXPLANATION

This design enables polymorphism, allowing different employee types to calculate salaries differently while using the same interface, improving maintainability.

Test
Q.10 Medium OOP in C#
What happens when a derived class constructor does not explicitly call the base class constructor in C#?
A Compilation error occurs
B Parameterless base constructor is called automatically
C Runtime error occurs
D The program crashes
Correct Answer:  B. Parameterless base constructor is called automatically
EXPLANATION

C# automatically calls the parameterless constructor of the base class if no explicit base() call is made, ensuring base class initialization.

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