You are on page 1of 20

Quiz Time ---------1Q) Which of the following statement(s) is correct..

A - The JVM runs till the main method exits, even if there are other user thread s running. B - A thread can be suspended for an indefinite amount of time. C - An interrupted exception occurs when the sleep() method is called on a threa d. D - A thread can be created in java only by subclassing the Thread class. 2Q) Which method is required to be implemented if the Runnable interface is impl emented? A B C D stop() run() wait() resume()

3Q) What can make a thread to stop execution? A B C D The program exits via a call to System.exit(0) function. Another different thread assigned to a higher priority. A call to the stop method of the Thread class. A call to the halt method of the Thread class.

4Q) Method wait() defined in class java.lang.Object causes current thread to wai t until another thread invokes the __________ method for this object. A - Object.notify(); B - Object.notifyAll(); 5Q) A collection is a container, which holds the group of ____________ as a sing le entity. 6Q) When sleep() method is invoked, thread enters in to ____________ state. A B C D wait ready running dead

7Q) Choose the correct reason which could stop the execution of current thread. A B C D An InterruptedException is thrown A thread of higher priority becomes ready The thread constructs a new Thread. The thread executes wait() call.

8Q) Which of the method(s) is static in java.lang.Thread? A B C D E start() sleep(long) join() run() setPriority(int)

9Q) We can use the _______ method to force one thread to wait for another thread to finish its execution.

A B C D

sleep() suspend() stop() join()

10Q) Keyword when applied on a method indicates that at a time only one thread s hould execute the method. A B C D E static volatile native Synchronized final

11Q) What happens, if a thread attempts to execute a synchronized method and is unable to get an object's lock _________________. A - the thread enters into wait state indefinetly. B - the thread enters into wait state till the lock becomes available. C - LockNotFoundException will be thrown 12Q) _____________ list is a resizeable implementation of list. 13Q) sleep(long) throws ________________ A - RuntimeException B - InterruptedException 14Q) This set of collection can be used to store the nulls, in both keys and val ues. A-HashMap B-HashTree C-HashSet D-Hashtable 15Q) import java.util.*; public class Example { public static void main(String[] args) { // insert code here set.add(new integer(2)); set.add(new integer(l)); System.out.println(set); } } Which code, inserted at line 4, guarantees that this program will give output [1, 2]? A) B) C) D) E) Set set = new TreeSet(); Set set = new HashSet(); Set set = new SortedSet(); List set = new SortedList(); Set set = new LinkedHashSet();

16Q) Which of the following classes allow elements to be accessed in the order t hat they were added? A) LinkedHashMap B) LinkedHashSet C) Both

D) None 17Q) Real life coading requires the developers to provide concurrency in their t ools, i.e. the illusion of two or more tasks are being performed in parallel. Ja va provides _______ feature for it. 1) 2) 3) 4) Multitasking Multithreading Multiprocessing Multitesting

18Q) Map interface, like other core interfaces, is derived from Collection inter face. A) True B) False 19Q) Which of these statements are true ? a)A b)A c)A d)A e)A HashSet does not permit duplicates Vector permits duplicates TreeSet is an ordered Set LinkedList is sorted in descending order LinkedList is sorted in ascending order

20Q) join() method is used for ___________ 1) 2) 3) 4) allowing other threads to execute first to pause the execution of other thread waits to join with other threads to terminate the current thread

21Q) In java we can restrict other classes to create an instance of a class usin g _______________ 1) 2) 3) 4) Singleton patterns Multithreading Collections Interfaces

22Q) Static method can access only________ variables. 1) 2) 3) 4) Private Final Static Protected

23Q) Which of these statements are true? a. b. c. d. LinkedList extends List AbstractSet extends Set HashSet extends AbstractSet TreeSet extends AbstractSet

24Q) Which of these are interfaces in the collection framework. Select the two c orrect answers. A. HashMap B. ArrayList C. Collection D. SortedMap A) A,B

B) B,C C) C,D D) D,A 25Q) Which implementation exhibits the following properties? 1. Entries are not organized as key/value pairs. 2. Duplicate entries are rejected. 3. Entries are sorted using a Comparator or the Comparable interface A. B. C. D. List Map SortedSet SortedMap

26Q) O/P import java.util.*; class F { public static void main (String args[]) { LinkedList a1 = new LinkedList(); ArrayList b1 = new ArrayList(); Vector c1 = new Vector(); System.out.print((a1 instanceof List)+","); System.out.print((b1 instanceof List)+","); System.out.print(c1 instanceof LinkedList); }} A) B) C) D) true,true,true true,true,false Compiletime Error No Error and no output

