Browse papers
A

Section A: Long Answer Questions

Attempt any TWO questions.

3 questions·10 marks each
1long10 marks

What is a friend function? Explain its characteristics. Write a C++ program using a friend function to add two objects of a class.

Friend Function

A friend function is a non-member function that is granted special permission to access the private and protected members of a class. It is declared inside the class body using the keyword friend, but it is not a member of that class.

class Sample {
    friend void show(Sample s); // declaration
};

Characteristics

  1. It is not a member of the class, so it is not called using an object (obj.func()); it is called like an ordinary function.
  2. It can access private and protected members of the class.
  3. It must be declared with the keyword friend inside the class, but defined outside without friend and without the scope-resolution ClassName::.
  4. It does not have a this pointer, so it must receive the object as an argument.
  5. Friendship is not mutual and not inherited unless explicitly granted.
  6. It can be a friend of more than one class.

C++ Program: Adding Two Objects Using a Friend Function

#include <iostream>
using namespace std;

class Complex {
    int real, imag;
public:
    Complex(int r = 0, int i = 0) { real = r; imag = i; }
    // friend function declaration
    friend Complex add(Complex c1, Complex c2);
    void display() { cout << real << " + " << imag << "i" << endl; }
};

// friend function definition (no Complex:: , no friend keyword here)
Complex add(Complex c1, Complex c2) {
    Complex temp;
    temp.real = c1.real + c2.real;   // direct access to private members
    temp.imag = c1.imag + c2.imag;
    return temp;
}

int main() {
    Complex a(3, 4), b(5, 6), c;
    c = add(a, b);          // called like a normal function
    cout << "Sum = "; c.display();   // Output: Sum = 8 + 10i
    return 0;
}

Output: Sum = 8 + 10i

friend-function
2long10 marks

What is function overloading? Differentiate between function overloading and function overriding with examples.

Function Overloading

Function overloading is a form of compile-time (static) polymorphism in which two or more functions in the same scope share the same name but differ in their parameter list (number, types, or order of arguments). The compiler decides which version to call based on the arguments supplied.

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }

Note: functions cannot be overloaded by return type alone.

Function Overriding

Function overriding is run-time (dynamic) polymorphism in which a derived class provides a new definition for a virtual function already defined in its base class, with the same name and same signature.

class Base {
public:
    virtual void show() { cout << "Base\n"; }
};
class Derived : public Base {
public:
    void show() override { cout << "Derived\n"; }
};

Difference Table

BasisFunction OverloadingFunction Overriding
BindingCompile-time (early) bindingRun-time (late) binding
ScopeSame class/scopeBase and derived classes
SignatureMust differ in parameter listMust be identical
InheritanceNot requiredRequires inheritance
virtual keywordNot neededBase function must be virtual
Polymorphism typeStaticDynamic
function-overloading
3long10 marks

Explain exception handling in C++. Write a program using try, catch and throw to handle division by zero.

Exception Handling in C++

Exception handling is a mechanism to detect and respond to run-time errors in a controlled way, separating error-handling code from normal program logic. C++ provides three keywords:

  • try – encloses the block of code that may generate an exception.
  • throw – signals (raises) an exception when an error condition occurs, passing an object/value describing the error.
  • catch – immediately follows the try block and handles the thrown exception of a matching type.

Working: When code inside try throws an exception, normal flow stops, the stack unwinds, and control transfers to the first catch block whose parameter type matches the thrown value. If no handler matches, terminate() is called.

try {
    // code that may throw
    throw value;
}
catch (type arg) {
    // handle exception
}

Program: Handling Division by Zero

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter numerator and denominator: ";
    cin >> a >> b;

    try {
        if (b == 0)
            throw "Division by zero error!";   // throw an exception
        cout << "Result = " << (float)a / b << endl;
    }
    catch (const char* msg) {                  // catch the exception
        cout << "Exception: " << msg << endl;
    }

    return 0;
}

Sample run: Input 10 0 → Output: Exception: Division by zero error!
Input 10 2 → Output: Result = 5

exception-handling
B

Section B: Short Answer Questions

Attempt any EIGHT questions.

9 questions·5 marks each
4short5 marks

What is a constructor? List its characteristics.

Constructor

A constructor is a special member function of a class that has the same name as the class and is automatically invoked when an object is created. Its main purpose is to initialize the object's data members.

Characteristics

  1. It has the same name as the class.
  2. It has no return type, not even void.
  3. It is called automatically when an object is created.
  4. It can be overloaded (default, parameterized, copy constructors).
  5. It is usually declared in the public section.
  6. It cannot be virtual and cannot be inherited.
  7. If no constructor is defined, the compiler provides a default constructor.
constructor
5short5 marks

Differentiate between early binding and late binding.

Early Binding vs Late Binding

Binding means associating a function call with the function body (its address).

BasisEarly (Static) BindingLate (Dynamic) Binding
When resolvedAt compile timeAt run time
Also calledStatic / compile-time bindingDynamic / run-time binding
MechanismFunction call linked directlyResolved via virtual functions and the vtable
KeywordNo special keywordUses virtual functions and base-class pointers/references
SpeedFaster (no run-time lookup)Slower (extra indirection)
FlexibilityLess flexibleMore flexible (supports polymorphism)
ExampleNormal function call, overloaded functionsOverridden virtual function called through base pointer

Example (late binding):

Base* p = new Derived();
p->show();   // resolved at run time -> Derived::show() if show() is virtual
polymorphism
6short5 marks

