AlgoMaster Logo

Best Practices - Quiz

Last Updated: December 6, 2025

1 min read

Best Practices Exercises

30 quizzes

1
Code Completion

Convert a string variable userInput to all uppercase letters before logging it.

java
1
String upper = userInput.();

Click an option to fill the blank:

2
Code Completion

Add a constant timeout value to a HashMap in a clear, self-documenting way.

java
1
config.put("timeout", String.valueOf());

Click an option to fill the blank:

3
Code Completion

Safely retrieve a value from an Optional<String> named email, providing a default if missing.

java
1
String value = email.("unknown@example.com");

Click an option to fill the blank:

4
Multiple Choice

According to Java coding standards, which is the best name for a constant representing a maximum file size?

5
Multiple Choice

Which method name best follows clean code and Effective Java naming guidelines for a method that returns the total price?

6
Multiple Choice

Which principle from clean code is most closely related to keeping functions small and focused?

7
Multiple Choice

You want a flexible way to add logging behavior to a class without changing its inheritance hierarchy. Which approach is recommended?

8
Multiple Choice

Which situation most commonly leads to a NullPointerException in Java?

9
Multiple Choice

You frequently add and remove elements in the middle of a list. Which Java collection is generally more efficient?

10
Multiple Choice

What is the main benefit of writing unit tests before refactoring code?

11
Multiple Choice

In JUnit 5, which annotation marks a method as a test case?

12
Multiple Choice

Which practice helps most when debugging a complex issue?

13
Multiple Choice

Which statement about performance optimization is most aligned with best practices?

14
Multiple Choice

Which JUnit assertion is best to verify that a method throws an IllegalArgumentException?

15
Sequencing

Order the steps to create and use a JUnit 5 test for a Calculator class.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to debug a failing unit test in an IDE.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code related to clean code naming?

1public class NamingExample {
2    public static void main(String[] args) {
3        int itemCount = 3;
4        int totalItems = itemCount + 2;
5        System.out.println(totalItems);
6    }
7}
18
Output Prediction

What is the output of this code using composition?

1class Logger {
2    private final String name;
3    Logger(String name) { this.name = name; }
4    String format(String msg) { return name + ":" + msg; }
5}
6class Service {
7    private final Logger logger = new Logger("Service");
8    String start() { return logger.format("started"); }
9}
10public class Main {
11    public static void main(String[] args) {
12        Service service = new Service();
13        System.out.println(service.start());
14    }
15}
19
Output Prediction

What is the output of this code demonstrating Optional to avoid NullPointerException?

1import java.util.Optional;
2public class OptionalDemo {
3    public static void main(String[] args) {
4        Optional<String> name = Optional.ofNullable(null);
5        String result = name.orElse("guest");
6        System.out.println(result);
7    }
8}
20
Output Prediction

What is the output of this JUnit assertion-like logic?

1public class AssertLike {
2    private static boolean equals(int expected, int actual) {
3        return expected == actual;
4    }
5    public static void main(String[] args) {
6        boolean passed = equals(4, 2 + 2);
7        System.out.println(passed ? "PASS" : "FAIL");
8    }
9}
21
Bug Spotting

Find the bug related to null handling in this code.

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

java
1
public class UserService {
2
    public String getUserName(User user) {
3
        return user.getName().toUpperCase();
4
    }
5
}
6
class User {
7
    private final String name;
8
    User(String name) { this.name = name; }
9
    String getName() { return name; }
10
}
22
Bug Spotting

Find the performance-related bug in this string-building code.

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

java
1
public class ReportBuilder {
2
    public String buildReport(int n) {
3
        String report = "";
4
        for (int i = 0; i < n; i++) {
5
            report = report + "line" + i + ";";
6
        }
7
        return report;
8
    }
9
}
23
Matching

Match each Java testing concept with its description.

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

24
Matching

Match each debugging tool action with what it does.

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

25
Fill in the Blanks

Complete the code to define a clean, testable service using composition.

java
1
public class EmailService {
2
private final mailSender;
3
4
public EmailService( mailSender) {
5
this.mailSender = mailSender;
6
}
7
8
public void sendWelcomeEmail(String address) {
9
mailSender.(address, "Welcome");
10
}
11
}

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the JUnit 5 test to verify that dividing by zero throws an exception.

java
1
import org.junit.jupiter.api.Test;
2
import static org.junit.jupiter.api.Assertions.*;
3
4
class CalculatorTest {
5
6
@Test
7
void divideByZeroThrows() {
8
Calculator calculator = new Calculator();
9
(ArithmeticException.class, () -> calculator.(10, ));
10
}
11
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to iterate over a list of orders and calculate the total price cleanly.

java
1
import java.util.List;
2
3
public class OrderService {
4
public double totalPrice(List<Order> orders) {
5
double total = 0.0;
6
for ( order : orders) {
7
total += order.();
8
}
9
return ;
10
}
11
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to measure response time for a method call.

java
1
public class TimingUtil {
2
public static long measure(Runnable task) {
3
long start = System.();
4
task.();
5
long end = System.();
6
return - start;
7
}
8
}

Click an option to fill blank 1:

29
Hotspot Selection

Click the line that will cause a NullPointerException when this code runs.

Click on the line to select.

java
1
public class DebugHotspot {
2
    public static void main(String[] args) {
3
        String value = null;
4
        String trimmed = value.trim();
5
        System.out.println(trimmed);
6
    }
7
}
30
Hotspot Selection

Click the line that is likely the performance bottleneck due to inefficient collection choice.

Click on the line to select.

java
1
import java.util.ArrayList;
2
import java.util.List;
3
 
4
public class PerformanceHotspot {
5
    public static void main(String[] args) {
6
        List<Integer> numbers = new ArrayList<>();
7
        for (int i = 0; i < 100000; i++) {
8
            numbers.add(i);
9
        }
10
        numbers.add(0, -1);
11
        System.out.println(numbers.size());
12
    }
13
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.