Browse papers
A

Section A: Long Answer Questions

Attempt any TWO questions.

3 questions·10 marks each
1long10 marks

What are templates in C++? Explain function templates and class templates with a program for each.

Templates in C++

A template is a feature of C++ that allows writing generic, type-independent code. Instead of writing separate functions or classes for each data type, a single template is written using a placeholder type parameter, and the compiler generates the appropriate concrete code for each type used. This supports code reusability and is the basis of generic programming (the STL is built on templates).

The general syntax uses the keyword template followed by a type parameter list:

template <class T>   // or template <typename T>

1. Function Template

A function template defines a family of functions that can operate on different data types. The compiler deduces T from the arguments and generates a separate version (template instantiation) for each type.

#include <iostream>
using namespace std;

template <class T>
T getMax(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    cout << getMax(10, 20) << endl;        // int    -> 20
    cout << getMax(3.5, 2.1) << endl;      // double -> 3.5
    cout << getMax('a', 'z') << endl;      // char   -> z
    return 0;
}

Output:

20
3.5
z

2. Class Template

A class template lets a class work with a generic data type. The type is supplied explicitly when an object is created, e.g. Calculator<int>.

#include <iostream>
using namespace std;

template <class T>
class Calculator {
    T a, b;
public:
    Calculator(T x, T y) : a(x), b(y) {}
    T add() { return a + b; }
    T multiply() { return a * b; }
};

int main() {
    Calculator<int> c1(10, 5);
    cout << "Int add: " << c1.add() << ", mul: " << c1.multiply() << endl;

    Calculator<double> c2(2.5, 4.0);
    cout << "Double add: " << c2.add() << ", mul: " << c2.multiply() << endl;
    return 0;
}

Output:

Int add: 15, mul: 50
Double add: 6.5, mul: 10

Summary

  • Function template — generic function; type is deduced from arguments.
  • Class template — generic class; type is specified explicitly at object creation.
  • Both promote reusability and type safety, generating type-specific code at compile time.
templates
2long10 marks

What is a copy constructor? When is it called? Explain the difference between a shallow copy and a deep copy with examples.

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. It takes a reference to an object of the same class (usually const) as its parameter.

Syntax:

ClassName(const ClassName &obj);

If the programmer does not define one, the compiler provides a default copy constructor that performs a member-by-member (shallow) copy.

When is the Copy Constructor Called?

  1. When an object is initialized from another object of the same class: Student s2 = s1; or Student s2(s1);
  2. When an object is passed by value to a function.
  3. When an object is returned by value from a function.
  4. When the compiler creates a temporary object.

Shallow Copy vs Deep Copy

Shallow Copy

The default copy constructor copies member values directly. If the class contains a pointer, only the pointer (address) is copied — both objects point to the same memory. This causes problems: changes through one object affect the other, and the destructor may double-free the same memory (dangling pointer / crash).

class Shallow {
    int *p;
public:
    Shallow(int v) { p = new int(v); }
    // default copy constructor => both objects share the same *p
    ~Shallow() { delete p; }   // double delete if copied!
};

Deep Copy

A user-defined copy constructor allocates new memory and copies the value pointed to, so each object owns an independent copy.

class Deep {
    int *p;
public:
    Deep(int v) { p = new int(v); }
    Deep(const Deep &obj) {        // deep copy constructor
        p = new int(*obj.p);       // allocate fresh memory, copy value
    }
    ~Deep() { delete p; }          // safe: each object frees its own memory
};

Difference Table

AspectShallow CopyDeep Copy
Pointer handlingCopies address onlyAllocates new memory, copies value
Memory sharingShared between objectsIndependent memory per object
Default behaviorDone by default copy constructorMust be written explicitly
RiskDangling pointer, double freeSafe

Conclusion: When a class manages dynamically allocated resources, a deep copy (user-defined copy constructor) is required to avoid shared-memory and double-free bugs.

constructorcopy
3long10 marks

Explain file handling in C++. Write a program to write records to a file and read them back using ifstream and ofstream.

File Handling in C++

File handling allows a program to store data permanently in secondary storage and retrieve it later, instead of losing it when the program ends. C++ provides file handling through the <fstream> header, which defines three stream classes:

ClassPurpose
ofstreamOutput file stream — used to write to a file
ifstreamInput file stream — used to read from a file
fstreamBoth reading and writing

Steps in File Handling

  1. Declare a stream object.
  2. Open the file (via constructor or open()), choosing a mode such as ios::in, ios::out, ios::app, ios::binary.
  3. Read/Write data using <<, >>, getline(), read(), write(), etc.
  4. Close the file with close() to flush buffers and release the resource.

Program: Write Records and Read Them Back

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

