Which of the following will compile correctly
-
short myshort = 99S;
-
String name = 'Excellent tutorial Mr Green';
-
char c = 17c;
-
int z = 015;
Option D is correct - int z = 015 compiles because Java allows integer literals with leading zeros (interpreted as octal, 015 = 13 decimal). Options A and C are incorrect (99S and 17c are invalid suffixes). Option B is incorrect (strings require double quotes, not single).
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.