Professional Documents
Culture Documents
2) How can you have a "try" block that invokes methods that throw two different exceptions?
a) Catch one exception in a "catch" block and the other in a "finally" block.
b) Setup nested "catch" blocks for each exception.
c) Catch one exception in a "catch" block and the other via the return value.
d) d)Use wait() between the calls to process all exceptions before continuing.
e) e)Include a "catch" block for each exception.
3] Which one of the following describes the difference between StringBuffer and String?
a) StringBuffer is used only to buffer data from an input or output stream.
b) b)StringBuffer allows text to be changed after instantiation.
c) c)StringBuffer holds zero length Strings.
d) StringBuffer supports Unicode.
e) StringBuffer is an array of Strings.
8) While overriding the method, its access modifier can be changed from: (Select correct option)
a) public to private b) protected to public c) public to default d) default to private
9) Select the correct sequence of the access modifiers from most restrictive to less resteictive:
a) public,protected,default,private b) public,default,protected,private
c) private,default,protected,public d) private, protected,default,public
class B extends A {}
Select the one correct answer.
a) The main() method must declare that it throws B.
b) The finally block must follow the catch block in the main() method.
c) The catch block in the main() method must declare that it catches B rather than A.
d) A single try block cannot be followed by both a finally and a catch block.
e) The declaration of class A is illegal.
14). Given:
public class Messager implements Runnable {
public static void main(String[] args) {
new Thread(new Messager("Wallace")).start() ;
new Thread(new Messager("Gromit")).start();
}
private String name;
public Messager(String name) { this.name = name; }
public void run() {
message(1);
message(2);
}
private synchronized void message(int n)
{
System.out.print(name + "-" + n + " ");
}
}
Which of the following is a possible result? (Choose all that apply.)
a) Wallace-1 Wallace-2 Gromit-1
b) Wallace-1 Gromit-2 Wallace-2 Gromit-1
c) Wallace-1 Gromit-1 Gromit-2 Wallace-2
d) Gromit-1 Gromit-2
e) Gromit-2 Wallace-1 Gromit-1 Wallace-2
f) The code does not compile.
g) An error occurs at run time.
15) . The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
. Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
a) public class MyRunnable extends Runnable{public void run(){}}
b) public class MyRunnable extends Object{public void run(){}}
c) public class MyRunnable implements Runnable{public void run(){}}
d) public class MyRunnable implements Runnable{void run(){}}
e) public class MyRunnable implements Runnable{public void start(){}}
16) Given:
class Emu {
static String s = "-";
public static void main(String[] args) {
try {
throw new Exception();
} catch (Exception e) {
try {
try { throw new Exception();
} catch (Exception ex) { s += "ic "; }
throw new Exception(); }
catch (Exception x) { s += "mc "; }
finally { s += "mf "; }
} finally { s += "of "; }
System.out.println(s);
}}
What is the result?
a) -ic of
b) -mf of
c) -mc mf
d) -ic mf of
e) -ic mc mf of
f) -ic mc of mf
g) Compilation fails.
17) Which of the following collections can you use to store key-value pairs and is thread safe?
a) HashTabl b) HashMa c) TreeMap d) Vector
18). Given:
public static void before () {
Set set = new TreeSet();
set.add("2");
set.add(3);
set.add("1");
Iterator it = set.iterator();
while (it.hasNext())
System.out.print(it.next() + " ");
}Which of the following statements are true?
21. . Which of these classes have a one-parameter constructor taking a string? Select the two
A) Void B) Integer C) Boolean D) Character E) Object
22.. What is the name of the interface used to represent collections that maintain non-unique elements in order? Select the one
correct answer.
a. Collection B) Set C) SortedSet D) List E) Sequence
23. Given:
public static void before () {
Set set = new TreeSet();
set.add("2");
set.add(3);
set.add("1");
Iterator it = set.iterator();
while (it.hasNext())
System.out.print(it.next() + " ");
}
Which of the following statements are true?
A. The before() method will print 1 2
B. The before() method will print 1 2 3
C. The before() method will print three numbers, but the order cannot be determined.
D. The before() method will not: compile.
E. The before() method will throw an exception at runtime.
24. Given:
class Swill {
public static void main(String[] args) {
String s = "-";
switch(TimeZone.CST) {
case EST: s += "e";
case CST: s += "c";
case MST: s += "m";
default: s += "X";
case PST: s += "p";
}
System.out.println(s);
}
}
enum TimeZone {EST, CST, MST, PST }
What is the result?
A. -c
B. -X
C. -cm
D. -cmp
E. -cmXp
F. Compilation fails.