InterviewSolution
Saved Bookmarks
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Which one is a valid declaration of a boolean? |
| Answer» A boolean can only be assigned the literal true or false. | |
| 2. |
Which three are legal array declarations? |
| Answer» (1), (2), and (4) are legal array declarations. With an array declaration, you can place the brackets to the right or left of the identifier. Option A looks strange, but it's perfectly legal to split the brackets in a multidimensional array, and place them on both sides of the identifier. Although coding this way would only annoy your fellow programmers, for the exam, you need to know it's legal. (3) and (5) are wrong because you can't declare an array with a size. The size is only needed when the array is actually instantiated (and the JVM needs to know how much space to allocate for the array, based on the type of array and the size). | |
| 3. |
Which three are valid declarations of a float? |
| Answer» (1) and (3) are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits). (6) is correct because (F) is appended to the literal, declaring it as a float rather than a double (the default for floating point literals). (2), (4),and (5) are all doubles. | |
| 4. |
Which is a valid declarations of a String? |
| Answer» Option A sets the String reference to null. Option B is wrong because null cannot be in single quotes. Option C is wrong because there are multiple characters between the single quotes ('abc'). Option D is wrong because you can't cast a char (primitive) to a String (object). | |
| 5. |
What is the numerical range of a char? |
| Answer» A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values. | |
| 6. |
Which three piece of codes are equivalent to line 3? |
| Answer» (1), (2) and (3) are correct. Interfaces can have constants, which are always implicitly public, static, and final. Interface constant declarations of public, static, and final are optional in any combination. | |
| 7. |
Which three are valid declarations of a char? |
| Answer» (1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the integer value 27128, which is legal because it fits into an unsigned 16-bit integer. char c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, which fits into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of a character. char c2 = 'face'; is wrong because you can't put more than one character in a char literal. The only other acceptable char literal that can go between single quotes is a Unicode value, and Unicode literals must always start with a '\u'. char c4 = \u0022; is wrong because the single quotes are missing. char c5 = '\iface'; is wrong because it appears to be a Unicode representation (notice the backslash), but starts with '\i' rather than '\u'. | |
| 8. |
Which is the valid declarations within an interface definition? |
| Answer» Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract. Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract. Option C is wrong. static is concerned with the class and not an instance. Option D is wrong. protected is not permitted when declaring a method of an interface. See information below. Member declarations in an interface disallow the use of some declaration modifiers; you cannot use transient, volatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface. | |
| 9. |
In the given program, how many lines of output will be produced? |
| Answer» The loops use the array sizes (length). It produces 11 lines of output as given below. D:\Java>javac Test.java D:\Java>java Test size = 1 size = 2 size = 3 size = 4 size = 2 size = 3 size = 3 size = 4 size = 5 size = 6 size = 7 Therefore, 11 is the answer. | |
| 10. |
What will be the output of the program? |
| Answer» An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException. | |
| 11. |
Which four options describe the correct default values for array elements of the types indicated? |
| Answer» (1), (3), (4), (5) are the correct statements. (2) is wrong because the default value for a String (and any other object reference) is null, with no quotes. (6) is wrong because the default value for boolean elements is false. | |
| 12. |
Which one of these lists contains only Java programming language keywords? |
| Answer» All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function. Option A is wrong because the keyword for the primitive int starts with a lowercase i. Option C is wrong because "virtual" is a keyword in C++, but not Java. Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final. Option E is wrong because "include" is a keyword in C, but not in Java. | |
| 13. |
Which will legally declare, construct, and initialize an array? |
| Answer» The only legal array declaration and assignment statement is Option D Option A is wrong because it initializes an int array with String literals. Option B is wrong because it use something other than curly braces for the initialization. Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array. | |
| 14. |
Which is a reserved word in the Java programming language? |
| Answer» The word "native" is a valid keyword, used to modify a method declaration. Option A, D and E are not keywords. Option C is wrong because the keyword for subclassing in Java is extends, not 'subclasses'. | |
| 15. |
Which is a valid keyword in java? |
| Answer» interface is a valid keyword. Option B is wrong because although "String" is a class type in Java, "string" is not a keyword. Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float. Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java. | |