LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Exercise 2 of 4

Strings and StringBuilder Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.

Question 11 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String sentence = "Java  is  fun";
        String[] words = sentence.split("\\s+");
        StringBuilder sb = new StringBuilder();
        for (int i = words.length - 1; i >= 0; i--) {
            sb.append(words[i]);
            if (i > 0) sb.append(' ');
        }
        System.out.println(sb + "|" + new StringBuilder(sentence).reverse());
    }
}
A fun is Java|nuf si avaJ
B nuf si avaJ|fun is Java
C fun is Java|nuf si avaJ
D fun is Java|fun is Java
Question page & discussion
Question 12 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String html = """
                <ul>
                  <li>Java</li>
                </ul>
                """;
        System.out.print(html);
        System.out.println(html.lines().count() + " " + html.endsWith("\n"));
    }
}
A <ul> <li>Java</li> </ul> 3 true
B <ul> <li>Java</li> </ul> 3 true
C <ul> <li>Java</li> </ul> 3 true
D <ul> <li>Java</li> </ul> 3 false
Question page & discussion
Question 13 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String row = String.format("|%-8s|%6.2f|%03d|", "Pen", 12.5, 7);
        System.out.println(row + " " + row.length());
        System.out.println("%s scored %d%%".formatted("Asha", 91));
    }
}
A |Pen | 12.50|007| 21 Asha scored 91%
B |Pen| 12.50|007| 15 Asha scored 91%
C | Pen| 12.50|7 | 21 Asha scored 91%%
D |Pen |12.5|007| 19 Asha scored 91%
Question page & discussion
Question 14 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String s = "Programming in Java";
        long a = s.chars().filter(c -> c == 'a').count();
        int upper = 0, spaces = 0;
        for (char c : s.toCharArray()) {
            if (Character.isUpperCase(c)) upper++;
            if (c == ' ') spaces++;
        }
        System.out.println(a + " " + upper + " " + spaces + " " + s.replace("m", "").length());
    }
}
A 3 2 2 17
B 2 2 2 17
C 3 1 2 19
D 3 2 3 17
Question page & discussion
Question 15 Hard

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        StringBuilder a = new StringBuilder("hi");
        StringBuilder b = new StringBuilder("hi");
        System.out.println(a.equals(b) + " " + a.toString().equals(b.toString()) + " " + (a.compareTo(b) == 0) + " "
                + "hi".contentEquals(a) + " " + a.toString().equals("hi"));
    }
}
A false true true true true
B true true true true true
C false false true true true
D false true false true true
Question page & discussion
Question 16 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static boolean isPalindrome(String s) {
        String clean = s.toLowerCase().replaceAll("[^a-z0-9]", "");
        return new StringBuilder(clean).reverse().toString().equals(clean);
    }

    public static void main(String[] args) {
        for (String s : new String[]{"Madam", "A man, a plan, a canal: Panama", "Java", "12321", ""})
            System.out.print(isPalindrome(s) + " ");
        System.out.println();
    }
}
A true true false true true
B true false false true true
C false true false true false
D true true false true false
Question page & discussion
Question 17 Easy

What is the output of the following Java program?

Main.java
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> langs = List.of("C", "C++", "Java");
        System.out.println(String.join(" | ", langs));
        System.out.println("=".repeat(5) + String.join("", "a", "b", "c").toUpperCase() + "=".repeat(5));
        System.out.println(String.join(",", List.of()).isEmpty());
    }
}
A C | C++ | Java =====ABC===== true
B C|C++|Java =====abc===== true
C C | C++ | Java | =====ABC===== false
D [C, C++, Java] =====ABC===== true
Question page & discussion
Question 18 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String a = "25", b = "17";
        System.out.println(a + b);
        System.out.println(Integer.parseInt(a) + Integer.parseInt(b));
        System.out.println(Integer.parseInt("ff", 16) + " " + Integer.toBinaryString(10) + " " + Double.parseDouble("1e3"));
        try {
            Integer.parseInt(" 42");
        } catch (NumberFormatException e) {
            System.out.println("bad number");
        }
    }
}
A 2517 42 255 1010 1000.0 bad number
B 42 42 255 1010 1000.0 bad number
C 2517 42 255 1010 1e3 bad number
D 2517 42 255 1010 1000.0 42
Question page & discussion
Question 19 Medium

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    static boolean anagram(String a, String b) {
        char[] x = a.replace(" ", "").toLowerCase().toCharArray();
        char[] y = b.replace(" ", "").toLowerCase().toCharArray();
        Arrays.sort(x);
        Arrays.sort(y);
        return Arrays.equals(x, y);
    }

    public static void main(String[] args) {
        System.out.println(anagram("Listen", "Silent") + " " + anagram("Dormitory", "Dirty room") + " " + anagram("Java", "Jaav") + " " + anagram("Hello", "World"));
    }
}
A true true true false
B true false true false
C false true true false
D true true false false
Question page & discussion
Question 20 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String[] inputs = {"YES", "No", "maybe", " yes "};
        for (String in : inputs) {
            String r = switch (in.strip().toLowerCase()) {
                case "yes", "y" -> "accept";
                case "no", "n" -> "reject";
                default -> "ask again";
            };
            System.out.print(r + " ");
        }
        System.out.println();
    }
}
A accept reject ask again accept
B ask again ask again ask again ask again
C accept reject ask again ask again
D accept accept ask again accept
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links