Kyūkei Master Hub
SCS2108 | Phase Study Card
👁️ -- opens | 0%

Phase 16: Wednesday Test Sprint (Part 1 - Questions 1 to 4)

Master the exact 4 questions from Part 1 of your Wednesday test paper: Jagged Arrays, Interface Contracts, Explicit Constructors with Setters, and Single-Table Superclass-Subclass Inheritance.

Question 1: Jagged 2D Array Initialization & Display

Write a Java program segment to initialize and display a 2D array called data with three rows that have the following data elements: row 1 {2, 3, 7}, row 2 {7}, and row 3 {9, 6}.

Compilable Model Code:

public class JaggedArrayDemo { public static void main(String[] args) { // 1. Initialize Jagged 2D Array with varying row lengths int[][] data = { {2, 3, 7}, // Row 0 (length 3) {7}, // Row 1 (length 1) {9, 6} // Row 2 (length 2) }; // 2. Display elements using nested loops System.out.println("--- Jagged 2D Array Elements ---"); for (int i = 0; i < data.length; i++) { System.out.print("Row " + (i + 1) + ": "); for (int j = 0; j < data[i].length; j++) { System.out.print(data[i][j] + " "); } System.out.println(); // Newline after each row } } }
💡 Distinction Tip: Why data[i].length is Mandatory
In Java, 2D arrays are actually arrays of arrays on the heap. A Jagged (or Ragged) array is a 2D array where each row array has a different length! You MUST use data[i].length inside the inner loop (rather than hardcoding a fixed size like 3). Hardcoding data[0].length for all rows causes an ArrayIndexOutOfBoundsException when reading row 2!

Question 2: University Student Interface Mechanism

Write a program to display the name, programme, year and registration number of a University student using interface mechanism.

Compilable Model Code:

// 1. Define the Interface contract interface StudentInterface { void displayStudentDetails(); // Abstract method (public & abstract by default) } // 2. Class implementing the Interface contract class UniversityStudent implements StudentInterface { private String name; private String programme; private int year; private String regNumber; // Constructor to initialize state public UniversityStudent(String name, String programme, int year, String regNumber) { this.name = name; this.programme = programme; this.year = year; this.regNumber = regNumber; } // 3. Override interface method with public visibility! @Override public void displayStudentDetails() { System.out.println("========== University Student Details =========="); System.out.println("Name: " + name); System.out.println("Programme: " + programme); System.out.println("Year of Study: " + year); System.out.println("Registration Number: " + regNumber); System.out.println("================================================"); } } // 4. Test execution main program public class UniversityStudentTest { public static void main(String[] args) { // Polymorphic reference via interface variable! StudentInterface student = new UniversityStudent( "Tinashe Moyo", "BSc Computer Science (SCS)", 2, "N02219884B" ); student.displayStudentDetails(); } }
💡 Examiner Distinction Tip: Interface Access Modifier Rule
When implementing an interface method, the overriding method in the class MUST be marked `public`! By default, interface methods are implicitly public abstract. If you forget to write public in UniversityStudent, Java assumes package-private visibility, which reduces method visibility and causes a COMPILE ERROR!

Question 3: Employee Class with Explicit Constructor & Setters

Create an Employee class with four instance variables name, age, designation and salary. The class has one explicitly defined constructor, which takes a parameter. The instance variable name is assigned in the constructor. Add methods to assign all the other instance variables of the class. Add a method to print Employee details. Create two objects and test the program.

Compilable Model Code:

class Employee { // 1. Instance variables private String name; private int age; private String designation; private double salary; // 2. Explicitly defined constructor taking 1 parameter (name) public Employee(String name) { this.name = name; } // 3. Setter methods to assign the other instance variables public void setAge(int age) { this.age = age; } public void setDesignation(String designation) { this.designation = designation; } public void setSalary(double salary) { this.salary = salary; } // 4. Method to print Employee details public void printEmployeeDetails() { System.out.println("Employee Name: " + name); System.out.println("Age: " + age); System.out.println("Designation: " + designation); System.out.println("Salary: $" + salary); System.out.println("------------------------------------"); } } public class EmployeeTest { public static void main(String[] args) { // Object 1 Instantiation & Setters Employee emp1 = new Employee("Chipo Ndlovu"); emp1.setAge(26); emp1.setDesignation("Software Developer"); emp1.setSalary(85000.0); // Object 2 Instantiation & Setters Employee emp2 = new Employee("Farai Dube"); emp2.setAge(34); emp2.setDesignation("Database Administrator"); emp2.setSalary(98000.0); // Test display System.out.println("--- Employee Record 1 ---"); emp1.printEmployeeDetails(); System.out.println("--- Employee Record 2 ---"); emp2.printEmployeeDetails(); } }
💡 Examiner Distinction Tip: Default Constructor Removal
Because we defined an explicit constructor public Employee(String name), Java automatically deleted the default no-arg constructor. If you attempt Employee emp = new Employee();, compiler throws an error! You MUST pass the `name` parameter upon instantiation.

Question 4: Inheritance (Employee Superclass ➔ Manager Subclass)

Write a Java program that demonstrates inheritance.
Requirements:
(i) Create a superclass named Employee with attributes: employeeID, name, salary.
(ii) Include a constructor and a method displayEmployee().
(iii) Create a subclass named Manager that extends Employee.
(iv) Add an additional attribute department.
(v) Include a method displayManager() that displays all employee details including the department.
(vi) Create an object of Manager and display its information.

Compilable Model Code:

// (i) Superclass Employee class Employee { protected String employeeID; protected String name; protected double salary; // (ii) Superclass Constructor public Employee(String employeeID, String name, double salary) { this.employeeID = employeeID; this.name = name; this.salary = salary; } // (ii) Method to display employee details public void displayEmployee() { System.out.println("Employee ID: " + employeeID); System.out.println("Name: " + name); System.out.println("Salary: $" + salary); } } // (iii) Subclass Manager extending Employee class Manager extends Employee { // (iv) Additional attribute private String department; // Subclass Constructor using super(...) public Manager(String employeeID, String name, double salary, String department) { super(employeeID, name, salary); // Invokes Employee constructor (MUST be 1st line!) this.department = department; } // (v) Method displayManager() displaying all details public void displayManager() { System.out.println("========== Manager Profile =========="); displayEmployee(); // Calls superclass method to print ID, Name, Salary System.out.println("Department: " + department); System.out.println("====================================="); } } public class InheritanceDemo { public static void main(String[] args) { // (vi) Create object of Manager and display information Manager mgr = new Manager("MGR-2026-09", "Dr. Tendai Khumalo", 135000.0, "Engineering & ICT"); mgr.displayManager(); } }
💡 Examiner Distinction Tip: `super(...)` Placement Rule
In the `Manager` constructor, `super(employeeID, name, salary)` MUST be the very first statement inside the constructor block! If you try to execute `this.department = department;` before calling `super(...)`, the compiler will refuse to compile the program.