int main() {
    // ---- Writing records to a file ----
    ofstream fout("students.txt");
    if (!fout) { cout << "Error opening file!"; return 1; }

    fout << "Ram 85\n";
    fout << "Sita 90\n";
    fout << "Hari 78\n";
    fout.close();
    cout << "Records written successfully.\n";

    // ---- Reading records back from the file ----
    ifstream fin("students.txt");
    if (!fin) { cout << "Error opening file!"; return 1; }

    string name;
    int marks;
    cout << "\nName\tMarks\n";
    while (fin >> name >> marks) {
        cout << name << "\t" << marks << endl;
    }
    fin.close();
    return 0;
}

Output:

Records written successfully.

Name    Marks
Ram     85
Sita    90
Hari    78

Explanation

  • ofstream fout("students.txt") creates/opens the file for writing; << writes the records.
  • ifstream fin("students.txt") opens the same file for reading; >> reads each field and the while loop continues until end-of-file.
  • close() ensures buffered data is saved and the file is released.
file-handling
B

Section B: Short Answer Questions

Attempt any EIGHT questions.

9 questions·5 marks each
4short5 marks

Explain the four pillars of OOP.

The Four Pillars of OOP

  1. Encapsulation — Binding data members and the member functions that operate on them into a single unit (a class), and hiding internal data from outside access using access specifiers (private, protected, public). Data is accessed only through public methods, protecting it from misuse (data hiding).

  2. Abstraction — Showing only the essential features of an object while hiding the complex implementation details. The user knows what an object does, not how. Example: using cout without knowing how it internally writes to the screen.

  3. Inheritance — The mechanism by which a new class (derived class) acquires the properties and behaviours of an existing class (base class). It promotes code reusability and supports an is-a relationship (e.g., a Car is-a Vehicle).

  4. Polymorphism — "One name, many forms." The same function or operator behaves differently in different contexts. It is achieved through:

    • Compile-time (static): function overloading, operator overloading.
    • Run-time (dynamic): virtual functions and function overriding.

Together these four pillars make programs modular, reusable, extensible and easier to maintain.

oop-concepts
5short5 marks

What is a copy constructor? Write its syntax.

Copy Constructor

A copy constructor is a special member function that initializes a new object as a copy of an existing object of the same class. It takes a single argument which is a reference (usually const) to an object of the same class, preventing infinite recursion (a value argument would itself require a copy).

If not defined by the programmer, the compiler supplies a default copy constructor that performs a member-wise (shallow) copy.

Syntax

className (const className &obj) {
    // copy each data member from obj to this object
}

Example

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

Point p1(3, 4);
Point p2 = p1;   // copy constructor called

It is invoked when an object is initialized from another object, passed by value, or returned by value.

constructor
6short5 marks

Differentiate between public and private inheritance.

Public vs Private Inheritance

The access specifier used while inheriting decides the access level of the base class members inside the derived class and to the outside world.

class Derived : public Base { ... };    // public inheritance
class Derived : private Base { ... };    // private inheritance
BasisPublic InheritancePrivate Inheritance
Base public members becomepublic in derivedprivate in derived
Base protected members becomeprotected in derivedprivate in derived
Base private membersNot accessibleNot accessible
Relationship modelledis-a relationshiphas-a / implemented-in-terms-of relationship
Access by objects of derivedPublic base members accessible through derived objectBase members not accessible through derived object
Default for classMust be written explicitlyThis is the default if no specifier is given

Note: In both cases, base private members are never directly accessible in the derived class. Public inheritance is the most common form and preserves the interface of the base class, whereas private inheritance hides the base interface and is used for implementation reuse only.

inheritance
7short5 marks

What is the role of the catch block in exception handling?

Role of the catch Block in Exception Handling

C++ handles runtime errors using the try, throw, and catch mechanism. The catch block is the exception handler — its role is to receive and handle the exception thrown from the corresponding try block.

Roles / Functions:

  1. It catches the exception object thrown by a throw statement inside the associated try block.
  2. Its parameter type must match the type of the thrown object; this determines which catch block executes.
  3. It contains the code to handle the error gracefully (display a message, perform cleanup, recover) so the program does not terminate abnormally.
  4. Multiple catch blocks can follow one try to handle different exception types; catch(...) catches any type.
try {
    int a = 10, b = 0;
    if (b == 0) throw "Division by zero!";
    cout << a / b;
}
catch (const char* msg) {       // catch block handles the exception
    cout << "Error: " << msg;
}

Output: Error: Division by zero!

If no matching catch block exists, the program calls terminate() and aborts.

exception-handling
8short5 marks

Explain the get() and put() functions in file handling.

get() and put() Functions in File Handling

Both are unformatted single-character I/O functions used to read and write one character at a time from/to a stream or file.

