Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { System.out.println('a' + 1 +…

1304Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        System.out.println('a' + 1 + "b" + 'c');
        System.out.println(true ? 1 : 2.0);
    }
}
  • Ab1bc 1
  • B98bc 1.0
  • C98bc 1
  • Dabc 1.0
Correct answer: B

Explanation

'a' + 1 is integer addition (97 + 1 = 98) before "b" and 'c' are concatenated, giving 98bc; in the ternary the operands are promoted to double, so 1 prints as 1.0.

Discussion 0

Nobody has replied yet. Start the discussion!