What is the 'this' pointer?

The this Pointer

The this pointer is an implicit pointer automatically available inside every non-static member function of a class. It holds the address of the object on which the member function is currently invoked, so each function knows which object's data it is working with.

Key Points

  1. It is passed implicitly as a hidden argument to all non-static member functions.
  2. It is not available in static member functions (they belong to the class, not an object).
  3. It is used to:
    • Distinguish a data member from a parameter of the same name (e.g. this->x = x;).
    • Return the current object by reference to allow method chaining (return *this;).
class Box {
    int x;
public:
    Box& setX(int x) {
        this->x = x;   // 'this' resolves the name clash
        return *this;  // return current object for chaining
    }
};
this-pointer
7short5 marks

Explain the friend class concept.

Friend Class

A friend class is a class whose all member functions are granted access to the private and protected members of another class. When class B is declared a friend of class A, every member function of B can access A's private and protected members.

class A {
    int secret = 10;
    friend class B;   // B is a friend of A
};

class B {
public:
    void show(A a) {
        cout << a.secret;   // legal: B can access A's private member
    }
};

Key Points

  1. Declared inside the granting class using friend class ClassName;.
  2. Gives the entire friend class access to private/protected members.
  3. Friendship is one-way (if B is friend of A, A is not automatically friend of B).
  4. Friendship is not inherited and not transitive.
  5. Useful when two classes are tightly coupled and must share internal data (e.g. linked-list node and list classes).
friend-function
8short5 marks

What are the advantages of inheritance?

Advantages of Inheritance

Inheritance allows a new class (derived/child) to acquire the properties and behaviors of an existing class (base/parent). Its main advantages are:

  1. Code reusability – existing base-class code is reused, avoiding rewriting common functionality.
  2. Reduced redundancy – common members are defined once in the base class.
  3. Extensibility – new features can be added in derived classes without modifying the base class.
  4. Maintainability – changes made in the base class automatically propagate to derived classes.
  5. Supports polymorphism – enables run-time polymorphism through virtual functions and overriding.
  6. Represents real-world hierarchies – models "is-a" relationships (e.g. a Car is a Vehicle), improving readability and design.
  7. Transitive nature – a derived class can itself be a base for further classes (multilevel inheritance).
inheritance
9short5 marks

Write a note on dynamic memory allocation in C++.

Dynamic Memory Allocation in C++

Dynamic memory allocation is the process of allocating memory at run time from the heap (free store), rather than at compile time. It is useful when the required amount of memory is not known in advance. C++ uses the operators new and delete.

new Operator

Allocates memory on the heap and returns a pointer to it.

int* p = new int;        // single int
int* arr = new int[5];   // array of 5 ints
*p = 100;

delete Operator

Frees the dynamically allocated memory to avoid memory leaks.

delete p;        // free single object
delete[] arr;    // free array

Key Points

  • Memory is allocated from the heap, not the stack.
  • new returns the address of the allocated memory; if allocation fails it throws bad_alloc.
  • Every new must be matched by a delete (and new[] by delete[]) to prevent memory leaks and dangling pointers.
  • Unlike C's malloc()/free(), new also calls constructors and delete calls destructors for objects, and new is type-safe (no casting needed).
memory
10short5 marks

What is type conversion? Mention its types.

Type Conversion

Type conversion is the process of converting a value of one data type into another data type. In C++ it can be implicit (done automatically by the compiler) or explicit (done manually by the programmer using a cast).

Types of Type Conversion

  1. Implicit conversion (type promotion): Performed automatically by the compiler when types are compatible, usually from a smaller to a larger type (e.g. intfloat). No data loss for widening; possible loss for narrowing.

    int i = 5; float f = i;   // int automatically converted to float
    
  2. Explicit conversion (type casting): Done manually by the programmer.

    float x = (float) 7 / 2;     // C-style cast
    double d = static_cast<double>(a);  // C++ cast
    

In the context of user-defined types (objects), the three categories are:

  • Basic type to class type – using a constructor.
  • Class type to basic type – using an overloaded conversion (cast) operator.
  • Class type to another class type – using a constructor or conversion operator.
type-conversion
11short5 marks

Differentiate between overloading and overriding.

Overloading vs Overriding

BasisOverloadingOverriding
DefinitionSame function name with different parameter lists in the same scopeDerived class redefines a base-class virtual function with the same signature
PolymorphismCompile-time (static)Run-time (dynamic)
BindingEarly bindingLate binding
InheritanceNot requiredRequired (base and derived classes)
SignatureMust differMust be identical
virtual keywordNot neededBase function must be virtual

Overloading example:

int area(int s);          // square
int area(int l, int b);   // rectangle

Overriding example:

class Shape { public: virtual void draw(); };
class Circle : public Shape { public: void draw() override; };
function-overloading
12short5 marks

What is a template function?

Template Function

A template function (function template) is a function written with a generic (parameterized) data type so that the same logic works for many data types without rewriting it for each type. It implements generic programming and is a form of compile-time polymorphism. The compiler generates an actual function for each type used (template instantiation).

Syntax

template <class T>
T functionName(T a, T b) {
    // generic body
}

Example

#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;       // T = int   -> 20
    cout << maximum(3.5, 2.1) << endl;     // T = double -> 3.5
    cout << maximum('a', 'z') << endl;     // T = char  -> z
    return 0;
}

Advantages: code reusability, type safety, and a single definition that works for int, float, char, etc.

templates

Frequently asked questions

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