Skip to main content

Practice Code Structure

Section 1.10 Identification

Activity 1.10.1.

    These code blocks are the same as the ones you saw in the previous question, and they all have the same functionality. Among these options which ones are preferable?
  • public static String guessGame(int number) { 
       if (number % 4 == 0) {
          return "Good guess!";
       } else if (number % 7 == 0) {
          return "Good guess!";
       } 
       return "Try again";
    }
    
  • This code can be improved!
  • public static String guessGame(int number) { 
        if (number % 4 == 0) {
            return "Good guess!";
        }
        if (number % 7 == 0) {
            return "Good guess!";
        } 
        return "Try again";
    }
    
  • This code can be improved!
  • 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.