get()

  • A member function of input stream classes (istream / ifstream).
  • Reads a single character (including whitespace such as spaces and newlines) from the file/stream.
char ch;
ifstream fin("data.txt");
fin.get(ch);     // reads one character into ch

put()

  • A member function of output stream classes (ostream / ofstream).
  • Writes a single character to the file/stream.
ofstream fout("data.txt");
fout.put('A');   // writes the character 'A'

Example: Copy a file character by character

ifstream fin("source.txt");
ofstream fout("copy.txt");
char ch;
while (fin.get(ch)) {   // read until EOF
    fout.put(ch);        // write each character
}

Difference: get() is used for reading a character, while put() is used for writing a character. Unlike >>, get() does not skip whitespace, which makes them useful for copying files exactly.

file-handling
9short5 marks

What is an object? How is memory allocated to an object?

Object

An object is an instance of a class — a concrete entity that occupies memory and represents a real-world thing. It bundles together data members (state) and member functions (behaviour) defined by its class. While a class is only a blueprint (a logical entity), an object is the actual usable entity created from it.

class Student {
    int roll;        // data member
public:
    void show();     // member function
};
Student s1;          // s1 is an object of class Student

Memory Allocation to an Object

  • Data members: When an object is created, separate memory is allocated for the data members of each object. Every object has its own copy of the (non-static) data members.
  • Member functions: Memory for member functions is not duplicated per object. The function code is stored only once in memory and is shared by all objects of the class. Each call passes a hidden this pointer so the function operates on the correct object's data.
  • Static members: A static data member has only one shared copy for the whole class, regardless of the number of objects.
  • Stack vs heap: An object declared normally (Student s1;) is allocated on the stack, whereas an object created with new (Student *p = new Student;) is allocated on the heap and must be freed with delete.

Summary: Memory is allocated for data members per object, but member function code is shared by all objects of the class.

class-object
10short5 marks

Write a short note on virtual destructor.

Virtual Destructor

A virtual destructor is a destructor declared with the keyword virtual in the base class. It ensures that when an object of a derived class is deleted through a base class pointer, the correct (derived) destructor is called first, followed by the base destructor.

Why it is needed

If the base destructor is not virtual and an object is deleted via a base pointer, only the base class destructor runs — the derived class destructor is skipped. This causes a resource/memory leak because resources allocated by the derived class are never released (undefined behaviour).

Example

class Base {
public:
    virtual ~Base() { cout << "Base destructor\n"; }   // virtual
};
class Derived : public Base {
public:
    ~Derived() { cout << "Derived destructor\n"; }
};

int main() {
    Base *p = new Derived();
    delete p;       // calls Derived then Base destructor
}

Output:

Derived destructor
Base destructor

If ~Base() were not virtual, only "Base destructor" would print and Derived's cleanup would be lost.

Conclusion: Always declare the base class destructor virtual when a class is meant to be used polymorphically (has virtual functions / is inherited and deleted through base pointers).

destructor
11short5 marks

What is a static member function?

Static Member Function

A static member function is a member function declared with the keyword static that belongs to the class as a whole rather than to any individual object.

Characteristics

  1. It can be called without creating an object, using the class name and scope resolution operator: ClassName::functionName();
  2. It can access only static data members and other static functions of the class — it cannot access non-static (ordinary) members.
  3. It does not have a this pointer, because it is not associated with any specific object.
  4. It is commonly used to operate on or report static data (e.g., counting the number of objects created).

Example

class Counter {
    static int count;          // static data member
public:
    Counter() { count++; }
    static int getCount() {    // static member function
        return count;
    }
};
int Counter::count = 0;

int main() {
    Counter c1, c2, c3;
    cout << "Objects created: " << Counter::getCount();  // called via class name
    return 0;
}

Output: Objects created: 3

static-members
12short5 marks

Explain the cin and cout objects.

cin and cout Objects

C++ performs console input/output using predefined stream objects declared in the header <iostream> within the std namespace.

cout (Console Output)

  • cout is an object of the class ostream, connected to the standard output device (the monitor/screen).
  • It is used with the insertion operator << to display data.
cout << "Hello" << endl;
cout << "Sum = " << 25;

cin (Console Input)

  • cin is an object of the class istream, connected to the standard input device (the keyboard).
  • It is used with the extraction operator >> to read data into variables.
int age;
cin >> age;        // reads an integer from keyboard

Key Points

ObjectClassDeviceOperator
cinistreamKeyboard (input)>> (extraction)
coutostreamScreen (output)<< (insertion)

Both operators can be cascaded (e.g., cin >> a >> b; and cout << a << b;), and >> skips leading whitespace by default.

io

Frequently asked questions

Where can I find the BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) question paper 2078?
The full BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2078 (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) 2078 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) 2078 paper?
The BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2078 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.