0% found this document useful (0 votes)
40 views13 pages

OOP - Short - Questions Full

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

OOP - Short - Questions Full

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

OOP Short Questions

 What is OOP?
ANS: It is a technique in which we visualize our programming problems in the form of objects and their
interactions as happens in real life.

 What are the various elements of OOP?

1. ANS: Objects, Classes:


2. Encapsulation:
3. Abstraction:
4. Inheritance
5. Polymorphism

 What are the characteristics of Object Oriented programming language?

Oriented Programming (OOP) are:

1. Encapsulation: This characteristic refers to bundling data (attributes) and the methods
(behaviors) that operate on the data into a single unit or object. Encapsulation allows
objects to hide their internal state and only expose necessary functionalities through a
public interface.
2. Abstraction: This is the process of hiding complex implementation details and showing
only the essential features of an object. It helps in managing complexity by reducing the
details that the user needs to focus on.
3. Inheritance: This allows one class (the derived or child class) to inherit properties and
behaviors from another class (the base or parent class). Inheritance promotes code reuse
and establishes a natural hierarchy between classes.
4. Polymorphism: It allows objects of different classes to respond to the same method call
in different ways. Polymorphism enables flexibility and dynamic behavior in a program.

 What are the basic Concepts used in the Object-Oriented Programming


language?

the basic concepts used in Object-Oriented Programming (OOP) are:

1. Objects: Fundamental units that represent real-world entities or concepts. Objects have
attributes (data) and behaviors (methods).
2. Classes: Blueprints or templates for creating objects. A class defines the structure and
behavior that the objects created from it will have.
3. Encapsulation: The bundling of data (attributes) and methods (behaviors) that
manipulate the data within a single unit, i.e., an object. It hides the internal details of an
object and only exposes the necessary parts.
4. Abstraction: The process of simplifying complex systems by focusing on the relevant
features and hiding unnecessary details.
5. Inheritance: A mechanism by which one class (child class) inherits attributes and
methods from another class (parent class). This promotes code reuse and establishes
hierarchical relationships.
6. Polymorphism: The ability of different objects to respond to the same method call in
their own way, providing flexibility and dynamic behavior.

 What Is an Object? (Object-Oriented Technology)


An object is,

1. Something tangible (Ali, School, House, Car).

2. Something conceptual (that can be apprehended intellectually for example time, date and so
on…).

An object has,

1. State (attributes)

2. Well-defined behavior (operations)

 What Is Object Encapsulation (Or Protection)?


Encapsulation means “we have enclosed all the characteristics of an object in the object itself”

 What Is A Class?
In OOP we create a general sketch for each kind of objects and then we create different instances
using this sketch we call this sketch or prototype or map as “class”. All objects of same kind exhibit
identical characteristics (information structure and behavior) however they have data of their
own.

 What Is A Meta-Class?
A Meta-Class in Object-Oriented Programming (OOP) is a class that defines the behavior and
structure of other classes. In simple terms, just like classes define the blueprint for objects, a
meta-class defines how classes behave.

 What Is Inheritance?
Inheritance is a key feature of object-oriented programming that allows a class (called a derived or
child class) to inherit attributes and behaviors from another class (called a base or parent class).
The derived class can have additional attributes and behaviors that are specific to it.

 What Is the Difference Between Object-Based and Object- Oriented?

The difference between object-based and object-oriented programming is:


1. Object-Based Programming:
o It supports the concept of objects and encapsulation but does not support all the
features of object-oriented programming.
o It does not support key concepts like inheritance and polymorphism.
o Example: JavaScript (before ES6) was considered object-based.
2. Object-Oriented Programming (OOP):
o OOP includes all the features of object-based programming and also supports key
features like inheritance, polymorphism, and abstraction.
o OOP languages provide a full framework for modeling real-world entities and
their relationships.

In summary, object-based programming is limited in functionality compared to object-


oriented programming, which provides a more comprehensive approach to creating complex,
reusable, and modular software systems

 What is Abstraction?
