LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Exercise 3 of 4

Strings and StringBuilder Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program misuses String or StringBuilder in a way javac rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String name = "java";
        name.charAt(0) = 'J';
        System.out.println(name);
    }
}
A charAt needs two arguments
B charAt returns a value, not a variable, and Strings are immutable; build a new String (or use a StringBuilder's setCharAt)
C name must be final
D 'J' must be "J"
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        sb.append(" World");
        String message = sb;
        System.out.println(message);
    }
}
A append returns void
B StringBuilder is not a String; convert it with sb.toString()
C message must be final
D StringBuilder cannot start with text
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String city = "Pune";
        System.out.println(city.length);
    }
}
A println cannot print ints
B For Strings, length is a method: city.length() (arrays use the length field)
C city must be a char array
D Pune is too short
Question page & discussion
Question 4 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String word = "Java";
        char first = word.substring(0, 1);
        System.out.println(first);
    }
}
A substring needs one argument
B substring returns a String, which cannot be assigned to a char; use word.charAt(0)
C char cannot hold letters
D word must be uppercase
Question page & discussion
Question 5 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String line = "-" * 20;
        System.out.println(line);
    }
}
A 20 is too many
B * is not defined for Strings; use "-".repeat(20)
C line must be a char
D println cannot print dashes
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java");
        sb = sb + " 21";
        System.out.println(sb);
    }
}
A sb must be final
B sb + " 21" produces a String, which cannot be assigned to a StringBuilder variable; use sb.append(" 21")
C " 21" must be an int
D StringBuilder has no toString
Question page & discussion
Question 7 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String csv = "a,b,c";
        String parts = csv.split(",");
        System.out.println(parts);
    }
}
A split needs a limit
B split returns a String[] array, not a String: write String[] parts = csv.split(",");
C csv must be final
D Commas are not allowed
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String s = "Java";
        if (s.charAt(0).equals('J')) {
            System.out.println("starts with J");
        }
    }
}
A 'J' must be lowercase
B charAt returns a primitive char, which has no methods; compare with s.charAt(0) == 'J'
C equals needs a String
D s must be final
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String sql = """SELECT * FROM users
                        WHERE active = 1""";
        System.out.println(sql);
    }
}
A SQL is not allowed in strings
B The opening """ of a text block must be followed by a line break; the content starts on the next line
C Text blocks need Java 8
D The closing """ must be on its own line
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        char c = '7';
        String digit = c - '0';
        System.out.println(digit);
    }
}
A '0' must be "0"
B c - '0' is an int (7), and an int cannot be assigned to a String; use String.valueOf(c - '0') or int digit
C chars cannot be subtracted
D c must be final
Question page & discussion
Up next · Exercise 4Java String Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links