15 Which of the following are valid statements?

  1. A System.out.println(1+1);

  2. B int i=2+'2';

  3. C String s="on"+'one';

  4. D byte b=255;


Correct Option: A,B

AI Explanation

To determine which of the statements are valid, let's go through each option:

A. A System.out.println(1+1); This statement is valid because it uses the System.out.println() method to print the result of the expression 1+1, which is 2.

B. B int i=2+'2'; This statement is not valid. In Java, when you use single quotes ('') around a character, it represents a single character. In this case, '2' is not a valid character. If you want to concatenate the integer value 2 with the string "2", you should use double quotes ("") instead of single quotes.

C. C String s="on"+'one'; This statement is not valid. Similar to the previous option, the use of single quotes around 'one' is not valid. If you want to concatenate the string "on" with the string "one", you should use double quotes for both strings.

D. D byte b=255; This statement is not valid. In Java, the range of values that can be assigned to a byte is from -128 to 127. The value 255 is outside this range.

Based on the explanations above, the valid statements are A and B. Therefore, the correct answer is A, B.

Find more quizzes: