
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 two of the following are legal declarations for nonnested classes and interfaces?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: (3), (6). Both are legal class declarations. (1) is wrong because a class cannot be abstract and final—there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected. Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
2. |
Which statement is true? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java. A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown. Learn more problems on : Flow 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 A Explanation: The program flows as follows: I will be incremented after the while loop is entered, then I will be incremented (by zero) when the for loop is entered. The if statement evaluates to false, and the continue statement is never reached. The break statement tells the JVM to break out of the outer loop, at which point I is printed and the fragment is done. Learn more problems on : Flow 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 B Explanation: The choices are between Option A and B, what this question is really testing is your knowledge of default values of an initialized array. This is an array type float i.e. it is a type that uses decimal point numbers therefore its initial value will be 0.0 and not 0 Learn more problems on : Objects and Collections Discuss about this problem : Discuss in Forum |
5. | Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: (1) is a restatement of the equals() and hashCode() contract. (4) is true because if the hashCode() comparison returns ==, the two objects might or might not be equal. (2) and (3) are incorrect because the hashCode() method is very flexible in its return values, and often two dissimilar objects can return the same hash code value. Learn more problems on : Objects and Collections Discuss about this problem : Discuss in Forum |
6. |
and assuming that the equals() and hashCode() methods are properly implemented, if the output is "x = 1111", which of the following statements will always be true? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: By contract, if two objects are equivalent according to the equals() method, then the hashCode() method must evaluate them to be ==. Option A is incorrect because if the hashCode() values are not equal, the two objects must not be equal. Option C is incorrect because if equals() is not true there is no guarantee of any result from hashCode(). Option D is incorrect because hashCode() will often return == even if the two objects do not evaluate to equals() being true. Learn more problems on : Objects and Collections Discuss about this problem : Discuss in Forum |
7. |
which one create an anonymous inner class from within class Bar? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: Option B is correct because anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works. Option A is incorrect because it passes an int to the Boo constructor, and there is no matching constructor in the Boo class. Option C is incorrect because it violates the rules of polymorphism—you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass is not guaranteed to have everything the subclass has. Option D uses incorrect syntax. Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum |
8. | Which statement is true about a static nested class? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class). Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class. Option C is incorrect because static nested classes can declare and define nonstatic members. Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything. Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum |
9. | Which constructs an anonymous inner class instance? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable. A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation. B and C use incorrect syntax. Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum |
10. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: The code in the HorseTest class is perfectly legal. Line 13 creates an instance of the method-local inner class Horse, using a reference variable declared as type Object. Line 14 casts the Horse object to a Horse reference variable, which allows line 15 to compile. If line 14 were removed, the HorseTest code would not compile, because class Object does not have a name variable. Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum |
11. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: You can define an inner class as abstract, which means you can instantiate only concrete subclasses of the abstract inner class. The object referenced by the variable t is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides the getNum() method (to return 57). Remember that to instantiate a Bar instance, we need an instance of the enclosing AbstractTest class to tie to the new Bar inner class instance. AbstractTest can't be instantiated because it's abstract, so we created an anonymous subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance. Learn more problems on : Inner Classes 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: The synchronized code is the key to answering this question. Because x and y are both incremented inside the synchronized method they are always incremented together. Also keep in mind that the two threads share the same reference to the Q126 object. Also note that because of the infinite loop at line 13, only one thread ever gets to execute. Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
13. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: Line 6 calls the run() method, so the run() method executes as a normal method should and it prints "1..2.." A is incorrect because line 5 is the proper way to create an object. B is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete. D is incorrect because the for loop only does two iterations. Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
14. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D 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 C Explanation: Both threads are operating on the same instance variables. Because the code is synchronized the first thread will complete before the second thread begins. Modify line 17 to print the thread names: System.out.println(Thread.currentThread().getName() + " x = " + x + ", y = " + y); Learn more problems on : Threads Discuss about this problem : Discuss in Forum |
16. | What allows the programmer to destroy an object x? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: Option D is correct. When an object is no longer referenced, it may be reclaimed by the garbage collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that would not otherwise be released. When a class is no longer needed, it may be unloaded. Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are:
None of these destroy the object to which they belong. Option B is wrong. I found 19 finalize() methods. The most interesting, from this questions point of view, was the finalize() method in class java.lang.Object which is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method does not destroy the object to which it belongs. Option C is wrong. But it is interesting. The Runtime class has many methods, two of which are:
Learn more problems on : Garbage Collections Discuss about this problem : Discuss in Forum |
17. | Which of the following statements is true? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: Option A is correct because it is sometimes advisable to thrown an assertion error even if assertions have been disabled. Option B is incorrect because it is considered appropriate to check argument values in private methods using assertions. Option C is incorrect; finally is never bypassed. Option D is incorrect because AssertionErrors should never be handled. Learn more problems on : Assertions Discuss about this problem : Discuss in Forum |
18. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: Line 13 fails because == compares reference values, not object values. Line 15 succeeds because both String and primitive wrapper constructors resolve to the same value (except for the Character wrapper). Lines 17, 19, and 21 fail because the equals() method fails if the object classes being compared are different and not in the same tree hierarchy. 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 B Explanation: This question is about the + (plus) operator and the overriden + (string cocatanation) operator. The rules that apply when you have a mixed expression of numbers and strings are: If either operand is a String, the + operator concatenates the operands. If both operands are numeric, the + operator adds the operands. The expression on line 6 above can be read as "Add the values i1 and i2 together, then take the sum and convert it to a string and concatenate it with the String from the variable s1". In code, the compiler probably interprets the expression on line 8 above as:
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: After line 7 executes, both s2 and s3 refer to a String object that contains the value "def". When line 8 executes, a new String object is created with the value "ghi", to which s2 refers. The reference variable s3 still refers to the (immutable) String object with the value "def". Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum |