MCQs
Compilation failed because static was an illegal start of expression - method variables
do not have a modifier (they are always considered local).
A default no-args constructor is not created because there is a constructor
supplied that has an argument, line 4. Therefore the sub-class constructor
must explicitly make a call to the super class constructor:
public Sub(String text)
{
super(text); // this must be the first line constructor
i = 2;
}
Option D is correct. The specification at line 3 is for a method and not a constructor
and this method is never called therefore there is no output. The constructor that is
called is the default constructor.
What will be the output of the program?
class A
{
final public int GetResult(int a, int b) { return 0; }
}
class B extends A
{
public int GetResult(int a, int b) {return 1; }
}
public class Test
{
public static void main(String args[])
{
B b = new B();
System.out.println("x = " + b.GetResult(0, 1));
}
}
The code doesn't compile because the method GetResult() in class A is final and
so cannot be overridden.
(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.
The private access modifier limits access to members of the same class.
Option A, B, D, and E are wrong because protected are the wrong access modifiers,
and final, static, and volatile are modifiers but not access modifiers.
(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.
(3), (6). Both are legal class declarations.
(1) is wrong because a class cannot be abstract and finalâ€there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.
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.