[go: up one dir, main page]

0% found this document useful (0 votes)
12 views7 pages

OOP Test C#

The document provides an overview of object-oriented programming (OOP) concepts, including definitions of classes, objects, access modifiers, inheritance, encapsulation, polymorphism, and abstract classes in C#. It explains the importance of encapsulation for security, the differences between abstract classes and interfaces, and includes examples of method overloading and class structures. Additionally, it outlines scenarios for using access modifiers to protect sensitive data and ensure safe method access.

Uploaded by

0762anushashaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

OOP Test C#

The document provides an overview of object-oriented programming (OOP) concepts, including definitions of classes, objects, access modifiers, inheritance, encapsulation, polymorphism, and abstract classes in C#. It explains the importance of encapsulation for security, the differences between abstract classes and interfaces, and includes examples of method overloading and class structures. Additionally, it outlines scenarios for using access modifiers to protect sensitive data and ensure safe method access.

Uploaded by

0762anushashaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Section A

Q .What is object-oriented programming?

Ans.Object oriented programming or (OOP) is a programming structure , based on objects and


the data and method that contains.There are 4 main pillars in object oriented programming : 1.
Encapsulation
2. Inheritance
3. Polymorphism
4. Abstract

Q. Define a class and an object in 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. Public : It is accessible for anyone from anywhere.


Private : It is accessible only within a class or limited space.
Protected : It is accessible within the class and other classes where shared.

Q. Explain the term “inheritance” with an example.

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() { } }

Q. What is the role of a constructor in a class?

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);
}

Q. Explain the concept of “encapsulation” and how it improves security in 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.

Q. What is polymorphism? Explain both compile-time and run-time polymorphism in


C#.

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.

In this case, the interface says:

“Any vehicle must have Start() and Stop() methods.”

You might also like