OOPM 2024 Answer Key
Q1 (2x5 = 10 marks) – Answer the following:
(a) What is token?
A token is the smallest element of a C++ program such as keywords, identifiers, constants,
strings, and operators.
Tokens are parsed by the compiler for syntactic meaning.
(b) Define Destructor.
Destructor is a special member function of a class which destroys objects when they go out of
scope.
It has the same name as class preceded by a tilde (~).
(c) What do you mean by base and derived class?
A base class provides general characteristics while derived class inherits these and adds
specific traits.
Inheritance supports reusability and extensibility.
(d) What is pure virtual function?
A pure virtual function is declared with = 0 and must be overridden in derived class.
It enables abstraction and is defined in abstract base classes.
(e) Define command line argument.
Command-line arguments are passed to the main function via parameters int argc, char*
argv[].
They provide input from outside the program during execution.
Q2 (a) List different features of object-oriented programming.
Encapsulation: bundling data and functions.
Inheritance: acquiring features from base class.
Polymorphism: same function with multiple forms.
Abstraction: hiding complex details.
Q2 (b) What do you mean by system-defined and user-defined data types?
System-defined types: int, float, char.
User-defined types: class, struct, union, enum.
They help in customizing data for specific requirements.
Q2 (c) Write a program to implement simple application of object-oriented
programming.
#include<iostream>
using namespace std;
class Car {
public:
void drive() { cout << "Driving a car"; }
};
int main() {
Car c;
c.drive();
return 0;
}
Q2 (d) Describe the merits and demerits of object-oriented programming.
Merits:
• Promotes modularity and code reuse.
• Real-world modeling possible.
• Easier debugging and maintenance.
Demerits:
• Larger program size.
• Slower than procedural approach.
• Difficult to learn for beginners.
Q3 (a) Write any two basic type modifiers.
1. signed – for signed integers.
2. unsigned – for non-negative integers.
Q3 (b) Explain formatted console I/O functions.
Formatted I/O allows display with specific formatting using setw, setprecision, setfill,
etc.
Requires inclusion of <iomanip> header file.
Q3 (c) What are storage class specifiers?
They define scope, visibility, and lifetime.
Types: auto, register, static, extern.
Q3 (d) Write a C++ program to add two numbers.
#include<iostream>
using namespace std;
int main() {
int a=5, b=3;
cout << "Sum: " << a + b;
return 0;
}
Q3 (e) What is the use of scope resolution operator (::)?
Used to access global variable when local variable has same name.
Also to define class functions outside the class.
Q4 (a) Write the syntax to declare class and object in C++.
class ClassName {
// members
};
ClassName obj;
Q4 (b) Explain access specifiers with example.
Access specifiers control visibility:
• public: accessible outside class
• private: accessible within class
class A { public: int x; private: int y; };
Q4 (c) Write a program using inline function.
#include<iostream>
using namespace std;
inline int square(int x) { return x*x; }
int main() {
cout << square(4);
return 0;
}
Q4 (d) Explain types of constructors.
1. Default Constructor – No parameters.
2. Parameterized – Accepts arguments.
3. Copy – Initializes using another object.
They initialize object state at creation.
Q5 (a) What is array of objects?
An array of objects stores multiple instances of a class in a single array.
Allows batch processing of objects.
Q5 (b) What is null constructor?
A null constructor (default constructor) has no parameters.
Automatically invoked when object is created without arguments.
Q5 (c) What is copy constructor?
A constructor which creates an object by copying another object of the same class.
Syntax: ClassName (const ClassName &obj);
Q5 (d) What is the use of static member?
Static members are shared by all objects of a class.
Useful for maintaining common values like object count.
Q5 (e) Write a program to show array of objects.
#include<iostream>
using namespace std;
class Student {
public:
int id;
void display() { cout << id << " "; }
};
int main() {
Student s[2] = {{1}, {2}};
s[0].display(); s[1].display();
return 0;
}
Q6 (a) What is inheritance? List its types.
Inheritance allows a class to acquire properties of another class.
Types: Single, Multiple, Multilevel, Hierarchical, Hybrid.
Q6 (b) Explain multilevel inheritance with a program.
#include<iostream>
using namespace std;
class A { public: void show() { cout<<"A "; } };
class B : public A { public: void display() { cout<<"B "; } };
class C : public B {};
int main() { C obj; obj.show(); obj.display(); return 0; }
Q6 (c) Differentiate between multiple and hierarchical inheritance.
Multiple: One class inherits from multiple classes.
Hierarchical: Multiple classes inherit from one base class.
Multiple can cause ambiguity.
Q7 (a) What is virtual base class?
Virtual base class prevents multiple copies of base class in inheritance.
Used in diamond problem of multiple inheritance.
Q7 (b) Write a program using hierarchical inheritance.
#include<iostream>
using namespace std;
class A { public: void show() { cout<<"A"; } };
class B: public A {};
class C: public A {};
int main() { B obj1; C obj2; obj1.show(); obj2.show(); return 0; }
Q7 (c) Write a program using hybrid inheritance.
#include<iostream>
using namespace std;
class A { public: void show() { cout<<"A"; } };
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
int main() { D obj; obj.show(); return 0; }
Q8 (a) What is polymorphism? Name its types.
Polymorphism means one function behaving differently in different scenarios.
Types: Compile-time and Run-time polymorphism.
Q8 (b) Explain operator overloading with a program.
#include<iostream>
using namespace std;
class Add {
int x;
public:
Add(int a) { x = a; }
Add operator+(Add obj) { return Add(x + obj.x); }
void show() { cout << x; }
};
int main() {
Add a1(3), a2(4), a3 = a1 + a2;
a3.show(); return 0;
}
Q8 (c) Explain the concept of virtual function with an example.
A virtual function is declared in base class and overridden in derived class to achieve runtime
polymorphism.
class Base { public: virtual void show() { cout << "Base"; } };
class Derived : public Base { public: void show() { cout << "Derived"; } };
Q9 (a) What is function overloading?
Function overloading means having multiple functions with same name but different
parameters.
Q9 (b) What is this pointer?
this is a pointer that holds the address of the current object.
Used to resolve ambiguity.
Q9 (c) Define friend function with example.
Friend function is a non-member function which can access private members of class.
class A {
int x = 5;
friend void show(A);
};
void show(A a) { cout << a.x; }
Q9 (d) Differentiate between early and late binding.
Early binding: Compile time (function overloading).
Late binding: Runtime (virtual function).
Late binding enables polymorphism.
Q9 (e) Write a program using function overloading.
void show(int a) { cout<<a; }
void show(float b) { cout<<b; }
int main() { show(5); show(3.2f); return 0; }
Q10 (a) What are the basic file operations?
Basic operations: open, read, write, close.
Classes used: ifstream, ofstream, fstream.
Q10 (b) Write a program to read and write data into a file.
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ofstream fout("data.txt");
fout << "Hello";
fout.close();
ifstream fin("data.txt");
string txt; fin >> txt;
cout << txt;
return 0;
}
Q10 (c) What are function templates? Write a program.
Templates allow a function to operate on different types.
template<class T>
T add(T a, T b) { return a + b; }
int main() { cout << add(3,4); }
Q11 (a) Define exception.
An exception is an error condition that disrupts normal flow of execution.
Handled using try-catch blocks.
Q11 (b) What is catch handler?
A catch block defines code to execute when specific exception type is thrown.
Multiple catch blocks can handle different types.
Q11 (c) Write a program to implement exception handling.
#include<iostream>
using namespace std;
int main() {
try {
throw 10;
} catch (int e) {
cout << "Exception: " << e;
}
return 0;
}
Q11 (d) Explain template argument.
Template argument is the data type passed to template function/class.
It allows generic programming.
Q11 (e) Write a program to implement class template.
template<class T>
class Box {
T val;
public:
Box(T v) { val = v; }
void show() { cout << val; }
};
int main() { Box<int> b(10); b.show(); return 0; }
Diploma Engineering (4th Semester) - 2023 Subject: Object Oriented Programming
Methodology (CSPE-407 B) Full Marks: 60 | Time: 2 Hours
Q.1 Answer the following questions: 2x5=10
(a) Define class and object.
Class is a blueprint of objects having methods and attributes.
Object is an instance of a class that holds real-time data.
(b) Why do we use private access specifier?
Private specifier restricts access to class members, enhancing security and encapsulation.
(c) What do you mean by 'catch all' in exception handling?
It catches all types of exceptions using catch(...) block, ensuring no exception goes
unhandled.
(d) What is an abstract class? When are they needed?
Abstract class cannot be instantiated and contains pure virtual functions.
They are needed when you want to enforce method definitions in derived classes.
(e) Define class template.
A class template allows creating generic classes that can work with any data type.
Q.2 (a) Describe the merits and limitations of object-oriented programming.
Merits: Code reusability, better software design, encapsulation, and maintenance.
Limitations: Steeper learning curve, larger program size, and more complex program
structure.
(b) Differentiate between object-oriented and procedure-oriented programming.
OOP focuses on objects and data; POP focuses on procedures and functions.
OOP offers encapsulation and inheritance; POP lacks modularity and reuse.
(c) Describe user-defined data types.
User-defined data types are custom types created using structures, classes, or unions in C++.
Q.3 (a) What is variable? Explain reference variable with an example.
A variable stores data in memory with a specific type and name.
Reference variable is an alias for another variable, declared with & symbol.
Example: int a = 10; int &ref = a;
Q.4 (a) Describe defining member function inside a class with an example.
A member function can be defined inside the class declaration.
Example:
class Demo {
public:
void show() {
cout << "Inside class";
}
};
(b) Differentiate between normal function and member function.
Normal functions are independent, not tied to any object.
Member functions are defined inside a class and access class members.
(c) Write a program to declare class with public, private and protected access specifiers.
Declare object and access data elements.
#include<iostream>
using namespace std;
class Demo {
public:
int a;
private:
int b;
protected:
int c;
public:
void setData() {
a = 1;
b = 2;
c = 3;
}
void show() {
cout << a << " " << b << " " << c;
}
};
int main() {
Demo obj;
obj.setData();
obj.show();
return 0;
}
Q.5 (a) What is inline function? Write a program to implement inline function.
An inline function expands code in-place to save function-call overhead.
#include<iostream>
using namespace std;
inline int add(int a, int b) {
return a + b;
}
int main() {
cout << add(5, 6);
return 0;
}
(b) What are constructors? Discuss about different types of constructor.
Constructor is a special method to initialize objects. Types: default, parameterized, copy, and
dynamic constructors.
(c) Write a program to implement copy constructor.
#include<iostream>
using namespace std;
class Demo {
int a;
public:
Demo(int x) { a = x; }
Demo(const Demo &d) {
a = d.a;
}
void show() {
cout << a;
}
};
int main() {
Demo d1(5);
Demo d2 = d1;
d2.show();
return 0;
}
Q.6 (a) Write down the advantages of using inheritance.
Code reuse, method overriding, data hiding, and better modularity.
(b) Write a program to implement multiple inheritance.
#include<iostream>
using namespace std;
class A {
public:
void showA() { cout << "Class A\n"; }
};
class B {
public:
void showB() { cout << "Class B\n"; }
};
class C : public A, public B {
};
int main() {
C obj;
obj.showA();
obj.showB();
return 0;
}
(c) What is virtual base class? Explain with example.
Virtual base class avoids duplication in multiple inheritance.
Example: class B : virtual public A {}
(d) What is late binding?
Late binding means method call is resolved at runtime using virtual functions.
Q.7 (a) What is multiple inheritance? Write its advantages.
Multiple inheritance allows a class to inherit from multiple base classes.
Advantage: Code reuse, flexibility in design.
(b) Discuss the role of inheritance in OOP.
Inheritance enables reuse of code, supports polymorphism, and establishes relationships
between classes.
(c) Define base and derived class.
Base class provides features to derived class.
Derived class extends base class functionality.
Q.8 (a) Define polymorphism. Discuss about the classification of polymorphism.
Polymorphism means one interface, many forms.
Types: Compile-time (function overloading, operator overloading) and Runtime (virtual
functions).
Q.9 (a) Write down the advantages of using polymorphism.
Simplifies code, supports extensibility, and allows dynamic function calls.
(b) What is virtual function? State the rules of virtual functions.
Virtual function is a member function redefined in a derived class.
Rules: Declared with virtual keyword; base class pointer can call derived function; must be
in base class.
(c) Write a program to implement friend function.
#include<iostream>
using namespace std;
class Demo {
int a;
public:
Demo() { a = 10; }
friend void show(Demo);
};
void show(Demo d) {
cout << d.a;
}
int main() {
Demo d;
show(d);
return 0;
}
(d) Write a program to overload binary operator for addition of two object. Define operator
overloading.
Operator overloading gives new meaning to existing operators.
#include<iostream>
using namespace std;
class Demo {
int a;
public:
Demo(int x) { a = x; }
Demo operator+(Demo d) {
return Demo(a + d.a);
}
void show() { cout << a; }
};
int main() {
Demo d1(10), d2(20);
Demo d3 = d1 + d2;
d3.show();
return 0;
}
Q.10 (a) What is stream? Define input and output stream.
A stream is a flow of data. Input stream reads data; output stream writes data.
(b) Describe opening file with constructor.
File can be opened automatically in constructor using fstream class with file name as
parameter.
(c) Briefly explain file modes.
File modes specify file access type: ios::in, ios::out, ios::app, ios::binary, etc.
Q.11 (a) Define exception with example. Explain two types of exception.
Exception is an error that disrupts normal flow.
Types: Synchronous (e.g., divide by zero), Asynchronous (e.g., hardware failure).
Example:
try {
int a = 5, b = 0;
if(b == 0) throw "Divide by zero!";
}
catch(const char* msg) {
cout << msg;
}
(b) Briefly explain the exception handling mechanism.
Uses try, catch, and throw to handle exceptions without program termination.
(c) Differentiate between template and macro.
Template is type-safe and checked at compile time.
Macro is a preprocessor directive and not type-safe.