Browse papers
A

Section A: Long Answer Questions

Attempt any TWO questions.

3 questions·10 marks each
1long10 marks

What is the difference between structure and class in C++? Explain the role of the 'this' pointer with an example program.

Structure vs. Class in C++

In C++ both struct and class can hold data members and member functions, support inheritance, constructors, destructors and access specifiers. The only technical difference is the default access level:

Featurestructclass
Default member accesspublicprivate
Default inheritance accesspublicprivate
Members & methods allowedYesYes
Constructors / destructorsYesYes
Conventional usePlain Old Data (POD), simple recordsFull encapsulation / OOP abstraction
struct S { int x; };   // x is public by default
class  C { int x; };   // x is private by default

By convention struct is used for passive data aggregates and class for objects that enforce encapsulation.

The this Pointer

this is an implicit pointer available inside every non-static member function. It holds the address of the object on which the member function was invoked. Its main uses are:

  1. Resolving the name conflict between a data member and a parameter of the same name.
  2. Returning the current object (e.g. to support method chaining).
  3. Passing the current object to other functions.

Example Program

#include <iostream>
using namespace std;

class Box {
    int length;
public:
    Box& setLength(int length) {
        this->length = length;   // 'this->length' = member, 'length' = parameter
        return *this;            // enables chaining
    }
    void show() { cout << "Length = " << this->length << endl; }
};

int main() {
    Box b;
    b.setLength(10).show();      // method chaining via 'this'
    return 0;
}

Output: Length = 10

Here this->length = length; distinguishes the member length from the parameter, and return *this; returns a reference to the calling object, allowing setLength(10).show().

class-objectthis-pointer
2long10 marks

Explain multiple and hybrid inheritance. What is the diamond problem? How is it resolved using virtual base classes? Illustrate with a program.

Multiple and Hybrid Inheritance

Multiple inheritance: a derived class inherits from two or more base classes.

class Derived : public Base1, public Base2 { ... };

Hybrid inheritance: a combination of two or more types of inheritance (e.g. multiple + multilevel, or hierarchical + multiple) in a single hierarchy.

The Diamond Problem

When two base classes (B and C) both inherit from a common base A, and a class D inherits from both B and C, then D ends up with two copies of A's members. This is ambiguous — the compiler cannot decide which copy of A to use. The class hierarchy resembles a diamond:

      A
     / \
    B   C
     \ /
      D

Resolution with Virtual Base Classes

Declaring A as a virtual base class when B and C inherit from it makes the compiler keep only one shared copy of A in D, removing the ambiguity.

Illustrative Program

#include <iostream>
using namespace std;

class A {
public:
    int data;
    A() { data = 100; }
};

class B : virtual public A { };   // virtual base
class C : virtual public A { };   // virtual base

class D : public B, public C { }; // single copy of A

int main() {
    D obj;
    cout << "data = " << obj.data << endl;  // unambiguous now
    return 0;
}

Output: data = 100

Without the virtual keyword, obj.data would be ambiguous (error: request for member data is ambiguous). With virtual inheritance there is a single subobject of A, so the access is valid.

inheritancevirtual-base
3long10 marks

What are class templates? Write a C++ program to implement a generic 'Stack' class using templates with push and pop operations.

Class Templates

A class template is a blueprint that lets a single class definition work with any data type. The actual type is supplied as a parameter when an object is created, so the compiler generates a type-specific class on demand. This achieves generic programming and avoids code duplication.

General syntax:

template <class T>
class ClassName { /* T used as a type */ };

Generic Stack Using Templates

#include <iostream>
using namespace std;

template <class T>
class Stack {
    static const int MAX = 100;
    T arr[MAX];
    int top;
public:
    Stack() { top = -1; }

    bool isEmpty() { return top == -1; }
    bool isFull()  { return top == MAX - 1; }

    void push(T value) {
        if (isFull()) { cout << "Stack Overflow\n"; return; }
        arr[++top] = value;
    }

    T pop() {
        if (isEmpty()) { cout << "Stack Underflow\n"; return T(); }
        return arr[top--];
    }
};

int main() {
    Stack<int> s;          // stack of int
    s.push(10);
    s.push(20);
    cout << s.pop() << endl;   // 20
    cout << s.pop() << endl;   // 10

    Stack<char> cs;        // same class, different type
    cs.push('A');
    cout << cs.pop() << endl;  // A
    return 0;
}

Output:

20
10
A

The same Stack template is instantiated as Stack<int> and Stack<char> without rewriting the class, demonstrating the reusability of class templates.

templatesdata-structure
B

Section B: Short Answer Questions

