Level 6 — Programming — Jul/Aug 2025 — Practical
Level 6 • July/August 2025 • Practical 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
- Obtain a supported JDK and NetBeans, Eclipse or IntelliJ from the official provider.
- Install the JDK and the selected IDE using the supported installation instructions.
- Launch the IDE, select the installed JDK and create a Java project. Verify that a small program builds and runs.
The paper permits any of the listed IDEs; no particular current release is required by this revision example.
Question 2
Answer
Save as HelloJava.java.
public class HelloJava {
public static void main(String[] args) {
System.out.println("Working in the Java environment");
}
}
- Save the file under the project’s source directory using the public class name.
- Compile/build it in the IDE.
- Run it and verify the line in the output console.
Question 3
Answer
Save as person.java.
public class person {
public static void main(String[] args) {
String name = "Omondi"; int age = 24; char gender = 'M';
double salary = 12344.56; boolean disable = false;
System.out.printf("Wira Company%nName: %s%nAge: %d%nGender: %c%nSalary: %.2f%nDisable: %b%n",
name, age, gender, salary, disable);
}
}
Save as person2.java.
public class person2 {
public static void main(String[] args) {
String name = "Omondi"; int age = 24; char gender = 'M';
double salary = 12344.56; boolean disable = false;
System.out.println("Wira Company\nName\tAge\tGender\tSalary\tDisable");
System.out.printf("%s\t%d\t%c\t%.2f\t%b%n", name, age, gender, salary, disable);
}
}
The initialisation table gives salary12344.56 but an example output prints12344.46. The programs consistently use the table’s12344.56; change the source value if the assessor specifies otherwise.
Question 4
Answer
Save as years.java.
import java.util.Scanner;
public class years {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Age: "); int age = in.nextInt();
if (age < 0) { System.out.println("Invalid age"); return; }
System.out.println(age >= 18 ? "Adult" : "Child");
}
}
Boundary tests:17→Child,18→Adult; negative age is rejected.
Question 5
Answer
Save as VowelChoice.java.
import java.util.Scanner;
public class VowelChoice {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("One letter: "); String input = in.nextLine().trim();
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
System.out.println("Enter one letter"); return;
}
switch (Character.toLowerCase(input.charAt(0))) {
case 'a': case 'e': case 'i': case 'o': case 'u':
System.out.println("Vowel"); break;
default: System.out.println("Not a vowel");
}
}
}
The printed table omits O/o. The program includes it to classify all five standard English vowel letters; to reproduce the table literally, remove the case for o and explicitly record that omission.
Question 6
Answer
Save as OddIntegers.java.
public class OddIntegers {
public static void main(String[] args) {
for (int number = 1; number <= 21; number += 2) {
System.out.println(number);
}
}
}
Output:1,3,5,7,9,11,13,15,17,19,21.
Question 7
Answer
Save as MarksWhile.java.
import java.util.Scanner;
public class MarksWhile {
static double mark(Scanner in, String prompt) {
while (true) {
System.out.print(prompt);
try {
double m = Double.parseDouble(in.nextLine());
if (Double.isFinite(m) && m >= 0 && m <= 100) return m;
} catch (NumberFormatException ex) { }
System.out.println("Enter 0-100");
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Capture a student? y/n: ");
String again = in.nextLine();
while (again.equalsIgnoreCase("y")) {
System.out.print("Name: "); String name = in.nextLine();
double kiswahili = mark(in, "Kiswahili: ");
double english = mark(in, "English: ");
System.out.printf("%s | Kiswahili %.2f | English %.2f | Average %.2f%n",
name, kiswahili, english, (kiswahili + english) / 2);
System.out.print("Another? y/n: "); again = in.nextLine();
}
}
}
Save as doWhile.java.
import java.util.Scanner;
public class doWhile {
static double mark(Scanner in, String prompt) {
while (true) {
System.out.print(prompt);
try {
double m = Double.parseDouble(in.nextLine());
if (Double.isFinite(m) && m >= 0 && m <= 100) return m;
} catch (NumberFormatException ex) { }
System.out.println("Enter 0-100");
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String again;
do {
System.out.print("Name: "); String name = in.nextLine();
double kiswahili = mark(in, "Kiswahili: ");
double english = mark(in, "English: ");
System.out.printf("%s | Kiswahili %.2f | English %.2f | Average %.2f%n",
name, kiswahili, english, (kiswahili + english) / 2);
System.out.print("Another? y/n: "); again = in.nextLine();
} while (again.equalsIgnoreCase("y"));
}
}
- For marks70 and80, total=150.
- Average=150/2=75.
The while version can capture zero students; do…while captures at least one. Both allow repeated entries until the user chooses to stop.
Question 8
Answer
Save as Cylinder.java.
import java.util.Scanner;
public class Cylinder {
double height, radius;
Cylinder(double height, double radius) { this.height = height; this.radius = radius; }
double getVolume() { return Math.PI * radius * radius * height; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Height and radius: ");
double height = in.nextDouble(), radius = in.nextDouble();
if (!Double.isFinite(height) || !Double.isFinite(radius) || height <= 0 || radius <= 0) {
System.out.println("Use positive finite dimensions"); return;
}
Cylinder cylinder = new Cylinder(height, radius);
System.out.printf("Volume = %.4f cubic units%n", cylinder.getVolume());
}
}
- Illustration r=2 and h=5.
- V=πr²h=π×4×5=20π≈62.8319 cubic units.
Question 9
Answer
Save as area.java.
import java.util.Scanner;
class Rectangle {
protected double length, width;
Rectangle(double length, double width) { this.length = length; this.width = width; }
}
public class area extends Rectangle {
area(double length, double width) { super(length, width); }
double getArea() { return length * width; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Length and width: ");
double length = in.nextDouble(), width = in.nextDouble();
if (!Double.isFinite(length) || !Double.isFinite(width) || length <= 0 || width <= 0) {
System.out.println("Use positive finite dimensions"); return;
}
area rectangle = new area(length, width);
System.out.printf("Area = %.2f square units%n", rectangle.getArea());
}
}
Illustration5×3 gives15 square units. The paper’s lowercase class name area is legal, though standard Java naming would normally use an initial capital.
Question 10
Answer
Save as BmiExercise.java.
public class BmiExercise {
public static void main(String[] args) {
String name = "Jonathan", residence = "Kabale", gender = "male";
String mobile = "0720111111"; int age = 36;
double weight = 56, height = 1.5;
double bmi = weight / (height * height);
String status;
if (bmi < 18.5) status = "Underweight";
else if (bmi < 25) status = "Healthy";
else if (bmi < 30) status = "Overweight";
else if (bmi < 40) status = "Obese";
else status = "Severe Obese";
System.out.printf("Name: %s%nAge: %d%nResidence: %s%nGender: %s%nMobile: %s%n",
name, age, residence, gender, mobile);
System.out.printf("Weight: %.2f kg%nHeight: %.2f m%nBMI: %.4f%nStatus: %s%n",
weight, height, bmi, status);
}
}
- Height squared=1.5×1.5=2.25m².
- BMI=56/2.25=24.8889.
- The exercise’s status for this sample is Healthy.
This is the paper’s fictional programming example. The printed bands overlap at18.5 and leave decimal gaps and exactly40 ambiguous. The code explicitly uses continuous thresholds18.5,25,30,40; it classifies the unrounded BMI, then formats it for display.
Question 11
Answer
Save as executeGoods.java.
class Goods {
int code, quantity; String itemName, category; double price;
Goods(int code, String itemName, String category, double price, int quantity) {
this.code = code; this.itemName = itemName; this.category = category;
this.price = price; this.quantity = quantity;
}
double getCost() { return price * quantity; }
void display() {
System.out.printf("Code: %d%nItem: %s%nCategory: %s%nPrice: %.2f%nQuantity: %d%nCost: %.2f%n",
code, itemName, category, price, quantity, getCost());
}
}
public class executeGoods {
public static void main(String[] args) {
Goods goods = new Goods(1001, "bicycle", "Not specified in the paper", 6500, 10);
System.out.printf("Calculated cost = %.2f%n", goods.getCost());
goods.display();
}
}
- Cost=price×quantity=6500×10=KSh65000.
The source leaves Category blank, so the example records it as unspecified rather than inventing a supplied value. Compile and run every requested program separately, then check its inputs, outputs and boundary cases.