public class Test
{
public static void main (String[] args)
{
String foo = args[1];
String bar = args[2];
String baz = args[3];
System.out.println("baz = " + baz); /* Line 8 */
}
}
And the command line invocation:
> java Test red green blue
A -
baz =
B -
baz = null
C -
baz = blue
D -
Runtime Exception
2-
What will be the output of the program?
class Happy extends Thread
{
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
final Happy h = new Happy();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("A");
h.sb2.append("B");
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("D");
h.sb2.append("C");
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start();
}
}
A -
ABBCAD
B -
ABCBCAD
C -
CDADACB
D -
Output determined by the underlying platform.
3-
What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
Thread x = new Thread(t);
x.start(); /* Line 7 */
}
public void run()
{
for(int i = 0; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}
A -
0..1..2..
B -
0..1..2..3..
C -
1..2..3..
D -
Compilation fails.
4-
Which statement is true?
A -
A static method cannot be synchronized.
B -
If a class has synchronized code, multiple threads can still access the nonsynchronized code.
C -
Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
D -
When a thread sleeps, it releases its locks.
5-
Which statement is true?
A -
Calling Runtime.gc() will cause eligible objects to be garbage collected.
B -
The garbage collector uses a mark and sweep algorithm.
C -
If an object can be accessed from a live thread, it can't be garbage collected.
D -
If object 1 refers to object 2, then object 2 can't be garbage collected.
6-
Which statement is true about assertions in the Java programming language?
A -
Assertion expressions should not contain side effects.
B -
Assertion expression values can be any primitive type.
C -
Assertions should be used for enforcing preconditions on public methods.
D -
An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.
7-
What will be the output of the program?
public class ObjComp
{
public static void main(String [] args )
{
int result = 0;
ObjComp oc = new ObjComp();
Object o = oc;
if (o == oc)
result = 1;
if (o != oc)
result = result + 10;
if (o.equals(oc) )
result = result + 100;
if (oc.equals(o) )
result = result + 1000;
System.out.println("result = " + result);
}
}
A -
1
B -
10
C -
1101
D -
1101
8-
What will be the output of the program?
public class Test178
{
public static void main(String[] args)
{
String s = "foo";
Object o = (Object)s;
if (s.equals(o))
{
System.out.print("AAA");
}
else
{
System.out.print("BBB");
}
if (o.equals(s))
{
System.out.print("CCC");
}
else
{
System.out.print("DDD");
}
}
}
A -
AAACCC
B -
AAADDD
C -
BBBCCC
D -
BBBDDD
9-
What will be the output of the program?
int i = 1, j = 10;
do
{
if(i++ > --j) /* Line 4 */
{
continue;
}
} while (i < 5);
System.out.println("i = " + i + "and j = " + j); /* Line 9 */
A -
i = 6 and j = 5
B -
i = 5 and j = 5
C -
i = 6 and j = 6
D -
i = 5 and j = 6
10-
What will be the output of the program?
public class ExamQuestion7
{
static int j;
static void methodA(int i)
{
boolean b;
do
{
b = i<10 | methodB(4); /* Line 9 */
b = i<10 || methodB(8); /* Line 10 */
}while (!b);
}
static boolean methodB(int i)
{
j += i;
return true;
}
public static void main(String[] args)
{
methodA(0);
System.out.println( "j = " + j );
}
}