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.
2-
What will be the output of the program?
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
3-
What will be the output of the program?
import java.util.*;
public class NewTreeSet2 extends NewTreeSet
{
public static void main(String [] args)
{
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet
{
void count()
{
for (int x = 0; x < 7; x++,x++ )
{
System.out.print(" " + x);
}
}
}
A -
0 2 4
B -
0 2 4 6
C -
Compilation fails at line 2
D -
Compilation fails at line 10
4-
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
5-
What will be the output of the program?
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.
6-
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print("A");
}
catch (Exception ex) /* Line 10 */
{
System.out.print("B"); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print("C"); /* Line 16 */
}
System.out.print("D"); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}
A -
AB
B -
BC
C -
ABC
D -
BCD
7-
Which interface does java.util.Hashtable implement?
A -
Java.util.Map
B -
Java.util.List
C -
Java.util.HashTable
D -
Java.util.Collection
8-
What will be the output of the program?
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.
9-
What two statements are true about properly overridden hashCode() and equals() methods?
hashCode() doesn't have to be overridden if equals() is.
equals() doesn't have to be overridden if hashCode() is.
hashCode() can always return the same value, regardless of the object that invoked it.
equals() can be true even if it's comparing different objects.