OOP - Object Oriented Programming, Past Papers

Object Oriented Programming BSCS 2nd Term 2016 Past Paper UOS

University of Sargodha BS 2nd Term Object Oriented Programming Exam Paper 2016 for Computer Science

University of Sargodha
BS 2nd Term Examination 2016
Subject: Computer Science
Paper: Object Oriented Programming (CMP:2124)

Time Allowed: 2:30 Hours
Maximum Marks: 60

Objective Part (Compulsory)

Q.1 Write short answers of the following questions in 2-3 lines only. (12*2)
i. What is destructor? How many parameters are passed to a destructor?
ii. What is difference b/w public and protected access specifier?
iii. What is diamond problem? Where this problem is encountered in inheritance?
iv. What are two categories of Inheritance?
v. What do you mean operator overloading? Can we introduce a new operator by using operator overloading?
vi. What is difference b/w try block & catch block?
vii. Write the syntax of header of a function of a class where + operator is overloaded to add two objects and one object is returned?
viii. What is difference between function overloading and function overriding?
ix. What do you know about Encapsulation?
x. Is new operator necessary to create object? When new operator is used?
xi. What are the necessary conditions for polymorphism?
xii. How static data member is different from non-static data member?

Subjective Part (12*3)

Q.2 Write a class Runner that contains the following data members:

  • The name of runner
  • The distance covered by runner

The class has the following member functions:

  • Get function to input runner name and distance
  • Show function to display runner name and distance
  • Create two objects of the class and also display the name of the Runner who has covered longer distance.

Q.3 Write classes Array that contain the array of integers as data member. The class contain the member functions:

  • A constructor that initializes the array element to -1.
  • Input function to input the values in the array.
  • Show function to display the values of array.
  • A member function to make the values of the array two times.

Q.4 Write a program that declares a Shape class as abstract class. There are two classes Square (all sides are equal) and Right Triangle being derived from Shape class. Write suitable program to calculate the area and perimeter of the relevant shapes (i.e. Right Triangle and Square). Formulas used to calculate the area and perimeter are as follows:
Area of Square = (side)^2
Perimeter of Square = 4 * side
Area of Right Triangle = ½ * base * height
Perimeter of Right Triangle = side1 + side2 + side3

Note: use the principle of polymorphism in Q #4.

Q.5 Create a Distance class which has two data members feet and inches of type int and float respectively. Write the appropriate constructor and other member functions in order to add the corresponding values of two objects and storing the result in d3 by using expression d3.add(d1, d2) in the main function where d1, d2, d3 are the objects of Distance class.

Q.6 Create a Car class that includes name as its data member. Create another class Toyota that is derived from Car class and includes his Model # (int type) and colour (string type) as his data member. Write appropriate constructors (multiple constructors) and display method to create and display objects of Toyota class.

Short questions solved (OOP 2016)

  1. Destructor: a special member function (~ClassName) called automatically when an object is destroyed, to release resources. It takes no parameters and has no return type, so it cannot be overloaded.
  2. public vs protected: public members are accessible from anywhere; protected members are accessible inside the class and its derived classes, but not from outside code.
  3. Diamond problem: occurs in multiple inheritance when class D inherits from B and C, which both inherit from A – D gets two copies of A’s members, causing ambiguity. Solved with virtual inheritance (class B : virtual public A).
  4. Two categories of inheritance: single inheritance (one base class) and multiple inheritance (more than one base class). (Other forms – multilevel, hierarchical, hybrid – combine these.)
  5. Operator overloading: giving an existing operator a new meaning for user-defined types (e.g. + for two Distance objects). You cannot create new operators, and some (::, ., .*, ?:, sizeof) cannot be overloaded.
  6. try vs catch: the try block contains code that may throw an exception; the catch block that follows receives and handles the thrown exception.
  7. Header for overloaded +: ClassName operator+(const ClassName& obj) – e.g. Distance operator+(const Distance& d).
  8. Overloading vs overriding: overloading = same function name with different parameter lists in the same scope (compile-time). Overriding = a derived class redefines a base-class virtual function with the same signature (run-time polymorphism).
  9. Encapsulation: wrapping data and the functions that operate on it into one unit (a class), and hiding the data using private members accessed through public functions.
  10. Is new necessary? No – objects can be created on the stack (Student s;). new is used to create objects dynamically on the heap at run time (Student* p = new Student;), which must be released with delete.
  11. Conditions for (run-time) polymorphism: inheritance, a virtual function in the base class overridden in derived classes, and calling it through a base-class pointer or reference.
  12. Static vs non-static data member: a static member has one shared copy for all objects (defined once outside the class); each object has its own copy of a non-static member.

Solved programs

Q.2 Runner

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

class Runner {
    string name;
    float distance;
public:
    void get() {
        cout << "Enter name: ";     getline(cin >> ws, name);
        cout << "Enter distance: "; cin >> distance;
    }
    void show() const { cout << name << " covered " << distance << " km\n"; }
    float getDistance() const { return distance; }
    string getName() const { return name; }
};

int main() {
    Runner r1, r2;
    r1.get(); r2.get();
    r1.show(); r2.show();
    if (r1.getDistance() > r2.getDistance())
        cout << r1.getName() << " covered the longer distance\n";
    else if (r2.getDistance() > r1.getDistance())
        cout << r2.getName() << " covered the longer distance\n";
    else
        cout << "Both covered the same distance\n";
}

Q.3 Array

#include <iostream>
using namespace std;

class Array {
    static const int SIZE = 10;
    int arr[SIZE];
public:
    Array() { for (int i = 0; i < SIZE; i++) arr[i] = -1; }   // initialise to -1
    void input() { 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 doubleValues() { for (int i = 0; i < SIZE; i++) arr[i] *= 2; }
};

int main() {
    Array a;
    a.show();          // all -1
    a.input();
    a.doubleValues();
    a.show();
}

Q.4 Abstract Shape with polymorphism

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

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

class Square : public Shape {
    double side;
public:
    Square(double s) : side(s) {}
    double area() const override { return side * side; }
    double perimeter() const override { return 4 * side; }
};

class RightTriangle : public Shape {
    double base, height;
public:
    RightTriangle(double b, double h) : base(b), height(h) {}
    double area() const override { return 0.5 * base * height; }
    double perimeter() const override {
        double hyp = sqrt(base * base + height * height);   // third side
        return base + height + hyp;
    }
};

int main() {
    Shape* shapes[2] = { new Square(4), new RightTriangle(3, 4) };
    for (Shape* s : shapes) {          // polymorphic calls
        cout << "Area: " << s->area() << ", Perimeter: " << s->perimeter() << endl;
        delete s;
    }
}

Output: Square 16 / 16; Right triangle 6 / 12.

Q.5 Distance with d3.add(d1, d2)

#include <iostream>
using namespace std;

class Distance {
    int feet;
    float inches;
public:
    Distance() : feet(0), inches(0) {}
    Distance(int f, float i) : feet(f), inches(i) {}
    void add(const Distance& d1, const Distance& d2) {
        feet = d1.feet + d2.feet;
        inches = d1.inches + d2.inches;
        if (inches >= 12) { feet += int(inches / 12); inches -= 12 * int(inches / 12); }
    }
    void show() const { cout << feet << "' " << inches << "\"" << endl; }
};

int main() {
    Distance d1(5, 9.5f), d2(3, 4.0f), d3;
    d3.add(d1, d2);     // 9' 1.5"
    d3.show();
}

Q.6 Car and Toyota

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

class Car {
protected:
    string name;
public:
    Car() : name("Unknown") {}
    Car(string n) : name(n) {}
};

class Toyota : public Car {
    int model;
    string colour;
public:
    Toyota() : Car("Toyota"), model(0), colour("None") {}
    Toyota(string n, int m) : Car(n), model(m), colour("White") {}
    Toyota(string n, int m, string c) : Car(n), model(m), colour(c) {}
    void display() const {
        cout << "Name: " << name << ", Model #: " << model << ", Colour: " << colour << endl;
    }
};

int main() {
    Toyota t1;
    Toyota t2("Corolla", 2016);
    Toyota t3("Camry", 2018, "Black");
    t1.display(); t2.display(); t3.display();
}

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.