PT 1 QBank OOP Ans

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Question and Answer Bank for OOP PT1

Chapter1

1)Define class. Give syntax.(2 m)


Ans
Class: Class is a user defined data type that combines data and functions together. It is a
collection of objects of similar type.
Syntax:
Class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};

2)Define manipulators. Give two examples. (2 m)


Ans
In C++, manipulators are special functions or objects used to modify the format or behavior of
input and output operations. They are typically used with streams (cin, cout, etc.) to control how
data is formatted and displayed.
Key Characteristics of Manipulators:
1. Stream Formatting: Manipulators can change the way data is formatted when it is output to a
stream.
2. Syntax: They are often used in conjunction with the insertion (<<) and extraction (>>) operators.
3. Standard Library: Many common manipulators are defined in the C++ Standard Library and are
included in the <iomanip> header file.
1. setw (Set Width)
The setw manipulator sets the width of the next input/output field. It is often used to format output
into columns.
Eg.setw(10) sets the width of the field to 10 characters.

2. setprecision
The setprecision manipulator sets the number of decimal places to display for floating-point
numbers.
Eg.setprecision(2) sets the number of decimal places to 2.

3)Describe the concept of memory management in C++.(2 m)


Ans
Memory management is a process of managing computer memory, assigning the memory space to
the programs to improve the overall system performance.

C++ a defines unary operators such as new and delete to perform the tasks, i.e., allocating and
freeing the memory.
•New operator
A new operator is used to create the object while a delete operator is used to delete the object.
When the object is created by using the new operator, then the object will exist until we explicitly
use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is
not related to the block structure of the program
pointer_variable = new data-type
The above syntax is used to create the object using the new operator.

1
In the above syntax, 'pointer_variable' is the name of the pointer variable, 'new' is the operator, and
'data-type' defines the type of the data.
Example 1:
int *p;
p = new int;
In the above example, 'p' is a pointer of type int.

• Delete operator
When memory is no longer required, then it needs to be deallocated so that the memory can be
used for another purpose. This can be achieved by using the delete operator, as shown below:
delete pointer_variable;
In the above statement, 'delete' is the operator used to delete the existing object, and
'pointer_variable' is the name of the pointer variable.
In the previous case, we have created pointer 'p' by using the new operator, and can be deleted by
using the following statements:
delete p;

4)State any 4 applications of OOP. (2 m)


Ans
Following are the application of OOP:
• Most popular application of OOP is in the area of user interface
design such as windows. Hundreds of windowing systems have
been developed using OOP techniques.
• C++ is suitable for any programming task including development
of editors, compilers
• Developing databases
• Developing communication systems and complex real-time
applications.
• Simulation and modeling.

5)Explain use of scope resolution operator. (2 m)


6)Write a program to demonstrate the use of scope resolution operator. (4 m)
Ans
➢ Scope of a variable extends from the point of declaration to the end of the block.
➢ A variable declared inside a block is 'local' variable and a variable declared outside block is called
as global variable.
➢ When we want to use a global variable but also has a local variable with same name.
➢ To access the global version of the variable, C++ provides scope resolution operator.
Example:-
#include <iostream>
using namespace std;
char a = 'm';
static int b = 50;
int main() {
char a = 's';
cout<< "The value static variable b is : "<< ::b;
cout<< "\nThe value of local variable a is : " << a;
cout<< "\nThe value of global variable a is : " << ::a;
return 0;
}

2
7)Define class and object. (2 m)
Ans
Class:Class is a user defined data type that combines data and functions together. It is a
collection of objects of similar type.
Object: Object is a basic run time entity that represents a person, place or any item that the program
has to handle.

8)Differentiate between procedural and object oriented programming. (4 m)


Ans

9)Write a program to implement the concept of classes and objects. (4 m)