27Q) What is the output of the following code? import java.util.*; public class Test{ public static void main(String a[]){ Map s = new LinkedHashMap(); s.put("0",null); s.put("1","one"); s.put("2","two"); System.out.println(s); } } A. {} B. {null} C. {0=null, 1=one, 2=two} D. Compilation Error 28Q) What is the result of attempting to compile and run the following code ? public class Test { public static void main(String[] args){ Integer a = new Integer(4); Integer b = new Integer(8); Integer c = new Integer(4); HashSet hs = new HashSet(); hs.add(a); hs.add(b); hs.add(c); System.out.println(hs);

} } 1) Will 2) will 3) Will 4) Will

print print print print

[8, [4, [8, [4,

4] 8, 4] 4, 4] 8]

29Q) import java.util.*; class G { public static void main (String[] args) { Object a = new ArrayList(); Object l = new LinkedList(); Object v = new Vector(); System.out.print((a instanceof RandomAccess)+","); System.out.print((l instanceof RandomAccess)+","); System.out.print(v instanceof RandomAccess); } } What is the result of attempting to compile and run the program? A) B) C) D) Prints: Prints: Prints: Prints: false,false,false false,true,true false,true,false true,false,true

30Q) What does the following code do, if exception is thrown from addUser() meth od? void process () throws Exception { try { addUser (new User ("spam")); } catch (Exception e) { processThisException() never raises any exception processThisException (e); } } 1. If addUser raises an exception, the system will jump into the catch statement and will call processThisException(). Then, it will forward the exception to th e method who called process(). 2. If addUser raises an exception, the system will jump into the catch statement and will call processThisException(). But it will not forward the exception to the method who called process(). 3. This isn't allowed, it won't compile because there is no finally block. 4. None of the above. (1) (2) (3) (4) Only Only Only Only 1 2 3 4

Answers 1. B,C 2. B 3. A,B,C 4. A,B 5. Objects 6. A 7. A,B,D 8. B 9. D 10. D - Synchronized methods prevent more than one thread from accessing an obje cts critical method code. 11. B - the thread execution grinds to a halt because the code is waiting for lo cks to be removed from objects. 12. ArrayList 13. B 14. A 15. A 16. C 17. 2 18. B 19. A,B,C 20. 2 21. 1 22. 3 23. C,D 24. C 25. C 26. C 27. C 28. 4 29. D 30. B

************************************************* Test Time --------1Q) Consider the code below: try { selectUser(); } catch (Exception e) { //... } finally { //... } Which statement block above (try-catch-finally) is usually the one that will con tain the cleaning statements that need to be executed in all the cases? 1) try() 2) catch() 3) finally() 4) None of the above 2Q) An integer "divide by zero" throws an instance of ________ class a. unchecked exception b. ArithmeticException

c. d. 1) 2) 3) 4)

UnKnownException SecurityException d a c b

3Q) Which statement is true. The class java.lang.Exception : i. Is public ii. Extends Throwable iii. Implements Throwable 1. 2. 3. 4. 1) 2) 3) 4) ii only ii, iii i, ii i only Only Only Only Only 1 2 3 4

4Q) Exceptions come in two varieties: checked and unchecked. You must either cat ch or throw all 1. unchecked exceptions 2. checked exceptions 3. Both of them 4. None of them 1) Only 1 2) Only 2 3) Only 3 4) Only 4 5Q) When creating a new Thread to do a task, why does the class implementing Run nable use the run method for the code to execute? 1. The method is defined in the ThreadRunner interface, which Thread implements. 2. The method is defined in the Runnable interface, and you need to implement th e interface's method. 3. The run method of Thread is abstract and must be overridden with the job to d o. 4. Like the start and stop methods of Applet, the run method is just a known nam e that is called by the system thread when there is a job to do. 1) 2) 3) 4) Only Only Only Only 4 3 2 1

6Q) If a method of the object needs to be executed by only one thread at a time, then such method should be modified with which keyword ? Choose one. 1. 2. 3. 4. native synchronized private volatile

1) Only 4 2) Only 3

3) Only 2 4) Only 1 7Q) A constructor is used to... a. Free memory. b. Initialize a newly created object. c. Import packages. d. Create a JVM 1) 2) 3) 4) a b c d

8Q) What is the purpose of the main method ? a. To build a user interface. b. To hold the APIs of the application. c. To create buttons and scrollbars. d. To act as the entry point for the program. 1) b 2) d 3) a 4) c 9Q) Which a. public b. public c. public 1) a 2) b 3) c 4) All of the following is an abstract method abstract abc( ); abstract void ABC( ); abstract ABC( ) { };

