AlgoMaster Logo

Methods - Quiz

Last Updated: January 3, 2026

Methods Exercises

31 quizzes

1
Code Completion

Call the method that returns the maximum of two integers

java
1
int result = Math.(a, b);

Click an option to fill the blank:

2
Code Completion

Invoke the instance method on the calculator object to multiply two numbers

java
1
int total = calculator.(x, y);

Click an option to fill the blank:

3
Code Completion

Create a new thread by passing the task as a Runnable

java
1
Thread worker = new Thread();

Click an option to fill the blank:

4
Multiple Choice

Which best describes a method in Java?

5
Multiple Choice

What is the return type of a method that does not return any value?

6
Multiple Choice

Which method signature correctly overloads this method: int calc(int a, int b)?

7
Multiple Choice

Given void printInfo(String name, int age), what is true about calling it?

8
Multiple Choice

Which is a valid use of varargs in a method declaration?

9
Multiple Choice

What happens when a primitive int is passed to a method in Java?

10
Multiple Choice

Which statement about recursion is correct?

11
Multiple Choice

Which best describes method overloading?

12
Multiple Choice

What is required in every non-void method?

13
Multiple Choice

Which call correctly uses a varargs method: void sendEmails(String subject, String... recipients)?

14
Multiple Choice

Given void adjust(Employee e), what can this method do?

15
Sequencing

Order the steps to define and use a simple instance method that calculates tax on a price

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to create a recursive method that sums numbers from 1 to n

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code?

1public class Test {
2    static int add(int a, int b) {
3        return a + b;
4    }
5    public static void main(String[] args) {
6        int result = add(3, 4);
7        System.out.println(result);
8    }
9}
18
Output Prediction

What is the output of this code using overloaded methods?

1public class OverloadTest {
2    static String describe(int x) {
3        return "int";
4    }
5    static String describe(double x) {
6        return "double";
7    }
8    public static void main(String[] args) {
9        System.out.println(describe(5));
10    }
11}
19
Output Prediction

What is the output of this recursive method?

1public class RecTest {
2    static int countdown(int n) {
3        if (n == 0) return 0;
4        return 1 + countdown(n - 1);
5    }
6    public static void main(String[] args) {
7        System.out.println(countdown(3));
8    }
9}
20
Output Prediction

What is the output of this code about pass by value with primitives?

1public class PassTest {
2    static void change(int x) {
3        x = 99;
4    }
5    public static void main(String[] args) {
6        int value = 10;
7        change(value);
8        System.out.println(value);
9    }
10}
21
Output Prediction

What is the output of this code demonstrating varargs?

1public class VarTest {
2    static int countArgs(String... args) {
3        return args.length;
4    }
5    public static void main(String[] args) {
6        System.out.println(countArgs("a", "b", "c"));
7    }
8}
22
Bug Spotting

Find the bug in this attempt to overload a method by return type only

Click on the line(s) that contain the bug.

java
1
public class Calculator {
2
    public int add(int a, int b) {
3
        return a + b;
4
    }
5
    public double add(int a, int b) {
6
        return (double) (a + b);
7
    }
8
}
23
Bug Spotting

Find the bug in this recursive factorial method

Click on the line(s) that contain the bug.

java
1
public class MathUtil {
2
    public static int factorial(int n) {
3
        if (n == 0) {
4
            return 0;
5
        }
6
        return n * factorial(n - 1);
7
    }
8
}
24
Matching

Match each method-related concept with the best description

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each recursion term with its role

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to define a method that returns the larger of two double values

java
1
public class CompareUtil {
2
public static max( x, double y) {
3
if (x > y) {
4
return x;
5
}
6
return y;
7
}
8
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code so the greet method prints a personalized message

java
1
public class Greeter {
2
public static void greet( name) {
3
System.out.println("Hello, " + name);
4
}
5
6
public static void main(String[] args) {
7
greet();
8
}
9
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to use varargs for summing any number of int values

java
1
public class SumUtil {
2
public static int sum( nums) {
3
int total = 0;
4
for (int n : nums) {
5
total += n;
6
}
7
return total;
8
}
9
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code so that updateTitle can modify the Book object's state

java
1
public class Book {
2
private String title;
3
4
public Book(String title) {
5
this.title = title;
6
}
7
8
public void updateTitle( newTitle) {
9
this.title = newTitle;
10
}
11
12
public String () {
13
return title;
14
}
15
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that actually changes the state of the object passed into the method

Click on the line to select.

java
1
class Counter {
2
    int value;
3
}
4
 
5
public class Demo {
6
    static void increment(Counter c) {
7
        c.value++;
8
    }
9
 
10
    public static void main(String[] args) {
11
        Counter counter = new Counter();
12
        increment(counter);
13
    }
14
}
31
Hotspot Selection

Click the line where the recursive call happens

Click on the line to select.

java
1
public class PowerUtil {
2
    public static int pow(int base, int exp) {
3
        if (exp == 0) {
4
            return 1;
5
        }
6
        return base * pow(base, exp - 1);
7
    }
8
}