Level 6 — Programming — Jul/Aug 2024 — Written

Level 6 • July/August 2024 • Written assessment

Work through each question before checking the answer. These revision answers are prepared for learners; they are not an official marking scheme.

Question 1

Replace the while loop adding 0 through 5 with a for loop that prints the same sum.

Answer

int sum = 0;
for (int x = 0; x <= 5; x++) {
    sum += x;
}
System.out.println("The sum is: " + sum);
Step-by-step calculation

  1. Values added are 0, 1, 2, 3, 4, 5.
  2. 0+1+2+3+4+5 = 15.

Question 2

Explain two Java data-type categories.

Answer

  • Primitive types hold values such as int, double, boolean and char.
  • Reference types refer to objects, including classes such as String, arrays and interfaces. A reference can be null.

Question 3

Give four applications of OOP.

Answer

  • Graphical desktop applications.
  • Business/accounting systems.
  • Games and simulations.
  • Mobile applications and reusable service libraries.

Question 4

Distinguish source and machine code.

Answer

Source code is the programmer-readable text written in a language such as Java. Machine code is processor-specific binary instructions executed by the hardware. Java normally first compiles to JVM bytecode, which the JVM interprets or JIT-compiles.

Question 5

Give one difference between a class and an object.

Answer

class Student { String name; }
// Inside a method:
Student learner = new Student();

Student is a class defining structure/behaviour; learner refers to one object created from that class.

Question 6

Illustrate a base class and a derived class.

Answer

class Animal {
    void eat() { System.out.println("Eating"); }
}
class Dog extends Animal {
    void bark() { System.out.println("Barking"); }
}

Animal is the base/superclass. Dog is derived and inherits the accessible eat() method while adding bark().

Question 7

Distinguish polymorphism and abstraction.

Answer

  • Polymorphism allows a common interface/reference to invoke behaviour appropriate to an actual object, for example an overridden draw() method on different Shape subclasses.
  • Abstraction exposes necessary operations while hiding implementation details, for example a Shape interface declaring draw() without specifying how every shape is drawn.

Question 8

List three common Java standard-library packages.

Answer

  • java.lang — core classes such as String and Math; automatically imported.
  • java.util — collections, Scanner and other utilities.
  • java.io — stream/file I/O classes.

Question 9

Distinguish if from if…else.

Answer

A simple if executes its body only when its condition is true. if…else chooses one branch when true and the other when false.

Question 10

Give three Java variable types by scope/storage.

Answer

  • Local variables declared within a method/block.
  • Instance fields belonging to each object.
  • Static/class fields shared by instances of a class.

Question 11

Give two useful situations for comments.

Answer

  • Explain why a non-obvious algorithm or decision is necessary.
  • Document a method’s purpose, expected inputs or important constraints.

Question 12

Give two inheritance advantages.

Answer

  • Reuses accessible superclass behaviour rather than duplicating it.
  • Supports extension and substitutability through overridden methods and polymorphic references.

Question 13

Identify three NetBeans IDE components.

Answer

  • Source-code editor.
  • Projects/files explorer.
  • Debugger and output/build console.

Question 14

Explain seven system-development phases for the proposed constituency management system, then three Java features.

Answer

  1. Planning/feasibility: define scope, cost, resources and viability.
  2. Requirements analysis: document users, workflows, records, access and reporting needs.
  3. Design: specify data structures, interfaces, components and controls.
  4. Implementation: write/configure the system.
  5. Testing: verify functions, integration, security and acceptance criteria.
  6. Deployment: migrate validated data, train users and release the system.
  7. Maintenance: correct defects and improve the system as requirements change.
  • JVM bytecode supports portability across compatible platforms.
  • Object-oriented classes/interfaces support modular design and reuse.
  • Automatic memory management and exception handling assist reliable development; neither removes the need for testing.

Question 15

Write an addNumbers method and a program accepting two numbers. Explain three operator groups and two casting types.

Answer

Save as AddNumbers.java.

import java.util.Scanner;
public class AddNumbers {
    static double addNumbers(double a, double b) { return a + b; }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a and b: ");
        double a = in.nextDouble(), b = in.nextDouble();
        System.out.println("Sum = " + addNumbers(a, b));
    }
}
  • Arithmetic operators such as +, -, * and / calculate values.
  • Relational operators such as = and == compare values.
  • Logical operators such as &&, || and ! combine/invert boolean conditions.
int whole = 25;
double widened = whole;    // widening conversion
int narrowed = (int) 25.9; // explicit narrowing; result 25

Widening numeric conversion is generally implicit, though precision can be lost in some conversions. Narrowing needs a cast and can truncate or lose range/precision.

Question 16

Explain four access modifiers; correct CompareValues; give switch syntax.

Answer

  • public: accessible wherever the enclosing type/module permits access.
  • protected: accessible in the same package and in subclasses, subject to Java’s cross-package subclass rules.
  • Package-private (no access keyword): accessible within the same package.
  • private: accessible within the declaring class and its permitted nested/enclosing-class context.

Save as CompareValues.java.

public class CompareValues {
    public static void main(String[] args) {
        int a = 4;
        int b = 9;
        int min = (a < b) ? a : b;
        System.out.println(min);
    }
}

Fix the main method signature, statement semicolons, missing declaration/assignment of min and missing closing braces. Output: 4.

switch (choice) {
    case 1:
        System.out.println("First");
        break;
    case 2:
        System.out.println("Second");
        break;
    default:
        System.out.println("Other");
}

Question 17

Explain do…while with a flowchart; write Celsius-to-Fahrenheit code; explain four Java-supported inheritance forms.

Answer

A do...while loop executes its body before testing the condition; true repeats and false exits.
A do…while loop executes its body before testing the condition; true repeats and false exits.

Because the condition is tested after the body, a do…while executes at least once.

Save as Temperature.java.

import java.util.Scanner;
public class Temperature {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Celsius: ");
        double celsius = in.nextDouble();
        double fahrenheit = celsius * (9.0 / 5.0) + 32;
        System.out.printf("Fahrenheit = %.2f%n", fahrenheit);
    }
}
Step-by-step calculation

  1. For 100°C, 9.0/5.0 = 1.8.
  2. F = 100×1.8 + 32 = 212°F. Floating-point division avoids Java integer 9/5 = 1.
  • Single inheritance: one subclass extends one superclass.
  • Multilevel inheritance: a subclass extends a class that already extends another class.
  • Hierarchical inheritance: several subclasses extend one superclass.
  • Multiple interface inheritance/implementation: a class can implement several interfaces. Java does not support extending multiple classes.