Which of the following will compile correctly

  1. short myshort = 99S;

  2. String name = 'Excellent tutorial Mr Green';

  3. char c = 17c;

  4. int z = 015;


Correct Option: D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) short myshort = 99S; - This option is incorrect because the letter 'S' at the end of the number is not a valid suffix for a short data type. The correct suffix for a short data type is 's' (lowercase).

Option B) String name = 'Excellent tutorial Mr Green'; - This option is incorrect because string literals in Java should be enclosed in double quotation marks ("). Single quotation marks (') are used for character literals.

Option C) char c = 17c; - This option is incorrect because the letter 'c' at the end of the number is not a valid suffix for a char data type. If you want to assign a numeric value to a char variable, you can use a numeric literal without any suffix.

Option D) int z = 015; - This option is correct because the number 015 is a valid octal (base 8) literal in Java. Octal literals are represented by a leading zero (0) followed by digits from 0 to 7.

The correct answer is D. This option is correct because it assigns a valid octal value to an int variable.

Find more quizzes: