LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Predict the Output · Question 9

A chain of String methods Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

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 Correct
  • B learn-java-online 21 true true
  • C --learn-java-online-- 21 false true
  • D learn-java-online 17 true false
Correct Answer: Option A

strip removes the outer spaces first, so only the two inner spaces become hyphens.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links