LearnFrenzy
Sign in Take a free test

Arrays in Java · Predict the Output · Question 20

Printing a char array versus an int array Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        char[] word = {'J', 'a', 'v', 'a'};
        System.out.println(word);                       // println(char[]) prints the characters
        System.out.println(String.valueOf(word, 1, 2));
        System.out.println(word.length + word[0]);       // int + char: arithmetic
    }
}
  • A Java av 78 Correct
  • B [J, a, v, a] av 78
  • C Java va 4J
  • D Java av 4J
Correct Answer: Option A

println has a special overload for char[] that prints the characters. 4 + 'J' (74) is the int 78.

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