Blog
OOP CMP-2123 BSIT BSSE Past Paper – UOS Main Campus
ProgramBSIT
Course codeCMP-2123
CampusUOS Main
AnswersSolved ✓
University of Sargodha main campus Object Oriented Programming (CMP-2123) paper for BSIT (Reg + SS) and BSSE (Reg). Retyped from the original paper, with solved answers below.
Paper set byMr. Muhammad IqbalView official UOS faculty profile →
Solved answers
Q1 – Output of the program (10)
6 M 4.5 1 C 0.2 2 A 0.4 2.4
Trace (a and b are static, so all objects share them; ch is per object):
ob1 = new MyClass(3,'A',0.5): a = 2 + 3 = 5, b = 1.5 + 0.5 = 2.0, ob1.ch = ‘A’.ob2 = new MyClass(1,'M',2.5): a = 5 + 1 = 6, b = 2.0 + 2.5 = 4.5, ob2.ch = ‘M’.ob2.display()prints 6 M 4.5.ob3 = new MyClass(): a = 1, b = 0.2, ob3.ch = ‘C’.ob3.display()prints 1 C 0.2.ob1.add(ob2): temp = new MyClass() resets a = 1, b = 0.2; ob2.ch = ob1.ch = ‘A’; a = 1 + 1 = 2; b = 0.2 + 0.2 = 0.4; returns this (ob1), so ob3 now refers to ob1.ob1.display()prints 2 A 0.4;ob3.getB()returns a + b = 2 + 0.4 = 2.4.
We compiled and ran this program to confirm the output.
Q2 – Student → Scholar → SalariedScholar (10)
class Student {
protected String name;
public Student() { name = "Unknown"; }
public Student(String name) { this.name = name; }
public void display() { System.out.println("Name: " + name); }
}
class Scholar extends Student {
protected String thesisTitle;
public Scholar() { super(); thesisTitle = "Not assigned"; }
public Scholar(String name, String thesisTitle) { super(name); this.thesisTitle = thesisTitle; }
@Override
public void display() { super.display(); System.out.println("Thesis: " + thesisTitle); }
}
class SalariedScholar extends Scholar {
private double salary;
public SalariedScholar() { super(); salary = 0.0; }
public SalariedScholar(String name, String thesisTitle, double salary) {
super(name, thesisTitle);
this.salary = salary;
}
public void setSalary(double salary) { this.salary = salary; }
public double getSalary() { return salary; }
@Override
public void display() { super.display(); System.out.println("Salary: " + salary); }
}
public class TestScholar {
public static void main(String[] args) {
SalariedScholar s1 = new SalariedScholar("Ali", "Machine Learning in Agriculture", 45000);
SalariedScholar s2 = new SalariedScholar();
s2.setSalary(30000);
s1.display();
System.out.println();
s2.display();
}
}Output:
Name: Ali Thesis: Machine Learning in Agriculture Salary: 45000.0 Name: Unknown Thesis: Not assigned Salary: 30000.0
Each class has a default and a parameterized constructor (multiple constructors). super(...) passes values up the inheritance chain, and each display() overrides the parent’s and calls super.display() first.
Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