10Q) State which of the following statements are True (1) Each method in a parent class can be overridden at most once in any one subc lass (2) A method can be overloaded in the class it is defined as well as in the subc lass of its class (3) Overriding methods must return exactly the same type as the method they over ride (4) An overriding method must not be less accessible than the method it override s A. B. C. D. 1) 2) 3) 4) All 4, 1 and 2 1,2 and 4 1 and 3 A B C D

11Q) Which of the following statements are true? 1) The RandomAccessFile class allows you to move forwards and backwards without re-opening the file 2) An instance of RandomAccessFile may be used as a constructor for FileInputStr eam 3) The methods of RandomAccessFile do not throw exceptions

4) Creating a RandomAccessFile instance with a constructor will throw an excepti on if the file does not exist. 1) 2) 3) 4) 1 1 2 1 and and and and 2 4 4 3

12Q) Look at the following class and pick the answer. public class test { public test(){} public void abc() { static int a = 10; byte b = 20;} } a. b. c. d. 1) 2) 3) 4) Above class will compile. Above class will NOT compile. Method abc cannot have a static variable. Only static method can declare static variables. a and c Only c a and d b and c

13Q) Which of these statements are true? a. b. c. e. (1) (2) (3) (4) LinkedList extends List AbstractSet extends Set HashSet extends AbstractSet TreeSet extends AbstractSet a,b,c only a,c,e only c,e only b,c,e only

14Q) public class Test { final Vector v; Test() { v = new Vector(); } Test(int i){} public void someMethod() { System.out.println(v.isEmpty()); } public static void main(String[] args) { Test t=new Test(10); } } 1) compile time error 2) runtime exception 3) the code compiles and runs fine 4) none of the above 15Q) class RedException extends Exception {} class BlueException extends Exception {} class White {

void m1() throws RedException {throw new RedException();} public static void main (String[] args) { White white = new White(); int a,b,c,d; a = b = c = d = 0; try {white.m1(); a++;} catch (RedException e) {b++;} catch (BlueException e) {c++;} finally {d++;} System.out.print(a+","+b+","+c+","+d); }} What is the result of attempting to compile and run the program? 1) 2) 3) 4) Prints: 0,1,0,0 Prints: 1,1,0,1 Prints: 0,1,0,1 Compile-time error class class class class Level1Exception extends Exception {} Level2Exception extends Level1Exception {} Level3Exception extends Level2Exception {} Brown { public static void main(String args[]) { int a, b, c, d, f; a = b = c = d = f = 0; int x = 4; try { switch (x) { case 1: throw new Level1Exception(); case 2: throw new Level2Exception(); case 3: throw new Level3Exception(); } a++; } catch (Level3Exception e) {b++;} catch (Level2Exception e) {c++;} catch (Level1Exception e) {d++;} finally {f++;} System.out.print(a+","+b+","+c+","+d+","+f);

16Q)

}} What is the result of attempting to compile and run the program? 1) Prints: 0,0,0,1,1 2) Prints: 1,1,1,1,1 3) Prints: 1,0,0,0,1 4) Compile-time error 5) Run-time error 17Q)Files are opened by instantiating objects of which of the following stream c lasses a. b. c. d. 1) 2) 3) 4) FileInputStream FileOutputStream RandomAccessFile BufferedReader a and b a, b and c b and c None of the above

18Q) Mark instance variables as _________ if you do not want their state to be a part of the serialization process.

A)native B)volatile C)transient D)abstract 1) 2) 3) 4) A B C D

19Q) public class Holt extends Thread { private String sThreadName; public static void main(String argv[]) { Holt h = new Holt(); h.go(); } Holt() { } Holt(String s) { sThreadName = s; } public String getThreadName() { return sThreadName; } public void go() { Holt first = new Holt("first"); first.start(); Holt second = new Holt("second"); second.start(); } public void start() { for (int i = 0; i < 2; i++) { System.out.println(getThreadName() + i); try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } } } 20Q) What is the Output of the below program? class Base{ public static void main(String[] args){ System.out.println("hello"); } } public class Holt extends Base{}

Answers ------1) 3 2) 4 3) 3 4) 2 5) 3 6) 3 7) 2 8) 2 9) 2 10) 1 11) 2 12) 4 13) 3 14) 1 15) 4 16) 3 17) 2 18) 3 19) first0 first1 second0 second1 20) hello *************************************************** Test Time --------1Q) An Interface is an entirely abstract class? [True/False] 2Q) Abstract base classes may be viewed as a mixture of design and implementatio n, while interface is an expression of pure design.[True/False] 3Q) A Class in java can extend more than one abstract base class and implements more than one interface.[True/False] 4Q) In java a final class must be sub-classed before it can be used.[True/False] 5Q) Which of the following are true? A - For each try block there must be at least one catch block. B - A try block may be followed by any no of finally blocks. C - A try block must be followed by atleast one catch or finally block. D - If both catch and finally blocks are defined, catch block must precede the f inally block. 6Q) Combining Data and functionality is _______ Character of OOPS. 7Q) An Instance of a class is also called as ________. 8Q) Default value of an instance integer member is ________.

