You are on page 1of 22

Question : 1 Max Marks of this Question : 1.

0 All codes that are to be executed in a thread are to be placed inside the ________________method.

Question : 2 Max Marks of this Question : 1.0 Given the scenario: This class is intended to allow users to write a series of messages, so that each message is identified with a timestamp and the name of the thread that wrote the message: public class Logger { private StringBuilder contents = new StringBuilder(); public void log(String message) { contents.append(System.currentTimeMillis()); contents.append(": "); contents.append(Thread.currentThread().getName()); contents.append(message); contents.append("\n"); } public String getContents() { return contents.toString(); } } How can we ensure that instances of this class can be safely used by multiple threads? a. b. c. d. e. f. Replacing StringBuilder with StringBuffer will make this class thread-safe. This class is already thread-safe. Synchronize the getContents() method only. Synchronize the log() method only. Synchronize both log() and getContents(). This class cannot be made thread-safe.

Question : 3 Max Marks of this Question : 1.0 What will be the result of invoking the wait() method on an object without ensuring that the current Thread holds the lock of the object? Select the one correct answer. a. b. c. d. Nothing special will happen. The thread will be blocked until it gains the lock of the object. An IllegalMonitorStateException will be thrown if the wait() method is called while the current thread does not hold the lock of the object. The code will fail to compile.

Question : 4 Max Marks of this Question : 1.0 Given the following: class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.print(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.print(" foo"); } }; t.start(); } } What is the result?

a. b. c. d. e. f. g. h.

foo bar baz foo Compilation fails. bar foo An exception is thrown at runtime. MyThread bar foo bar MyThread foo

Question : 5 Max Marks of this Question : 1.0 A thread wants to make a second thread ineligible for execution. To do this, the first thread can call the yield( ) method on the second thread. a. True b. False

Question : 6 Max Marks of this Question : 1.0 Which of these are plausible reasons why a thread might be alive, but still not be running? Select the four correct answers. a. b. c. d. e. The execution has reached the end of the run() method. The thread is waiting for some condition as a result of a wait() call. The thread is waiting to acquire the lock of an object in order to execute a certain method on that object. The thread is sleeping as a result of a call to the sleep() method. The thread does not have the highest priority and is currently not executing.

Question : 7 Max Marks of this Question : 1.0 Given the following 1. public class WaitTest 2. { 3. public static void main(String [] args) 4. { 5. System.out.print("1 "); 6. synchronized(args) 7. { 8. System.out.print("2 "); 9. try 10. { 11. args.wait(); 12. } 13. catch(InterruptedException e) 14. { 15. } 16. } 17. System.out.print("3 "); 18. } 19. } What is the result of trying to compile and run this program?

a. b. c. d. e. f.

It will fail to compile because it has to be synchronized on the this object. It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 11. 123 At runtime, it throws an IllegalMonitorStateException when trying to wait. 13 12

Question : 8 Max Marks of this Question : 1.0 Which statement is true? Select the one correct answer. Inside a synchronized method, one can assume that no other threads are currently executing any other methods in the same class. Methods declared synchronized should not be recursive, since the object lock will not allow new invocations of the method. No two threads can concurrently execute synchronized methods on the same object. Synchronized methods can only call other synchronized methods directly.

a. b. c. d.

Question : 9 Max Marks of this Question : 1.0 What can be guaranteed by calling the method yield()? Select the one correct answer. a. b. c. d. e. The current thread will not continue until other threads have terminated. The current thread will sleep for some time while some other threads run. All lower priority threads will be granted CPU time. None of the above. The thread will wait until it is notified.

Question : 10 Max Marks of this Question : 1.0 Which statements are true about the following code? public class Joining { static Thread createThread(final int i, final Thread t1) { Thread t2 = new Thread() { public void run() { System.out.println(i+1); try { t1.join(); } catch (InterruptedException e) { } System.out.println(i+2); } }; System.out.println(i+3); t2.start(); System.out.println(i+4); return t2; } public static void main(String[] args) { createThread(10, createThread(20, Thread.currentThread())); } } Select the two correct answers.

