AlgoMaster Logo

Strings - Quiz

Last Updated: January 3, 2026

Strings Exercises

32 quizzes

1
Code Completion

Join two strings representing a first and last name with a space between them.

java
1
String fullName = firstName.(" " + lastName);

Click an option to fill the blank:

2
Code Completion

Check if an email string contains the '@' character to validate basic structure.

java
1
boolean valid = email.("@");

Click an option to fill the blank:

3
Code Completion

Remove leading and trailing spaces from a user input string before processing.

java
1
String cleaned = input.();

Click an option to fill the blank:

4
Multiple Choice

Which statement correctly creates a String from a literal using the string pool?

5
Multiple Choice

What does message.charAt(0) return when message is "Java"?

6
Multiple Choice

Which String method can be used to extract "World" from "Hello World"?

7
Multiple Choice

Why are Java Strings immutable?

8
Multiple Choice

Which class is best when building a long log message in a single-threaded method?

9
Multiple Choice

Which feature distinguishes StringBuffer from StringBuilder?

10
Multiple Choice

Which call correctly formats a price with two decimal places?

11
Multiple Choice

Which comparison correctly checks if two String variables have the same text?

12
Multiple Choice

You need to validate that an input string contains only digits. Which regex is most appropriate?

13
Multiple Choice

What is the result of this code? String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2);

14
Multiple Choice

Which method is best to ignore letter case when checking a username?

15
Multiple Choice

Which StringBuilder method removes all characters it currently holds?

16
Sequencing

Order the steps to use a regex to find all numbers in a text with Pattern and Matcher.

Drag and drop to reorder, or use the arrows.

17
Sequencing

Order the steps to efficiently build a large CSV line using StringBuilder.

Drag and drop to reorder, or use the arrows.

18
Output Prediction

What is the output of this code involving substring and length?

1String msg = "Hello, World!";
2String part = msg.substring(7, 12);
3System.out.println(part + " (" + part.length() + ")");
19
Output Prediction

What does this code print about string references and content?

1String a = "test";
2String b = new String("test");
3System.out.println(a.equals(b) + ":" + (a == b));
20
Output Prediction

What is the output when using StringBuilder with chained appends?

1StringBuilder sb = new StringBuilder("Java");
2sb.append(" ").append("Strings");
3System.out.println(sb.toString());
21
Output Prediction

Predict the output when formatting a double value for display.

1double value = 12.3456;
2String formatted = String.format("Result: %.2f", value);
3System.out.println(formatted);
22
Output Prediction

What single line does this regex code print?

1import java.util.regex.*;
2
3public class Main {
4    public static void main(String[] args) {
5        String text = "Order #12345 shipped";
6        Pattern p = Pattern.compile("#(\\d+)");
7        Matcher m = p.matcher(text);
8        String result = m.find() ? m.group(1) : "none";
9        System.out.println(result);
10    }
11}
23
Bug Spotting

Find the bug in this code that is meant to compare two strings ignoring case.

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

java
1
public class CompareNames {
2
    public static boolean sameUser(String a, String b) {
3
        if (a.toLowerCase() == b.toLowerCase()) {
4
            return true;
5
        }
6
        return false;
7
    }
8
}
24
Bug Spotting

Find the bug in this code that is meant to build a log message using StringBuilder.

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

java
1
public class Logger {
2
    public static String buildLog(String level, String message) {
3
        StringBuilder sb = new StringBuilder();
4
        sb.append("[");
5
        sb.append(level);
6
        sb.append("] ");
7
        sb.concat(message);
8
        return sb.toString();
9
    }
10
}
25
Matching

Match each string-related class with its main characteristic.

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

26
Matching

Match each regex construct with what it matches.

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

27
Fill in the Blanks

Complete the code to build and print an email address using StringBuilder.

java
1
String username = "user";
2
String domain = "example.com";
3
StringBuilder sb = new StringBuilder();
4
sb.(username).("@").(domain);
5
System.out.println(sb.toString());

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to validate that a phone string matches a simple digit-only pattern.

java
1
import java.util.regex.*;
2
3
public class PhoneValidator {
4
public static boolean isValid(String phone) {
5
Pattern pattern = Pattern.("$");
6
Matcher matcher = pattern.matcher(phone);
7
return matcher.();
8
}
9
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to format a user's name and age into a single line.

java
1
String name = "Alice";
2
int age = 30;
3
String result = String.("Name: %s, Age: %d", , );
4
System.out.println(result);

Click an option to fill blank 1:

30
Fill in the Blanks

Complete the code to safely compare two Strings for equality, handling possible null values.

java
1
String a = getFirst();
2
String b = getSecond();
3
boolean same = != null && .();
4
System.out.println("Same: " + same);

Click an option to fill blank 1:

31
Hotspot Selection

Click the line that will throw a StringIndexOutOfBoundsException at runtime.

Click on the line to select.

java
1
String text = "Hi";
2
char ch = text.charAt(2);
3
System.out.println(ch);
32
Hotspot Selection

Click the line that incorrectly compares two String values using == instead of equals.

Click on the line to select.

java
1
String status1 = new String("OK");
2
String status2 = new String("OK");
3
if (status1 == status2) {
4
    System.out.println("Same status");
5
}