Write a Java program segment to check whether a given integer n is prime.
Iterative Constructs
Original Khojo Papers practice question — not from a past board paper.
A while loop tests the condition before the body runs, so the body may never execute; a do-while loop tests it after, so the body always executes at least once. The while loop is entry-controlled and the do-while exit-controlled. A do-while statement must be terminated by a semicolon after the condition.
No citable source has been recorded for this record. Treat it as practice material, not as fact.
Both are equivalent when the condition is true at the outset.
From the same topic and chapter, at a similar level.
Write a Java program segment to check whether a given integer n is prime.
Iterative Constructs
How many times does the body of this loop execute: for (int i = 10; i > 0; i -= 3) { … }
Iterative Constructs
Write a Java program segment to find the sum of the first n natural numbers using a for loop.
Iterative Constructs
Distinguish between the break and the continue statements.
Iterative Constructs
Explain the ternary operator in Java and rewrite "if (a > b) max = a; else max = b;" using it.
Operators and Expressions in Java
What is the output of: String s = "Examination"; System.out.println(s.length() + " " + s.substring(0, 4) + " " + s.toUpperCase().charAt(2));
String Handling