Java · Question 24 · Medium

What will be the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        String s = "Mumbai";
        System.out.println(s.length() + s.charAt(0));
    }
}
  1. 83
  2. 77
  3. 6M
  4. Compilation error
Answer and explanation

Answer: A. 83

Explanation: Both operands are numeric: length() is 6 and the char 'M' has code 77. With no String operand, + performs addition and gives 83.

Previous: What will be the output of the following Java program? · Next: Which Java collection class does NOT allow duplicate elements and keeps…

Practise java

Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.

More java questions

More technical & programming practice

Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge