OOP - Object Oriented Programming, Past Papers

Object Oriented Programming BSCS 2nd Term 2018 Past Paper UOS

OOP Past Paper 2018 UOS

University of Sargodha (UOS) Object Oriented Programming past paper 2018 for BS 2nd term, with the questions in text form and solved answers. All C++ programs are compiled and tested.

OOP Past Paper 2018 (text version)

University of Sargodha – Object Oriented Programming, Examination 2018 (BS 2nd term)
Some objective questions (5 and 9) and the paper header were missing from the copy we received.

Objective part – short answers

  1. What is a default constructor? How many parameters are used in a default constructor?
  2. What is the difference between private and protected access specifiers? Which one is the default access specifier in C++?
  3. Write the syntax of a pure virtual function named getdata() with return type int.
  4. Write the names of two types of inheritance supported by C++.
  5. What do you know about abstract classes and concrete classes?
  6. Write two differences between function overloading and function overriding.
  7. Write the names of two unary operators used in the context of pointers.
  8. What are the two necessary conditions for polymorphism?
  9. How is a static data member different from a non-static data member? Give an example of each.
  10. What is the function of a copy constructor? Write a statement that uses the copy constructor.

Subjective part

  • Q.2 Create a class Distance with two data members, feet (int) and inches (float). Write at least two constructors and a suitable method to compare two objects to find which has the greater distance. The result is displayed by main().
  • Q.3 Write a class named Array that contains an array of integers as a data member, with: a constructor that initializes all elements to −1; an input function; a show function to display all values; and a member function that makes the values two times.
  • Q.4 Write a program that declares a Value class with only one data member of int type. Write an appropriate constructor and other member functions to validate the statement val3 = val1 + val2; in main().
  • Q.6 Create a class Human with name (string) as its data member. Derive a class Teacher from Human with salary as a data member, and another class Student from Human with degree name as a data member. Implement the concept of polymorphism and display all values of the objects in main(). (Wording reconstructed from an incomplete copy.)

Solved answers – OOP 2018

Short questions

  1. A default constructor is a constructor that can be called with no arguments – either it has zero parameters or all its parameters have default values. It runs automatically when an object is created without arguments, e.g. Student s;
  2. private members are accessible only inside the class (and its friends); protected members are also accessible in derived classes. The default access specifier for class members is private (for struct it is public).
  3. virtual int getdata() = 0;
  4. Any two of: single, multiple, multilevel, hierarchical and hybrid inheritance.
  5. An abstract class has at least one pure virtual function, cannot be instantiated and is used as a base class (e.g. Shape). A concrete class has no pure virtual functions (it implements all of them), so objects can be created (e.g. Circle).
  6. Overloading: same name, different parameters, usually in the same class, resolved at compile time. Overriding: same name and same parameters in base and derived classes, needs inheritance (and virtual for run-time polymorphism), resolved at run time.
  7. & (address-of) and * (dereference / indirection).
  8. (1) An inheritance relationship (base and derived classes) and (2) a virtual function in the base class that is overridden and called through a base-class pointer or reference.
  9. A static data member has a single copy shared by all objects and is defined outside the class; a non-static member has a separate copy in each object.
    class BSCS {
        static int count;     // one copy shared by all objects
        int marks;            // each object has its own copy
    };
    int BSCS::count = 0;      // static member defined outside the class
  10. A copy constructor initializes a new object as a copy of an existing object of the same class; signature Student(const Student& s).
    Student s1("Ali", 3.5);
    Student s2 = s1;      // copy constructor called
    Student s3(s1);       // copy constructor called

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

Q.2 Distance class with compare()

#include <iostream>
using namespace std;

class Distance {
private:
    int feet;
    float inches;
    void normalize() {                 // keep inches below 12
        while (inches >= 12.0f) { inches -= 12.0f; feet++; }
    }
public:
    Distance() : feet(0), inches(0.0f) {}
    Distance(int ft, float in) : feet(ft), inches(in) { normalize(); }

    float totalInches() const { return feet * 12 + inches; }

    // returns 1 if this is greater, -1 if smaller, 0 if equal
    int compare(const Distance& d) const {
        if (totalInches() > d.totalInches()) return 1;
        if (totalInches() < d.totalInches()) return -1;
        return 0;
    }
    void show() const { cout << feet << "' " << inches << "\""; }
};

int main() {
    Distance d1(26, 12.3f), d2(5, 6.4f);
    d1.show(); cout << " and "; d2.show(); cout << endl;
    int r = d1.compare(d2);
    if (r > 0) cout << "d1 is greater" << endl;
    else if (r < 0) cout << "d2 is greater" << endl;
    else cout << "Both are equal" << endl;
    return 0;
}

Output: d1 = 26′ 12.3″ is normalized to 27′ 0.3″, so “d1 is greater”.

Q.3 Array class

#include <iostream>
using namespace std;

class Array {
private:
    static const int SIZE = 5;
    int arr[SIZE];
public:
    Array() {                                  // all elements = -1
        for (int i = 0; i < SIZE; i++) arr[i] = -1;
    }
    void input() {
        cout << "Enter " << SIZE << " integers: ";
        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() {                      // make values two times
        for (int i = 0; i < SIZE; i++) arr[i] *= 2;
    }
};

int main() {
    Array a1;
    a1.show();          // -1 -1 -1 -1 -1
    a1.input();
    a1.doubleValues();
    a1.show();
    return 0;
}

Sample: input 3 8 −2 10 5 → after doubling: 6 16 −4 20 10.

Q.4 Value class with overloaded +

#include <iostream>
using namespace std;

class Value {
private:
    int v;
public:
    Value() : v(0) {}
    Value(int x) : v(x) {}

    Value operator+(const Value& other) const {   // val3 = val1 + val2
        return Value(v + other.v);
    }
    void show() const { cout << v << endl; }
};

int main() {
    Value val1(26), val2(14), val3;
    val3 = val1 + val2;
    val3.show();       // 40
    return 0;
}

Output: 40.

Q.6 Human, Teacher and Student – polymorphism

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

class Human {
protected:
    string name;
public:
    Human(string n) : name(n) {}
    virtual void show() const = 0;        // pure virtual -> abstract base
    virtual ~Human() {}
};

class Teacher : public Human {
    double salary;
public:
    Teacher(string n, double s) : Human(n), salary(s) {}
    void show() const {
        cout << "Teacher: " << name << ", Salary: " << salary << endl;
    }
};

class Student : public Human {
    string degree;
public:
    Student(string n, string d) : Human(n), degree(d) {}
    void show() const {
        cout << "Student: " << name << ", Degree: " << degree << endl;
    }
};

int main() {
    Human* people[2];
    people[0] = new Teacher("Dr. Imran", 120000);
    people[1] = new Student("Sana", "BSCS");
    for (int i = 0; i < 2; i++) {
        people[i]->show();     // correct show() chosen at run time
        delete people[i];
    }
    return 0;
}

Output: Teacher: Dr. Imran, Salary: 120000 / Student: Sana, Degree: BSCS. Calling show() through a Human* pointer picks the right version at run time.

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.