a. b. c. d. e.

The number 14 is printed before the number 22. The number 11 is printed before the number 23. The last number printed is 12. The first number printed is 13. The number 24 is printed before the number 21.

Question : 11 Max Marks of this Question : 1.0 Given the following: class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); } } What is the result of this code? a. b. c. d. An exception occurs at runtime. The output cannot be determined. Thread one. Thread two. Compilation fails.

Question : 12 Max Marks of this Question : 1.0 Given the following class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); } public void run() { for(int i=0;i<3;++i) { System.out.print(i + ".."); } } } What is the result of this code? a. b. c. d. e. An exception occurs at runtime. Compilation fails. 1..2..3.. 0..1..2.. 0..1..2..3..

Question : 13 Max Marks of this Question : 1.0 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. b. c. d. e.

public class MyRunnable extends Runnable { public void run() { } } public class MyRunnable implements Runnable { void run() { } } public class MyRunnable extends Object { public void run() { } } public class MyRunnable implements Runnable { public void start() { } } public class MyRunnable implements Runnable { public void run() { } }

Question : 14 Max Marks of this Question : 1.0 How can the priority of a thread be set? Select the one correct answer. a. b. c. d. By using the setPriority() method in the class Thread. By passing the priority as a parameter to the constructor of the thread. Both of the above. None of the above.

Question : 16 Max Marks of this Question : 1.0 Assume you have a class that holds two private variables: a and b. Which of the following pairs can prevent concurrent access problems in that class? (Choose all that apply.) public int read() { synchronized(a) { return a+b; } } public void set(int a, int b) { synchronized(a) { this.a=a; this.b=b; } } public int read() { return a+b; } public void set(int a, int b) { this.a=a; this.b=b; } public synchronized int read() { return a+b; } public synchronized void set(int a, int b) { this.a=a; this.b=b; } public int read() { synchronized(a) { return a+b; } } public void set(int a, int b) { synchronized(b) { this.a=a; this.b=b; } } public synchronized(this) int read() { return a+b; } public synchronized(this) void set(int a, int b) { this.a=a; this.b=b; } public int read() { synchronized(this) { return a+b; } } public void set(int a, int b) { synchronized(this) { this.a=a; this.b=b; } }

a. b. c. d. e. f.

Question : 17 Max Marks of this Question : 1.0 Which statements are true? Select the two correct answers.

a. b. c. d. e.

The class Thread is abstract. A program terminates when the last non-daemon thread ends. The class Thread implements Runnable. Classes implementing the Runnable interface must define a method named start. Calling the method run() on an object implementing Runnable will create a new thread.

Question : 18 Max Marks of this Question : 1.0 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. b. c. d. e. f. g. Wallace-1 Gromit-2 Wallace-2 Gromit-1 Gromit-1 Gromit-2 Wallace-1 Gromit-1 Gromit-2 Wallace-2 An error occurs at run time. The code does not compile. Wallace-1 Wallace-2 Gromit-1 Gromit-2 Wallace-1 Gromit-1 Wallace-2

Question : 19 Max Marks of this Question : 1.0 Which one of these events will cause a thread to die? Select the one correct answer. Not Attempted a. b. c. d. e. Execution of the thread's constructor ends. The method sleep() is called. Execution of the start() method ends. The method wait() is called. Execution of the run() method ends.

Question : 20 Max Marks of this Question : 1.0 Which are true? (Choose all that apply.)

a. b. c. d. e. f. g.

When a thread is waiting as a result of wait(), it release its lock. The notify() method causes a thread to immediately release its lock. The notify() method is defined in class java.lang.Thread. The notifyAll() method must be called from a synchronized context. waiting threads, regardless of the object they're waiting on. To call wait(), an object must own the lock on the thread. The difference between notify() and notifyAll() is that notifyAll() notifies all

