Questions
Which 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.
- 1 and 2
- 2 and 3
- 3 and 4
- 1 and 3
What will be the output of the program?
public class X {
public static void main(String[] args) {
try {
badMethod(); /* Line 5 */
System.out.print("A");
} catch (Exception ex) // Line 7
{
System.out.print("B"); // Line 9
} finally // Line 10
{
System.out.print("C"); // Line 12
}
System.out.print("D"); // Line 15
}
public static void badMethod() {
throw new RuntimeException();
}
}
- AB
- BC
- ABC
- BCD
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
Which of these will create and start this thread?
- new Runnable(MyRunnable).start();
- new Thread(MyRunnable).run();
- new Thread(new MyRunnable()).start();
- new MyRunnable().start();
public class ExceptionTest
{
class TestException extends Exception {}
public void runTest() throws TestException {}
public void test() /* Point X */
{
runTest();
}
}
At Point X in line 5, which code is necessary to make the code compile?
- no code is necessary
- throws Exception
- catch ( Exception e )
- throws RuntimeException
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print(A);
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print(B);
}
catch (Exception ex1)
{
System.out.print(C);
}
finally
{
System.out.print(D);
}
System.out.print(E);
}
public static void badMethod()
{
throw new RuntimeException();
}
}
- BD
- BCD
- BDE
- BCDE
What will be the output of the program?
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();
}
}
- ACCBAD
- ABBCAD
- CDDACB
- Indeterminate output
What will be the output of the program?
int x = l, y = 6;
while (y--)
{
x++;
}
System.out.println(x = + x + y = + y);
- x = 6 y = 0
- x = 7 y = 0
- x = 6 y = -1
- compilation fails
What will be the output of the program?
for (int i = 0; i < 4; i += 2)
{
System.out.print(i + );
}
System.out.println(i); /* Line 5 */
- 0 2 4
- 0 2 4 5
- 0 1 2 3 4
- compilation fails
What will be the output of the program?
int x = 3;
int y = 1;
if (x = y) /* Line 3 */
{
System.out.println(x = + x);
}
- x = 1
- x = 3
- compilation fails
- no output
Which of the following cannot directly cause a thread to stop executing?
- Calling the SetPriority() method on a Thread object.
- Calling the wait() method on an object.
- Calling notify() method on an object.
- Calling read() method on an InputStream object.
Which of the following would compile without error?
- int a = Math.abs(-5);
- int b = Math.abs(5.0);
- int c = Math.abs(5.5F);
- int d = Math.abs(5L);
Which three are methods of the Object class?
- notify();
- notifyAll();
- isInterrupted();
- synchronized();
- interrupt();
- wait(long msecs);
- sleep(long msecs);
- yield();
- 1, 2, 4
- 2, 4, 5
- 1, 2, 6
- 2, 3, 4
Which method registers a thread in a thread scheduler?
- run();
- construct();
- start();
- register();
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 */
}
}
- Compilation will succeed.
- Compilation will fail at line 3.
- Compilation will fail at line 5.
- Compilation will fail at line 15.
What will be the output of the program?
System.out.println(Math.sqrt(-4D));
- -2
- NaN
- Compile Error
- Runtime Exception
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?
- after line 5
- after line 6
- after line 7
- There is no way to be absolutely certain.
Which interface does java.util.Hashtable implement?
- Java.util.Map
- Java.util.List
- Java.util.HashTable
- Java.util.Collection
What will be the output of the program?
String x = new String("xyz");
String y = "abc";
x = x + y;
How many String objects have been created?
- 2
- 3
- 4
- 5
Which statement is true given the following?
Double d = Math.random();
- 0.0 < d <= 1.0
- 0.0 <= d < 1.0
- Compilation fail
- Cannot say.
What will be the output of the program?
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
- true
- false
- Compilation fails
- An exception is thrown at runtime
What will be the output of the program?
public class Test
{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
- 0
- 1
- 2
- Compilation fails.
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?
- 0
- 1
- 2
- 3
What will be the output of the program?
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
- 12 15
- 15 15
- 3 4 5 3 7 5
- 3 7 5 3 7 5
What will be the output of the program?
class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}
- 0
- 7
- 8
- 14
What will be the output of the program?
class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /Line 10/
System.out.println("dokey");
}
}
- ok
- dokey
- ok dokey
- No output is produced
- Compilation error