
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 of the following class level (nonlocal) variable declarations will not compile? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: Option C will not compile; the synchronized modifier applies only to methods. Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier. Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
2. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option A Explanation: Option A is correct. When you create an array (f1 = new float[10];) the elements are initialises to the default values for the primitive data type (float in this case - 0.0), so f1 will contain 10 elements each with a value of 0.0. f2 has been declared but has not been initialised, it has the ability to reference or point to an array but as yet does not point to any array. f2 = f1; copies the reference (pointer/memory address) of f1 into f2 so now f2 points at the array pointed to by f1. This means that the values returned by f2 are the values returned by f1. Changes to f1 are also changes to f2 because both f1 and f2 point to the same array. Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
3. |
Which statement is true? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: Option B is correct. Class B inherits Class A's constructor which has no arguments. Option A is wrong. Class B inherits Class A's constructor which uses default access. Option C is wrong. There is just no implicit call to this( ). Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
4. |
which two statements, added independently at beginning of the program, allow the code to compile?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: (2) and (5). TreeMap is the only class that must be imported. TreeSet does not need an import statement because it is described with a fully qualified name. (1) is incorrect because TreeMap must be imported. (3) is incorrect syntax for an import statement. (4) is incorrect because it will not import TreeMap, which is required. Learn more problems on : Declarations and Access Control Discuss about this problem : Discuss in Forum |
5. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0. Learn more problems on : Operators and Assignments Discuss about this problem : Discuss in Forum |
6. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: As instance variables, b1 and b2 are initialized to false. The if tests on lines 7 and 9 are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21) will be skipped. 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 D Explanation: In Java, boolean instance variables are initialized to false, so the if test on line 7 is true and hand is incremented. Line 9 is legal syntax, a do nothing statement. The else-if is true so hand has 7 added to it and is then incremented. 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 D Explanation: Compilation fails because the argument of the while loop, the condition, must be of primitive type boolean. In Java, 1 does not represent the true state of a boolean, rather it is seen as an integer. Learn more problems on : Flow Control Discuss about this problem : Discuss in Forum |
9. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: If you examine the code carefully you will notice a missing curly bracket at the end of the code, this would cause the code to fail. Learn more problems on : Flow Control Discuss about this problem : Discuss in Forum |
10. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: The code will not compile because a continue statement can only occur in a looping construct. If this syntax were legal, the combination of the continue and the if statements would create a kludgey kind of loop, but the compiler will force you to write cleaner code than this. Learn more problems on : Flow Control Discuss about this problem : Discuss in Forum |
11. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: Compilation fails because ArithmeticException has already been caught. ArithmeticException is a subclass of java.lang.Exception, by time the ArithmeticException has been specified it has already been caught by the Exception class. If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses). Learn more problems on : Exceptions Discuss about this problem : Discuss in Forum |
12. | Which statement is true? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: A is wrong. A try statement can exist without catch, but it must have a finally statement. B is wrong. A try statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause. If that catch block completes normally, then the try statement completes normally. C is wrong. Exceptions of type Error and RuntimeException do not have to be caught, only checked exceptions (java.lang.Exception) have to be caught. However, speaking of Exceptions, Exceptions do not have to be handled in the same method as the throw statement. They can be passed to another method. If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:
Learn more problems on : Exceptions Discuss about this problem : Discuss in Forum |
13. |
which statement is true? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: The so-called "hashing algorithm" implemented by class Test1 will always return the same value, 42, which is legal but which will place all of the hash table entries into a single bucket, the most inefficient setup possible. Option A and D are incorrect because these classes are legal. Option B is incorrect based on the logic described above. Learn more problems on : Objects and Collections Discuss about this problem : Discuss in Forum |
14. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: Option C is correct because first the Foo instance is created, which means the Foo constructor runs and prints "foo". Next, the makeBar() method is invoked which creates a Bar, which means the Bar constructor runs and prints "bar", and finally the go() method is invoked on the new Bar instance, which means the go() method prints "hi". Learn more problems on : Inner Classes Discuss about this problem : Discuss in Forum |
15. |
At what point is the Bar object, created on line 6, eligible for garbage collection? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: Option B is correct. All references to the Bar object created on line 6 are destroyed when a new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore the Bar object, created on line 6, is eligible for garbage collection after line 14. Option A is wrong. This actually protects the object from garbage collection. Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in newBar on line 12. This preserver the object created on line 6. Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14. Learn more problems on : Garbage Collections Discuss about this problem : Discuss in Forum |
16. |
When is the Demo object eligible for garbage collection? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: Option D is correct. By a process of elimination. Option A is wrong. The variable d is a member of the Test class and is never directly set to null. Option B is wrong. A copy of the variable d is set to null and not the actual variable d. Option C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start() method finishes the variable d still holds a reference. Learn more problems on : Garbage Collections Discuss about this problem : Discuss in Forum |
17. |
After line 8 runs. how many objects are eligible for garbage collection? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option B Explanation: By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method. Ref: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html Learn more problems on : Garbage Collections Discuss about this problem : Discuss in Forum |
18. | What causes compilation to fail? |
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option D Explanation: Option D is correct. Compilation fails because of an unreachable statement at line 14. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 6 would also be unreachable. The answer must be something other than assert. Examine the following: A while statement can complete normally if and only if at least one of the following is true: - The while statement is reachable and the condition expression is not a constant expression with value true. -There is a reachable break statement that exits the while statement. The while statement at line 11 is infinite and there is no break statement therefore line 14 is unreachable. You can test this with the following code:
Learn more problems on : Assertions Discuss about this problem : Discuss in Forum |
19. | What will be the output of the program?
|
|||||||||||||||||||
Your Answer: Option (Not Answered) Correct Answer: Option C Explanation: The sqrt() method returns NaN (not a number) when it's argument is less than zero. 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 B Explanation: Both substring() and charAt() methods are indexed with a zero-base, and substring() returns a String of length arg2 - arg1. Learn more problems on : Java.lang Class Discuss about this problem : Discuss in Forum |