
Instruction:
- This is a FREE online test. DO NOT pay money to anyone to attend this test.
- Total number of questions : 20.
- Time alloted : 30 minutes.
- Each question carry 1 mark, no negative marks.
- DO NOT refresh the page.
- All the best :-).
1. | Which cause a compiler error? |
||||||||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: Option B generates a compiler error: <identifier> expected. The compiler thinks you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create one 3 x 3 two-dimensional array. To correct the problem and make option B compile you need to add an extra pair of curly brackets: int [ ] [ ] scores = { {2,7,6}, {9,3,45} }; Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
2. | Which three are valid method signatures in an interface?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: (2), (3), and (5). These are all valid interface method signatures. (1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static. Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
3. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: Option D is correct, compilation fails - The return type of getLength( ) in the super class is an object of reference type Integer and the return type in the sub class is an object of reference type Long. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed. Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
4. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: In the first two iterations x is incremented once and y is not because of the short circuit && operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration x is doubly incremented and y is incremented. Learn more problems on : Operators and Assignments Discuss about this problem : Discuss in Forum |
5. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: The case statement takes only a single argument. The case statement on line 4 is given two arguments so the compiler complains. Learn more problems on : Flow Control Discuss about this problem : Discuss in Forum |
6. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: The variable i will have the values 0, 1 and 2. When i is 0, nothing will be printed because of the break in case 0. When i is 1, "one two three" will be output because case 1, case 2 and case 3 will be executed (they don't have break statements). When i is 2, "two three" will be output because case 2 and case 3 will be executed (again no break statements). Finally, when the for loop finishes "done" will be output. Learn more problems on : Flow Control Discuss about this problem : Discuss in Forum |
7. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: Look closely at line 2, is this an equality check (==) or an assignment (=). The condition at line 2 evaluates to false and also assigns false to bool. bool is now false so the condition at line 6 is not true. The condition at line 10 checks to see if bool is not true ( if !(bool == true) ), it isn't so line 12 is executed. Learn more problems on : Flow Control Discuss about this problem : Discuss in Forum |
8. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: An exception Exc1 is thrown and is caught by the catch statement on line 11. The code is executed in this block. There is no finally block of code to execute. Learn more problems on : Exceptions Discuss about this problem : Discuss in Forum |
9. |
which answer most closely indicates the behavior of the program? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: Once the program throws a RuntimeException (in the throwit() method) that is not caught, the finally block will be executed and the program will be terminated. If a method does not handle an exception, the finally block is executed before the exception is propagated. Learn more problems on : Exceptions Discuss about this problem : Discuss in Forum |
10. | Which interface provides the capability to store objects using a key-value pair? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. Learn more problems on : Objects and Collections Discuss about this problem : Discuss in Forum |
11. | Which of the following statements about the hashcode() method are incorrect?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: (2) is an incorrect statement because there is no such requirement. (3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string. Learn more problems on : Objects and Collections Discuss about this problem : Discuss in Forum |
12. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: This code would be legal if line 11 ended with a semicolon. Remember that line 5 is a statement that doesn't end until line 11, and a statement needs a closing semicolon! Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum |
13. | Which of the following will directly stop the execution of a Thread? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: Option A is correct. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. Option B is wrong. notify() - wakes up a single thread that is waiting on this object's monitor. Option C is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor. Option D is wrong. Typically, releasing a lock means the thread holding the lock (in other words, the thread currently in the synchronized method) exits the synchronized method. At that point, the lock is free until some other thread enters a synchronized method on that object. Does entering/exiting synchronized code mean that the thread execution stops? Not necessarily because the thread can still run code that is not synchronized. I think the word directly in the question gives us a clue. Exiting synchronized code does not directly stop the execution of a thread. Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
14. | Which class or interface defines the wait(), notify(),and notifyAll() methods? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: The Object class defines these thread-specific methods. Option B, C, and D are incorrect because they do not define these methods. And yes, the Java API does define a class called Class, though you do not need to know it for the exam. Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
15. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: You have two different threads that share one reference to a common object. The updating and output takes place inside synchronized code. One thread will run to completion printing the numbers 1-10. The second thread will then run to completion printing the numbers 11-20. Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
16. | Which two can be used to create a new Thread?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: There are two ways of creating a thread; extend (sub-class) the Thread class and implement the Runnable interface. For both of these ways you must implement (override and not overload) the public void run() method. (1) is correct - Extending the Thread class and overriding its run method is a valid procedure. (4) is correct - You must implement interfaces, and runnable is an interface and you must also include the run method. (2) is wrong - Runnable is an interface which implements not Extends. Gives the error: (No interface expected here) (3) is wrong - You cannot implement java.lang.Thread (This is a Class). (Implements Thread, gives the error: Interface expected). Implements expects an interface. (5) is wrong - You cannot implement java.lang.Thread (This is a class). You Extend classes, and Implement interfaces. (Implements Thread, gives the error: Interface expected) Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
17. | The following block of code creates a Thread using a Runnable target:
Which of the following classes can be used to create the target, so that the preceding code compiles correctly? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: The class correctly implements the Runnable interface with a legal public void run() method. Option A is incorrect because interfaces are not extended; they are implemented. Option B is incorrect because even though the class would compile and it has a valid public void run() method, it does not implement the Runnable interface, so the compiler would complain when creating a Thread with an instance of it. Option D is incorrect because the run() method must be public. Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
18. |
Select how you would start the program to cause it to print: Arg is 2 |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: Arguments start at array element 0 so the fourth arguement must be 2 to produce the correct output. Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum |
19. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: Math.random() returns a double value greater than or equal to 0 and less than 1. Its value is stored to an int but as this is a narrowing conversion, a cast is needed to tell the compiler that you are aware that there may be a loss of precision. The value after the decimal point is lost when you cast a double to int and you are left with 0. Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum |
20. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: No constructor has been defined for class B therefore it will make a call to the default constructor but since class B extends class A it will also call the Super() default constructor. Since a constructor has been defined in class A java will no longer supply a default constructor for class A therefore when class B calls class A's default constructor it will result in a compile error. Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum |