Skip to main content

Practice Code Structure

Section 1.9 Code Comprehension

Activity 1.9.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 guessGame(int number) { 
       if (number % 4 == 0) {
          return "Good guess!";
       } else if (number % 7 == 0) {
          return "Good guess!";
       } 
       return "Try again";
    }
    
    Hint.
    Hint: You can use the following test cases to compare the functionality of the code blocks.
    • guessGame(28)
    • guessGame(12)
    • guessGame(77)
    • guessGame(3)
    Select all that apply.
  • public static String guessGame(int number) { 
        if (number % 4 == 0) {
            return "Good guess!";
        }
        if (number % 7 == 0) {
            return "Good guess!";
        } 
        return "Try again";
    }
    
  • Correct!
  • public static String guessGame(int number) { 
        if (number % 4 == 0 || number % 7 == 0) {
            return "Good guess!";
        } 
        return "Try again";
    }
    
  • Correct!
  • public static String guessGame(int number) { 
        if (number % 4 != 0 && number % 7 != 0) {
            return "Try again";
        } 
        return "Good guess!";
    }
    
  • Correct!
You have attempted 1 of 2 activities on this page.