Attempt any EIGHT questions.

9 questions·5 marks each
4short5 marks

What are the benefits of object-oriented programming over procedural programming?

Benefits of OOP over Procedural Programming

  1. Encapsulation / Data hiding — data and the functions that operate on it are bundled inside a class; private data is protected from accidental external access, unlike global data in procedural code.
  2. Reusability through inheritance — existing classes can be extended without rewriting code, whereas procedural programs often duplicate logic.
  3. Polymorphism — the same interface can behave differently for different objects (function/operator overloading, virtual functions), giving flexibility and easier extension.
  4. Abstraction — implementation details are hidden behind a clean interface, reducing complexity.
  5. Easier maintenance and scalability — changes are localised to a class, so large programs are easier to debug, modify and extend; procedural code where functions share global data is harder to maintain.
  6. Modeling real-world entities — objects map naturally to real-world things, making design more intuitive.
oop-concepts
5short5 marks

Explain the syntax of a class template.

Syntax of a Class Template

A class template is declared with the template keyword followed by one or more type parameters in angle brackets, then the class definition:

template <class T>          // or: template <typename T>
class ClassName {
    T data;                 // T used as a data type
public:
    T getData() { return data; }
    void setData(T d) { data = d; }
};

Defining a member function outside the class requires repeating the template header:

template <class T>
T ClassName<T>::getData() { return data; }

Creating an object requires specifying the actual type as a template argument:

ClassName<int> obj1;      // T becomes int
ClassName<double> obj2;   // T becomes double

Multiple parameters and non-type parameters are allowed, e.g. template <class T, int SIZE>. class and typename are interchangeable in the parameter list.

templates
6short5 marks

What is the use of the const keyword with member functions?

const Member Functions

A member function declared with const after its parameter list promises not to modify the object's (non-mutable) data members:

class Point {
    int x, y;
public:
    int getX() const { return x; }   // const member function
};

Uses and effects:

  1. Read-only guarantee — inside a const function the implicit this pointer is const, so any attempt to modify a member (or call a non-const member) causes a compile-time error. This enforces correctness.
  2. Callable on const objects — a const object (or const reference/pointer) can only call const member functions. So accessor functions must be const to remain usable on constant objects.
  3. Const-correctness & overloading — functions can be overloaded on const-ness, and const-correct interfaces document intent clearly and let the compiler optimise.

Typically getter/accessor functions are made const, while mutators are not.

syntax
7short5 marks

Differentiate between compile-time and run-time polymorphism.

Compile-time vs Run-time Polymorphism

AspectCompile-time (Static)Run-time (Dynamic)
BindingEarly / static binding — resolved at compile timeLate / dynamic binding — resolved at run time
Achieved byFunction overloading, operator overloading, templatesVirtual functions (function overriding) via base-class pointer/reference
KeywordNone specialvirtual
MechanismCompiler picks function by signaturevtable / vptr lookup at run time
PerformanceFaster (no runtime lookup)Slight overhead due to indirection
FlexibilityLess flexibleMore flexible; supports extensibility

Compile-time example: print(int) and print(double) — the compiler chooses based on argument type.

Run-time example:

class Base { public: virtual void show(){ cout<<"Base"; } };
class Derived: public Base { public: void show(){ cout<<"Derived"; } };
Base* p = new Derived();
p->show();   // prints "Derived" — decided at run time
polymorphism
8short5 marks

Explain the role of the access specifier in inheritance.

Role of Access Specifiers in Inheritance

The access specifier used in class Derived : <specifier> Base controls how the inherited members of the base class are accessible in the derived class. It can be public, protected, or private.

Base member accesspublic inheritanceprotected inheritanceprivate inheritance
public memberstays publicbecomes protectedbecomes private
protected memberstays protectedstays protectedbecomes private
private membernot inherited (inaccessible)not inheritednot inherited

Key points:

  • public inheritance models an "is-a" relationship; the public interface of the base remains part of the derived class's public interface.
  • protected inheritance keeps inherited public/protected members accessible to the derived class and its further sub-classes but hides them from outside users.
  • private inheritance models "implemented-in-terms-of"; all inherited members become private and are not exposed further.
  • A base class's private members are never directly accessible to the derived class regardless of the specifier (they remain encapsulated).
inheritance
9short5 marks

What is a nested class? Give an example.

Nested Class

A nested class is a class declared inside the body of another (enclosing) class. It is a member of the enclosing class and is referenced through the enclosing class's name using the scope-resolution operator (Outer::Inner). A nested class can access the enclosing class's static members and types, but does not automatically have access to non-static members of an enclosing object (it needs an object reference for that). Nested classes improve logical grouping and encapsulation of helper types.

