Q) Final keyword in java is used with
- class
- class fields
- class methods
- All of the above
Answer: 4
Don’t want somewhere in the program to change class fields, then make it final.
don’t want to allow a class to be inherited, then make the class final
want to allow another class to inherit method of a class but don’t allow them to override it then make the method final. Read complete notes on final keyword in java with example
Q) False statement about final method in java
- Value of final variable cannot be changed once initialized.
- Final method is inherited but we cannot override it
- If you make a class final then you cannot extend the class
- Constructor can be declared as final.
Answer: 4
final variable act as a const, hence cannot be changed. final method we make to stop another class to override it. Class is made final to restrict, so, it cannot be inherited. Constructor cannot be final as they cannot be inherited.
Q) Super keyword in java is used to
- Refer immediate parent class instance variables.
- Invoke immediate parent class methods.
- Invoke immediate parent class constructor.
- All
Answer: 4
Q) True statement about extending a class in java is
- A class can extends only one another class
- A class can extends multiple classes.
- A class can extends multiple interfaces.
- None
Answer: 2
A class can extends only one another class and cannot extend multiple classes as java does not support multiple inheritance for classes.
A class does not extends interfaces but implement them, hence, answer C is incorrect. However, multiple inheritance in java is supported for interfaces. Means. you can implement multiple inheritance using interface.
Q) Correct statement about a class and interface is/are
- An interface can extend multiple interfaces
- A class can implement multiple interfaces.
- Java support multiple inheritance using interfaces.
- All of the above.
Answer: 4