Level 6 — Programming — Nov/Dec 2025 — Written
Level 6 • November/December 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
int[] temperature = {28, 32, 30, 35};
for (int value : temperature) System.out.println(value);
Question 2
Answer
if (score >= 90 && score <= 100) {
System.out.println("High");
} else if (score >= 70 && score < 90) {
System.out.println("Moderate");
}
This treats fractional scores in [70,90) as Moderate; for integer scores the upper limit is89. Validate scores before classification.
Question 3
Answer
// Single-line explanation
/* A comment
across several lines. */
Question 4
Answer
| Expression | Result |
|---|---|
| m > n | false |
| m <= n | true |
| m == n | false |
| m != n | true |
Question 5
Answer
final String COUNTRY = "Kenya";
final int POPULATION = 54_000_000;
Question 6
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 7
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 8
Answer
- System.out.println(“Done!”); uses the correct case.
- public static void main(String[] args) uses lowercase public and void; Void is a reference type and is not the required void entry-method return type.
- Java has no ** exponentiation operator; double y = Math.pow(2,3); computes8. The ^ operator is bitwise XOR, not exponentiation.
- class is a keyword; rename the variable, for example int classNumber;.
Question 9
Answer
Save as RatingChoice.java.
import java.util.Scanner;
public class RatingChoice {
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("Excellent"); break;
case 2: System.out.println("Very Good"); break;
case 3: System.out.println("Average"); break;
default: System.out.println("Invalid selection");
}
}
}
Question 10
Answer
public class Library {
public void books() {
System.out.println("Available books");
}
}
// In another method:
// new Library().books();
Question 11
Answer
Save as SeasonDemo.java.
public class SeasonDemo {
enum Season { Spring, Summer, Autumn, Winter }
public static void main(String[] args) {
for (Season season : Season.values()) System.out.println(season);
}
}
Question 12
Answer
- Human-readable source is Java program text in .java files.
- Intermediate bytecode in .class files targets the JVM rather than one hardware CPU.
- Native machine code is CPU-specific and can be generated by the JVM’s JIT compiler.
String word = " OpenAI Java ";
System.out.println(word.length()); // 13
System.out.println(word.trim()); // OpenAI Java
System.out.println(word.toUpperCase()); // " OPENAI JAVA "
System.out.println(word.substring(1, 7)); // OpenAI
Save as Output.java.
import java.util.Scanner;
class Items {
protected double price;
protected int quantity;
Items(double price, int quantity) {
this.price = price; this.quantity = quantity;
}
}
class Calculator extends Items {
Calculator(double price, int quantity) { super(price, quantity); }
double total() { return price * quantity; }
}
public class Output {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Price and quantity: ");
double price = in.nextDouble(); int quantity = in.nextInt();
if (price < 0 || quantity < 0) {
System.out.println("Values must be nonnegative"); return;
}
Calculator item = new Calculator(price, quantity);
System.out.printf("Total cost = %.2f%n", item.total());
}
}
Items supplies the fields, Calculator inherits them and computes the total, and Output is the driver. The source’s user/output arrows describe the data flow rather than multiple superclass inheritance.
Question 13
Answer
switch (Character.toUpperCase(character)) {
case 'O': System.out.println("Orange"); break;
case 'G': System.out.println("Grape"); break;
case 'L': System.out.println("Lemon"); break;
case 'A': System.out.println("Apple"); break;
default: System.out.println("Unknown fruit");
}
Save as Squares.java.
public class Squares {
public static void main(String[] args) {
for (int number = 1; number <= 5; number++) {
System.out.printf("%d\t%d%n", number, number * number);
}
}
}
Unary: ++number acts on one operand. Binary: number*number acts on two operands.
Question 14
Answer
The tool is pseudocode because it expresses the algorithm as structured text without Java-specific syntax.
Save as TotalDesign.java.
public class TotalDesign {
public static void main(String[] args) {
int m = 200, n = 100;
int total = m + n;
System.out.println(total);
}
}
Output:300. For the interest program below, assume annual compounding; the question does not explicitly state whether interest is simple or compound.
Save as InterestThreeYears.java.
public class InterestThreeYears {
public static void main(String[] args) {
double principal = 3000;
int year = 1;
System.out.println("Year\tInterest\tAmount");
do {
double interest = principal * 0.07;
principal += interest;
System.out.printf("%d\t%.2f\t\t%.2f%n", year, interest, principal);
year++;
} while (year <= 3);
}
}
- Year1: 3000×0.07 =210; amount3210.
- Year2: 3210×0.07 =224.70; amount3434.70.
- Year3: 3434.70×0.07 =240.429; amount3675.129 ≈ KSh3675.13.
- If simple interest were intended, total interest =3000×0.07×3 =630 and final amount =3630; do not mix this with compounding.
class Book { String title; }
// Inside a method:
Book copy = new Book(); // Book is the class; copy refers to an object.
Question 15
Answer
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name; this.salary = salary;
}
}
// Inside a method:
Employee martin = new Employee("Martin", 40000);
static int calculate(int a, int b) { return a + b; }
static double calculate(double a, double b) { return a + b; }
static int calculate(int a, int b, int c) { return a + b + c; }
Save as Parity.java.
import java.util.Scanner;
public class Parity {
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");
}
}