
TOOLBOX
|
Explanations for SCJP Quiz answers
Q1. The output will be "Not equal! 10". Please note that && is short-circuit logical AND operator. If first operand (before &&) is false (or evaluated to false) then the other operand will not be evaluated. This illustrates that the Output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false. If you change the value of b1 to true, processing occurs as you would expect and the output would be "We are equal 20".
Q2. The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to create a StringBuffer object using "new". B is a valid construction of a Boolean (any string other than "true" or "false" to the Boolean constructor will result in a Boolean with a value of "false"). C will fail to compile because the valid range for a byte is -128 to +127 (i.e., 8 bits, signed). D is correct, 0x1234 is the hexadecimal representation in java. E fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting), which is not valid. You either need an explicit cast, as in "(float)1.2" or "1.2f", to indicate a float.
Q3. A is correct. When we pass references in Java what actually gets passed is the value of that reference (i.e. memory address of the object being referenced and not the actual object called by that reference) and it gets passed as value (i.e a copy of the reference is made). Now when we make changes to the object referenced by that reference it reflects on that object even outside of the method being called but any changes made to the reference itself is not reflected on that reference outside of the method which is called. In the example above when the reference v is passed from method first() to second() the value of v is passed. When we assign the value val to v it is valid only inside the method second() and thus inside the method second() what gets printed is 15 (initial value of i in the object referenced by val), then a blank space and then 0 (value of local variable i). After this when we return to the method first() v actually refers to the same object to which it was referring before the method second() was called, but one thing should be noted here that the value of i in that object (referred by v inside the method first()) was changed to 20 in the method second() and this change does reflect even outside the method second(), hence 20 gets printed in the method first(). Thus overall output of the code in consideration is
15 0
20
Q4. D is correct. The code compiles successfully. In this code the optional value for the ternary operator, 9.0(a double) and 9(an int), are of different types. The result of a ternary operator must be determined at the compile time, and here the type chosen using the rules of promotion for binary operands is double. Since the result is a double the output value is printed in a floating point format. The choice of which value to be printed is made on the basis of the result of the comparison "a < 5" which results in false, hence the variable "a" takes the second of the two possible values, which is 9, but because the result type is promoted to double the output value is actually written as 9.0, rather than the more obvious 9, hence D is correct.
Q5. A, B, C and E are correct. Since inner is not a static inner class, it has a reference to an enclosing object and all the variables of that object are accessible. Therefore A and B are correct even if b is private. Variables in the enclosing method are only accessible when they are marked as final, hence c is accessible but not d. E is obviously correct as it is a parameter to the method containing line 12 itself.
Q6. A is correct. The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly (default modifier) it can only be private or friendly in the superclass. Also, if the method in superclass is left as it is (i.e. friendly access) the method in subclass can be friendly, protected or public.
Q7. C and D are correct. A and B are obviously incorrect because there is nothing wrong in an interface referring to an object. C is correct because an explicit cast is needed to go down the hierarchy. D is correct; no explicit cast is needed at line 4 because we are going up the hierarchy. E is incorrect because if we put an explicit cast at line 6 the code will compile and run perfectly. No exception will be thrown because the runtime class of du (that is Truck) can be converted to type FourWheeler without any problem.
Q8. D is correct. A is incorrect as void methods can be overridden without any problem. B is incorrect as char ch declaration is valid. C is incorrect as char type in java is internally stored as integer and there is a method which takes int as an input. D is correct, on line 9 char ch is widened to an int and passed to int version of the myMethod(). E is incorrect as int version of myMethod() is called.
Q9. A is correct. Exception :java.lang.IllegalAccessExcption must be caught or placed in the throws clause of the throwMethod(), i.e. the declaration of throwMethod() be changed to "static void throwMethod() throws IllegalAccessExcption". Thus compilation error will occur.
Q10. C is correct. The code on compilation will give compile time error because the scope of variable i is only within "for" loop.
Q11. D is correct. Before any object is constructed the object of the parent class is constructed (as there is a default call to the parent's constructor from the constructor of the child class via the super() statement). Also note that when an object is constructed the variables are initialized first and then the constructor is executed. So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result. After that constructor of X will be called which implies "X" will be printed. Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed. Thus YXYZ will be printed.
Q12. B is correct. The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class, then compilation error would be reported as at compile time. When the compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.
Q13. E is correct. Please note that there will not be any compilation error when the above code is compiled. Also note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed (i.e. run() method will be called) always before "for" loop is executed. Thus the output cannot be determined.
Q14. C is correct. The use of synchronized ensures that the number generated will not be duplicated, no matter how many Threads are trying to create the number. Thus D is incorrect. A and B are incorrect as the above code will not give any compile-time or runtime error.
Q15. B is correct. Here s1 and s2 are two different object references referring to different objects in memory. Please note that operator == checks for the memory address of two object references being compared and not their value. The "equals()" method of String class compares the values of two Strings. Thus s1==s2 will return "false" while s1.equals(s2) will return "true". Thus only "Equals" will be printed.
Q16. C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method (method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called. Thus C is correct answer.
Q17. C and D are correct. A is not correct because there is no guarantee that listeners will be notified in the order in which they were added. B is not correct because when you use adapter then there is no need to supply blank method bodies for methods you don't want to use. The adapter class implements the listener interface with do nothing methods (empty body methods). C is correct because it is possible to have more than one listener attached to a component. D is correct as you can remove the listeners from the component. Similar to add Listener methods, there are remove Listener methods available.
Q18. The setForeground() and setBackground() methods belong to Component class.
Q19. A is correct. In order to write a primitive data type such as an int, you need to use a FilterInputStream subclass such as DataInputStream. This class defines writeInt(), which is perfect for our needs.
Q20. C is correct. There is nothing wrong in writing to a FileOutputStream even after chaining a DataOutputStream onto it. C is correct as FileOutputStream writes 100 as a byte whereas the code tries to read it as integer.
|
|