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

  1. Q1Multiple choice1 mark
    Consider 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?
    1. (a)33
    2. (b)1212
    3. (c)11
    4. (d)66
    Show answer and step marks
    1. Correct option: (a) 33+1
    Answer: (a)
  2. Q2Assertion and reason1 mark
    Assertion (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.
    1. (a)Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation of Assertion (A).
    2. (b)Both Assertion (A) and Reason (R) are true but Reason (R) is not the correct explanation of Assertion (A).
    3. (c)Assertion (A) is true but Reason (R) is false.
    4. (d)Assertion (A) is false but Reason (R) is true.
    Show answer and step marks
    1. Correct option: (c) Assertion (A) is true but Reason (R) is false.+1
    Answer: (c)
  3. Q3Multiple choice1 mark
    Consider 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?
    1. (a)2
    2. (b)3
    3. (c)4
    4. (d)456
    Show answer and step marks
    1. Correct option: (b) 3+1
    Answer: (b)
  4. Q4Short answer3 marks
    Study 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
    1. (a) The semicolon after the for statement makes its body empty, so the block runs once and Done is printed once.+1
    2. (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
    3. (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.
  5. Q5Short answer3 marks
    At 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
    1. (a) Output: 2704+1
    2. (b) With int operands, n / 10 performs integer division and discards the fractional part, so 4072 / 10 gives 407.+1
    3. (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).
  6. Q6Long answer4 marks
    Consider the following Java code segment designed to print numbers from 11 to 55: 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 11 to 55. [2 marks]
    Show answer and step marks
    1. Error: a semicolon (;) is placed immediately after the while condition.+1
    2. 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
    3. Correction: remove the semicolon so the block becomes the loop body.+1
    4. 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
See a sample marked answer sheet

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

More Class 9 Computer Applications mock tests