Ans
#include<iostream>
using namespace std;
class student
{
int rollno;
char name[20];
float marks;
public:
void accept();
void display();
};
void student::accept()
{
cout<<"\nEnter data of student:";
cout<<"\nRoll number:";
cin>>rollno;
cout<<"\nName:";

3
cin>>name;
cout<<"\nMarks:";
cin>>marks;
}
void student::display()
{
cout<<"\nStudents data is:";
cout<<"\nRoll number:"<<rollno;
cout<<"\nName:"<<name;
cout<<"\nMarks:"<<marks;
}
int main()
{
student S;
S.accept();
S.display();
}

10)Describe any 2 features of OOP in detail. (4 m)


Ans
Features of Object Oriented Programming are:
➢ Follows bottom-up approach in program design.
➢ Emphasis is on data rather than procedure.
➢ Programs are divided into what are known as objects.
➢ Data structures are designed such that they characterize the objects
➢ Functions that operate on the data of an object are tied together in the data structures.
➢ Data is hidden and cannot be accessed by external functions.
➢ Objects may communicate with each other through functions.
➢ New data and functions can be easily added whenever necessary

11)Describe following terms: Inheritance, data abstraction, data encapsulation, dynamic


binding. (4 m)
Ans
1. Inheritance
Inheritance is a feature in C++ that allows one class (the derived or child class) to inherit attributes
and methods from another class (the base or parent class). It promotes code reusability and
establishes a natural hierarchy between classes.

2. Data Abstraction
Data abstraction refers to the concept of hiding the implementation details of a class and exposing
only the necessary interfaces to the user. In C++, this is often achieved using abstract classes or
interfaces.

3. Data Encapsulation
Data encapsulation is the concept of wrapping data (variables) and methods (functions) that operate
on the data into a single unit or class. It also involves restricting access to some of the object's
components, which is often achieved using access specifiers like private, protected, and public.

4. Dynamic Binding (Polymorphism)


Dynamic binding refers to the process where the function call is resolved at runtime instead of
compile-time. This is primarily used in polymorphism, where a base class pointer or reference can
point to objects of derived classes, and the correct function is called based on the actual object type.

4
12)Write a program to declare a class ‘student’ having data members as ‘stud_name’ and
‘roll_no’. Accept and display this data for 5 students. (4 m)
Ans
#include <iostream>
#include <string>
using namespace std;

class Student {
private:
string stud_name;
int roll_no;

public:
void setData(string name, int roll) {
stud_name = name;
roll_no = roll;
}

void display() {
cout << "Name: " << stud_name << ", Roll No: " << roll_no << endl;
}
};

int main() {
Student students[5];
string name;
int roll_no;

// Accepting data for 5 students


for (int i = 0; i < 5; i++) {
cout << "Enter name for student " << i + 1 << ": ";
getline(cin, name);
cout << "Enter roll number for student " << i + 1 << ": ";
cin >> roll_no;
cin.ignore(); // to ignore the newline character left in the input buffer
students[i].setData(name, roll_no);
}

// Displaying the data for all students


cout << "\nStudent Details:" << endl;
for (int i = 0; i < 5; i++) {
students[i].display();
}

return 0;
}

5
13)Explain the structure of C++ program. (4 m)
Ans
General C++ program has following structure.

Description:-
1. Include header files
In this section a programmer include all header files which are require to execute given program. The
most important file is iostream.h header file. This file defines most of the C++statements like cout
and cin. Without this file one cannot load C++ program.
2. Class Declaration
In this section a programmer declares all classes which are necessary for given program. The
programmer uses general syntax of creating class.
3. Member Functions Definition
This section allows programmer to design member functions of a class. The programmer can have
inside declaration of a function or outside declaration of a function.
4. Main Function Program
In this section programmer creates objects and calls various functions writer within various class.

14)Write a C++ program to declare a class book with members as book_id and name. Accept
and display data of five books. (4 m)
Ans
#include <iostream>
#include <string>
using namespace std;