9Q) A method can have several try blocks.[True/False] 10Q) Static members are also called as _________. 11Q) What happens if RuntimeException is thrown in the finalize method? A - The B - The lected. C - The D - The running exception crashes. exception is simply ignored and the object is eligible to be garbage col exception is simply ignored and the object is not garbage collected. exception causes jvm to crash.

12Q) Defining similar method signatures in Base and Derived classes is _________ 1) 2) 3) 4) Method Method Method We can Overloading Overlaping Overriding not perform this

13Q) JVM is platform independent and bytecode is platform dependent.[T/F] 14Q) Every object will have 2 characteristics. What are they? 15Q) ______ is used to dynamically allocate the memory for an object. 16Q) Static members can be executed using ________, without creating an object. 17Q) __________ are containers used to group related classes. 1) 2) 3) 4) Inheritence Collections Packages Exceptions

18Q) What is the O/P of below program. public class NewClass { static{ System.out.println("Static block"); } { System.out.println("Instance block"); } public static void show(){ System.out.println("Show method"); } public static void main(String[] args) { NewClass.show(); } } O/P=? 19Q) In an Interface, variables declared are of ______ and ________ 1) 2) 3) 4) public static and final private final and static integer and final abstract and final

20Q) public class NewClass { int x; NewClass() { x = 10; } public void show() { System.out.println(x); } public static int main(String[] args) { NewClass nc = new NewClass(); nc.show(); return 0; } } op=? Answers: 1. True 2. True 3. False 4. False 5. ABC True 6. Encapsulation 7. Object 8. Zero 9. True 10. Class Members 11. B 12. 3 13. False 14. state and behaviour 15. new 16. class name 17. 3 18. static block and show method 19. 1 20. Runtime Error

********************************************************** Quiz Time ---------1Q) Which of the following statement(s) is correct.. A - The JVM runs till the main method exits, even if there are other user thread s running. B - A thread can be suspended for an indefinite amount of time. C - An interrupted exception occurs when the sleep() method is called on a threa d. D - A thread can be created in java only by subclassing the Thread class. 2Q) Which method is required to be implemented if the Runnable interface is impl emented?

A B C D

stop() run() wait() resume()

3Q) What can make a thread to stop execution? A B C D The program exits via a call to System.exit(0) function. Another different thread assigned to a higher priority. A call to the stop method of the Thread class. A call to the halt method of the Thread class.

4Q) Method wait() defined in class java.lang.Object causes current thread to wai t until another thread invokes the __________ method for this object. A - Object.notify(); B - Object.notifyAll(); 5Q) A collection is a container, which holds the group of ____________ as a sing le entity. 6Q) When sleep() method is invoked, thread enters in to ____________ state. A B C D wait ready running dead

7Q) Choose the correct reason which could stop the execution of current thread. A B C D An InterruptedException is thrown A thread of higher priority becomes ready The thread constructs a new Thread. The thread executes wait() call.

8Q) Which of the method(s) is static in java.lang.Thread? A B C D E start() sleep(long) join() run() setPriority(int)

9Q) We can use the _______ method to force one thread to wait for another thread to finish its execution. A B C D sleep() suspend() stop() join()

10Q) Keyword when applied on a method indicates that at a time only one thread s hould execute the method. A B C D E static volatile native Synchronized final

11Q) What happens, if a thread attempts to execute a synchronized method and is unable to get an object's lock _________________. A - the thread enters into wait state indefinetly. B - the thread enters into wait state till the lock becomes available. C - LockNotFoundException will be thrown 12Q) _____________ list is a resizeable implementation of list. 13Q) sleep(long) throws ________________ A - RuntimeException B - InterruptedException 14Q) This set of collection can be used to store the nulls, in both keys and val ues. A-HashMap B-HashTree C-HashSet D-Hashtable 15Q) import java.util.*; public class Example { public static void main(String[] args) { // insert code here set.add(new integer(2)); set.add(new integer(l)); System.out.println(set); } } Which code, inserted at line 4, guarantees that this program will give output [1, 2]? A) B) C) D) E) Set set = new TreeSet(); Set set = new HashSet(); Set set = new SortedSet(); List set = new SortedList(); Set set = new LinkedHashSet();