Question : 21 Max Marks of this Question : 1.0 Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU? a. b. c. d. After object B is notified, or after two seconds. After the lock on B is released, or after two seconds. Two seconds after lock B is released. Two seconds after object B is notified.

Question : 22 Max Marks of this Question : 1.0 What will be the result of attempting to compile and run the following program? public class MyClass extends Thread { public MyClass(String s) { msg = s; } String msg; public void run() { System.out.println(msg); } public static void main(String[] args) { new MyClass("Hello"); new MyClass("World"); } } Select the one correct answer. a. b. c. d. e. The program will fail to compile. The program will compile without errors and will print Hello and World when run, but the order is unpredictable. The program will compile without errors and will simply terminate without any output when run. The program will compile without errors and will print Hello and World, in that order, every time the program is run. The program will compile without errors and will print a never-ending stream of Hello and World.

Question : 23 Max Marks of this Question : 1.0 If you attempt to compile and execute the following application, will it ever print out the

message in xxx? Class TestThread3 extends Thread { public void run() { System.out.println("Running"); System.out.println("Done"); } private void xxx() { System.out.println("In xxx"); } public static void main(String args[]) { TestThread3 ttt = new TestThread3( ); ttt.xxx( ); ttt.start( ); } } a. False b. True

Question : 24 Max Marks of this Question : 1.0 Given: public static synchronized void main(String[] args) throws InterruptedException { Thread t = new Thread(); t.start(); System.out.print("X"); t.wait(10000); System.out.print("Y"); } What is the result of this code? a. b. c. d. e. f. g. It prints XY with a 10-second delay between X and Y. It prints XY with a 10000-second delay between X and Y. The code does not compile. It prints X and exits. It prints X and never exits. It prints XY and exits almost immeditately. An exception is thrown at runtime.

Question : 25 Max Marks of this Question : 1.0 Which statements are true about locks? Select the two correct answers. Invoking notify() on a object whose lock is held by the current thread will relinquish the lock. A thread can hold more than one lock at a time. Invoking wait() on a Thread object will relinquish all locks held by the thread. Invoking wait() on an object whose lock is held by the current thread will relinquish the lock. Multiple threads can hold the same lock at the same time.

a. b. c. d. e.

Question : 26 Max Marks of this Question : 1.0 Given the following program, which statement is true? public class MyClass extends Thread { static Object lock1 = new Object(); static Object lock2 = new Object(); static volatile int i1, i2, j1, j2, k1, k2; public void run() { while (true) { doit(); check(); } } void doit() { synchronized(lock1) { i1++; } j1++; synchronized(lock2) { k1++; k2++; } j2++; synchronized(lock1) { i2++; } } void check() { if (i1 != i2) System.out.println("i"); if (j1 != j2)

System.out.println("j"); if (k1 != k2) System.out.println("k"); } public static void main(String[] args) { new MyClass().start(); new MyClass().start(); } } Select the one correct answer. a. b. c. d. e. One can be certain that the letter k will never be printed during execution. The program will fail to compile. One can be certain that the letters i and k will never be printed during execution. One can be certain that none of the letters i, j, and k will ever be printed during execution. One cannot be certain whether any of the letters i, j, and k will be printed during execution.

Question : 27 Max Marks of this Question : 1.0 Which of the following statements about threads is true? Select the one correct answer. a. b. c. d. Thread priority is an integer ranging from 1 to 100. Every thread starts with a priority of 5. Threads are guaranteed to run with the priority that you set using the setPriority( ) method. Threads inherit their priority from their parent thread.

Question : 28 Max Marks of this Question : 1.0 To bring a thread to a runnable state, the _________________________method is to be called.

