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

Declare and print the temperature array28,32,30,35.

Answer

int[] temperature = {28, 32, 30, 35};
for (int value : temperature) System.out.println(value);

Question 2

Print High for90–100 and Moderate for70–89.

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

Show two Java comment forms.

Answer

// Single-line explanation
/* A comment
   across several lines. */

Question 4

For m=25,n=30, evaluate m>n, m<=n, m==n, m!=n.

Answer

Expression Result
m > n false
m <= n true
m == n false
m != n true

Question 5

Declare constants country Kenya and population54000000.

Answer

final String COUNTRY = "Kenya";
final int POPULATION = 54_000_000;

Question 6

State four Java naming rules.

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

Name three Java compilation/execution translator types.

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

Debug system.out.Println, Public static Void main, y=2**3 and int class.

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

Write switch code for1→Excellent,2→Very Good,3→Average.

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

Declare Library with a books() method accessible from another class.

Answer

public class Library {
    public void books() {
        System.out.println("Available books");
    }
}
// In another method:
// new Library().books();

Question 11

Create Season enum and print Spring,Summer,Autumn,Winter.

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

Describe source, bytecode and machine code; demonstrate string methods on ” OpenAI Java “; implement the Items/Calculator cost scenario.

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

Map O/o,G/g,L/l,A/a to fruit; print1–5 with squares; distinguish unary/binary.

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

Identify and implement the m=200,n=100 addition tool; use do…while for3 years at7% from3000; distinguish class/object.

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);
    }
}
Step-by-step calculation

  1. Year1: 3000×0.07 =210; amount3210.
  2. Year2: 3210×0.07 =224.70; amount3434.70.
  3. Year3: 3434.70×0.07 =240.429; amount3675.129 ≈ KSh3675.13.
  4. 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

Construct Employee Martin/40000 using this, overload calculate and write an odd/even program.

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");
    }
}