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
Answer
int sum = 0;
for (int x = 0; x <= 5; x++) {
sum += x;
}
System.out.println("The sum is: " + sum);
- Values added are 0, 1, 2, 3, 4, 5.
- 0+1+2+3+4+5 = 15.
Question 2
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
Answer
- Graphical desktop applications.
- Business/accounting systems.
- Games and simulations.
- Mobile applications and reusable service libraries.
Question 4
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
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
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
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
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
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
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
Answer
- Explain why a non-obvious algorithm or decision is necessary.
- Document a method’s purpose, expected inputs or important constraints.
Question 12
Answer
- Reuses accessible superclass behaviour rather than duplicating it.
- Supports extension and substitutability through overridden methods and polymorphic references.
Question 13
Answer
- Source-code editor.
- Projects/files explorer.
- Debugger and output/build console.
Question 14
Answer
- Planning/feasibility: define scope, cost, resources and viability.
- Requirements analysis: document users, workflows, records, access and reporting needs.
- Design: specify data structures, interfaces, components and controls.
- Implementation: write/configure the system.
- Testing: verify functions, integration, security and acceptance criteria.
- Deployment: migrate validated data, train users and release the system.
- 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
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
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
Answer

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);
}
}
- For 100°C, 9.0/5.0 = 1.8.
- 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.