Java Advanced Practice Test - Threads, Assertions, and Collections

Advanced Java practice test covering threading concepts, assert statements, collections, exception handling, and complex code behavior analysis

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which two statements are equivalent?

  1. 3/2
  2. 3<2
  3. 3*4
  4. 3<<2
  1. 1 and 2
  2. 2 and 3
  3. 3 and 4
  4. 1 and 4
Question 2 Multiple Choice (Single Answer)

Which statement is true for the class java.util.ArrayList?

  1. The elements in the collection are ordered.
  2. The collection is guaranteed to be immutable.
  3. The elements in the collection are guaranteed to be unique.
  4. The elements in the collection are accessed using a unique key.
Question 3 Multiple Choice (Single Answer)

What will be the output of the program?
Float f = new Float("12");
switch (f)
{
case 12: System.out.println("Twelve");
case 0: System.out.println("Zero");
default: System.out.println("Default");
}

  1. Zero
  2. Twelve
  3. Default
  4. Compilation fails
Question 4 Multiple Choice (Single Answer)

class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}
Which of the following line of code is suitable to start a thread ?

  1. Thread t = new Thread(X);
  2. Thread t = new Thread(X); t.start();
  3. X run = new X(); Thread t = new Thread(run); t.start();
  4. Thread t = new Thread(); x.run();
Question 5 Multiple Choice (Single Answer)

Which is true about a method-local inner class?

  1. It must be marked final.
  2. It can be marked abstract.
  3. It can be marked public.
  4. It can be marked static.
Question 6 Multiple Choice (Single Answer)

What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run()
{
System.out.print("Thread ");
}
}

  1. Compilation fails
  2. An exception occurs at runtime.
  3. It prints "Thread one. Thread two."
  4. The output cannot be determined.
Question 7 Multiple Choice (Single Answer)

Which of the following statements is true?

  1. In an assert statement, the expression after the colon ( : ) can be any Java expression.
  2. If a switch block has no default, adding an assert default is considered appropriate.
  3. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
  4. It is appropriate to handle assertion failures using a catch clause.
Question 8 Multiple Choice (Single Answer)

public void foo( boolean a, boolean b)
{
if( a )
{
System.out.println("A"); /* Line 5 /
}
else if(a && b) /
Line 7 /
{
System.out.println( "A && B");
}
else /
Line 11 */
{
if ( !b )
{
System.out.println( "notB") ;
}
else
{
System.out.println( "ELSE" ) ;
}
}
}

  1. If a is true and b is true then the output is "A && B"
  2. If a is true and b is false then the output is "notB"
  3. If a is false and b is true then the output is "ELSE"
  4. If a is false and b is false then the output is "ELSE"
Question 9 Multiple Choice (Single Answer)

What will be the output of the program?
class MyThread extends Thread
{
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run()
{
System.out.print("Inside Thread ");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.print(" Inside Runnable");
}
}
class Test
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}
}

  1. Prints "Inside Thread Inside Thread"
  2. Prints "Inside Thread Inside Runnable"
  3. Does not compile
  4. Throws exception at runtime
Question 10 Multiple Choice (Single Answer)

What will be the output of the program?
class s implements Runnable
{
int x, y;
public void run()
{
for(int i = 0; i < 1000; i++)
synchronized(this)
{
x = 12;
y = 12;
}
System.out.print(x + " " + y + " ");
}
public static void main(String args[])
{
s run = new s();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start();
t2.start();
}
}

  1. DeadLock
  2. It print 12 12 12 12
  3. Compilation Error
  4. Cannot determine output.
Question 11 Multiple Choice (Single Answer)

What will be the output of the program?
public class Test
{
public static void aMethod() throws Exception
{
try /* Line 5 /
{
throw new Exception(); /
Line 7 /
}
finally /
Line 9 /
{
System.out.print("finally "); /
Line 11 /
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /
Line 20 /
{
System.out.print("exception ");
}
System.out.print("finished"); /
Line 24 */
}
}

  1. finally
  2. exception finished
  3. finally exception finished
  4. Compilation fails
Question 12 Multiple Choice (Single Answer)

Which of the following class level (nonlocal) variable declarations will not compile?

  1. protected int a;
  2. transient int b = 3;
  3. private synchronized int e;
  4. volatile int d;
Question 13 Multiple Choice (Single Answer)

What will be the output of the program?
public class Test
{
public static int y;
public static void foo(int x)
{
System.out.print("foo ");
y = x;
}
public static int bar(int z)
{
System.out.print("bar ");
return y = z;
}
public static void main(String [] args )
{
int t = 0;
assert t > 0 : bar(7);
assert t > 1 : foo(8); /* Line 18 */
System.out.println("done ");
}
}

  1. bar
  2. bar done
  3. foo done
  4. Compilation fails
Question 14 Multiple Choice (Single Answer)

public class Test
{
public void foo()
{
assert false; /* Line 5 /
assert false; /
Line 6 /
}
public void bar()
{
while(true)
{
assert false; /
Line 12 /
}
assert false; /
Line 14 */
}
}
What causes compilation to fail?

  1. Line 5
  2. Line 6
  3. Line 12
  4. Line 14
Question 15 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 16 Multiple Choice (Single Answer)

class A
{
A( ) { }
}

class B extends A
{ }
Which statement is true?

  1. Class B'S constructor is public.
  2. Class B'S constructor has no arguments.
  3. Class B'S constructor includes a call to this( ).
  4. None of these.
Question 17 Multiple Choice (Single Answer)

What will be the output of the program?
public class ArrayTest
{
public static void main(String[ ] args)
{
float f1[ ], f2[ ];
f1 = new float[10];
f2 = f1;
System.out.println("f2[0] = " + f2[0]);
}
}

  1. It prints f2[0] = 0.0
  2. It prints f2[0] = NaN
  3. An error at f2 = f1; causes compile to fail.
  4. It prints the garbage value.
Question 18 Multiple Choice (Single Answer)

What will be the output of the program (in jdk1.6 or above)?
public class BoolTest
{
public static void main(String [] args)
{
Boolean b1 = new Boolean("false");
boolean b2;
b2 = b1.booleanValue();
if (!b2)
{
b2 = true;
System.out.print("x ");
}
if (b1 & b2) /* Line 13 */
{
System.out.print("y ");
}
System.out.println("z");
}
}

  1. z
  2. x z
  3. y z
  4. Compilation fails.
Question 19 Multiple Choice (Single Answer)

What will be the output of the program?
public class NFE
{
public static void main(String [] args)
{
String s = "42";
try
{
s = s.concat(".5"); /* Line 8 */
double d = Double.parseDouble(s);
s = Double.toString(d);
int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
System.out.println(x);
}
catch (NumberFormatException e)
{
System.out.println("bad number");
}
}
}

  1. 42
  2. 42.5
  3. 43
  4. bad number
Question 20 Multiple Choice (Single Answer)

What will be the output of the program?
class Two
{
byte x;
}

class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}

void start() 
{
    Two t = new Two();
    System.out.print(t.x + &quot; &quot;);
    Two t2 = fix(t);
    System.out.println(t.x + &quot; &quot; + t2.x);
}

Two fix(Two tt) 
{
    tt.x = 42;
    return tt;
}

}

  1. null null 42
  2. 0 0 42
  3. 0 42 42
  4. 0 0 0
Question 21 Multiple Choice (Single Answer)

What will be the output of the program?
public class Test138
{
public static void stringReplace (String text)
{
text = text.replace ('j' , 'c'); /* Line 5 /
}
public static void bufferReplace (StringBuffer text)
{
text = text.append ("c"); /
Line 9 /
}
public static void main (String args[])
{
String textString = new String ("java");
StringBuffer textBuffer = new StringBuffer ("java"); /
Line 14 */
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println (textString + textBuffer);
}
}

  1. java
  2. javac
  3. javajavac
  4. Compile error
Question 22 Multiple Choice (Single Answer)

/* Missing statements ? /
public class NewTreeSet extends java.util.TreeSet
{
public static void main(String [] args)
{
java.util.TreeSet t = new java.util.TreeSet();
t.clear();
}
public void clear()
{
TreeMap m = new TreeMap();
m.clear();
}
}
which two statements, added independently at beginning of the program, allow the code to compile?
No statement is required
import java.util.
;
import.java.util.Tree*;
import java.util.TreeSet;
import java.util.TreeMap;

  1. 1 only
  2. 2 and 5
  3. 3 and 4
  4. 3 and 4
Question 23 Multiple Choice (Single Answer)

What will be the output of the program?

int i = O;
while(1)
{
if(i == 4)
{
break;
}
++i;
}
System.out.println(i = + i);

  1. i = 0
  2. i = 3
  3. i = 4
  4. Compilation fails
Question 24 Multiple Choice (Single Answer)

What will be the output of the program?
public class If1
{
static boolean b;
public static void main(String [] args)
{
short hand = 42;
if ( hand < 50 & !b ) /* Line 7 /
hand++;
if ( hand > 50 ); /
Line 9 */
else if ( hand > 40 )
{
hand += 7;
hand++;
}
else
--hand;
System.out.println(hand);
}
}

  1. 41
  2. 42
  3. 50
  4. 51
Question 25 Multiple Choice (Single Answer)

What will be the output of the program?
public class If2
{
static boolean b1, b2;
public static void main(String [] args)
{
int x = 0;
if ( !b1 ) /* Line 7 /
{
if ( !b2 ) /
Line 9 /
{
b1 = true;
x++;
if ( 5 > 6 )
{
x++;
}
if ( !b1 )
x = x + 10;
else if ( b2 = true ) /
Line 19 /
x = x + 100;
else if ( b1 | b2 ) /
Line 21 */
x = x + 1000;
}
}
System.out.println(x);
}
}

  1. 0
  2. 1
  3. 101
  4. 111