OOP - Object Oriented Programming, Past Papers

Object Oriented Programming BSCS 2nd Term 2022 Past Paper UOS

OOP Past Paper 2022 UOS

University of Sargodha (UOS) Object Oriented Programming past paper 2022 for BS 2nd term, with all questions in text form and solved answers. Every C++ program has been compiled and tested.

OOP Past Paper 2022 (text version)

University of Sargodha – Object Oriented Programming, Examination 2022 (BS 2nd term)
The header (programme, paper code, marks) was not included in the copy we received; the questions are below.

Objective part – short answers

  1. What is a destructor? How many parameters are passed to the destructor?
  2. How can a private data member of a class be made available outside the class?
  3. Write a declaration of classes that shows multiple inheritance.
  4. Write the names of two operators that cannot be overloaded.
  5. Write the syntax of the header of a class member function where the + operator is overloaded to add two objects and one object is returned.
  6. What is operator overloading? Can we introduce a new operator by using operator overloading?
  7. What is the diamond problem and how is it solved?
  8. What is function overriding?

Subjective part

  • Q. Write a program that declares a Geometry class as an abstract class. Two classes, Circle and Rectangle, are inherited from Geometry. Write a suitable program to calculate the area and perimeter of the relevant shape.
  • Q. Create a Distance class having two private data members, feet (int) and inches (float). Write an appropriate constructor and other functions to add the values of two objects and store the result in d3 using the expression d3.add(d1, d2) in main(), where d1, d2 and d3 are objects of the Distance class.
  • Q. Create a class named Student that includes name and CGPA as data members. Create another class named Scholar, inherited from Student, that includes stipend as its data member. Write appropriate multiple constructors and display functions to display objects of the Scholar class.
  • Q. Write a class named Array that contains an array of integers as a data member, with member functions: a constructor that initializes all array elements to −5; an input function to accept values from the user; a show function to display all values; and a function that makes all values one half of their existing values.

Solved answers – OOP 2022

Short questions

  1. A destructor is a special member function, named like the class with a tilde (~Student()), called automatically when an object is destroyed to release resources. It takes no parameters and returns nothing.
  2. Through public getter/setter functions of the class, or by declaring a friend function or friend class that is allowed to access private members.
  3. class A { };
    class B { };
    class C : public A, public B { };   // C inherits from both A and B
  4. Any two of: scope resolution ::, member access ., pointer-to-member .*, conditional ?:, sizeof, typeid. (Note: * and -> can be overloaded.)
  5. class Complex {
    public:
        Complex operator+(const Complex& other) const;   // returns one object
    };
  6. Operator overloading gives an existing operator a special meaning for user-defined types (e.g. c1 + c2 for Complex objects) using an operator function. No – we cannot create new operators (such as **); we can only redefine existing ones, and their precedence, associativity and number of operands stay the same.
  7. Diamond problem: when B and C both inherit from A, and D inherits from B and C, D gets two copies of A’s members, causing ambiguity. It is solved with virtual inheritance:
    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
  8. Function overriding: a derived class provides its own definition of a base-class function with the same name, parameters and return type. If the base function is virtual, the call is resolved at run time according to the actual object (run-time polymorphism).

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

Abstract Geometry class with Circle and Rectangle

#include <iostream>
using namespace std;

class Geometry {                     // abstract class
public:
    virtual double area() const = 0;        // pure virtual functions
    virtual double perimeter() const = 0;
    virtual ~Geometry() {}
};

class Circle : public Geometry {
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() const { return 3.14159 * radius * radius; }
    double perimeter() const { return 2 * 3.14159 * radius; }
};

class Rectangle : public Geometry {
    double length, width;
public:
    Rectangle(double l, double w) : length(l), width(w) {}
    double area() const { return length * width; }
    double perimeter() const { return 2 * (length + width); }
};

int main() {
    Geometry* shapes[2] = { new Circle(5), new Rectangle(4, 6) };
    for (int i = 0; i < 2; i++) {
        cout << "Area = " << shapes[i]->area()
             << ", Perimeter = " << shapes[i]->perimeter() << endl;
        delete shapes[i];
    }
    return 0;
}

Output: Circle r = 5 → Area 78.54, Perimeter 31.42; Rectangle 4 × 6 → Area 24, Perimeter 20. Geometry cannot be instantiated because it has pure virtual functions.

Distance class with d3.add(d1, d2)

#include <iostream>
using namespace std;

class Distance {
private:
    int feet;
    float inches;
public:
    Distance() : feet(0), inches(0.0f) {}
    Distance(int ft, float in) : feet(ft), inches(in) {}

    void add(Distance d1, Distance d2) {
        feet = d1.feet + d2.feet;
        inches = d1.inches + d2.inches;
        if (inches >= 12.0f) {
            inches -= 12.0f;
            feet++;
        }
    }
    void show() const { cout << feet << " feet " << inches << " inches" << endl; }
};

int main() {
    Distance d1(4, 9.5f), d2(2, 7.0f), d3;
    d3.add(d1, d2);
    d3.show();      // 7 feet 4.5 inches
    return 0;
}

4′ 9.5″ + 2′ 7″ = 6′ 16.5″ → 7 feet 4.5 inches.

Student and Scholar classes

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

class Student {
protected:
    string name;
    double cgpa;
public:
    Student() : name("Unknown"), cgpa(0.0) {}
    Student(string n, double g) : name(n), cgpa(g) {}
    void display() const { cout << "Name: " << name << ", CGPA: " << cgpa; }
};

class Scholar : public Student {
private:
    double stipend;
public:
    Scholar() : Student(), stipend(0.0) {}
    Scholar(string n, double g) : Student(n, g), stipend(0.0) {}
    Scholar(string n, double g, double s) : Student(n, g), stipend(s) {}
    void display() const {
        Student::display();
        cout << ", Stipend: " << stipend << endl;
    }
};

int main() {
    Scholar s1;
    Scholar s2("Ayesha", 3.4);
    Scholar s3("Hamza", 3.9, 15000);
    s1.display();
    s2.display();
    s3.display();
    return 0;
}

Three constructors (default, two-argument, three-argument) show constructor overloading; Scholar’s constructors pass name and CGPA to the Student constructor through the initializer list.

Array class (stored as float so that halving odd numbers keeps the fraction)

#include <iostream>
using namespace std;

class Array {
private:
    static const int SIZE = 5;
    float arr[SIZE];
public:
    Array() {                              // all elements = -5
        for (int i = 0; i < SIZE; i++) arr[i] = -5;
    }
    void input() {
        cout << "Enter " << SIZE << " values: ";
        for (int i = 0; i < SIZE; i++) cin >> arr[i];
    }
    void show() const {
        for (int i = 0; i < SIZE; i++) cout << arr[i] << " ";
        cout << endl;
    }
    void half() {                          // make every value one half
        for (int i = 0; i < SIZE; i++) arr[i] /= 2;
    }
};

int main() {
    Array a;
    a.show();      // -5 -5 -5 -5 -5
    a.input();
    a.half();
    a.show();
    return 0;
}

Sample: input 10 7 4 9 1 → after halving: 5 3.5 2 4.5 0.5.

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.