Question : 29 Max Marks of this Question : 1.0 Given the following: class Test { public static void main(String [] args) { printAll(args); } public static void printAll(String[] lines) { for(int i=0;i<="" td=""> a. b. c. d. e. Each String in the array lines will print, with exactly a 1-second pause between lines. This code will not compile. Each String in the lines array will print, with at least a one-second pause between lines. Each String in the array lines will print, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread. Each String in the array lines will print, with no pause in between because this method is not executed in a Thread. Max Marks of this Question : 1.0

Question : 30

Which are methods of the Object class? (Choose all that apply.) a. b. c. d. e. f. g. h. sleep(long msecs); yield(); isInterrupted(); wait(long msecs); notifyAll(); notify(); synchronized(); interrupt();

Mock exam 1 for SCJP 6


The sample test is modeled on the Sun Certification for JavaTM 6 Programmer exam. The test has 50 questions and needs to be executed in 2 hours. The real exam may be a little tougher than this. You need to score 35 correct answers out of 60 to clear the real exam in 180 minutes. Please let us know at ngabrani At hotmail dot com if you find any issues with the test. The site also offers another mock exam and questions by topic. 1. Which declaration of the main method below would allow a class to be started as a standalone program. Select the one correct answer. A. public static int main(char args[]) B. public static void main(String args[]) C. public static void MAIN(String args[]) D. public static void main(String args) E. public static void main(char args[]) 2. What all gets printed when the following code is compiled and run? Select the three correct answers.
public class xyz { public static void main(String args[]) { for(int i = 0; i < 2; i++) { for(int j = 2; j>= 0; j--) { if(i == j) break; System.out.println("i=" + i + " j="+j);

} } }

A. i=0 j=0 B. i=0 j=1 C. i=0 j=2 D. i=1 j=0 E. i=1 j=1 F. i=1 j=2 G. i=2 j=0 H. i=2 j=1 I. i=2 j=2 3. What gets printed when the following code is compiled and run with the following command java test 2 Select the one correct answer.
public class test { public static void main(String args[]) { Integer intObj=Integer.valueOf(args[args.length-1]); int i = intObj.intValue(); if(args.length > 1) System.out.println(i); if(args.length > 0) System.out.println(i - 1); else System.out.println(i - 2);

} }

A. test B. test -1 C. 0 D. 1 E. 2 4. In Java technology what expression can be used to represent number of elements in an array named arr ? 5. How would the number 5 be represented in hex using up-to four characters. 6. Which of the following is a Java keyword. Select the four correct answers. A. extern B. synchronized C. volatile D. friend

E. friendly F. transient G. this H. then 7. Is the following statement true or false. The constructor of a class must not have a return type. A. true B. false 8. What is the number of bytes used by Java primitive long. Select the one correct answer. A. The number of bytes is compiler dependent. B. 2 C. 4 D. 8 E. 64 9. What is returned when the method substring(2, 4) is invoked on the string "example"? Include the answer in quotes as the result is of type String. 10. Which of the following is correct? Select the two correct answers. A. The native keyword indicates that the method is implemented in another language like C/C++. B. The only statements that can appear before an import statement in a Java file are comments. C. The method definitions inside interfaces are public and abstract. They cannot be private or protected. D. A class constructor may have public or protected keyword before them, nothing else. 11. What is the result of evaluating the expression 14 ^ 23. Select the one correct answer. A. 25 B. 37 C. 6 D. 31 E. 17 F. 9 G. 24 12. Which of the following are true. Select the one correct answers. A. && operator is used for short-circuited logical AND. B. ~ operator is the bit-wise XOR operator. C. | operator is used to perform bitwise OR and also short-circuited logical OR. D. The unsigned right shift operator in Java is >>. 13. Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class. 14. Which of the following is true. Select the two correct answers. A. A class that is abstract may not be instantiated. B. The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++. C. A static variable indicates there is only one copy of that variable.

D. A method defined as private indicates that it is accessible to all other classes in the same package. 15. What all gets printed when the following program is compiled and run. Select the two correct answers.

public class test { public static void main(String args[]) { int i, j=1; i = (j>1)?2:1; switch(i) { case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } } }

A. 0 B. 1 C. 2 D. 3 16. What all gets printed when the following program is compiled and run. Select the one correct answer.

public class test { public static void main(String args[]) { int i=0, j=2; do { i=++i; j--; } while(j>0); System.out.println(i); } }

A. 0 B. 1 C. 2 D. The program does not compile because of statement "i=++i;" 17. What all gets printed when the following gets compiled and run. Select the three correct answers.

public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i/j > 1) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } }

A. 0 B. 1 C. 2 D. 3 E. 4 18. What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i == j) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); }

finally { System.out.println(3); } System.out.println(4); } }

A. 0 B. 1 C. 2 D. 3 E. 4 19. What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test { public static void main(String args[]) { String s1 = "abc"; String s2 = "abc"; if(s1 == s2) System.out.println(1); else System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); } }

A. 1 B. 2 C. 3 D. 4 20. What all gets printed when the following gets compiled and run. Select the two correct answers.

public class test { public static void main(String args[]) { String s1 = "abc"; String s2 = new String("abc"); if(s1 == s2) System.out.println(1); else

System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); } }

A. 1 B. 2 C. 3 D. 4 21. Which of the following are legal array declarations. Select the three correct answers. A. int i[5][]; B. int i[][]; C. int []i[]; D. int i[5][5]; E. int[][] a; 22. What is the range of values that can be specified for an int. Select the one correct answer. A. The range of values is compiler dependent. B. -231 to 231 - 1 C. -231-1 to 231 D. -215 to 215 - 1 E. -215-1 to 215 23. How can you ensure that the memory allocated by an object is freed. Select the one correct answer. A. By invoking the free method on the object. B. By calling system.gc() method. C. By setting all references to the object to new values (say null). D. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object. 24. What gets printed when the following code is compiled and run. Select the one correct answer.

public class test { public static void main(String args[]) { int i = 1; do { i--; } while (i > 2); System.out.println(i); } }

A. 0 B. 1 C. 2 D. -1 25. Which of these is a legal definition of a method named m assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select the one correct answer. A. void m() throws IOException{} B. void m() throw IOException{} C. void m(void) throws IOException{} D. m() throws IOException{} E. void m() {} throws IOException 26. Which of the following are legal identifier names in Java. Select the two correct answers. A. %abcd B. $abcd C. 1abcd D. package E. _a_long_name 27. At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.

