BE Computer Engineering (IOE, TU) Object Oriented Programming (IOE, CT 501) Question Paper 2079 Nepal
This is the official BE Computer Engineering (IOE, TU) Object Oriented Programming (IOE, CT 501) question paper for 2079, as set in the regular annual examination. It carries 80 full marks and a time allowance of 180 minutes, across 12 questions. On Kekkei you can attempt this Object Oriented Programming (IOE, CT 501) past paper online with a timer, get instant AI feedback and step-by-step solutions, and track the topics where you lose marks — completely free. Whether you are revising for your BE Computer Engineering (IOE, TU) Object Oriented Programming (IOE, CT 501) exam or solving previous years' question papers, this 2079 paper is a great way to practise under real exam conditions.
Section A: Long Answer Questions
Attempt all / any as specified.
Differentiate between procedural programming and object-oriented programming. Define a class and an object with suitable examples. Write a C++ program to create a class Distance having data members feet and inches. Use a parameterized constructor to initialize the data members and a member function display() to show the distance. Also create an array of three Distance objects and display them.
Procedural vs Object-Oriented Programming
| Procedural Programming (POP) | Object-Oriented Programming (OOP) |
|---|---|
| Program is divided into functions (procedures). | Program is divided into objects. |
| Emphasis is on doing things (algorithms). | Emphasis is on data. |
| Data moves openly around the system between functions; mostly global data. | Data is encapsulated and hidden; accessed through member functions. |
| Follows a top-down approach. | Follows a bottom-up approach. |
| No data hiding; data is not secure. | Supports data hiding, inheritance, polymorphism. |
| Hard to extend/maintain for large programs. | Easier to extend, reuse and maintain. |
| Examples: C, Pascal, FORTRAN. | Examples: C++, Java, Python. |
Class and Object
- Class: A class is a user-defined data type that binds together data members and member functions into a single unit. It is a blueprint/template from which objects are created. It does not occupy memory until an object is created.
- Object: An object is an instance of a class. It is a real-world entity that occupies memory and has state (data) and behaviour (functions).
class Car { // class = blueprint
string model; // data member
int speed;
public:
void run() { /* ... */ } // member function
};
Car c1, c2; // c1, c2 are objects (instances)
C++ Program: Distance class with parameterized constructor and array of objects
#include <iostream>
using namespace std;
class Distance {
int feet;
int inches;
public:
// Parameterized constructor
Distance(int f, int i) {
feet = f;
inches = i;
}
void display() {
cout << feet << " feet " << inches << " inches" << endl;
}
};
int main() {
// Array of three Distance objects
Distance d[3] = { Distance(5, 8), Distance(6, 2), Distance(4, 11) };
cout << "The three distances are:" << endl;
for (int i = 0; i < 3; i++) {
d[i].display();
}
return 0;
}
Sample Output
The three distances are:
5 feet 8 inches
6 feet 2 inches
4 feet 11 inches
(a) Explain the different types of inheritance supported in C++ with suitable diagrams. What is the ambiguity problem in multiple inheritance and how is it resolved using virtual base classes? (b) Define a base class Shape with a virtual function area(). Derive classes Circle and Rectangle from it and override area() in each. Write a program that uses base-class pointers to compute and display the area of different shapes, demonstrating run-time polymorphism.
(a) Types of Inheritance in C++
Inheritance lets a derived class reuse members of a base class. C++ supports five types:
- Single inheritance — one base, one derived.
A -> B - Multilevel inheritance — derived class itself acts as a base.
A -> B -> C - Multiple inheritance — one derived class inherits from many bases.
A, B -> C - Hierarchical inheritance — many derived classes from one base.
A -> B, A -> C - Hybrid inheritance — combination of two or more of the above (e.g. multilevel + multiple).
(Diagrams: each is drawn as a directed graph of class boxes with arrows from base to derived as shown by the arrow notation above.)
Ambiguity in Multiple Inheritance (Diamond Problem)
When class D inherits from B and C, and both B and C inherit from a common base A, two copies of A's members are inherited into D. Accessing an A member through D becomes ambiguous because the compiler cannot decide which copy (via B or via C) to use.
Resolution — Virtual Base Class: Declaring A as a virtual base class in B and C ensures that only one shared copy of A is inherited by D, removing the ambiguity.
class A { public: int x; };
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { }; // only one copy of A::x
(b) Run-time Polymorphism with Virtual Function area()
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() = 0; // virtual function
virtual ~Shape() {}
};
class Circle : public Shape {
double r;
public:
Circle(double radius) : r(radius) {}
double area() override { return 3.14159 * r * r; }
};
class Rectangle : public Shape {
double l, b;
public:
Rectangle(double len, double br) : l(len), b(br) {}
double area() override { return l * b; }
};
int main() {
Shape* s[2];
s[0] = new Circle(5);
s[1] = new Rectangle(4, 6);
for (int i = 0; i < 2; i++)
cout << "Area = " << s[i]->area() << endl;
delete s[0];
delete s[1];
return 0;
}
Output
Area = 78.5397
Area = 24
Because area() is virtual, the call s[i]->area() is resolved at run time based on the actual object the base-class pointer points to — this is run-time (dynamic) polymorphism.
What is operator overloading? Explain the rules and restrictions for overloading operators in C++. Write a C++ program to define a class Complex to represent complex numbers, and overload the + and * operators to add and multiply two complex numbers respectively. Also overload the insertion operator (<<) using a friend function to display a complex number.
Operator Overloading
Operator overloading is the mechanism of giving an additional meaning to an existing C++ operator so that it works with user-defined types (objects), without changing its original meaning for built-in types. It is a form of compile-time polymorphism achieved using the operator keyword.
Rules and Restrictions
- Only existing operators can be overloaded; new operators (e.g.
**) cannot be created. - At least one operand must be a user-defined type (object).
- The arity (number of operands), precedence and associativity of an operator cannot be changed.
- The operators
.,.*,::,?:andsizeofcannot be overloaded. - Operators
=,(),[],->can be overloaded only as member functions. - Overloaded operators do not have default arguments.
- It cannot change the basic meaning for built-in types (e.g. you cannot redefine
+for twoints).
C++ Program: Complex class — overload +, * and <<
#include <iostream>
using namespace std;
class Complex {
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Overload + as member function
Complex operator+(const Complex& c) {
return Complex(real + c.real, imag + c.imag);
}
// Overload * : (a+bi)(c+di) = (ac-bd) + (ad+bc)i
Complex operator*(const Complex& c) {
return Complex(real*c.real - imag*c.imag,
real*c.imag + imag*c.real);
}
// Overload << as a friend function
friend ostream& operator<<(ostream& out, const Complex& c) {
out << c.real << " + " << c.imag << "i";
return out;
}
};
int main() {
Complex c1(2, 3), c2(1, 4);
Complex sum = c1 + c2;
Complex prod = c1 * c2;
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "Sum = " << sum << endl;
cout << "Prod = " << prod << endl;
return 0;
}
Output
c1 = 2 + 3i
c2 = 1 + 4i
Sum = 3 + 7i
Prod = -10 + 11i
Here (2+3i)+(1+4i)=3+7i and (2+3i)(1+4i)=(2-12)+(8+3)i=-10+11i.
(a) What are templates? Differentiate between function templates and class templates with examples. Write a function template to find the maximum of two values of any data type. (b) Explain exception handling in C++ using try, catch and throw keywords. Write a program that throws and handles an exception when an attempt is made to divide a number by zero.
(a) Templates
A template is a feature that allows writing generic functions and classes that work with any data type. The actual type is supplied as a parameter, and the compiler generates the specific version when needed. Templates support generic programming and avoid code duplication.
| Function Template | Class Template |
|---|---|
| Defines a family of functions that operate on different data types. | Defines a family of classes with members parameterised by type. |
| Type is usually deduced from arguments. | Type must be given explicitly, e.g. Stack<int> s; |
Example: a generic max() function. | Example: a generic Stack<T>, Array<T> container. |
// Class template example
template <class T>
class Box {
T value;
public:
Box(T v) : value(v) {}
T get() { return value; }
};
Function Template: maximum of two values
#include <iostream>
using namespace std;
template <class T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << maximum(10, 20) << endl; // int
cout << maximum(3.5, 2.1) << endl; // double
cout << maximum('x', 'a') << endl; // char
return 0;
}
Output: 20, 3.5, x
(b) Exception Handling in C++
Exception handling deals with run-time errors in a controlled way using three keywords:
try— encloses the code that may raise an exception.throw— signals (raises) an exception when an error occurs.catch— handles the exception thrown from the matchingtryblock.
When throw is executed, control jumps from the try block to the matching catch block, skipping the remaining statements in try.
Program: handle division by zero
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
try {
if (b == 0)
throw "Division by zero error!"; // throw
cout << "Result = " << a / b << endl;
}
catch (const char* msg) { // catch
cout << "Exception caught: " << msg << endl;
}
return 0;
}
Sample Output (for a=10, b=0)
Exception caught: Division by zero error!
Section B: Short Answer Questions
Attempt all / any as specified.
What is a copy constructor? Explain when it is invoked. Write a C++ program to demonstrate the use of a copy constructor and explain the difference between a shallow copy and a deep copy.
Copy Constructor
A copy constructor is a special constructor that creates a new object as an exact copy of an existing object of the same class. Its signature takes a reference to the same class (passed as const &):
ClassName(const ClassName& obj);
When is it invoked?
- When an object is initialised from another object of the same class:
B b = a; - When an object is passed by value to a function.
- When an object is returned by value from a function.
If the programmer does not define one, the compiler supplies a default copy constructor that performs a member-wise (shallow) copy.
Shallow Copy vs Deep Copy
- Shallow copy: Copies member values as-is. If a member is a pointer, only the pointer (address) is copied, so both objects share the same memory. Modifying one affects the other, and destroying both causes a double-free / dangling pointer.
- Deep copy: Allocates separate memory and copies the pointed-to data, so each object has its own independent copy. The user-defined copy constructor implements deep copy.
Program demonstrating a (deep) copy constructor
#include <iostream>
#include <cstring>
using namespace std;
class Student {
char* name;
public:
Student(const char* n) {
name = new char[strlen(n) + 1];
strcpy(name, n);
}
// Copy constructor -> deep copy
Student(const Student& s) {
name = new char[strlen(s.name) + 1];
strcpy(name, s.name);
}
void show() { cout << name << endl; }
~Student() { delete[] name; }
};
int main() {
Student s1("Ram");
Student s2 = s1; // copy constructor invoked
s1.show();
s2.show();
return 0;
}
Output:
Ram
Ram
Because s2 has its own copy of name, the two objects are independent and destruction is safe.
What are static data members and static member functions? Explain their characteristics. Write a program using a static data member to count the number of objects created from a class.
Static Data Members and Static Member Functions
Static Data Member
A data member declared with the keyword static. Its characteristics:
- Only one copy is shared by all objects of the class (class-level, not object-level).
- It is initialised to zero by default and must be defined/initialised outside the class.
- It exists even if no object of the class is created.
Static Member Function
A member function declared static. Its characteristics:
- Can be called using the class name without any object:
ClassName::func(); - It can access only static data members and other static functions.
- It has no
thispointer, since it is not tied to a particular object.
Program: count number of objects using a static data member
#include <iostream>
using namespace std;
class Counter {
static int count; // static data member
public:
Counter() { count++; }
static int getCount() { // static member function
return count;
}
};
int Counter::count = 0; // definition + initialisation
int main() {
Counter a, b, c;
cout << "Objects created = " << Counter::getCount() << endl;
return 0;
}
Output
Objects created = 3
Each constructor call increments the single shared count, so it correctly reports the total number of objects.
What is a pure virtual function? Explain the concept of an abstract class with a suitable example. Why can an object of an abstract class not be created?
Pure Virtual Function
A pure virtual function is a virtual function that has no definition in the base class and is declared by assigning = 0:
virtual void draw() = 0; // pure virtual function
It forces every derived class to provide its own implementation (override) of the function.
Abstract Class
An abstract class is a class that contains at least one pure virtual function. It is meant to serve only as a base/interface for derived classes and defines a common protocol that derived classes must implement.
class Shape { // abstract class
public:
virtual double area() = 0; // pure virtual function
};
class Circle : public Shape {
double r;
public:
Circle(double radius) : r(radius) {}
double area() override { return 3.14159 * r * r; }
};
Here Shape is abstract; Circle provides the body of area(), so Circle is a concrete class and Circle c(5); is valid.
Why can an object of an abstract class not be created?
Because an abstract class has at least one incomplete (pure virtual) function with no body. If an object were allowed, a call to that function would have no implementation to execute. Therefore the compiler prohibits instantiation of an abstract class. It can, however, be used through pointers or references to achieve polymorphism.
Explain the classes used for file handling in C++. Write a C++ program to write a list of student records (roll number and name) to a file and then read and display the records from the file.
File-Handling Classes in C++
C++ file handling is provided through the <fstream> header, which defines three stream classes:
| Class | Purpose |
|---|---|
ifstream | Input file stream — reads (read) data from a file. |
ofstream | Output file stream — writes (write) data to a file. |
fstream | Supports both reading and writing. |
Files are opened with a constructor or the open() function (using modes such as ios::in, ios::out, ios::app, ios::binary) and closed with close().
Program: write student records to a file, then read and display them
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// ----- Writing records -----
ofstream fout("students.txt");
int roll;
string name;
for (int i = 0; i < 3; i++) {
cout << "Enter roll and name: ";
cin >> roll >> name;
fout << roll << " " << name << endl;
}
fout.close();
// ----- Reading records -----
ifstream fin("students.txt");
cout << "\nRecords from file:\n";
while (fin >> roll >> name) {
cout << "Roll: " << roll << ", Name: " << name << endl;
}
fin.close();
return 0;
}
Sample Output
Records from file:
Roll: 1, Name: Ram
Roll: 2, Name: Sita
Roll: 3, Name: Hari
The ofstream object writes each roll/name pair to students.txt, and the ifstream object reads them back until end-of-file.
Explain the order in which constructors and destructors are called in a derived class under single inheritance. Illustrate your answer with a suitable C++ program that includes constructors and destructors in both base and derived classes.
Order of Constructor and Destructor Calls (Single Inheritance)
In a derived class:
- Constructors are called in order of derivation: the base-class constructor first, then the derived-class constructor. (Construction proceeds from base to derived.)
- Destructors are called in the reverse order: the derived-class destructor first, then the base-class destructor.
This is because a derived object is built on top of its base part, so the base must exist before the derived part is constructed, and must be removed last.
Illustrative Program
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "Base constructor\n"; }
~Base() { cout << "Base destructor\n"; }
};
class Derived : public Base {
public:
Derived() { cout << "Derived constructor\n"; }
~Derived() { cout << "Derived destructor\n"; }
};
int main() {
Derived d; // object creation
return 0; // object goes out of scope -> destructors
}
Output
Base constructor
Derived constructor
Derived destructor
Base destructor
This confirms: constructors run base -> derived, destructors run derived -> base.
Write short notes on any TWO of the following:
(a) this pointer
(b) Function overloading vs operator overloading
(c) Rethrowing an exception
(d) Inline functions
(Attempt any two — both shown for completeness.)
(a) this Pointer
The this pointer is an implicit pointer available inside every non-static member function that points to the object that invoked the function. It is used to:
- Resolve ambiguity when a parameter name is the same as a data member (
this->x = x;). - Return the calling object itself for method chaining (
return *this;). Static member functions do not have athispointer.
(b) Function Overloading vs Operator Overloading
| Function Overloading | Operator Overloading |
|---|---|
| Multiple functions share the same name but differ in number/type of parameters. | Gives an operator an additional meaning for user-defined types. |
| Resolved by the compiler from the argument list. | Implemented with the operator keyword. |
e.g. add(int,int) and add(double,double). | e.g. Complex operator+(Complex). |
| Both are forms of compile-time polymorphism. |
(c) Rethrowing an Exception
An exception caught in a catch block can be passed on (re-raised) to an outer/enclosing try-catch by writing throw; (with no operand). This lets the inner handler do partial processing (e.g. logging) and let an outer handler finish the handling.
try { /* ... */ }
catch (...) {
cout << "Logged here\n";
throw; // rethrow to outer handler
}
(d) Inline Functions
An inline function is one whose body the compiler substitutes directly at each call site (using the inline keyword), avoiding function-call overhead (no stack push/pop). It is best for small, frequently used functions. inline is only a request; the compiler may ignore it for large or recursive functions. Functions defined inside a class body are inline by default.
inline int square(int x) { return x * x; }
What is a friend function? Explain its merits and demerits. Write a C++ program to find the sum of private data members of two different classes using a single friend function.
Friend Function
A friend function is a function that is not a member of a class but is granted access to its private and protected members. It is declared inside the class using the keyword friend, but defined outside without the scope-resolution operator and without the friend keyword.
Merits
- Can access private/protected members of one or more classes.
- Useful for operations involving two or more different classes (a single function can bridge them).
- Often used to overload operators like
<<and>>.
Demerits
- Violates data hiding / encapsulation, the core OOP principle.
- It is not called using an object and has no
thispointer. - Too many friend functions reduce maintainability.
Program: sum of private members of two classes using one friend function
#include <iostream>
using namespace std;
class B; // forward declaration
class A {
int x;
public:
A(int v) : x(v) {}
friend int sum(A, B);
};
class B {
int y;
public:
B(int v) : y(v) {}
friend int sum(A, B);
};
int sum(A a, B b) { // friend of both A and B
return a.x + b.y;
}
int main() {
A a(10);
B b(20);
cout << "Sum = " << sum(a, b) << endl;
return 0;
}
Output
Sum = 30
The single function sum() is a friend of both A and B, so it can read the private members x and y directly.
Differentiate between compile-time polymorphism and run-time polymorphism with examples. Explain how function overloading and virtual functions support these two forms of polymorphism in C++.
Compile-time vs Run-time Polymorphism
Polymorphism means "one name, many forms." It is of two kinds:
| Compile-time (Static) Polymorphism | Run-time (Dynamic) Polymorphism |
|---|---|
| Function call is bound to its definition at compile time (early binding). | Function call is bound at run time (late binding). |
| Achieved by function overloading and operator overloading. | Achieved by virtual functions (overriding) using base-class pointers/references. |
| Faster — no run-time overhead. | Slightly slower — uses a virtual table (vtable). |
| Less flexible. | More flexible; supports generic, extensible design. |
Function Overloading (Compile-time)
The compiler selects the correct overloaded function by matching the argument list at compile time:
int area(int s) { return s * s; } // square
int area(int l, int b) { return l * b; } // rectangle
// area(5) and area(4,6) resolved at compile time
Virtual Functions (Run-time)
When a base-class pointer points to a derived object and calls a virtual function, the derived version is chosen at run time:
class Shape { public: virtual void draw() { cout << "Shape\n"; } };
class Circle : public Shape { public: void draw() override { cout << "Circle\n"; } };
Shape* p = new Circle();
p->draw(); // prints "Circle" -> decided at run time
Thus function overloading supports compile-time polymorphism while virtual functions support run-time polymorphism in C++.
Frequently asked questions
- Where can I find the BE Computer Engineering (IOE, TU) Object Oriented Programming (IOE, CT 501) question paper 2079?
- The full BE Computer Engineering (IOE, TU) Object Oriented Programming (IOE, CT 501) 2079 (regular) question paper is available free on Kekkei. You can read every question online and attempt the paper under timed exam conditions.
- Does the Object Oriented Programming (IOE, CT 501) 2079 paper come with solutions?
- Yes. Every question on this Object Oriented Programming (IOE, CT 501) past paper includes a step-by-step solution, plus instant AI feedback when you attempt it on Kekkei.
- How many marks is the BE Computer Engineering (IOE, TU) Object Oriented Programming (IOE, CT 501) 2079 paper?
- The BE Computer Engineering (IOE, TU) Object Oriented Programming (IOE, CT 501) 2079 paper carries 80 full marks and is meant to be completed in 180 minutes, across 12 questions.
- Is practising this Object Oriented Programming (IOE, CT 501) past paper free?
- Yes — reading and attempting this Object Oriented Programming (IOE, CT 501) past paper on Kekkei is completely free.