16Q) Which of the following classes allow elements to be accessed in the order t hat they were added? A) LinkedHashMap B) LinkedHashSet C) Both D) None 17Q) Real life coading requires the developers to provide concurrency in their t ools, i.e. the illusion of two or more tasks are being performed in parallel. Ja va provides _______ feature for it. 1) 2) 3) 4) Multitasking Multithreading Multiprocessing Multitesting

18Q) Map interface, like other core interfaces, is derived from Collection inter face. A) True

B) False 19Q) Which of these statements are true ? a)A b)A c)A d)A e)A HashSet does not permit duplicates Vector permits duplicates TreeSet is an ordered Set LinkedList is sorted in descending order LinkedList is sorted in ascending order

20Q) join() method is used for ___________ 1) 2) 3) 4) allowing other threads to execute first to pause the execution of other thread waits to join with other threads to terminate the current thread

21Q) In java we can restrict other classes to create an instance of a class usin g _______________ 1) 2) 3) 4) Singleton patterns Multithreading Collections Interfaces

22Q) Static method can access only________ variables. 1) 2) 3) 4) Private Final Static Protected

23Q) Which of these statements are true? a. b. c. d. LinkedList extends List AbstractSet extends Set HashSet extends AbstractSet TreeSet extends AbstractSet

24Q) Which of these are interfaces in the collection framework. Select the two c orrect answers. A. HashMap B. ArrayList C. Collection D. SortedMap A) A,B B) B,C C) C,D D) D,A 25Q) Which implementation exhibits the following properties? 1. Entries are not organized as key/value pairs. 2. Duplicate entries are rejected. 3. Entries are sorted using a Comparator or the Comparable interface A. B. C. D. List Map SortedSet SortedMap

26Q) O/P import java.util.*; class F { public static void main (String args[]) { LinkedList a1 = new LinkedList(); ArrayList b1 = new ArrayList(); Vector c1 = new Vector(); System.out.print((a1 instanceof List)+","); System.out.print((b1 instanceof List)+","); System.out.print(c1 instanceof LinkedList); }} A) B) C) D) true,true,true true,true,false Compiletime Error No Error and no output

27Q) What is the output of the following code? import java.util.*; public class Test{ public static void main(String a[]){ Map s = new LinkedHashMap(); s.put("0",null); s.put("1","one"); s.put("2","two"); System.out.println(s); } } A. {} B. {null} C. {0=null, 1=one, 2=two} D. Compilation Error 28Q) What is the result of attempting to compile and run the following code ? public class Test { public static void main(String[] args){ Integer a = new Integer(4); Integer b = new Integer(8); Integer c = new Integer(4); HashSet hs = new HashSet(); hs.add(a); hs.add(b); hs.add(c); System.out.println(hs); } } 1) Will print [8, 4] 2) will print [4, 8, 4] 3) Will print [8, 4, 4] 4) Will print [4, 8] 29Q) import java.util.*; class G { public static void main (String[] args) { Object a = new ArrayList(); Object l = new LinkedList(); Object v = new Vector(); System.out.print((a instanceof RandomAccess)+","); System.out.print((l instanceof RandomAccess)+",");

System.out.print(v instanceof RandomAccess); } } What is the result of attempting to compile and run the program? A) B) C) D) Prints: Prints: Prints: Prints: false,false,false false,true,true false,true,false true,false,true

30Q) What does the following code do, if exception is thrown from addUser() meth od? void process () throws Exception { try { addUser (new User ("spam")); } catch (Exception e) { processThisException() never raises any exception processThisException (e); } } 1. If addUser raises an exception, the system will jump into the catch statement and will call processThisException(). Then, it will forward the exception to th e method who called process(). 2. If addUser raises an exception, the system will jump into the catch statement and will call processThisException(). But it will not forward the exception to the method who called process(). 3. This isn't allowed, it won't compile because there is no finally block. 4. None of the above. (1) (2) (3) (4) Only Only Only Only 1 2 3 4

Answers 1. B,C 2. B 3. A,B,C 4. A,B 5. Objects 6. A 7. A,B,D 8. B 9. D 10. D - Synchronized methods prevent more than one thread from accessing an obje

cts critical method code. 11. B - the thread execution grinds to a halt because the code is waiting for lo cks to be removed from objects. 12. ArrayList 13. B 14. A 15. A 16. C 17. 2 18. B 19. A,B,C 20. 2 21. 1 22. 3 23. C,D 24. C 25. C 26. C 27. C 28. 4 29. D 30. B ********************************************************

You might also like