Java Practice Test - 8

Java Practice Test - 8

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What will be the output of the following program?

class Exc0 extends Exception { } 
class Exc1 extends Exc0 { } /* Line 2 */
public class Test 
{  
    public static void main(String args[]) 
    { 
        try 
        {  
            throw new Exc1(); /* Line 9 */
        } 
        catch (Exc0 e0) /* Line 11 */
        {
            System.out.println(Ex0 caught); 
        } 
        catch (Exception e) 
        {
            System.out.println(exception caught);  
        } 
    } 
}
  1. Ex0 caught
  2. exception caught
  3. Compilation fails because of an error it line 2
  4. Compilation fails because of an error in line 9
Question 2 Multiple Choice (Single Answer)

Which two can be used to create a new Thread?

  1. Extend java.lang.Thread and override the run() method.
  2. Extend java.lang.Runnable and override the start() method.
  3. Implement java.lang.Thread and implement the run() method.
  4. Implement java.lang.Runnable and implement the run() method.
  5. Implement java.lang.Thread and implement the start() method.
  1. 1 and 2
  2. 2 and 3
  3. 1 and 4
  4. 3 and 4
Question 3 Multiple Choice (Single Answer)

The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

  1. public class MyRunnable extends Runnable{public void run(){}}
  2. public class MyRunnable extends Object{public void run(){}}
  3. public class MyRunnable implements Runnable{public void run(){}}
  4. public class MyRunnable implements Runnable{void run(){}}
Question 4 Multiple Choice (Single Answer)

What will be the output of the program?

public class ThreadDemo 
{ 
    private int count = 1; 
    public synchronized void doSomething() 
    { 
        for (int i = 0; i < 10; i++) 
            System.out.println(count++); 
    } 
    public static void main(String[] args) 
    { 
        ThreadDemo demo = new ThreadDemo(); 
        Thread a1 = new A(demo); 
        Thread a2 = new A(demo); 
        a1.start(); 
        a2.start(); 
    } 
} 
class A extends Thread 
{ 
    ThreadDemo demo; 
    public A(ThreadDemo td) 
    { 
        demo = td; 
    } 
    public void run() 
    { 
        demo.doSomething(); 
    } 
}
  1. It will print the numbers 0 to 19 sequentially.
  2. It will print the numbers 1 to 20 sequentially.
  3. It will print the numbers 1 to 20, but the order cannot be determined.
  4. The code will not compile.
Question 5 Multiple Choice (Single Answer)

What will be the output of the program?
int i = l, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
}
System.out.println(j = + j);

  1. j = -1
  2. j = 0
  3. j = 1
  4. compilation fails
Question 6 Multiple Choice (Single Answer)

What will be the output of the program?
class Tree { }
class Pine extends Tree { }
class Oak extends Tree { }
public class Forest1
{
public static void main (String [] args)
{
Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println ("Pine");
else if( tree instanceof Tree )
System.out.println ("Tree");
else if( tree instanceof Oak )
System.out.println ( "Oak" );
else
System.out.println ("Oops ");
}
}

  1. Pine
  2. Tree
  3. Forest
  4. Oops
Question 7 Multiple Choice (Single Answer)

What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}

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

What will be the output of the program?
String x = "xyz";
x.toUpperCase(); /* Line 2 */
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);

  1. abcXyZ
  2. abcxyz
  3. xyzabc
  4. XyZabc
Question 9 Multiple Choice (Single Answer)

Which statement is true?

  1. Assertions can be enabled or disabled on a class-by-class basis.
  2. Conditional compilation is used to allow tested classes to run at full speed.
  3. Assertions are appropriate for checking the validity of arguments in a method.
  4. The programmer can choose to execute a return statement or to throw an exception if an assertion fails.
Question 10 Multiple Choice (Single Answer)

Which cause a compiler error?

  1. int[ ] scores = {3, 5, 7};
  2. int [ ][ ] scores = {2,7,6}, {9,3,45};
  3. String cats[ ] = {"Fluffy", "Spot", "Zeus"};
  4. boolean results[ ] = new boolean [] {true, false, true};
  5. Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};
Question 11 Multiple Choice (Single Answer)

What two statements are true about the result obtained from calling Math.random()?

  1. The result is less than 0.0.
  2. The result is greater than or equal to 0.0..
  3. The result is less than 1.0.
  4. The result is greater than 1.0.
  5. The result is greater than or equal to 1.0.
  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. 4 and 5
Question 12 Multiple Choice (Single Answer)

Which three are valid method signatures in an interface?

  1. private int getArea();
  2. public float getVol(float x);
  3. public void main(String [ ] args);
  4. public static void main(String [ ] args);
  5. boolean setFlag(Boolean [ ] test);
  1. 1 and 2
  2. 2, 3 and 5
  3. 3, 4, and 5
  4. 2 and 4
Question 13 Multiple Choice (Single Answer)

