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
Questions
Which two statements are equivalent?
- 3/2
- 3<2
- 3*4
- 3<<2
- 1 and 2
- 2 and 3
- 3 and 4
- 1 and 4
Which statement is true for the class java.util.ArrayList?
- The elements in the collection are ordered.
- The collection is guaranteed to be immutable.
- The elements in the collection are guaranteed to be unique.
- The elements in the collection are accessed using a unique key.
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");
}
- Zero
- Twelve
- Default
- Compilation fails
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 ?
- Thread t = new Thread(X);
- Thread t = new Thread(X); t.start();
- X run = new X(); Thread t = new Thread(run); t.start();
- Thread t = new Thread(); x.run();
Which is true about a method-local inner class?
- It must be marked final.
- It can be marked abstract.
- It can be marked public.
- It can be marked static.
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 ");
}
}
- Compilation fails
- An exception occurs at runtime.
- It prints "Thread one. Thread two."
- The output cannot be determined.
Which of the following statements is true?
- In an assert statement, the expression after the colon ( : ) can be any Java expression.
- If a switch block has no default, adding an assert default is considered appropriate.
- In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
- It is appropriate to handle assertion failures using a catch clause.
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" ) ;
}
}
}
- If a is true and b is true then the output is "A && B"
- If a is true and b is false then the output is "notB"
- If a is false and b is true then the output is "ELSE"
- If a is false and b is false then the output is "ELSE"
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();
}
}
- Prints "Inside Thread Inside Thread"
- Prints "Inside Thread Inside Runnable"
- Does not compile
- Throws exception at runtime
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();
}
}
- DeadLock
- It print 12 12 12 12
- Compilation Error
- Cannot determine output.
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 */
}
}
- finally
- exception finished
- finally exception finished
- Compilation fails
Which of the following class level (nonlocal) variable declarations will not compile?
- protected int a;
- transient int b = 3;
- private synchronized int e;
- volatile int d;
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 ");
}
}
- bar
- bar done
- foo done
- Compilation fails
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?
- Line 5
- Line 6
- Line 12
- Line 14
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("4 ");
case 5: System.out.println("5 ");
default: assert z < 10;
}
if ( z < 10 )
assert z > 4: z++; /* Line 22 */
System.out.println(z);
}
}
which line is an example of an inappropriate use of assertions?
- Line 11
- Line 12
- Line 14
- Line 22
class A
{
A( ) { }
}
class B extends A
{ }
Which statement is true?
- Class B'S constructor is public.
- Class B'S constructor has no arguments.
- Class B'S constructor includes a call to this( ).
- None of these.
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]);
}
}
- It prints f2[0] = 0.0
- It prints f2[0] = NaN
- An error at f2 = f1; causes compile to fail.
- It prints the garbage value.
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");
}
}
- z
- x z
- y z
- Compilation fails.
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");
}
}
}
- 42
- 42.5
- 43
- bad number
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 + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
Two fix(Two tt)
{
tt.x = 42;
return tt;
}
}
- null null 42
- 0 0 42
- 0 42 42
- 0 0 0
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);
}
}
- java
- javac
- javajavac
- Compile error
/* 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 only
- 2 and 5
- 3 and 4
- 3 and 4
What will be the output of the program?
int i = O;
while(1)
{
if(i == 4)
{
break;
}
++i;
}
System.out.println(i = + i);
- i = 0
- i = 3
- i = 4
- Compilation fails
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);
}
}
- 41
- 42
- 50
- 51
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);
}
}
- 0
- 1
- 101
- 111