Java Comprehensive Test
Answer all questions. The test is out of 100 marks. This paper tests your understanding of Java
syntax, control structures, number system conversions, pseudocode, and algorithmic thinking.
Think critically and provide explanations where required.
Section A – Basic Java Syntax (15 marks)
1. Write a Java program that prints the following pattern using nested loops:
*
**
***
****
*****
(5 marks)
2. Identify and correct the errors in the following code snippet:
public class Main {
public static void main(String[] args) {
int num = "10";
if num > 5 {
[Link]("Greater!");
}
}
}
(5 marks)
3. Explain the difference between compile-time and run-time errors with examples in Java. (5
marks)
Section B – Decision Making (20 marks)
4. A company gives discounts based on the total bill:
- If bill < 500, no discount
- If 500 ≤ bill < 1000, 10% discount
- If bill ≥ 1000, 20% discount
Write a Java program using if-else-if to calculate the final bill. (8 marks)
5. Rewrite the above program using a switch-case structure, assuming the bill amount can only be
categorized as LOW, MEDIUM, or HIGH. (7 marks)
6. Discuss a real-world situation where a switch-case would be better than multiple if-else
statements. Provide justification. (5 marks)
Section C – Loops (20 marks)
7. Write a Java program using a while loop to reverse a number entered by the user. (6 marks)
8. Using a do-while loop, simulate a simple ATM PIN verification system where the user gets a
maximum of 3 attempts. (7 marks)
9. A case study: A teacher wants to calculate the average marks of her class of 30 students.
Write pseudocode and then implement it in Java using a for loop. (7 marks)
Section D – Number System Conversion (15 marks)
10. Convert the following manually (show steps):
a) 101101 (binary) → decimal (3 marks)
b) 745 (octal) → binary (3 marks)
c) 1A3 (hexadecimal) → decimal (3 marks)
11. Write a Java program that takes a decimal number as input and prints its binary, octal, and
hexadecimal equivalents. (6 marks)
Section E – Algorithmic Thinking & Pseudocode (30 marks)
12. Case Study: Hospital Appointment System
A hospital wants a program to schedule appointments. Patients can be categorized as
"Emergency", "Follow-up", or "General".
- Emergency patients should always be given priority.
- Follow-up patients should be served before General patients.
- General patients are served last.
(a) Write pseudocode for this system. (10 marks)
(b) Write a Java program implementing the logic using if-else-if. (10 marks)
(c) Explain how you would modify this system if a new category "VIP" patients is introduced, who
should be served right after Emergency. (10 marks)
Total: 100 marks