Abstraction in Object-Oriented Programming is the process of simplifying complex systems by
focusing only on the essential details relevant to the problem at hand, while ignoring the
irrelevant ones. It helps manage complexity by presenting only necessary information and hiding
the rest.

 What is a Class Diagram?


A class diagram is a visual representation used in object-oriented design to show the structure of a
system. It displays classes, their attributes, methods, and the relationships between them (like
inheritance, associations, and dependencies). It's a key part of UML (Unified Modeling Language)
used to model real-world entities and their interaction

 What is Method Overriding?


Method overriding is a feature in Object-Oriented Programming where a subclass provides its own
implementation of a method that is already defined in its parent class. The overridden method in
the subclass has the same name, return type, and parameters as the one in the parent class,
allowing the subclass to modify or extend the behavior of the method.

 What is Operator Overloading?


Operator overloading in Object-Oriented Programming allows you to define or change the
behavior of operators (like +, -, *, etc.) for user-defined types (classes). This enables operators to
be used in a way that is specific to the objects of the class, making code more intuitive. For
example, you can overload the + operator to add two objects of a class, similar to how it works
with basic data types.

 What is Method Overloading?


Method overloading is a feature in Object-Oriented Programming where multiple methods have
the same name but differ in the number or type of parameters. This allows a class to perform
different tasks based on the arguments passed to the method, enabling more flexibility and
readability in code. The method signature (name and parameter list) distinguishes the overloaded
methods.
 What is Polymorphisms?
Polymorphism in Object-Oriented Programming allows objects of different classes to be treated as
instances of the same class through a common interface. It enables a single function or method to
work in different ways based on the object it is acting upon.

 What is Inheritance?
Inheritance in Object-Oriented Programming is a mechanism where a subclass acquires properties
and behaviors (attributes and methods) from a parent (base) class, promoting code reuse and
hierarchical relationships.

 What is a base class?


A base class, also known as a parent or superclass, is the class from which other classes
(derived or subclasses) inherit properties and behaviors in Object-Oriented Programming

 What is a concrte class?


A concrete class is a class in Object-Oriented Programming that provides implementations for all
its methods and can be instantiated to create objects, unlike abstract classes that cannot be
instantiated.

 What are data members?


Data members are variables defined within a class that hold the attributes or state of an object.
They represent the properties of a class and are accessed through class methods.

 What is a constructor?
A constructor is a special method in a class that is automatically called when an object of the class
is created. It initializes the object's data members and sets the object to a valid state

 What is a destructor?
A destructor is a special method in a class that is automatically called when an object is destroyed.
It is used to release resources, such as memory or file handles, that were allocated to the object
during its lifetime

 What is global variable?


A global variable is a variable that is declared outside of all functions or classes and is accessible
from any part of the program, meaning it can be used by any function or method within the
program's scope

 What is local variable?


A local variable is a variable that is declared within a function or block of code and is only
accessible within that specific function or block, making it unavailable outside its scope

 What is a null pointer?


A null pointer is a pointer that does not point to any valid memory address or object. It is typically
used to indicate that the pointer is not currently referencing anything, and its value is set to
nullptr in modern C++ or NULL in older versions

 What is a pointer?
A pointer is a variable that stores the memory address of another variable. In programming, it is
used to directly access and manipulate the value stored at a specific memory location

 What is meant by protected?


In Object-Oriented Programming, protected is an access specifier that allows a class member
(data or methods) to be accessed within the class itself, by derived (sub)classes, but not from
outside the class or by unrelated classes.

 Explain what type of copy the default assignment operator "=" does when
applied to objects. (shallow copy or deep copy)
The default assignment operator "=" in C++ performs a shallow copy. It copies the values of an
object's data members, but for pointers, it copies only the addresses, not the data they point to.
This can lead to multiple objects sharing the same memory location

 How we can use the concept of overloading to convert a type according to


