public abstract class AbstractTest
{
public int getNum()
{
return 45;
}
public abstract class Bar
{
public int getNum()
{
return 38;
}
}
public static void main (String [] args)
{
AbstractTest t = new AbstractTest()
{
public int getNum()
{
return 22;
}
};
AbstractTest.Bar f = t.new Bar()
{
public int getNum()
{
return 57;
}
};
System.out.println(f.getNum() + " " + t.getNum());
}
}
A -
57 22
B -
45 38
C -
45 57
D -
An exception occurs at runtime.
2-
What will be the output of the program?
public class Q126 implements Runnable
{
private int x;
private int y;
public static void main(String [] args)
{
Q126 that = new Q126();
(new Thread(that)).start( ); /* Line 8 */
(new Thread(that)).start( ); /* Line 9 */
}
public synchronized void run( ) /* Line 11 */
{
for (;;) /* Line 13 */
{
x++;
y++;
System.out.println("x = " + x + "y = " + y);
}
}
}
A -
An error at line 11 causes compilation to fail
B -
Errors at lines 8 and 9 cause compilation to fail.
C -
The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")
D -
The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")
3-
What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread(); /* Line 5 */
t.run(); /* Line 6 */
}
public void run()
{
for(int i=1; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}
A -
This code will not compile due to line 5.
B -
This code will not compile due to line 6.
C -
1..2..
D -
1..2..3..
4-
What will be the output of the program?
public class ThreadTest extends Thread
{
public void run()
{
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String []argv)
{
(new ThreadTest()).start();
}
}
A -
The code fails to compile in the main() method
B -
The code fails to compile in the run() method
C -
Only the text "In run" will be displayed
D -
The text "In run" followed by "Leaving run" will be displayed
5-
What will be the output of the program?
public class Test107 implements Runnable
{
private int x;
private int y;
public static void main(String args[])
{
Test107 that = new Test107();
(new Thread(that)).start();
(new Thread(that)).start();
}
public synchronized void run()
{
for(int i = 0; i < 10; i++)
{
x++;
y++;
System.out.println("x = " + x + ", y = " + y); /* Line 17 */
}
}
}
A -
Compilation error.
B -
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
C -
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
D -
Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
6-
What allows the programmer to destroy an object x?
A -
x.delete()
B -
x.finalize()
C -
Runtime.getRuntime().gc()
D -
Only the garbage collection system can destroy an object.
7-
Which of the following statements is true?
A -
It is sometimes good practice to throw an AssertionError explicitly.
B -
Private getter() and setter() methods should not use assertions to verify arguments.
C -
If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
D -
It is proper to handle assertion statement failures using a catch (AssertionException ae) block.
8-
What will be the output of the program?
public class WrapTest
{
public static void main(String [] args)
{
int result = 0;
short s = 42;
Long x = new Long("42");
Long y = new Long(42);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);
if (x == y) /* Line 13 */
result = 1;
if (x.equals(y) ) /* Line 15 */
result = result + 10;
if (x.equals(z) ) /* Line 17 */
result = result + 100;
if (x.equals(x2) ) /* Line 19 */
result = result + 1000;
if (x.equals(z2) ) /* Line 21 */
result = result + 10000;
System.out.println("result = " + result);
}
}
A -
result = 1
B -
result = 10
C -
result = 11
D -
result = 11010
9-
What will be the output of the program?
class Q207
{
public static void main(String[] args)
{
int i1 = 5;
int i2 = 6;
String s1 = "7";
System.out.println(i1 + i2 + s1); /* Line 8 */
}
}
A -
18
B -
117
C -
567
D -
Compiler error
10-
What will be the output of the program?
public class StringRef
{
public static void main(String [] args)
{
String s1 = "abc";
String s2 = "def";
String s3 = s2; /* Line 7 */
s2 = "ghi";
System.out.println(s1 + s2 + s3);
}
}