|
1. ž A C D
Options A C and D all make incorrect statements about encapsulation. The accessor methods are provided to access private data. Without them, you cannot access the encapsulated data. C is incorrect as encapsulation always makes code easy to extend. Code modifications are also easier due to standardized (through accessors) way of accessing the encapsulating data.
B and E are correct statements about encapsulation. Encapsulated code is ver well maintainable as all the implementation details are hidden from other classes. Hence, any modifications to it does not affect other code. E is also correct; to encapsulate the member variables, you need to declare them private. You also need to provide public getter and setter methods. The programming interface (API of a class) is kept in public methods. Thus, access modifiers play important role for enforcing encapsulation.
2. ž E
Options A B C and D all make incorrect statements about Test class. A is incorrect. The Test class declares its member variables as public. Therefore, it is not well encapsulated. B is also incorrect, as only adding the getter methods is not sufficient. You also need to declare the member variables var1 and var2 as private. Options C and D are incorrect. Although it appears from the setter methods that var1 will be half the value of var2, it might not always be the case. Note that var1 and var2 are declared as public member variables. Therefore, they can also be modified by accessing them directly.
3. ž C F
Options C and F represent the appropriate declarations that can be placed in Elephant class. Since the problem states, “Elephant is an animal”, the Elephant class should be the subclass of Animal class. The question says that Animal class is already defined. So Elephant class should extend of Animal class. The problem definition also says “Elephant … has weight and a flag indicating whether it has developed a tusk”. Therefore, C and F are valid as members of Elephant class.
A is incorrect as nothing in problem definition suggests that Elephant needs to keep reference to its super class. B and E are incorrect as they would be the members of Animal class as “An animal has a care taker and registration number”. D is incorrect as it is more appropriate to store the tusk information (whether or not the elephant has tusk) in a boolean variable, as there are only two possible values.
4. ž E
Options E represent the most appropriate declaration for the Elephant class. Since the problem states, “Elephant is an animal”, the Elephant class should be subclass of Animal class. The question says that the Animal class is already defined. So Elephant class should extend the Animal class. The problem definition also says that the Elephant class should be accessible to all classes in Zoo application; therefore, it is appropriate to declare it public.
5. ž B E
Option B and E represents the valid methods as they are overloading the method-name properly. B is correct as it takes float as method argument that is different from the method defined on line 2. Similarly E is correct, as it does not take any argument, thus its method signature is different from the method defined on line 2.
Option A is invalid as it also takes int argument like the sine method on line 2. The only difference is the return type. However, you cannot differentiate methods based on only the return type. C is also incorrect as it has same method signature as the method on line 2. The fact that it throws exception is not sufficient to distinguish it from method on line 2. D is also incorrect as it also has exact same signature as method on line 2. Having a different access modifier (private) than method on line 2 does not make them different.
6. ž C D E
Options C D and E represent the valid method definitions that can be used in ScientificCalculator. All of them represent add method with different signatures than the one in the superclass. None of them overrides it. Instead, they are appropriately overloading the method (name) add in subclass.
Options A and B are incorrect. Option A represents method with exactly same signature as the add method in superclass. However, it does not have any access modifier. Therefore, it will have default access. Thus if we put it in subclass, it will override the method to make it more restrictive in access (from public -> default). Since ‘restricting a method’ is not allowed while overriding, the subclass will not compile. Option B is incorrect as it is also trying to override the add method. But it is throwing an exception which is not thrown by superclass. Since it is not allowed (to throw checked exception that is not thrown by superclass method while overriding), the method in B will not compile.
7. ž A B F
Options A B and F represent the messages that will be printed after Child class is executed. Note that Child class does not override any of the printIt method. Instead, it inherits both the methods. A is correct as the child.printIt(6.8f); will invoke the method printIt(float f) which is inherited from Parent class. B is correct as the child.printIt(“” + 9); will invoke the method printIt(String s). The expression (“” + 9) on line 12 results in String literal “9”, therefore the printIt method that takes String argument is invoked. F is correct; 11 is integral literal, which is of type int. Therefore the inherited method printIt(int i) is invoked.
C and D are incorrect, as the call at line 12 will invoke printIt method that takes String parameter. E is incorrect, as the call at line 13 will invoke printIt method that takes int parameter.
8. ž B F
Options B and F represent the valid calls to the Derived class constructors. The Derived class defines two constructors, one that takes single int argument and other that takes two int argument.
A is incorrect, as there is not zero-argument constructor defined for the class Derived. The compiler does not create the default constructor if a class already has at least one constructor. B and E are syntactically incorrect. B is invalid, as you cannot use the keyword super as a value. E is invalid, as Derived is not an inner class that needs to be instantiated on a Base class’s instance.
9. ž D
D is correct. When the classes are compiled, the compiler will put an implicit call super() on first line of Calf constructor (between line 15 and 16). At runtime, when the calf object is created with new Calf("white"); , the Calf constructor will be called. Since the first line of this constructor is implicit call super(), the zero-argument Cow constructor is executed first and “Creating Cow instance” is printed. Then the rest of Calf constructor is executed printing “Creating Calf instance”.
E is incorrect, this code will compile successfully. The compiler creates the implicit call if there is no call to constructors.
10. ž C
C is correct as it is explicitly calling the constructor of its superclass by passing appropriate argument (boolean value).
A is incorrect as the compiler will put implicit super() at the very first line of this constructor. However, the superclass MusicalInstrument does not have a zero-argument constructor. Thus, the compilation of Flute class will fail. B is also incorrect as it is explicitly calling zero-argument constructor of MusicalInstrument, which is not present. D is incorrect, as constructors cannot be called as methods. E is incorrect; when you do not have any constructor in Flute, the compiler will create default constructor for it. The default constructor always calls the zero-argument constructor of superclass. Since MusicalInstrument do not have it, the compilation of Flute will fail.