class Book {
private:
int book_id;
string name;

public:
void setData(int id, string bookName) {
book_id = id;
name = bookName;
}

void display() const {


cout << "Book ID: " << book_id << ", Name: " << name << endl;
}
};

int main() {
Book books[5]; // Array of 5 Book objects
int id;
string bookName;

// Accepting data for 5 books


for (int i = 0; i < 5; i++) {
cout << "Enter book ID for book " << i + 1 << ": ";

6
cin >> id;
cin.ignore(); // To ignore the newline character left in the input buffer
cout << "Enter name for book " << i + 1 << ": ";
getline(cin, bookName);

books[i].setData(id, bookName);
}

// Displaying the data for all 5 books


cout << "\nBook Details:" << endl;
for (int i = 0; i < 5; i++) {
books[i].display();
}

return 0;
}

Chapter2

15)Define destructor. (2 m)
Ans
In C++, a destructor is a special member function of a class that is automatically invoked when an
object of the class is destroyed. The destructor is used to perform cleanup tasks, such as releasing
memory, closing files, or other resources that the object may have acquired during its lifetime.

Key Points About Destructors:


1)Name: The destructor has the same name as the class, but it is preceded by a tilde (~). For
example, if the class name is MyClass, the destructor will be ~MyClass().

2)No Parameters and No Return Type: Destructors do not take any parameters, and they do not
return any value.

3)Automatic Invocation: The destructor is called automatically when an object goes out of scope or
is explicitly deleted using the delete keyword.

4)One Per Class: A class can have only one destructor, and it cannot be overloaded.

16)Give syntax for static variables. (2 m)


Ans
When a variable is declared as static within a class, it is shared among all instances of the class.
Only one copy of the static variable exists, regardless of how many objects of the class are created.

Syntax
static DataType variableName = initialValue;
Example
static int count;

17)Define constructor. List types of constructor. (2 m)


Ans
A constructor is a special member function that is executed automatically whenever the object of a
class is created.It initializes the object. Following are the types of constructors:

7
1. Default constructor
2. Parameterized constructor
3. Copy constructor
4. Constructor with default argument

18)State the characteristics of static member function. (2 m)


Ans
➢ A static function can have access to only other static members declared in the class.
➢ A static member function can be called using the class name instead of objects as follows:
class_name::function_name;

19)Explain the concept of array of objects. Give syntax and example. (4 m)


Ans
An array of objects in C++ is a collection of instances of a class, stored sequentially in memory,
similar to how a typical array holds primitive data types like int, float, etc. This concept allows you
to manage multiple objects of a class efficiently using array indexing, making it easier to
manipulate and access these objects.
Key Points:
1. Array Declaration: Just like arrays of primitive types, an array of objects is declared by specifying
the class name followed by the array name and the number of elements.
2. Object Initialization: Objects in the array can be initialized using constructors (default or
parameterized).
3. Accessing Objects: Objects in the array are accessed using the array index, and you can call
member functions or access member variables of each object using the dot (.) operator.

#include <iostream>
#include <string>
using namespace std;

class Book {
private:
int book_id;
string name;

public:
// Constructor to initialize the book details
Book() : book_id(0), name("Unknown") {} // Default constructor

void setData(int id, string bookName) {


book_id = id;
name = bookName;
}

void display() const {


cout << "Book ID: " << book_id << ", Name: " << name << endl;
}
};

int main() {
// Declaring an array of 3 Book objects
Book books[3];

// Setting data for each book using the setData method

8
books[0].setData(101, "C++ Programming");
books[1].setData(102, "Data Structures");
books[2].setData(103, "Algorithms");

// Displaying the data for all books


cout << "Book Details:" << endl;
for (int i = 0; i < 3; i++) {
books[i].display();
}

return 0;
}

20)Write any four characteristics of constructors. (4 m)


