LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Exercise 2 of 4

Strings and StringBuilder Predict the Output

  • 20 questions
  • Page 1 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 1 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String s = "  hello  ";
        s.trim();
        s.toUpperCase();
        s.concat("!");
        System.out.println("[" + s + "]");
        s = s.trim().toUpperCase().concat("!");
        System.out.println("[" + s + "]");
    }
}
A [ hello ] [HELLO!]
B [HELLO!] [HELLO!]
C [hello] [HELLO!]
D [ HELLO !] [HELLO!]
Question page & discussion
Question 2 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String a = "code";
        String b = "co" + "de";
        String c = new String("code");
        String d = c.intern();
        String part = "co";
        String e = part + "de";
        System.out.println((a == b) + " " + (a == c) + " " + (a == d) + " " + (a == e) + " " + a.equals(e));
    }
}
A true false true false true
B true true true true true
C false false true false true
D true false false false true
Question page & discussion
Question 3 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String email = "asha.rao@learnfrenzy.com";
        int at = email.indexOf('@');
        String user = email.substring(0, at);
        String domain = email.substring(at + 1);
        String tld = domain.substring(domain.lastIndexOf('.') + 1);
        System.out.println(user + " | " + domain + " | " + tld + " | " + user.length());
    }
}
A asha.rao | learnfrenzy.com | com | 8
B asha.rao@ | learnfrenzy.com | .com | 9
C asha | rao@learnfrenzy.com | com | 4
D asha.rao | @learnfrenzy.com | com | 8
Question page & discussion
Question 4 Medium

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String line = ",a,,b,,";
        System.out.println(Arrays.toString(line.split(",")) + " " + line.split(",").length);
        System.out.println(line.split(",", -1).length + " " + "a b  c".split(" ").length + " " + "a b  c".split(" +").length);
    }
}
A [, a, , b] 4 6 4 3
B [a, b] 2 6 3 3
C [, a, , b, , ] 6 6 4 3
D [a, , b] 3 5 4 3
Question page & discussion
Question 5 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("cat".compareTo("car") + " " + "car".compareTo("cart") + " " + "Zoo".compareTo("apple") + " "
                + "same".compareTo("same") + " " + "Zoo".compareToIgnoreCase("apple"));
    }
}
A 2 -1 -7 0 25
B 1 -1 -1 0 1
C 2 -1 25 0 -7
D -2 1 -7 0 25
Question page & discussion
Question 6 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("12345");
        sb.reverse().append('0').insert(2, '-').deleteCharAt(0);
        System.out.println(sb + " " + sb.length() + " " + sb.charAt(1));
    }
}
A 4-3210 6 -
B 54-3210 7 4
C 4-321 5 -
D 5-43210 7 -
Question page & discussion
Question 7 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static void addSuffix(StringBuilder b) { b.append("-ok"); }
    static void addSuffix(String s) { s = s + "-ok"; }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("job");
        StringBuilder alias = sb;
        alias.append("1");
        addSuffix(sb);
        String s = "job";
        addSuffix(s);
        System.out.println(sb + " " + s + " " + (sb == alias));
    }
}
A job1-ok job true
B job job false
C job1-ok job-ok true
D job1 job true
Question page & discussion
Question 8 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String s = "A";
        System.out.println(s + 'B' + 'C');
        System.out.println('B' + 'C' + s);
        System.out.println(s + 1 + 2);
        System.out.println(1 + 2 + s);
        System.out.println(s + (1 + 2));
    }
}
A ABC 133A A12 3A A3
B ABC BCA A12 3A A3
C ABC 133A A3 3A A3
D ABC 133A A12 12A A3
Question page & discussion
Question 9 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String raw = "  Learn JAVA Online  ";
        String slug = raw.strip().toLowerCase().replace(' ', '-');
        System.out.println(slug + " " + slug.length() + " " + slug.startsWith("learn") + " " + slug.contains("java-o"));
    }
}
A learn-java-online 17 true true
B learn-java-online 21 true true
C --learn-java-online-- 21 false true
D learn-java-online 17 true false
Question page & discussion
Question 10 Medium

What is the output of the following Java program?

Main.java
import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        String a = "Java", b = "JAVA", n = null;
        System.out.println(a.equals(b) + " " + a.equalsIgnoreCase(b) + " " + "Java".equals(n) + " " + Objects.equals(n, null));
        try {
            System.out.println(n.equals(a));
        } catch (NullPointerException e) {
            System.out.println("NPE");
        }
    }
}
A false true false true NPE
B true true false true false
C false true false false NPE
D false false false true NPE
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links