OOP - Object Oriented Programming, Past Papers

Object Oriented Programming Past Paper 2026 AD/BS 2nd Semester UOS (CMPC-5202)

OOP Past Paper 2026 UOS AD/BS 2nd Semester (CMPC-5202)

University of Sargodha (UOS) AD/BS 2nd semester Object Oriented Programming (CMPC-5202) past paper 2026 for CS, IT and SE, held in November 2026 – with the full question paper in text form and solved answers. All C++ programs are tested.

OOP Past Paper 2026 (text version)

University of Sargodha – AD/BS 2nd Semester Examination 2026, CS/IT/SE
Paper: Object Oriented Programming (CMPC-5202) · Time: 2:30 hours · Maximum marks: 40
Note: Objective part is compulsory. Attempt any three questions from the subjective part.

Objective part (compulsory, 2 × 8)

  1. What are the key differences between procedural programming and object-oriented programming?
  2. Define the term const member function. How is it different from a non-const function?
  3. Explain the term interface in the context of OOP.
  4. Give a brief overview of multiple inheritance.
  5. How are class templates useful in generic programming?
  6. Define abstract class with an example.
  7. What is the use of the Standard Template Library (STL)?
  8. Explain the role of the try-catch block in C++.

Subjective part (attempt any three, 3 × 8)

  • Q.2 Write a complete C++ program to demonstrate the concept of inheritance and method overriding.
  • Q.3 Explain the history and evolution of object-oriented programming. What makes it superior to procedural approaches?
  • Q.4 Describe the steps involved in identifying classes and their relationships during object-oriented design.
  • Q.5 Explain the difference between composition and inheritance. When should you use composition over inheritance in object-oriented design? Support your answer with appropriate examples and scenarios.
  • Q.6 Create a class Complex for complex numbers with: private members for real and imaginary parts; constructors (default and parameterized); overloaded + and − operators to perform arithmetic operations; a display function. Write a main function to test the functionality.

Solved answers – OOP 2026

Short questions

  1. Procedural programming organizes a program as functions working on shared data (top-down, e.g. C); data is exposed and hard to protect. OOP organizes it as objects that combine data and functions (bottom-up, e.g. C++, Java), giving encapsulation, inheritance and polymorphism – better data security, reuse and maintenance for large programs.
  2. A const member function is declared with const after its parameter list and promises not to modify the object’s data members. It can be called on const objects; a non-const function can change members and cannot be called on a const object.
    class Point {
        int x;
    public:
        int getX() const { return x; }   // const: cannot modify x
        void setX(int v) { x = v; }      // non-const: may modify
    };
    const Point p;   // p.getX() allowed, p.setX(5) is an error
  3. An interface is a contract listing operations a class must provide, without implementation. In C++ it is an abstract class with only pure virtual functions; derived classes implement them, so code can depend on the interface instead of concrete classes (e.g. a Printable interface with print()).
  4. Multiple inheritance: a class derives from two or more base classes and inherits members of all of them – class C : public A, public B { };. It enables combining features but can cause ambiguity and the diamond problem, which is solved with virtual inheritance.
  5. Class templates let one class definition work with any data type – the type is a parameter. This avoids rewriting the same class for int, double or string and keeps type safety (the STL containers are templates).
    template <typename T>
    class Box {
        T value;
    public:
        Box(T v) : value(v) {}
        T get() const { return value; }
    };
    Box<int> a(5);  Box<string> b("UOS");   // one class, many types
  6. An abstract class has at least one pure virtual function, so it cannot be instantiated; it defines a common interface for derived classes.
    class Shape {                      // abstract class
    public:
        virtual double area() const = 0;   // pure virtual function
    };
    class Square : public Shape {
        double s;
    public:
        Square(double a) : s(a) {}
        double area() const override { return s * s; }
    };
    // Shape sh;   -> error: cannot create an object of an abstract class
  7. The STL provides ready-made, tested generic components: containers (vector, list, map, set, stack, queue), algorithms (sort, find, count, reverse) and iterators that connect them – saving time and giving efficient, reusable code.
  8. try-catch handles run-time errors: code that may fail goes in try, an error is signalled with throw, and a matching catch handles it, so the program can recover instead of crashing.
    try {
        if (d == 0) throw runtime_error("Division by zero");
        cout << n / d;
    } catch (const exception& e) {
        cout << "Error: " << e.what() << endl;
    }

Subjective questions (programs compiled and tested with g++)

Q.2 Inheritance and method overriding

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

class Employee {                       // base class
protected:
    string name;
    double baseSalary;
public:
    Employee(string n, double s) : name(n), baseSalary(s) {}
    virtual double salary() const { return baseSalary; }
    virtual void show() const {
        cout << "Employee: " << name << ", Salary: " << salary() << endl;
    }
    virtual ~Employee() {}
};

class Manager : public Employee {      // derived class (inheritance)
    double bonus;
public:
    Manager(string n, double s, double b) : Employee(n, s), bonus(b) {}
    double salary() const override { return baseSalary + bonus; }   // overriding
    void show() const override {
        cout << "Manager: " << name << ", Salary: " << salary()
             << " (includes bonus " << bonus << ")" << endl;
    }
};

