MCQ on Java keywords

(Last Updated On: March 13, 2019)
Q) Final keyword in java is used with
  1. class
  2. class fields
  3. class methods
  4. All of the above
View Answer
Answer: D
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
  1. Value of final variable cannot be changed once initialized.
  2. Final method is inherited but we cannot override it
  3. If you make a class final then you cannot extend the class
  4. Constructor can be declared as final.
View Answer
Answer: D
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
  1. Refer immediate parent class instance variables.
  2. Invoke immediate parent class methods.
  3. Invoke immediate parent class constructor.
  4. All
View Answer
Answer: D

Q) True statement about extending a class in java is
  1. A class can extends only one another class
  2. A class can extends multiple classes.
  3. A class can extends multiple interfaces.
  4. None
View Answer
Answer: B
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.