1. Introduction
This roundup mirrors the pattern typically seen in college and university Java exams: a mix of short definitional questions, small output-prediction programs, and a couple of viva-style follow-ups that test whether you truly understand what you wrote.
Cricket analogy: Like a cricket coaching certification exam mixing rule definitions, predict-the-umpire's-decision scenarios, and an oral defense of your reasoning, this roundup mirrors college Java exams' definitional, output-prediction, and viva-style structure.
Use it as a revision sheet the week before an exam — work through each snippet by hand and predict the output before checking the answer, exactly as you would be asked to do in a written paper.
Cricket analogy: Use this like a batsman shadow-practicing against a bowling machine the week before a final, work through each code snippet by hand and predict the output before checking, just as you'd predict a delivery's line before it arrives.
2. Syntax
int a = 5, b = 2;
System.out.println(a / b); // integer division
System.out.println((double) a / b); // widened to doublefor (int i = 0; i < 3; i++) {
if (i == 1) continue;
System.out.print(i + " ");
}3. Explanation
University exams usually test three skill levels: recall (define a term like polymorphism or JVM), application (trace a loop or arithmetic expression by hand), and synthesis (write a short program from a specification). The trap questions almost always hinge on operator precedence, integer vs floating-point division, or loop boundary conditions.
Cricket analogy: Exams test recall, define powerplay; application, trace a required-run-rate calculation over by hand; and synthesis, design a batting order from a spec, with traps like forgetting a rain-affected over's boundary counts differently.
When answering a definition question, structure your response as: one-sentence definition, then a one-line code example, then a real-world analogy if time permits — this format consistently scores full marks on rubric-graded exams.
Cricket analogy: Structure a definition answer like a commentator's explainer: one crisp definition of yorker, one quick demonstration ball, then a real-world analogy, a dart aimed at the base of the stumps, this format scores full marks on rubric-graded exams too.
Exam strategy: for output-prediction questions, write out the value of every variable after each line on scratch paper rather than trying to trace the program in your head — most marks are lost to careless integer-division or off-by-one mistakes, not conceptual gaps.
4. Example
public class ExamDemo {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
continue;
}
sum += i;
}
System.out.println("Sum of odd numbers: " + sum);
int x = 7;
int y = 2;
System.out.println("x / y = " + (x / y));
System.out.println("x % y = " + (x % y));
}
}5. Output
Sum of odd numbers: 9
x / y = 3
x % y = 16. Key Takeaways
- Integer division truncates the decimal part (7 / 2 = 3), while % returns the remainder (7 % 2 = 1); a decimal result requires at least one operand to be a float or double.
- continue skips the rest of the current loop iteration and moves to the next one; break exits the loop entirely.
- Always trace variable values line by line on paper for output-prediction questions rather than guessing.
- Definition answers should follow: definition → code example → analogy, matching typical rubric grading.
- Common viva follow-ups probe why a design choice was made (e.g. 'why is main static?'), not just what the syntax does.
Practice what you learned
1. What is the output of: System.out.println(7 / 2 + 7 % 2);
2. Which loop construct guarantees the body executes at least once?
3. What does the following print? for (int i = 0; i < 5; i++) { if (i == 3) break; System.out.print(i); }
4. Why must the main method in Java be declared static?
5. What is the result of the expression: (int) 9.7?
Was this page helpful?
You May Also Like
Common Java Interview Questions
A curated roundup of the most frequently asked core Java interview questions — JVM internals, == vs equals(), String immutability, static vs instance members, and exception handling — with clear, accurate explanations.
OOP Interview Questions in Java
A focused roundup of Java OOP interview questions covering the four pillars — encapsulation, inheritance, polymorphism, abstraction — plus interface vs abstract class and overloading vs overriding.
for Loop in Java
Understand Java's classic three-part for loop and the enhanced for-each loop for iterating over arrays and collections concisely.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics