ICSE · Class 9 · Computer Applications

Class 9 Conditional Constructs in Java Mock Test (ICSE)

6 practice questions from Conditional 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 Conditional Constructs in Java

  • if, if-else and if-else-if
  • Nested if
  • switch-case with break and default
  • Fall through
  • Menu-driven programs
  • System.exit

Conditional Constructs in Java practice questions with answers

  1. Q1Multiple choice1 mark
    What will be the output of the following nested if code snippet? int a = 5, b = 10; if (a > 0) { if (b < 5) System.out.print("One "); else System.out.print("Two "); } else { System.out.print("Three "); }
    1. (a)Three
    2. (b)One
    3. (c)Two
    4. (d)One Two
    Show answer and step marks
    1. Correct option: (c) Two+1
    Answer: (c)
  2. Q2Multiple choice1 mark
    Which statement, when executed, ends the whole Java program immediately, wherever it appears?
    1. (a)break;
    2. (b)continue;
    3. (c)A nested if statement
    4. (d)System.exit(0);
    Show answer and step marks
    1. Correct option: (d) System.exit(0);+1
    Answer: (d)
  3. Q3Short answer2 marks
    Explain the term 'fall through' in the context of a switch-case statement in Java. Why is a `break` statement typically used to prevent it?
    Show answer and step marks
    1. Definition: Fall through occurs when execution flows into subsequent case blocks without checking if their case constants match, because a break statement is missing.+1
    2. Role of break: A break statement is used to terminate the switch block immediately after a matching case is executed, preventing unintended execution of subsequent cases.+1
    Answer: Fall through: when a matching case has no `break`, execution continues into the statements of the following cases (and `default`) without checking their case values. A `break` ends the switch block as soon as the matching case's statements have run, so the later cases are not executed.
  4. Q4Short answer3 marks
    Study the Java code given below. int age = 15; boolean member = true; if (age ≥ 12) { if (member) { System.out.println("Discount"); } else { System.out.println("Regular"); } } else { System.out.println("Child"); } (a) Name the condition tested by the inner if statement. [1] (b) State the output when age is 10 and member is false. [1] (c) State the output when age is 15 and member is false. [1]
    Show answer and step marks
    1. (a) The inner if tests the condition member, which is true when the value is true.+1
    2. (b) age is 10, so age ≥ 12 is false and the outer else prints Child.+1
    3. (c) age is 15, so the outer if is true; member is false, so the inner else prints Regular.+1
    Answer: (a) member; (b) Child; (c) Regular
  5. Q5Long answer4 marks
    Write a menu-driven program in Java using a switch-case\text{switch-case} statement to perform the following operations based on the user's choice: 1. Calculate the area of a circle (Formula: π×r2\pi \times r^2, take π=3.142\pi = 3.142) 2. Calculate the area of a rectangle (Formula: length×breadth\text{length} \times \text{breadth}) 3. Calculate the area of a square (Formula: side2\text{side}^2) For any invalid choice, display the message "Invalid Choice". Accept all necessary inputs using the Scanner\text{Scanner} class.
    Show answer and step marks
    1. Imports java.util.Scanner\text{java.util.Scanner}, declares the class and the main\text{main} method, and creates a Scanner\text{Scanner} object.+0.5
    2. Displays the menu and reads the user's choice with nextInt()\text{nextInt()}.+0.5
    3. Reads the required dimensions (radius; length and breadth; side) with a Scanner\text{Scanner} method such as nextDouble()\text{nextDouble()} or nextInt()\text{nextInt()}.+0.5
    4. Uses switch(choice)\text{switch(choice)} with a break\text{break} after each case.+0.5
    5. Case 1: computes and prints the area of the circle as 3.142×r×r3.142 \times r \times r.+0.5
    6. Case 2: computes and prints the area of the rectangle as length×breadth\text{length} \times \text{breadth}.+0.5
    7. Case 3: computes and prints the area of the square as side×side\text{side} \times \text{side}.+0.5
    8. The default\text{default} case prints "Invalid Choice".+0.5
    Answer: import java.util.Scanner; public class AreaMenu { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("1. Area of a circle"); System.out.println("2. Area of a rectangle"); System.out.println("3. Area of a square"); System.out.print("Enter your choice: "); int choice = sc.nextInt(); switch (choice) { case 1: System.out.print("Enter the radius: "); double r = sc.nextDouble(); System.out.println("Area of the circle = " + (3.142 × r × r)); break; case 2: System.out.print("Enter the length and breadth: "); double l = sc.nextDouble(); double b = sc.nextDouble(); System.out.println("Area of the rectangle = " + (l × b)); break; case 3: System.out.print("Enter the side: "); double s = sc.nextDouble(); System.out.println("Area of the square = " + (s × s)); break; default: System.out.println("Invalid Choice"); } } }
  6. Q6Long answer4 marks
    Write a menu-driven program in Java using a `switch-case` statement to perform the following operations based on the user's choice: 1. Accept an integer and check whether it is even or odd. 2. Accept an integer and check whether it is positive, negative or zero. Display an appropriate error message for any invalid choice.
    Show answer and step marks
    1. Imports `java.util.Scanner`, declares the class and `main` method, and creates a `Scanner` object.+0.5
    2. Displays the menu and reads the user's choice.+0.5
    3. Uses `switch(choice)` with a `break` after each case.+0.5
    4. Case 1: reads an integer and tests `n % 2 == 0` (or `n % 2 ≠ 0` for odd; `n % 2 == 1` is wrong for negative odd numbers).+0.5
    5. Case 1: prints that the number is even when the test is true and odd otherwise.+0.5
    6. Case 2: reads an integer and tests `n > 0` and `n < 0`.+0.5
    7. Case 2: prints positive, negative, or zero for the remaining case.+0.5
    8. The `default` case prints an error message for an invalid choice.+0.5
    Answer: import java.util.Scanner; public class NumberMenu { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("1. Check even or odd"); System.out.println("2. Check positive, negative or zero"); System.out.print("Enter your choice: "); int choice = sc.nextInt(); switch (choice) { case 1: System.out.print("Enter an integer: "); int n = sc.nextInt(); if (n % 2 == 0) System.out.println(n + " is even"); else System.out.println(n + " is odd"); break; case 2: System.out.print("Enter an integer: "); int m = sc.nextInt(); if (m > 0) System.out.println(m + " is positive"); else if (m < 0) System.out.println(m + " is negative"); else System.out.println(m + " is zero"); break; default: System.out.println("Invalid choice! Please enter 1 or 2."); } } }

Take a full Conditional 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 Conditional 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