Example

#include <iostream>
using namespace std;

class University {
public:
    class Student {          // nested class
        string name;
    public:
        void setName(string n) { name = n; }
        void show() { cout << "Student: " << name << endl; }
    };
};

int main() {
    University::Student s;    // access via enclosing class name
    s.setName("Ram");
    s.show();                // Output: Student: Ram
    return 0;
}

Here Student is nested inside University and is used as University::Student.

class-object
10short5 marks

Write a short note on operator overloading using friend function.

Operator Overloading Using a Friend Function

A friend function is a non-member function granted access to the private and protected members of a class. When an operator is overloaded as a friend, it is not a member of the class, so it takes all operands as explicit arguments (a binary operator takes two arguments; a member operator takes only one because the left operand is the implicit object).

Why use friend functions for operator overloading?

  • They allow the left operand to be a non-object type, which is essential for operators like <<, >> and for expressions such as 2 + obj where the left operand is a built-in type. A member function cannot do this because the left operand must be an object of the class.

Example: overloading + for a Complex number

#include <iostream>
using namespace std;

class Complex {
    int real, imag;
public:
    Complex(int r = 0, int i = 0) : real(r), imag(i) {}
    friend Complex operator+(Complex a, Complex b);   // friend declaration
    void show() { cout << real << " + " << imag << "i" << endl; }
};

// friend definition - takes BOTH operands
Complex operator+(Complex a, Complex b) {
    return Complex(a.real + b.real, a.imag + b.imag);
}

int main() {
    Complex c1(2, 3), c2(4, 5);
    Complex c3 = c1 + c2;
    c3.show();        // Output: 6 + 8i
    return 0;
}

The friend operator+ accesses the private members real and imag of both objects and returns their sum.

operator-overloading
11short5 marks

What is the difference between throw and rethrow?

throw vs rethrow

throw is used to raise/originate an exception. It is followed by an object or value that represents the error, and it transfers control to the nearest matching catch block.

if (b == 0) throw runtime_error("Divide by zero");

Rethrow is a throw statement with no operand, written simply as throw; inside a catch block. It re-raises the same exception currently being handled so that an outer (enclosing) try-catch can also handle it. This is used when a handler does partial processing (e.g. logging or cleanup) but cannot fully recover.

try {
    try {
        throw 10;          // original throw
    }
    catch (int e) {
        cout << "Inner handler\n";
        throw;             // rethrow same exception
    }
}
catch (int e) {
    cout << "Outer handler caught " << e << endl;
}
throw exprthrow; (rethrow)
Creates and raises a new exceptionRe-raises the current exception
Used anywhere (e.g. in try)Used only inside a catch block
Specifies the exception objectNo operand; preserves original object/type
exception-handling
12short5 marks

Explain the use of default and copy constructor together.

Default and Copy Constructors Together

A constructor is a special member function (same name as the class, no return type) that initialises an object when it is created.

Default constructor — takes no arguments (or all arguments have defaults). It initialises an object with default/zero values.

class Point {
    int x, y;
public:
    Point() { x = 0; y = 0; }              // default constructor
    Point(const Point &p) { x = p.x; y = p.y; }  // copy constructor
};

Copy constructor — takes a reference to an existing object of the same class (const ClassName&) and creates a new object as a copy of it. It is invoked when an object is initialised from another, passed by value, or returned by value.

Using them together

int main() {
    Point p1;          // default constructor -> (0,0)
    Point p2 = p1;     // copy constructor    -> copy of p1
    Point p3(p1);      // copy constructor    -> another copy
    return 0;
}
  • p1 is built with the default constructor.
  • p2 and p3 are built with the copy constructor from p1.

Providing a user-defined copy constructor is important when a class manages dynamic memory, to perform a deep copy and avoid two objects sharing/double-freeing the same pointer (the default compiler-generated copy constructor performs only a shallow, member-wise copy).

constructor

Frequently asked questions

Where can I find the BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) question paper 2081?
The full BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2081 (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 (BSc CSIT, CSC161) 2081 paper come with solutions?
Yes. Every question on this Object-Oriented Programming (BSc CSIT, CSC161) past paper includes a step-by-step solution, plus instant AI feedback when you attempt it on Kekkei.
How many marks is the BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2081 paper?
The BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2081 paper carries 60 full marks and is meant to be completed in 180 minutes, across 12 questions.
Is practising this Object-Oriented Programming (BSc CSIT, CSC161) past paper free?
Yes — reading and attempting this Object-Oriented Programming (BSc CSIT, CSC161) past paper on Kekkei is completely free.