Which is a valid declaration within an interface?
Answer: A
(A) is valid interface declarations.
(B) and (C) are incorrect because interface variables cannot be either protected or transient. (D) is incorrect because interface methods cannot be final or static.
Which two cause a compiler error?
1.float[ ] f = new float(3);
2.float f2[ ] = new float[ ];
3.float[ ]f1 = new float[3];
4.float f3[ ] = new float[3];
5.float f5[ ] = {1.0f, 2.0f, 2.0f};
Answer: D
(1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f = new float[3];
(2) causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3];
(3), (4), and (5) compile without error.
Which of the following class level (nonlocal) variable declarations will not compile?
Answer: C
Option C will not compile; the synchronized modifier applies only to methods.
Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier.
Which one creates an instance of an array?
Answer: A
Option A is correct. It uses correct array declaration and correct array construction.
Option B is incorrect. It generates a compiler error: incompatible types because the array variable declaration is not correct. The array construction expects a reference type, but it is supplied with a primitive type in the declaration.
Option C is incorrect. It generates a compiler error: incompatible types because a string literal is not assignable to a character type variable.
Option D is wrong, it generates a compiler error
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Which is valid in a class that extends class A?
Answer: A
Option A is correct - because the class that extends A is just simply overriding method1.
Option B is wrong - because it can't override as there are less access privileges in the subclass method1.
Option C is wrong - because to override it, the return type needs to be an integer. The different return type means that the method is not overriding but the same argument list means that the method is not overloading. Conflict - compile time error.
Option D is wrong - because you can't override a method and make it a class method i.e. using static.