class Super
{
public int i = 0;
public Super(String text) /* Line 4 */
{
i = 1;
}
}
class Sub extends Super
{
public Sub(String text)
{
i = 2;
}
public static void main(String args[])
{
Sub sub = new Sub("Hello");
System.out.println(sub.i);
}
}
A.
0
B.
1
C.
2
D.
Compilation fails.
Answer: Option D
Explanation:
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;
}
class Base
{
Base()
{
System.out.print("Base");
}
}
public class Alpha extends Base
{
public static void main(String[] args)
{
new Alpha(); /* Line 12 */
new Base(); /* Line 13 */
}
}
A.
Base
B.
BaseBase
C.
Compilation fails
D.
The code runs with no output
Answer: Option B
Explanation:
Option B is correct. It would be correct if the code had compiled, and the subclass Alpha had been saved in its own file. In this case Java supplies an implicit call from the sub-class constructor to the no-args constructor of the super-class therefore line 12 causes Base to be output. Line 13 also causes Base to be output.
Option A is wrong. It would be correct if either the main class or the subclass had not been instantiated.
Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
You can extend the Runnable interface as long as you override the public run() method.
The class must contain a method called run() from which all code for that thread will be initiated.
The class must contain an empty public void method named run().
The class must contain a public void method named runnable().
The class definition must include the words implements Threads and contain a method called run().
The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.
A.
1 and 3
B.
2 and 4
C.
1 and 5
D.
2 and 6
Answer: Option D
Explanation:
(2) and (6). When a thread's run() method completes, the thread will die. The run() method must be declared public void and not take any arguments.
(1) is incorrect because classes can never extend interfaces. (3) is incorrect because the run() method is typically not empty; if it were, the thread would do nothing. (4) is incorrect because the mandatory method is run(). (5) is incorrect because the class implements Runnable.
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}
A.
ABCD
B.
Compilation fails.
C.
C is printed before exiting with an error message.
D.
BC is printed before exiting with an error message.
Answer: Option C
Explanation:
Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.Error).
package foo;
import java.util.Vector; /* Line 2 */
private class MyVector extends Vector
{
int i = 1; /* Line 5 */
public MyVector()
{
i = 2;
}
}
public class MyNewVector extends MyVector
{
public MyNewVector ()
{
i = 4; /* Line 15 */
}
public static void main (String args [])
{
MyVector v = new MyNewVector(); /* Line 19 */
}
}
A.
Compilation will succeed.
B.
Compilation will fail at line 3.
C.
Compilation will fail at line 5.
D.
Compilation will fail at line 15.
Answer: Option B
Explanation:
Option B is correct. The compiler complains with the error "modifier private not allowed here". The class is created private and is being used by another class on line 19.
Which method registers a thread in a thread scheduler?
A.
run();
B.
construct();
C.
start();
D.
register();
Answer: Option C
Explanation:
Option C is correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
Option A is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.
Option B is wrong. There is no construct() method in the Thread class.
Option D is wrong. There is no register() method in the Thread class.
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
which of these will create and start this thread?
A.
new Runnable(MyRunnable).start();
B.
new Thread(MyRunnable).run();
C.
new Thread(new MyRunnable()).start();
D.
new MyRunnable().start();
Answer: Option C
Explanation:
Because the class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the instance of the Thread has to be started.
A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor.
B is incorrect for the same reason; you can't pass a class or interface name to any constructor.
D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */
}
When is the B object, created in line 3, eligible for garbage collection?
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 runs, how many objects are eligible for garbage collection?
A.
0
B.
1
C.
2
D.
3
Answer: Option C
Explanation:
This is an example of the islands of isolated objects. By the time line 11 has run, the objects instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of them.
Which of the following would compile without error?
A.
int a = Math.abs(-5);
B.
int b = Math.abs(5.0);
C.
int c = Math.abs(5.5F);
D.
int d = Math.abs(5L);
Answer: Option A
Explanation:
The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.
In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a".
The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".
String x = new String("xyz");
String y = "abc";
x = x + y;
How many String objects have been created?
A.
2
B.
3
C.
4
D.
5
Answer: Option C
Explanation:
Line 1 creates two, one referred to by x and the lost String "xyz". Line 2 creates one (for a total of three). Line 3 creates one more (for a total of four), the concatenated String referred to by x with a value of "xyzabc".
It is not possible in regular mathematics to get a value for the square-root of a negative number therefore a NaN will be returned because the code is valid.
public class Test
{
public static void main(String[] args)
{
final StringBuffer a = new StringBuffer();
final StringBuffer b = new StringBuffer();
new Thread()
{
public void run()
{
System.out.print(a.append("A"));
synchronized(b)
{
System.out.print(b.append("B"));
}
}
}.start();
new Thread()
{
public void run()
{
System.out.print(b.append("C"));
synchronized(a)
{
System.out.print(a.append("D"));
}
}
}.start();
}
}
A.
ACCBAD
B.
ABBCAD
C.
CDDACB
D.
Indeterminate output
Answer: Option D
Explanation:
It gives different output while executing the same compiled code at different times.
C:\>javac Test.java
C:\>java Test
ABBCAD
C:\>java Test
ACADCB
C:\>java Test
ACBCBAD
C:\>java Test
ABBCAD
C:\>java Test
ACBCBAD
C:\>java Test
ACBCBAD
C:\>java Test
ABBCAD