LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Predict the Output · Question 13

String.format with alignment and padding Answer, explanation & discussion

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

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% Correct
  • 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%
Correct Answer: Option A

%-8s pads on the right, %6.2f on the left, %03d with zeros; %% prints a single percent sign.

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