Methods Review

Methods Review#

These questionss are designed to asssess your current understanding of methods!

Questions 1

What is the output of this method?

public static void order() {
    System.out.println("I am number four");
    System.out.println("I am number one");
    System.out.println("I am number three");
    System.out.println("I am number two");
}

Question 2

What is the output of executing this main method?

public static void main(String[] args) {
    mTwo();
    mThree();
}

public static void mOne() {
    System.out.print("One ");
}

public static void mTwo() {
    System.out.print("Two ");
}

public static void mThree() {
    mOne();
    System.out.println("Three ");
}
  1.  Two Three
    
  2.  One Two Three
    
  3.  Two One Three
    
  4.  One Three
    

Question 3

True or False: The code below is possible.

public static void main(String[] args) {
    methodOne();
}

public static void methodOne() {
    System.out.println("I am first");
}

public static void methodOne() {
    System.out.println("I am second");
}

Question 4

Select the two best lists of methods for the following requirements:

We have a two-part game and ending:

  • Part one of the game prints all integer numbers between 1 and 10, and then prints out “This is part one.” with a newline.

  • Part two of the game prints out “This is part two.” and then prints all integer numbers between 1 and 10 with no newline at the end.

  • At the end we have an outro saying “Thanks for playing!” with a newline.

Hint

Look for a selection of methods with the least amount of redundancy within their logic, and which exhibit the least necessary amount of methods to complete the game task.

  1.  for (int counter = 0; counter <= 5; counter++) {
         System.out.print(counter + " ");
     }
    
  2.  for (int counter = 0; counter <= 5; counter++) {
         System.out.print(counter + " ");
     }
    
  3.  for (int counter = 0; counter <= 5; counter++) {
         System.out.print(counter + " ");
     }
    
  4.  for (int counter = 0; counter <= 5; counter++) {
         System.out.print(counter + " ");
     }
    

Question 5

Select and order the following text blocks to match the output of the program below:

public static void main(String[] args) {
    gameTwo();
    gameOne();
}

public static void gameOne() {
    System.out.println("playerOne has won!");
    gameTwo();
    
}

public static void gameTwo() {
    System.out.print("playerTwo has lost! ");
}
  1. playerOne has lost! playerTwo has won!

  2. playerTwo has lost!

  3. playerOne has won!

  4. playerTwo has lost! playerOne has won!