ICSE · Class 9 · Computer Applications
Class 9 Iterative Constructs in Java Mock Test (ICSE)
6 practice questions from Iterative Constructs in Java, in the style of your board paper, each with a worked answer and the marks for every step. Then take a full chapter test on paper, snap the pages and get every step marked.
Topics in Iterative Constructs in Java
- for, while and do-while loops
- Entry- and exit-controlled loops
- break and continue
- Infinite and empty loops
- Number programs using loops
Iterative Constructs in Java practice questions with answers
- Q1Multiple choice1 markConsider the following Java code snippet: int p = 1; for (int i = 1; i ≤ 4; i++) { if (i == 2) continue; if (i == 4) break; p = p × i; } System.out.println(p); What will be the output of the above code snippet?
- (a)
- (b)
- (c)
- (d)
Show answer and step marks
- Correct option: (a) +1
Answer: (a) - Q2Assertion and reason1 markAssertion (A): A break statement exits the loop in which it is written. Reason (R): The continue statement is used to end the entire loop permanently.
- (a)Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation of Assertion (A).
- (b)Both Assertion (A) and Reason (R) are true but Reason (R) is not the correct explanation of Assertion (A).
- (c)Assertion (A) is true but Reason (R) is false.
- (d)Assertion (A) is false but Reason (R) is true.
Show answer and step marks
- Correct option: (c) Assertion (A) is true but Reason (R) is false.+1
Answer: (c) - Q3Multiple choice1 markConsider the following Java code snippet: int n = 456; int c = 0; while(n > 0) { c++; n = n / 10; } System.out.println(c); What will be the output of the above snippet?
- (a)2
- (b)3
- (c)4
- (d)456
Show answer and step marks
- Correct option: (b) 3+1
Answer: (b) - Q4Short answer3 marksStudy the Java code given below. int i; for (i = 1; i ≤ 5; i++); { System.out.println("Done"); } (a) State the output of the program. [1] (b) Explain why the block runs only once although the for loop is meant to run five times. [1] (c) Explain why the loop would never end if the update i++ were removed from the for statement. [1]
Show answer and step marks
- (a) The semicolon after the for statement makes its body empty, so the block runs once and Done is printed once.+1
- (b) The semicolon ends the for statement with an empty body, so the loop only updates i and the block is a separate statement executed once.+1
- (c) Without i++, i stays at 1, so i ≤ 5 is always true and the empty body never changes i, so the loop never ends.+1
Answer: (a) Done is printed once; (b) the semicolon gives the for loop an empty body, so the block is outside the loop; (c) i never changes, so the condition i ≤ 5 stays true forever. - Q5Short answer3 marksAt a water treatment plant, the laboratory uses the following Java program to print a reversed four-digit sample code on the audit sheet: int n = 4072; int rev = 0; while (n ≠ 0) { rev = rev × 10 + n % 10; n = n / 10; } System.out.println(rev); (a) State the value printed by the program. [1] (b) Explain why the statement n = n / 10 removes the last digit of n from the number. [2]
Show answer and step marks
- (a) Output: 2704+1
- (b) With int operands, n / 10 performs integer division and discards the fractional part, so 4072 / 10 gives 407.+1
- (b) The units digit 2 is lost because the remainder left after division by 10 is dropped, and the loop then works on the remaining digits.+1
Answer: (a) 2704. (b) Integer division of n by 10 discards the remainder, so the units digit is dropped (4072 becomes 407). - Q6Long answer4 marksConsider the following Java code segment designed to print numbers from to : int i = 1; while (i ≤ 5); { System.out.println(i); i++; } (a) Identify the logical error in the code that creates an empty loop and an infinite loop, and explain why it occurs. [2 marks] (b) Rewrite the corrected Java code snippet so that it executes properly and prints the numbers from to . [2 marks]
Show answer and step marks
- Error: a semicolon (;) is placed immediately after the while condition.+1
- The semicolon is an empty statement that becomes the loop body, so i is never incremented, i ≤ 5 stays true and the loop runs forever.+1
- Correction: remove the semicolon so the block becomes the loop body.+1
- Correct code: int i = 1; while (i ≤ 5) { System.out.println(i); i++; }+1
Answer: (a) The semicolon after while (i ≤ 5) makes an empty loop body, so i stays 1 and the loop never ends. (b) int i = 1; while (i ≤ 5) { System.out.println(i); i++; }
Take a full Iterative Constructs in Java test
PrepitUp writes a fresh paper on this chapter at the length you pick. Write it on paper with a timer, photograph the pages, and see the marks for every step and what to practise next.
- 15 min10 marks
- 30 min20 marks
- 1.5 hr40 marks
- 3 hr80 marks
Get your Iterative Constructs in Java answers marked
Sign in, pick this chapter and a length, write the test on paper and get every step marked.
Sign in to take this test