How could you coordinate two threads that execute simultaneously inside synchronized methods of the same object?
Choice 1
place the threads in the same ThreadGroup and let the ThreadGroup acquire the object's monitor
Choice 2
dynamically control the priorities of the threads within the method
Choice 3
use Thread.yield() to switch between threads at set points
Choice 4
use Thread.wait() and Thread.notify()
Choice 5
use Thread.sleep()
Question 2.
int x=3, y=4, z=5;
switch(y) {
case 2:
x *= y;
case 4:
y = (x + z)/y;
case 6:
z += y++;
break;
case 8:
z++;
default:
x -= y;
}
What are the values of x, y, and z after execution?
Choice 1
x=12, y=5, z=10
Choice 2
x=12, y=5, z=9
Choice 3
x=3, y=3, z=7
Choice 4
x=0, y=3, z=8
Choice 5
x=3, y=3, z=8
Question 3
double dble;
byte bt = 99;
What is the correct way to cast a byte to a double?
Choice 1
dble (double) = bt
Choice 2
dble = bt(double)
Choice 3
you can not cast a byte to a double
Choice 4
dble = double(bt)
Choice 5
dble = (double) bt
Question 4
String readString() {
char buf[] = new char[80];
Reader in = new InputStreamReader(System.in);
in.read(buf, 0, 80);
return new String(buf);
}
What is wrong with method readString()?
Choice 1
String objects can not be instantiated with a character array
Choice 2
an InputStreamReader object can not be bound to standard input
Choice 3
all IO methods must be declared public
Choice 4
logic to catch exceptions for the InputStream statements is missing
Choice 5
you can not cast InputStreamReader into Reader
Question 5
class A {
int i, j, k;
public A() { i=3; }
public A(int i1) { i = i1; }
public A(int i1, int k1) {
this(i1);
k = k1;
}
}
What is wrong with class A?
Choice 1
methods can not invoke other methods in their own class
Choice 2
there are multiple methods named A()
Choice 3
nothing
Choice 4
all methods must declare return datatypes
Choice 5
a method can not have the same name as its class
Question 6
If memory may be running low, what could free some up?
Choice 1
call destroy() on any objects you no longer need
Choice 2
make more methods synchronized
Choice 3
invoke System.gc()
Choice 4
reduce the priority of less important threads
Choice 5
invoke System.garbageCollection()