Exercise: Language Fundamentals

Questions for: Language Fundamentals

public interface Foo 
{ 
    int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
  1. final int k = 4;
  2. public int k = 4;
  3. static int k = 4;
  4. abstract int k = 4;
  5. volatile int k = 4;
  6. protected int k = 4;
A:
1, 2 and 3
B:
2, 3 and 4
C:
3, 4 and 5
D:
4, 5 and 6
Answer: A

(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.

Which three are legal array declarations?
  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];
A:
1, 2, 4
B:
2, 4, 5
C:
2, 3, 4
D:
All are correct.
Answer: A

(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).

Which is a valid keyword in java?
A:
interface
B:
string
C:
Float
D:
unsigned
Answer: A

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.

Which is a reserved word in the Java programming language?
A:
method
B:
native
C:
subclasses
D:
reference
Answer: B

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'.

Which will legally declare, construct, and initialize an array?
A:
int [] myList = {"1", "2", "3"};
B:
int [] myList = (5, 8, 2);
C:
int myList [] [] = {4,9,7,0};
D:
int myList [] = {4, 3, 7};
Answer: D

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.

Ad Slot (Above Pagination)
Quiz