OOP Test C#
OOP Test C#
Ans. A class is the main element for creating objects ,defining properties and methods,
An object is a sub element in a class,it is used to store data and recall it later.
Q. What is the difference between public, private, and protected access modifiers in
C#?
Ans. Inheritance allows a class to inherit properties from other classes.there are 4 types of
inheritance:
1. Single inheritance
2. Multilevel inheritance
3. Hierarchy inheritance
4. Multiple inheritance
Example:
class Animal { public void Eat() { } }
class Dog : Animal { public void Bark() { } }
Ans: A constructor is a special method in a class that is automatically called when an object of
that class is created.
Section B
Q. What is method overloading? Give a simple code example in C#.
Ans.Method overloading means you can create many methods with the same name in a class,
but each one must have different types or numbers of parameters.
Example:
class marks{
public int Add()
public int Add(int a, int b);
public double Add(int a, int b,int c);
}
Ans. Encapsulation means hiding the details of how an object works and only showing what is
necessary. It keeps the data safe inside the class and only allows other parts of the program to
access or change it through special methods. This helps protect important information from
being changed in the wrong way.
Ans. Polymorphism means "many forms." In C#, it allows the same method name to do different
things depending on the situation. It helps make code easier to use and understand. ●
Compile-time Polymorphism (also called Method Overloading) : when multiple methods have
the same name but different types or numbers of parameters. The method is chosen at compile
time.
● Run-time Polymorphism (also called Method Overriding): when a child class changes the
behavior of a method from the parent class using override. The method that runs is
decided at runtime.
Q. Describe the purpose of abstract classes and how they differ from interfaces.
Ans. An abstract class is like an incomplete plan. You can’t use it directly to make an object. It
gives some ready-made parts (like methods and variables), but other parts must be finished by
the classes that use it.
● Abstract class: Like a half-built car — you give the engine and frame, and someone else
adds the seats and wheels.
● Interface: Like saying, "All vehicles must have a Start() and Stop() method" — but
not giving any part of the vehicle.
Q. Write a C# class named Person with properties Name and Age. Include a
constructor and a method Introduce() that prints a simple introduction.
Section C
11. Scenario:
● private: Use this for account number and balance to protect sensitive data. No one
outside the class should change these directly.
● public: Use this for methods like Deposit() and Withdraw() to allow safe access
and changes to the balance.
12. Scenario:
13. Scenario:
14. Scenario:
An interface is like a set of rules. Any class that implements the interface must write the code for
the methods defined in it.