Question discussion

What is the output (the two lines are shown separated by a comma) of the following Java program?public class Main { public static void main…

1758Medium DSSSB TGT Computer Science 0 replies 2 views
0

What is the output (the two lines are shown separated by a comma) of the following Java program?

public class Main {
    public static void main(String[] args) {
        char c = 'a';
        c++;
        System.out.println(c + 1);
        System.out.println((char) (c + 1));
    }
}
  • Ab, c
  • Bc, 99
  • C99, c
  • D99, 99
Correct answer: C

Explanation

c becomes 'b' (98); c + 1 is an int and prints 99, while the cast (char)(c + 1) gives 'c'.

Discussion 0

Nobody has replied yet. Start the discussion!