Ans
Constructors in C++ have several key characteristics that distinguish them from other member
functions. Here are four important characteristics:
1. Same Name as the Class
Description: A constructor has the same name as the class in which it is declared. This is how the
compiler identifies it as a constructor.

2. No Return Type
Description: Constructors do not have a return type, not even void. This is because constructors
are automatically invoked when an object of the class is created, and they are not meant to return
any value.

3. Automatic Invocation
Description: Constructors are called automatically when an object of the class is created. The
constructor is invoked as soon as an object is instantiated, without needing to explicitly call it.

4. Can Be Overloaded
Description: Constructors can be overloaded, meaning a class can have multiple constructors with
different sets of parameters. This allows objects to be initialized in different ways depending on the
constructor used.

21)Write a program to demonstrate the use of parametrized constructor. (4 m)


Ans
#include <iostream>
using namespace std;

class Rectangle {
private:
int length;
int width;

public:
// Parameterized constructor
Rectangle(int l, int w) {
length = l;
width = w;
}

// Function to calculate and return area


int area() {

9
return length * width;
}

// Function to display dimensions


void display() {
cout << "Length: " << length << ", Width: " << width << endl;
}
};

int main() {
// Creating an object of Rectangle using the parameterized constructor
Rectangle rect1(10, 5);

// Display the dimensions


rect1.display();

// Calculate and display the area


cout << "Area of the rectangle: " << rect1.area() << endl;

return 0;
}

22)Write a C++ program to find greatest number among two numbers from two different
classes using friend function. (4 m)
Ans
#include <iostream>
using namespace std;

class ClassB; // Forward declaration of ClassB

class ClassA {
private:
int numA;

public:
// Constructor to initialize numA
ClassA(int a) : numA(a) {}

// Friend function declaration


friend int findGreatest(ClassA a, ClassB b);
};

class ClassB {
private:
int numB;

public:
// Constructor to initialize numB
ClassB(int b) : numB(b) {}

// Friend function declaration


friend int findGreatest(ClassA a, ClassB b);
};

10
// Friend function definition
int findGreatest(ClassA a, ClassB b) {
if (a.numA > b.numB)
return a.numA;
else
return b.numB;
}

int main() {
// Create objects of ClassA and ClassB
ClassA objA(15); // Object of ClassA with numA = 15
ClassB objB(20); // Object of ClassB with numB = 20

// Find and display the greatest number


cout << "The greatest number is: " << findGreatest(objA, objB) << endl;

return 0;
}

23)Differentiate between constructor and destructor. (any 4 points) (4 m)


Ans

11
Chapter 3

24)Describe visibility modes used in inheritance. (any two) (2 m)


Ans
public
Members declared as public are accessible from anywhere in the program, both inside and outside
the class.

protected
Members declared as protected are accessible within the class itself and by derived classes
(subclasses) but not from outside the class.

private
Members declared as private are accessible only within the class itself and not from outside the
class or by derived classes.

25)Define inheritance. Explain different types of inheritance. (4 m)


Ans
The mechanism of deriving new class from an old/existing class is called inheritance.
OR
Inheritance is the process by which objects of one class acquired the properties of objects of
another classes.
Syntax:
class derived-class-name: visibility-mode base-class-name
{
------//
-----// members of derived class
-----//
};
Types of inheritance:
1) Single inheritance: In single inheritance, a derived class is derived from only one
base class.
Diagram:

12
2) Multiple inheritance: In multiple inheritance, derived class is derived from
more than one base classes.
Diagram:

3) Hierarchical inheritance: In hierarchical inheritance, more than one derived


classes are derived from single class.
Diagram:

4) Multilevel inheritance: In multilevel inheritance, a derived class is derived


from a derived class (intermediate base class) which in turn derived from a single
base class.
Diagram:

13
5) Hybrid inheritance: Hybrid inheritance is a combination of single, multiple,
multilevel and hierarchical inheritance.
Diagram:

14

You might also like