Java Practice Test - 10

Java Practice Test - 10

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which two statements are true about properly overridden hashCode() and equals() methods?

  1. hashCode() doesn't have to be overridden if equals() is.
  2. equals() doesn't have to be overridden if hashCode() is.
  3. hashCode() can always return the same value, regardless of the object that invoked it.
  4. equals() can be true even if it's comparing different objects.
  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. 1 and 3
Question 2 Multiple Choice (Single Answer)

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();
 }
}
  1. AB
  2. BC
  3. ABC
  4. BCD
Question 3 Multiple Choice (Single Answer)
public class MyRunnable implements Runnable 
{
    public void run() 
    {
        // some code here
    }
}

Which of these will create and start this thread?

  1. new Runnable(MyRunnable).start();
  2. new Thread(MyRunnable).run();
  3. new Thread(new MyRunnable()).start();
  4. new MyRunnable().start();
Question 4 Multiple Choice (Single Answer)
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?

  1. no code is necessary
  2. throws Exception
  3. catch ( Exception e )
  4. throws RuntimeException
Question 5 Multiple Choice (Single Answer)

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(); 
    } 
}
  1. BD
  2. BCD
  3. BDE
  4. BCDE
Question 6 Multiple Choice (Single Answer)

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(); 
    } 
}
  1. ACCBAD
  2. ABBCAD
  3. CDDACB
  4. Indeterminate output
Question 7 Multiple Choice (Single Answer)

What will be the output of the program?
int x = l, y = 6;
while (y--)
{
x++;
}
System.out.println(x = + x + y = + y);

  1. x = 6 y = 0
  2. x = 7 y = 0
  3. x = 6 y = -1
  4. compilation fails
Question 8 Multiple Choice (Single Answer)

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 */

  1. 0 2 4
  2. 0 2 4 5
  3. 0 1 2 3 4
  4. compilation fails
Question 9 Multiple Choice (Single Answer)

What will be the output of the program?
int x = 3;
int y = 1;
if (x = y) /* Line 3 */
{
System.out.println(x = + x);
}

  1. x = 1
  2. x = 3
  3. compilation fails
  4. no output
Question 10 Multiple Choice (Single Answer)

Which of the following cannot directly cause a thread to stop executing?

  1. Calling the SetPriority() method on a Thread object.
  2. Calling the wait() method on an object.
  3. Calling notify() method on an object.
  4. Calling read() method on an InputStream object.
Question 11 Multiple Choice (Single Answer)

Which of the following would compile without error?

  1. int a = Math.abs(-5);
  2. int b = Math.abs(5.0);
  3. int c = Math.abs(5.5F);
  4. int d = Math.abs(5L);
Question 12 Multiple Choice (Single Answer)

Which three are methods of the Object class?

  1. notify();
  2. notifyAll();
  3. isInterrupted();
  4. synchronized();
  5. interrupt();
  6. wait(long msecs);
  7. sleep(long msecs);
  8. yield();
  1. 1, 2, 4
  2. 2, 4, 5
  3. 1, 2, 6
  4. 2, 3, 4
Question 13 Multiple Choice (Single Answer)

Which method registers a thread in a thread scheduler?

  1. run();
  2. construct();
  3. start();
  4. register();
Question 14 Multiple Choice (Single Answer)

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 */
}
}

  1. Compilation will succeed.
  2. Compilation will fail at line 3.
  3. Compilation will fail at line 5.
  4. Compilation will fail at line 15.
Question 15 Multiple Choice (Single Answer)

What will be the output of the program?
System.out.println(Math.sqrt(-4D));

  1. -2
  2. NaN
  3. Compile Error
  4. Runtime Exception
Question 16 Multiple Choice (Single Answer)

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?

  1. after line 5
  2. after line 6
  3. after line 7
  4. There is no way to be absolutely certain.
Question 17 Multiple Choice (Single Answer)

Which interface does java.util.Hashtable implement?

  1. Java.util.Map
  2. Java.util.List
  3. Java.util.HashTable
  4. Java.util.Collection
Question 18 Multiple Choice (Single Answer)

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?

  1. 2
  2. 3
  3. 4
  4. 5
Question 19 Multiple Choice (Single Answer)

Which statement is true given the following?
Double d = Math.random();

  1. 0.0 < d <= 1.0
  2. 0.0 <= d < 1.0
  3. Compilation fail
  4. Cannot say.
Question 20 Multiple Choice (Single Answer)

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);
}
}

  1. true
  2. false
  3. Compilation fails
  4. An exception is thrown at runtime
Question 21 Multiple Choice (Single Answer)

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);
}
}

  1. 0
  2. 1
  3. 2
  4. Compilation fails.
Question 22 Multiple Choice (Single Answer)

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?

  1. 0
  2. 1
  3. 2
  4. 3
Question 23 Multiple Choice (Single Answer)

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] + &quot; &quot;);
    System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3) 
{
    a3[1] = 7;
    return a3;
}

}

  1. 12 15
  2. 15 15
  3. 3 4 5 3 7 5
  4. 3 7 5 3 7 5
Question 24 Multiple Choice (Single Answer)

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 );
}
}

  1. 0
  2. 7
  3. 8
  4. 14
Question 25 Multiple Choice (Single Answer)

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");
}
}

  1. ok
  2. dokey
  3. ok dokey
  4. No output is produced
  5. Compilation error