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 suffix "S" is not a valid identifier for a short variable in Java. If you want to assign a value to a short variable, you can simply write "short myshort = 99;".

Option B) String name = 'Excellent tutorial Mr Green'; - This option is incorrect because single quotes ('') are used to represent characters in Java, not strings. To declare a string variable, you should use double quotes (""). Therefore, the correct statement would be "String name = "Excellent tutorial Mr Green";".

Option C) char c = 17c; - This option is incorrect because the suffix "c" is not a valid way to represent a character value in Java. If you want to assign a character value, you can use single quotes. For example, "char c = '17';".

Option D) int z = 015; - This option is correct because the value "015" is a valid octal (base-8) representation in Java. The leading zero indicates that the value is in octal format. Therefore, this statement will compile correctly.

The correct answer is D.

Find more quizzes: