Skip to main content

Practice Code Structure

Section 1.11 Code Comprehension

Activity 1.11.1.

    Look at the first code block. Which of the following options have the same functionality as it (i.e., they produce the same output for the same inputs)? You can check the Hint for some input examples.
    public static void printCompetitionEligibility(boolean hasBuiltRobot, boolean passedSafetyTest, boolean registeredOnTime) {
        if (hasBuiltRobot) {
            if (passedSafetyTest) {
                if (registeredOnTime) {
                    System.out.println("Eligible to join the competition!");
                } else {
                    System.out.println("Not eligible.");
                }
            } else {
                System.out.println("Not eligible.");
            }
        } else {
            System.out.println("Not eligible.");
        }
    }
    
    Hint.
    Hint: You can use the following test cases to compare the functionality of the code blocks.
    • printCompetitionEligibility(true, true,true)
    • printCompetitionEligibility(true, true, false)
    • printCompetitionEligibility(true, false, true)
    • printCompetitionEligibility(true, false, false)
    • printCompetitionEligibility(false, true,true)
    • printCompetitionEligibility(false, true, false)
    • printCompetitionEligibility(false, false, true)
    • printCompetitionEligibility(false, false, false)
    Select all that apply.
  • public static void printCompetitionEligibility(boolean hasBuiltRobot, boolean passedSafetyTest, boolean registeredOnTime) {
       if (!hasBuiltRobot) { 
          System.out.println("Not eligible.");
       } 
       if (!passedSafetyTest) {
          System.out.println("Not eligible.");
       }
       if (!registeredOnTime) {
          System.out.println("Not eligible.");
       } else {
          System.out.println("Eligible to join the competition!");
       }
    }
    
  • Code block in option A does not alway have the same functionality as the first code block! Compare their outputs using printCompetitionEligibility(false,false,false)
  • public static void printCompetitionEligibility(boolean hasBuiltRobot, boolean passedSafetyTest, boolean registeredOnTime) {
       if (hasBuiltRobot && passedSafetyTest && registeredOnTime) {
          System.out.println("Eligible to join the competition!");
       } else {
          System.out.println("Not eligible.");
       }
    }
    
  • Correct!
  • public static void printCompetitionEligibility(boolean hasBuiltRobot, boolean passedSafetyTest, boolean registeredOnTime) {
       if (hasBuiltRobot && passedSafetyTest && registeredOnTime) {
          System.out.println("Eligible to join the competition!");
       }
       System.out.println("Not eligible.");
    }
    
  • Code block in option C does not always have the same functionality as the first code block! Compare the outputs using printCompetitionEligibility(true,true,true)
  • public static void printCompetitionEligibility(boolean hasBuiltRobot, boolean passedSafetyTest, boolean registeredOnTime) {
       if (!hasBuiltRobot) { 
          System.out.println("Not eligible.");
       } else if (!passedSafetyTest) {
          System.out.println("Not eligible.");
       } else if (!registeredOnTime) {
          System.out.println("Not eligible.");
       } else {
          System.out.println("Eligible to join the competition!");
       }
    }
    
  • Correct!
  • public static void printCompetitionEligibility(boolean hasBuiltRobot, boolean passedSafetyTest, boolean registeredOnTime) {
        if (hasBuiltRobot) {
            if (passedSafetyTest) {
                if (registeredOnTime) {
                    System.out.println("Eligible to join the competition!");
                }
            }
        } else {
            System.out.println("Not eligible.");
        }
    }
    
  • Code block in option E does not always have the same functionality as the first code block! Compare the outputs for printCompetitionEligibility(true,false,true);
You have attempted 1 of 2 activities on this page.