Which interface provides the capability to store objects using a key-value pair?

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

public class Test2
{
public static int x;
public static int foo(int y)
{
return y * 2;
}
public static void main(String [] args)
{
int z = 5;
assert z > 0; /* Line 11 /
assert z > 2: foo(z); /
Line 12 /
if ( z < 7 )
assert z > 4; /
Line 14 */

    switch (z) 
    {
        case 4: System.out.println(&quot;4 &quot;);
        case 5: System.out.println(&quot;5 &quot;);
        default: assert z &lt; 10;
    }

    if ( z &lt; 10 )
        assert z &gt; 4: z++; /* Line 22 */
    System.out.println(z);
}

}
which line is an example of an inappropriate use of assertions?

  1. Line 11
  2. Line 12
  3. Line 14
  4. Line 22
Question 15 Multiple Choice (Single Answer)

What will be the output of the program?
class Super
{
public Integer getLength()
{
return new Integer(4);
}
}

public class Sub extends Super
{
public Long getLength()
{
return new Long(5);
}

public static void main(String[] args) 
{ 
    Super sooper = new Super(); 
    Sub sub = new Sub(); 
    System.out.println( 
    sooper.getLength().toString() + &quot;,&quot; + sub.getLength().toString() ); 
} 

}

  1. 4, 4
  2. 4, 5
  3. 5, 4
  4. Compilation fails.
Question 16 Multiple Choice (Single Answer)

What will be the output of the program?
boolean bool = true;
if(bool = false) /* Line 2 /
{
System.out.println("a");
}
else if(bool) /
Line 6 /
{
System.out.println("b");
}
else if(!bool) /
Line 10 /
{
System.out.println("c"); /
Line 12 */
}
else
{
System.out.println("d");
}

  1. a
  2. b
  3. c
  4. d
Question 17 Multiple Choice (Single Answer)

What will be the output of the program?

for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: break;
case 1: System.out.print(one );
case 2: System.out.print(two );
case 3: System.out.print(three );
}
}
System.out.println(done);

  1. done
  2. one two done
  3. one two three done
  4. one two three two three done
Question 18 Multiple Choice (Single Answer)

Which of the following statements about the hashcode() method are incorrect?

  1. The value returned by hashcode() is used in some collection classes to help locate objects.
  2. The hashcode() method is required to return a positive int value.
  3. The hashcode() method in the String class is the one inherited from Object.
  4. Two new empty String objects will produce identical hashcodes.
  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. 1 and 4
Question 19 Multiple Choice (Single Answer)

Which class or interface defines the wait(), notify(),and notifyAll() methods?

  1. Object
  2. Thread
  3. Runnable
  4. Class
Question 20 Multiple Choice (Single Answer)

Which of the following will directly stop the execution of a thread?

  1. wait()
  2. notify()
  3. notifyall()
  4. exits synchronized code
Question 21 Multiple Choice (Single Answer)

What will be the output of the program?
public class TestObj
{
public static void main (String [] args)
{
Object o = new Object() /* Line 5 /
{
public boolean equals(Object obj)
{
return true;
}
} /
Line 11 */

    System.out.println(o.equals(&quot;Fred&quot;));
}

}

  1. It prints "true".
  2. It prints "Fred".
  3. An exception occurs at runtime.
  4. Compilation fails
Question 22 Multiple Choice (Single Answer)

public class MyProgram
{
public static void throwit()
{
throw new RuntimeException();
}
public static void main(String args[])
{
try
{
System.out.println("Hello world ");
throwit();
System.out.println("Done with try block ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
which answer most closely indicates the behavior of the program?

  1. The program will not compile.
  2. The program will print Hello world, then will print that a RuntimeExceptionhas occurred, then will print Done with try block, and then will print Finally executing.
  3. The program will print Hello world, then will print that a RuntimeExceptionhas occurred, and then will print Finally executing.
  4. The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.
Question 23 Multiple Choice (Single Answer)

What will be the output of the program?
int i = (int) Math.random();

  1. i = 0
  2. i = 1
  3. value of i is undetermined
  4. Statement causes a compile error
Question 24 Multiple Choice (Single Answer)

public class Myfile
{
public static void main (String[] args)
{
String biz = args[1];
String baz = args[2];
String rip = args[3];
System.out.println("Arg is " + rip);
}
}
Select how you would start the program to cause it to print: Arg is 2

  1. java Myfile 222
  2. java Myfile 1 2 2 3 4
  3. java Myfile 1 3 2 2
  4. java Myfile 0 1 2 3
Question 25 Multiple Choice (Single Answer)

What will be the output of the program?
class A
{
public A(int x){}
}
class B extends A { }
public class test
{
public static void main (String args [])
{
A a = new B();
System.out.println("complete");
}
}

  1. It compiles and runs printing nothing
  2. Compiles but fails at runtime
  3. Compile Error
  4. Prints "complete"