OOP - Object Oriented Programming, Past Papers, UOS Main Campus

OOP BSSE Mid Past Paper – UOS Main Campus

ProgramBSSE
ExamMid Term
CampusUOS Main
AnswersSolved ✓

University of Sargodha main campus Object Oriented Programming mid-term exam for BSSE (Reg and SS), two questions of 10 marks each, 1 hour. Retyped from the original paper, with solved answers below.

Paper set bySir LaliName as written on the original paper

Solved answers

Q1 – Definitions (10)

  • Class: a blueprint or template that defines the attributes (fields) and behaviour (methods) of a type of object. Example: class Car { String color; void drive() {} }.
  • Object: an instance of a class created at run time with new; it has its own state, behaviour and identity. Example: Car c = new Car();.
  • Constructor method: a special method with the same name as the class and no return type, called automatically when an object is created, used to initialize fields. It can be overloaded (default and parameterized).
  • Access modifiers: keywords that control visibility — private (same class only), default/package (same package), protected (package + subclasses), public (everywhere).
  • Encapsulation: wrapping data and the methods that work on it into one unit (class) and hiding the data with private, giving access only through public getter/setter methods.

Q2 – Program showing new, this, static, constructors, encapsulation and information hiding (10)

class BankAccount {
    private String owner;                  // encapsulation / information hiding
    private double balance;
    private static int totalAccounts = 0;  // static: one copy shared by all objects

    public BankAccount(String owner, double balance) {   // constructor
        this.owner = owner;                // this: the current object
        this.balance = balance;
        totalAccounts++;
    }
    public BankAccount(String owner) {     // overloaded constructor
        this(owner, 0.0);                  // this(...) calls the other constructor
    }
    public void deposit(double amount) {
        if (amount > 0) balance += amount; // data changed only through methods
    }
    public double getBalance() { return balance; }
    public String getOwner()   { return owner; }
    public static int getTotalAccounts() { return totalAccounts; }
}

public class BankAccountTest {             // separate test class
    public static void main(String[] args) {
        BankAccount a = new BankAccount("Sara", 5000);  // new creates an object
        BankAccount b = new BankAccount("Usman");
        a.deposit(1500);
        b.deposit(2000);
        System.out.println(a.getOwner() + ": " + a.getBalance());
        System.out.println(b.getOwner() + ": " + b.getBalance());
        System.out.println("Accounts: " + BankAccount.getTotalAccounts());
    }
}

Output:

Sara: 6500.0
Usman: 2000.0
Accounts: 2

Where each concept is used: new — creating a and b; this — this.owner = owner and this(owner, 0.0); static — totalAccounts and getTotalAccounts(); constructors — the two overloaded BankAccount constructors; encapsulation and information hiding — private fields changed only through public methods such as deposit(). (In the exam, underline these lines as the question asks.) We compiled and ran the program to confirm the output.

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

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.