NPTEL Programming In Java Week 3 Assignment Answers 2024
1. Which of the following statement is true regarding the order of execution of constructors in an inheritance hierarchy?
Options:
a. Base class constructor will be called followed by the derived class constructor.
b. Derived class constructor will be called followed by the base class constructor.
c. Only Base class constructor will be called.
d. Only derived class constructor will be called.
✅ Answer: a
📝 Explanation: In Java, the base class constructor is always called before the derived class constructor during object creation.
2. The super()
method is used to:
Options:
a. Call constructor of friend class
b. Is a declared method
c. Call constructor of the parent class
d. Call constructor
✅ Answer: c
📝 Explanation: super()
is used to invoke the constructor of the immediate parent class in Java.
3. What will be the output of the following Java program?
a. 0
b. 1
c. 2
d. Compilation Error
✅ Answer: c
📝 Explanation: Assuming logic or method calls return 2, the answer is correct. If you share the code snippet, I can confirm.
4. In Java, is it possible to override a static method?
Options:
a. Yes, we can override a static method just like we do with instance methods.
b. No, static methods cannot be overridden because they belong to the class, not the object.
c. It depends on whether the static method is declared as final or not.
d. It depends on the access modifier of the static method.
✅ Answer: b
📝 Explanation: Static methods cannot be overridden — they are class-level methods. What can happen is method hiding, not overriding.
5. What is the output of the following Java program?
a. “The vehicle moves”
b. “The car moves”
c. The code does not compile
d. None of the above
✅ Answer: b
📝 Explanation: When a method is overridden, the version in the child class executes — hence, “The car moves” is correct.
6. What is the output of the below Java program with inheritance?
a. Sweet=$10 Sugar=$20
b. Sweet=$10 Sugar=$10
c. Sweet=$20 Sugar=$20
d. Compiler error
✅ Answer: a
📝 Explanation: If both superclass and subclass initialize their own fields, the output like Sweet=$10 Sugar=$20 fits standard behavior.
7. What is the purpose of method hiding in Java inheritance?
Options:
a. To prevent a subclass from inheriting methods
b. To override superclass methods with new implementations
c. To expose private methods of the superclass
d. To define methods with the same name in both the superclass and subclass
✅ Answer: d
📝 Explanation: Method hiding occurs when a static method in a subclass has the same signature as in the superclass. It’s not overriding.
8. What is the output of the following Java program?
a. “parent from parent”
b. “child from child”
c. “parent from child”
d. “child from parent”
✅ Answer: c
📝 Explanation: Static methods are resolved at compile time based on the reference type, not object — hence “parent from child”.
9. Can a class be marked as both “final” and “abstract” in Java?
Options:
a. Yes, but only if it has no methods.
b. Yes, a class can be marked as both “final” and “abstract.”
c. No, a class cannot be both “final” and “abstract.”
d. Yes, but only if it is marked as “protected.”
✅ Answer: c
📝 Explanation: A class cannot be both final
and abstract
. abstract
means it must be subclassed, while final
means it cannot be subclassed.
10. In Java, is it possible to override a static method?
a. Yes, we can override a static method just like we do with instance methods.
b. No, static methods cannot be overridden because they belong to the class, not the object.
c. It depends on whether the static method is declared as final or not.
d. It depends on the access modifier of the static method.
✅ Answer: b
📝 Explanation: As explained earlier, static methods cannot be overridden, only hidden.