Questions
What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?
- public
- abstract
- protected
- synchronized
- default access
Which statement is true?
- A static method cannot be synchronized.
- If a class has synchronized code, multiple threads can still access the nonsynchronized code.
- Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
- When a thread sleeps, it releases its locks.
Which statement is true?
- Calling Runtime.gc() will cause eligible objects to be garbage collected.
- The garbage collector uses a mark and sweep algorithm.
- If an object can be accessed from a live thread, it can't be garbage collected.
- If object 1 refers to object 2, then object 2 can't be garbage collected.
What will be the output of the program?
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
- baz =
- baz = null
- baz = blue
- Runtime Exception
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 + "..");
}
}
}
- Compilation fails.
- 1..2..3..
- 0..1..2..3..
- 0..1..2..
Which statement is true about assertions in the Java programming language?
- Assertion expressions should not contain side effects.
- Assertion expression values can be any primitive type.
- Assertions should be used for enforcing preconditions on public methods.
- An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.
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 */
- i = 6 and j = 5
- i = 5 and j = 5
- i = 6 and j = 6
- i = 5 and j = 6
You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?
- public
- private
- protected
- default access
switch(x)
{
default:
System.out.println(Hello);
}
Which two are acceptable types for x?
- byte
- long
- char
- float
- Short
- Long
- 1 and 3
- 2 and 4
- 3 and 5
- 4 and 6
Which is a valid declaration within an interface?
- public static short stop = 23;
- protected short stop = 23;
- transient short stop = 23;
- final void madness(short stop);
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);
}
}
- 1
- 10
- 101
- 1101
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");
}
}
}
- AAACCC
- AAADDD
- BBBCCC
- BBBDDD
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();
}
}
- ABBCAD
- ABCBCAD
- CDADACB
- Output determined by the underlying platform.
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 );
}
}
- j = 0
- j = 4
- j = 8
- The code will run with no output
Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
- java.util.HashSet
- java.util.LinkedHashSet
- java.util.List
- java.util.ArrayList
Which of the following will not directly cause a thread to stop?
- notify()
- wait()
- InputStream access
- sleep()
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case y: System.out.print("0 "); /* Line 11 /
case x-1: System.out.print("1 "); / Line 12 /
case x: System.out.print("2 "); / Line 13 */
}
}
}
}
- 0 1 2
- 0 1 2 1 2 2
- Compilation fails at line 11.
- Compilation fails at line 12.
Which statement is true?
- Memory is reclaimed by calling Runtime.gc().
- Objects are not collected if they are accessible from live threads.
- An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.
- Objects that have finalize() methods always have their finalize()methods called before the program ends.
Which statement is true?
- 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.
- 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.
- 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.
- 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 as a direct and sole consequence of the notify call.
What will be the output of the program?
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?
- Synchronize the run method.
- Wrap a synchronize(this) around the call to f.increase().
- The existing code will cause a runtime exception.
- Synchronize the increase() method
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");
}
}
- finished
- Compilation fails.
- An AssertionError is thrown.
- An AssertionError is thrown and finished is output.
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 4; z++)
{
switch (z)
{
case x: System.out.print("0 ");
default: System.out.print("def ");
case x-1: System.out.print("1 ");
break;
case x-2: System.out.print("2 ");
}
}
}
}
- 0 def 1
- 2 1 0 def 1
- 2 1 0 def def
- 2 1 0 def 1 def 1
What will be the output of the program?
public class Test
{
public static void main (String args[])
{
String str = NULL;
System.out.println(str);
}
}
- NULL
- Compile Error
- Code runs but no output
- Runtime Exception
What will be the output of the program?
public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}
- 0 1 2
- 0 1 2 1 2 2
- 2 1 0 1 0 0
- 2 1 2 0 1 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);
}
}
- main() will finish before starting threads.
- main() will finish in the middle of one thread.
- main() will finish after one thread.
- Cannot be determined.