Skip to main content

Practice Code Structure

Section 1.3 Code Comprehension

Activity 1.3.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 String whereToPlay(boolean isSunny, int temperature) {
        if (isSunny) {
            if (temperature > 70) { 
                return "play outside";    
            }
        } 
        return "play inside";
    }
    
    Hint.
    Hint: You can use the following test cases to compare the functionality of the code blocks.
    • whereToPlay(true,77)
    • whereToPlay(true,20)
    • whereToPlay(false,70)
    • whereToPlay(false,90)
    Select all that apply.
  • public static String whereToPlay(boolean isSunny, int temperature) {
       if (isSunny || temperature > 70) { 
          return "play outside";    
       } 
       return "play inside";
    }
    
  • Code block in option A does not always have the same functionality as the first code block. Compare the outputs for whereToPlay(true,20);
  • public static String whereToPlay(boolean isSunny, int temperature) {
       if (isSunny && temperature > 70) { 
          return "play outside";    
       } 
       return "play inside";
    }
    
  • Correct!
  • public static String whereToPlay(boolean isSunny, int temperature) {
       if (!isSunny) {
          return "play inside";
       }
       if (temperature <= 70) { 
          return "play inside";    
       } 
       return "play outside";
    }
    
  • Correct!
You have attempted 1 of 2 activities on this page.