void method X() { String r = new String("abc"); String s = new String("abc"); r = r+1; //1 r = null; //2 s = s + r; //3 } //4

A. B. C. D. E.

Before statement labeled 1 Before statement labeled 2 Before statement labeled 3 Before statement labeled 4 Never.

28. String s = new String("xyz");

Assuming the above declaration, which of the following statements would compile. Select the one correct answer. A. s = 2 * s; B. int i = s[0]; C. s = s + s; D. s = s >> 2; E. None of the above. 29. Which of the following statements related to Garbage Collection are correct. Select the two correct answers.

A. It is possible for a program to free memory at a given time. B. Garbage Collection feature of Java ensures that the program never runs out of memory. C. It is possible for a program to make an object available for Garbage Collection. D. The finalize method of an object is invoked before garbage collection is performed on the object. 30. If a base class has a method defined as void method() { } Which of the following are legal prototypes in a derived class of this class. Select the two correct answers. A. void method() { } B. int method() { return 0;} C. void method(int i) { } D. private void method() { } 31. In which all cases does an exception gets generated. Select the two correct answers.
int i = 0, j = 1;

A. if((i == 0) || (j/i == 1)) B. if((i == 0) | (j/i == 1)) C. if((i != 0) && (j/i == 1)) D. if((i != 0) & (j/i == 1)) 32. Which of the following statements are true. Select the two correct answers. A. The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state. B. The wait(), notify(), and notifyAll() methods must be executed in synchronized code. C. The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state. D. The Thread class is an abstract class. 33. Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer. A. transient B. volatile C. synchronized D. native E. static F. final 34. What is the name of the Collection interface used to represent elements in a sequence (in a particular order). Select the one correct answer. A. Collection B. Set C. List D. Map 35. Which of these classes implement the Collection interface SortedMap. Select the one correct answers.

A. HashMap B. Hashtable C. TreeMap D. HashSet E. TreeSet F. Vector 36. Which of the following are true about interfaces. Select the two correct answers. A. Methods declared in interfaces are implicitly private. B. Variables declared in interfaces are implicitly public, static, and final. C. An interface can extend any number of interfaces. D. The keyword implements indicate that an interface inherits from another. 37. Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer. A. test(); B. super.test(); C. super.super.test(); D. ::test(); E. C.test(); F. It is not possible to invoke test() method defined in C from a method in A. 38. What is the return type of method round(double d) defined in Math class. 39. What gets written on the screen when the following program is compiled and run. Select the one right answer.
public class test { public static void main(String args[]) { int i; float f = 2.3f; double d = 2.7; i = ((int)Math.ceil(f)) * ((int)Math.round(d)); System.out.println(i); } }

A. 4 B. 5 C. 6 D. 6.1 E. 9 40. Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object. A. true B. false 41. Which of these classes defined in java.io and used for file-handling are abstract. Select the two correct answers.

A. InputStream B. PrintStream C. Reader D. FileInputStream E. FileWriter 42. Name the collection interface used to represent collections that maintain unique elements. 43. What is the result of compiling and running the following program.
public class test { public static void main(String args[]) { String str1="abc"; String str2="def"; String str3=str1.concat(str2); str1.concat(str2); System.out.println(str1);

} }

A. abc B. def C. abcabc D. abcdef E. defabc F. abcdefdef 44. Select the one correct answer. The number of characters in an object of a class String is given by A. The member variable called size B. The member variable called length C. The method size() returns the number of characters. D. The method length() returns the number of characters. 45. Select the one correct answer. Which method defined in Integer class can be used to convert an Integer object to primitive int type. A. valueOf B. intValue C. getInt D. getInteger 46. Name the return type of method hashCode() defined in Object class, which is used to get the unique hash value of an Object. 47. Which of the following are correct. Select the one correct answer. A. An import statement, if defined, must always be the first non-comment statement of the file. B. private members are accessible to all classes in the same package. C. An abstract class can be declared as final. D. Local variables cannot be declared as static. 48. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class. Select the one correct answer.

A. static B. final C. abstract D. native E. volatile F. transient 49. Which of these are core interfaces in the collection framework. Select the one correct answer. A. Tree B. Stack C. Queue D. Array E. LinkedList F. Map 50. Which of these statements are true. Select the two correct answers. A. For each try block there must be at least one catch block defined. B. A try block may be followed by any number of finally blocks. C. A try block must be followed by at least one finally or catch block. D. If both catch and finally blocks are defined, catch block must precede the finally block. Answers to Sample Test 1 1. b 2. b, c, f 3. d. Note that the program gets one command line argument - 2. args.length will get set to 1. So the condition if(args.length > 1) will fail, and the second check if(args.length > 0) will return true. 4. arr.length 5. Any of these is correct - 0x5, 0x05, 0X05, 0X5 6. b, c, f, g 7. a 8. d 9. "am" 10. a, c. Please note that b is not correct. A package statement may appear before an import statement. A class constructor may be declared private also. Hence d is incorrect. 11. a 12. a 13. protected 14. a, c 15. b, c 16. c 17. a, d, e 18. d, e 19. a, c

20. b, c 21. b, c, e 22. b 23. d 24. a 25. a 26. b, e . The option c is incorrect because a Java identifier name cannot begin with a digit. 27. d 28. c 29. c, d 30. a, c 31. b, d 32. b, c 33. c 34. c 35. c 36. b, c 37. f 38. long 39. e 40. a 41. a, c 42. Set 43. a 44. d 45. b 46. int 47. d 48. a 49. f 50. c, d

You might also like