Level 6 — Programming — Jul/Aug 2025 — Written
Level 6 • July/August 2025 • 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
final String NAME = "Mary";
final int AGE = 25;
Question 2
Answer
- Compiler: javac translates source into JVM bytecode.
- Interpreter: the JVM can interpret bytecode instructions.
- Just-in-time (JIT) compiler: converts frequently executed bytecode to native machine code. An assembler is a general translator category but is not normally part of a Java programmer’s source-to-bytecode workflow.
Question 3
Answer
- Begin with a valid Java identifier-start character, such as a letter or underscore; do not begin with a digit.
- Use valid identifier characters thereafter; no spaces or punctuation such as a hyphen.
- Do not use a keyword or reserved literal as an identifier. A lone underscore is reserved in modern Java.
- Java identifiers are case-sensitive; use a consistent readable convention such as camelCase.
Question 4
Answer
- Use System.out.println(“Area”); — System begins with uppercase S and out is lowercase.
- The application entry method is public static void main(String[] args), with lowercase main.
- Backslash is not Java’s division operator, and an expression such as x/2 cannot be assigned a value. If the intended variable name was x2, a valid declaration is int x2 = 5;.
- while is a keyword; use a valid identifier such as int count;.
Question 5
Answer
Save as GradeChoice.java.
import java.util.Scanner;
public class GradeChoice {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter 1, 2 or 3: ");
int number = in.nextInt();
switch (number) {
case 1: System.out.println("Distinction"); break;
case 2: System.out.println("Credit"); break;
case 3: System.out.println("Pass"); break;
default: System.out.println("Invalid selection");
}
}
}
Question 6
Answer
// Single-line explanation
/* A comment
across several lines. */
Question 7
Answer
| Expression | Result |
|---|---|
| a < b | true |
| a >= b | false |
| a != b | true |
| a == b | false |
The fourth expression is a == b in the original scan; the OCR text dropped part of it.
Question 8
Answer
public class vehicle {
public void parts() {
System.out.println("Engine, wheels and brakes");
}
}
// From another method:
// vehicle v = new vehicle();
// v.parts();
Question 9
Answer
if (average >= 80 && average <= 100) {
System.out.println("Excellent");
} else if (average >= 60 && average < 80) {
System.out.println("Good");
}
For fractional averages, the lower category uses [60,80); for integer marks this is exactly 60–79. Validate the overall 0–100 range before classifying.
Question 10
Answer
int[] marks = {45, 56, 67, 100};
for (int mark : marks) {
System.out.println(mark);
}
Question 11
Answer
Save as DayDemo.java.
public class DayDemo {
enum day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
public static void main(String[] args) {
for (day d : day.values()) System.out.println(d);
}
}
Question 12
Answer
- Source code: human-readable program text.
- Object code: translated output, often intermediate/native code requiring a runtime or linking. For Java, .class files contain JVM bytecode.
- Machine code: binary instructions for a specific CPU architecture.
String word = " Java ";
System.out.println(word.length()); // 8
System.out.println(word.trim()); // Java
System.out.println(word.substring(2, 6)); // Java
System.out.println(word.toUpperCase()); // two spaces + JAVA + two spaces
The Java method name is toUpperCase(), correcting the paper’s “toppercase” spelling.
Save as Calculate.java.
class goods {
protected double price;
protected int quantity;
goods(double price, int quantity) {
this.price = price; this.quantity = quantity;
}
}
public class Calculate extends goods {
Calculate(double price, int quantity) { super(price, quantity); }
double getCost() { return price * quantity; }
public static void main(String[] args) {
Calculate item = new Calculate(100, 10);
System.out.printf("Cost = KSh %.2f%n", item.getCost());
}
}
- Cost = 100×10 = KSh 1000.00.
Question 13
Answer
switch (Character.toUpperCase(color)) {
case 'R': System.out.println("Red"); break;
case 'B': System.out.println("Blue"); break;
case 'G': System.out.println("Green"); break;
default: System.out.println("Unknown colour");
}
Save as SquareRoots.java.
public class SquareRoots {
public static void main(String[] args) {
System.out.println("Number\tSquare root");
for (int i = 1; i <= 10; i++) {
System.out.printf("%d\t%.6f%n", i, Math.sqrt(i));
}
}
}
- Unary uses one operand: ++x, -x or !ready.
- Binary uses two operands: x+y, a<b or left&&right.
Question 14
Answer
The design is pseudocode: a language-independent textual algorithm.
Save as AddDesign.java.
public class AddDesign {
public static void main(String[] args) {
int a = 45, b = 55;
int c = a + b;
System.out.println(c);
}
}
Output: 100. The original paper’s starting principal is KSh 1000; OCR incorrectly read 1600.
Save as CompoundInterest.java.
public class CompoundInterest {
public static void main(String[] args) {
double principal = 1000, rate = 10;
int year = 1;
System.out.println("Year\tPrincipal\tInterest\tAmount");
do {
double interest = principal * rate / 100;
double amount = principal + interest;
System.out.printf("%d\t%.2f\t\t%.2f\t\t%.2f%n",
year, principal, interest, amount);
principal = amount;
year++;
} while (year <= 4);
}
}
- Year1: interest = 1000×0.10 = 100; amount = 1100.
- Year2: interest = 1100×0.10 = 110; amount = 1210.
- Year3: interest = 1210×0.10 = 121; amount = 1331.
- Year4: interest = 1331×0.10 = 133.10; amount = KSh 1464.10.
class Learner { String name; }
// Class: Learner is a template.
// Inside a method:
Learner mary = new Learner(); // mary refers to a created object
Question 15
Answer
class payroll {
String name;
double salary;
payroll(String name, double salary) {
this.name = name;
this.salary = salary;
}
}
// Inside a method:
payroll employee = new payroll("Johnson", 25000);
static int add(int a, int b, int c) { return a + b + c; }
static double add(double a, double b) { return a + b; }
static int add(int a, int b) { return a + b; }
Save as OddEven.java.
import java.util.Scanner;
public class OddEven {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Integer: ");
int number = in.nextInt();
System.out.println(number % 2 == 0 ? "Even" : "Odd");
}
}