class Intern : public Employee {
public:
    Intern(string n, double s) : Employee(n, s) {}
    double salary() const override { return baseSalary * 0.5; }    // overriding
    void show() const override {
        cout << "Intern: " << name << ", Stipend: " << salary() << endl;
    }
};

int main() {
    Employee* staff[3] = {
        new Employee("Ali", 80000),
        new Manager("Sara", 120000, 30000),
        new Intern("Hamza", 40000)
    };
    for (Employee* e : staff) {
        e->show();          // the overridden version runs (run-time polymorphism)
        delete e;
    }
    return 0;
}

Output: Employee: Ali, Salary: 80000 / Manager: Sara, Salary: 150000 (includes bonus 30000) / Intern: Hamza, Stipend: 20000. Manager and Intern inherit name and baseSalary from Employee and override salary() and show(); because the base functions are virtual, calls through an Employee* run the derived versions.

Q.3 History and evolution of OOP:

  • 1960s – Simula 67 (Ole-Johan Dahl and Kristen Nygaard, Norway) introduced classes, objects and inheritance for simulation programs.
  • 1970s – Smalltalk (Alan Kay, Xerox PARC) made “everything an object” with message passing, and coined the term object-oriented.
  • 1980s – C++ (Bjarne Stroustrup, 1983–85) added classes to C; Objective-C and Eiffel followed.
  • 1990s – Java (1995) made OOP mainstream with platform independence; Python and Ruby also became popular; UML (1997) standardized OO design.
  • 2000s onwards – C#, modern C++ (11/14/17/20), Kotlin, Swift combine OOP with generic and functional features.

Why it is superior to procedural approaches: encapsulation protects data from accidental change; abstraction hides complexity; inheritance and composition give code reuse; polymorphism lets one interface work with many types; and modelling real-world entities as objects makes large programs easier to design, test, maintain and extend, with teams working on separate classes.

Q.4 Steps for identifying classes and their relationships:

  1. Understand the requirements – read the problem statement and use cases.
  2. Find candidate classes – underline the nouns (noun-phrase analysis), e.g. in a library system: Book, Member, Librarian, Loan.
  3. Filter the candidates – remove duplicates, vague terms, and nouns that are only attributes (title, date).
  4. Identify attributes – the data each class must hold.
  5. Identify operations – verbs become methods (issueBook(), returnBook()); use CRC cards (Class–Responsibility–Collaborator).
  6. Identify relationships – association (Member borrows Book), aggregation/composition (Library has Books), inheritance (Student and Teacher are Members) and dependency, with multiplicities (1, 0..*, 1..*).
  7. Draw the class diagram in UML and refine it iteratively – check cohesion, coupling and missing responsibilities.

Q.5 Composition vs inheritance: Inheritance is an “is-a” relationship – a derived class reuses and extends a base class (a Car is a Vehicle); it is fixed at compile time and tightly couples child to parent. Composition is a “has-a” relationship – a class contains objects of other classes as members (a Car has an Engine); the part can be replaced and is reused through its public interface, giving looser coupling.

class Engine { public: void start() { /* ... */ } };
class Car {                 // composition: Car has-an Engine
    Engine engine;
public:
    void drive() { engine.start(); }
};
class ElectricCar : public Car { };   // inheritance: ElectricCar is-a Car

Use composition when: the relationship is “has-a” or “uses-a”; behaviour must change at run time (e.g. a Character with a replaceable Weapon object, or a Printer with interchangeable output strategies); you want to reuse a class without exposing its whole interface; or deep inheritance hierarchies would become fragile (a change in a base class breaks many subclasses). Use inheritance only for true “is-a” relationships where polymorphism is needed. Guideline: favour composition over inheritance.

Q.6 Complex class with + and − operators

#include <iostream>
using namespace std;

class Complex {
private:
    double real, imag;
public:
    Complex() : real(0), imag(0) {}                       // default constructor
    Complex(double r, double i) : real(r), imag(i) {}     // parameterized constructor

    Complex operator+(const Complex& c) const {
        return Complex(real + c.real, imag + c.imag);
    }
    Complex operator-(const Complex& c) const {
        return Complex(real - c.real, imag - c.imag);
    }
    void display() const {
        cout << real << (imag < 0 ? " - " : " + ") << (imag < 0 ? -imag : imag) << "i" << endl;
    }
};

int main() {
    Complex c1(4, 5), c2(2, -3), c3;
    cout << "c1 = "; c1.display();
    cout << "c2 = "; c2.display();
    c3 = c1 + c2;
    cout << "c1 + c2 = "; c3.display();     // 6 + 2i
    c3 = c1 - c2;
    cout << "c1 - c2 = "; c3.display();     // 2 + 8i
    Complex zero;
    cout << "default = "; zero.display();   // 0 + 0i
    return 0;
}

Output: c1 = 4 + 5i, c2 = 2 − 3i, c1 + c2 = 6 + 2i, c1 − c2 = 2 + 8i, default = 0 + 0i.

Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.