our own requirements? Give one example.
In C++, we can use the concept of operator overloading to convert one type into another based on
our requirements. This is done by overloading a type conversion operator inside a class.
class Distance {

int meters;

public:

Distance(int m) : meters(m) {}

operator int() {

return meters * 100;

};

int main() {

Distance d(5);

int cm = d;

cout << "Distance in cm: " << cm;

return 0;

 Differentiate between Simple association and Aggregation?

 Simple Association: A basic relationship between two objects where both can exist
independently.
 Aggregation: A special form of association where one object (the whole) contains or is
made up of other objects (the parts), but the parts can still exist independently.
Explanation:

 Simple Association is a weak relationship between objects. For example, a Student and
a Classroom have a simple association, meaning a student can exist without a specific
classroom, and the classroom can exist without a specific student.
 Aggregation is a stronger relationship. It represents a "whole-part" relationship. For
instance, a Library and Books have an aggregation relationship where a library contains
books, but the books can exist outside the library as well. The parts (books) are not
destroyed when the whole (library) is destroyed

 Friend function minimizes <Encapsulation=. Describe in your own


wordings?
A friend function allows external access to a class's private or protected members, which weakens
encapsulation by exposing internal details. It provides flexibility but reduces the strict boundaries
of data hiding.

 Program about constant member function.


#include <iostream>

using namespace std;

class Student {

int rollNo;

public:

Student(int r) : rollNo(r) {}

void showRollNo() const {

cout << "Roll Number: " << rollNo << endl;

};

int main() {

Student s1(101);

s1.showRollNo();

return 0;

OutPut: Roll Number: 101


 Can binary operators overloaded, how can they overload give an example
for any one operator.
Yes, binary operators can be overloaded in C++. They are overloaded by defining a function for the
operator inside a class. Here's an example of overloading the + operator:
class Complex {

public:

int real, imag;

Complex operator + (const Complex& obj) {

Complex result;

result.real = real + obj.real;

result.imag = imag + obj.imag;

return result;

};

 Give one-line definition of "Object Orientation".


Object Orientation is a programming paradigm that organizes software design around objects,
which encapsulate data and behavior.

 What do you mean by the term Abstraction? Why we use it.

The term abstraction in Object-Oriented Programming (OOP) is the concept of simplifying


complex real-world objects by only focusing on the relevant attributes and behaviors required for
solving a specific problem. As mentioned in your handout:

"Abstraction is a way to cope with complexity and it is used to simplify things."

Why do we use abstraction?

 Simplification: It helps to manage complexity by hiding unnecessary details and showing only
the relevant aspects of the object for the task at hand.
 Problem-solving: Abstraction allows us to focus on the features of an object that are directly
related to the problem we are solving. For example, when building a school system, we only care
about a student's academic information (e.g., roll number, grades) and not about unrelated
details like their personal life.
 Modularity: Abstraction helps to create a clear separation of concerns, where different parts of
the system only interact through well-defined interfaces without worrying about how other
parts work internally.

 What is IS-A relationship, show how it is implemented using c++ code (you
do not have to give the detailed code simply show in a single line or two
how it will be implemented).
The IS-A relationship is demonstrated through inheritance in C++. For instance:
class Animal {}; // Base class

class Dog : public Animal {}; // Dog IS-A Animal

 Write the syntax of declaring a pure virtual function in a class?


class ClassName {
public:
virtual ReturnType FunctionName(ParameterList) = 0;
};

 What is meant by direct base class ?


A direct base class is the immediate class from which another class inherits. It provides its
methods and properties directly to the derived class.

 Describe the way to declare a template class as a friend class of any other
class?
The syntax for this;

template <typename T>


class FriendClass;

class MyClass {
friend class FriendClass<int>;
 What is the purpose of template parameter?
A template parameter allows classes or functions to operate with any data type, making the code
flexible and reusable. It enables writing generic code that can be applied to multiple types without
modification.

 Describe in simple words how we can use template specialization to enforce


case sensitive specialization in String class.
Template specialization allows us to define a specific behavior for a particular data type. In a String
class, we can specialize the template for char to enforce case-sensitive comparisons. This ensures
that strings are compared exactly as they are, considering case differences.

 Can we use compiler generated default assignment operator in case our class
is using dynamic memory? Justify your answer.
No, we cannot use the compiler-generated default assignment operator when our class uses
dynamic memory because it performs a shallow copy, leading to issues like double deletion or
memory leaks. A custom assignment operator is needed for deep copying.

 Give the names of three ways to handle errors in a program.

Certainly! Here are three common ways to handle errors in a program:

1. Exception Handling.
2. Error Codes
3. Assertions

 Consider the following code,


class Base{
private:
void base1();
protected:
void base2();
public:
void base3();
};
class Derived: public Base{
private:
void derived1();
protected:
void derived2();
public:
void derived3();
};
int main(){
Derived * derived = new Derived();
return 0;
}
Fill the table below to tell which member functions of Base and Derived
classes we can access using the Derived pointer in the code indicated in
bold.
In the given code, you have a Derived class that inherits from the Base class. The access
specifiers (private, protected, public) control which member functions are accessible from
outside the class.

Here's a table indicating which member functions of the Base and Derived classes are accessible
using a Derived pointer:

Access Specifier Base Member Functions Derived Member Functions


Private Not accessible Not accessible
Protected Accessible (inherited) Accessible (inherited)
Public Accessible (inherited) Accessible (inherited)

 What is the output produced by the following program?


#include
void sample function(double test) throw (int);
int main()
{
try
{
cout =Trying.\n=;
sample_function(98.6);
cout <Trying after call.\n=;
}
catch(int)
{
cout <Catching.\n=;
}
cout <End program.\n=;
return 0;
}
void sample_function(double test) throw (int)
{
cout <Starting sample_function.\n=;
if(test < 100)
throw 42;
}
Trying.
Starting sample_function.
Catching.
End program.
4o mini

 Write a program in C++ which creates three classes named as


 Equation
 Linear
 Quadratic
Where Linear and Quadratic are inherited from Equation Each class has the
method Graph. Graph method should be pure virtual in Equation class. This
method should be overridden in both the inherited classes. It is meant to
display the Graph shape of its respective class. Graph method of Linear will
display the message; Straight line Similarly, the Graph method of Quadratic
will display the message; Parabola In main, call the Graph method of both
the Linear and Quadratic equation polymorphically through the parent class
(Equation).
 What is meant by Generic Programming?
 Describe three properties necessary for a container to implement Generic
Algorithms.
 Deque a Birectional Container?
 What is meant by Generic Programming?
 Sort the following data in the order in which compiler searches a
function? Complete Specialization, Generic Template, Partial
Specialization, Ordinary Function.
 State any conflict that may rise due to multiple inheritance?
 Describe three properties necessary for a container to implement Generic
Algorithms.
 Write three important features of virtual functions.
 There are some errors in the code given below, you have to
 Indicate the line no. with error/s
 Give the reason for error/s
 Correct the error/s.

#include this will be #include


#include
using namespace
std; template
class
MyClass{
public:
MyClass(){
cout"This is class1"endl;
}
};
template
class
MyClass{
public:
MyClass(){
cout"This is class2"endl;
}
};
int main(int argc, char *argv[])
{
MyClass c1;
MyClass c2;
system("PAU
SE"); return 0;
}

 Given are two classes A and B. class B is inherited from class A. Write a
code snippet(for main function) that polymorphically call the method of
class B. Also what changes do you suggest in the given code segment
that are required to call the class B method polymorphically.
class A
{
public:
void method() { cout"A's method \n"; }
};
class B : public A
{
public:
void method() { cout"B's method\n"; }
};

 Write a detailed note on Exceptions in Destructors with the help of a coding


example.
 Give one line definition of "Ternary Association".
 What do you mean by the term Abstraction? Why we use it.
 Derived class can inherit base class features? Explain it with example.

You might also like