public class SyncTest
{
public static void main (String [] args)
{
Thread t = new Thread()
{
Foo f = new Foo();
public void run()
{
f.increase(20);
}
};
t.start();
}
}
class Foo
{
private int data = 23;
public void increase(int amt)
{
int x = data;
data = x + amt;
}
}
and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?
A -
Synchronize the run method.
B -
Wrap a synchronize(this) around the call to f.increase().
C -
The existing code will cause a runtime exception.
D -
Synchronize the increase() method
2-
What will be the output of the program?
class Test116
{
static final StringBuffer sb1 = new StringBuffer();
static final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("A");
sb2.append("B");
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("C");
sb2.append("D");
}
}
}.start(); /* Line 28 */
System.out.println (sb1 + " " + sb2);
}
}
A -
main() will finish before starting threads.
B -
main() will finish in the middle of one thread.
C -
main() will finish after one thread.
D -
Cannot be determined.
3-
Which statement is true?
A -
If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
B -
If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
C -
If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
D -
If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution.
4-
Which statement is true?
A -
Memory is reclaimed by calling Runtime.gc().
B -
Objects are not collected if they are accessible from live threads.
C -
An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.
D -
Objects that have finalize() methods always have their finalize() methods called before the program ends.
5-
What will be the output of the program (when you run with the -ea option) ?
public class Test
{
public static void main(String[] args)
{
int x = 0;
assert (x > 0) : "assertion failed"; /* Line 6 */
System.out.println("finished");
}
}
A -
finished
B -
Compilation fails.
C -
An AssertionError is thrown.
D -
An AssertionError is thrown and finished is output.
6-
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?
A -
Line 11
B -
Line 12
C -
Line 14
D -
Line 22
7-
Which statement is true?
A -
Assertions can be enabled or disabled on a class-by-class basis.
B -
Conditional compilation is used to allow tested classes to run at full speed.
C -
Assertions are appropriate for checking the validity of arguments in a method.
D -
The programmer can choose to execute a return statement or to throw an exception if an assertion fails.
8-
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);
A -
abcXyZ
B -
abcxyz
C -
xyzabc
D -
XyZabc
9-
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 ");
}
}
A -
Pine
B -
Tree
C -
Forest
D -
Oops
10-
What two statements are true about the result obtained from calling Math.random()?