0

Method Overloading and Method Overriding

Description: Method Overloading and Method Overriding
Number of Questions: 15
Created by:
Tags: Method Overloading and Method Overriding Java/Oracle /C/C++ Java Basics of Java Computer Basics
Attempted 0/15 Correct 0 Score 0

Which of the following statements is/are correct for static method in Java?

A. Static methods are always public. B. Static method cannot access directly non-static method of the same class.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: B
Explanation:

It is not necessary that the static methods are always public. But it is true that static methods cannot access directly non-static method of the same class.

Which of the following statement(s) is/are correct? A. valueOf() is a static method. B. toString() is overridden in Integer and Double Class.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: C
Explanation:

Both the statements are correct. The methods valueOf() and toString() belong to String class. The valueOf() method is a static method, so it is always called by using the class name String and the method toString() is overridden in wrapper classes, such as Integer and Double.

Which of the following conditions is incorrect for performing overriding of methods in Java?

  1. The method name in subclass must be identical to that of the superclass method.

  2. The return type of both the methods must be different.

  3. Methods of the superclass declared as public cannot be overrided using private keyword in the subclass.

  4. An overriding method cannot raise more exceptions than those raised by superclass.

  5. The overrided methods cannot have different number of arguments.


Correct Option: B
Explanation:

This is an incorrect condition as for overriding of methods they must have same return type, both in superclass and subclass.

Which of the following statements is/are correct for recursion in Java? A. Recursion methods normally require extra memory. B. Recursion methods take more time.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: C
Explanation:

Both are the disadvantages of recursion. Recursion always takes more space and time.

What is the method used to convert String variable to primitive integer

  1. Integer.parseInt(String str)

  2. Integer.convertInt(String str)

  3. Integer.valueOf(String str)

  4. Integer.toInteger(String str)


Correct Option: A
Explanation:

This answer is correct. To convert a String to a primitive integer, static method parseInt(String str) is executed. This will take the String variable and convert to primitive integer

What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?

  1. public

  2. abstract

  3. protected

  4. synchronized

  5. default access


Correct Option: D
Explanation:

default access is the package oriented access modifier. Option 1 and 3 are wrong because public and protected are less restrictive. Option 2 and 4 are wrong because abstract and synchronized are not access modifiers.

Which method is not overridden by Annotation interface?

  1. hashCode()

  2. equals()

  3. clone()

  4. toString()


Correct Option: C
Explanation:

This answer is correct. Annotation interface overrides only three methods hashCode(), toString() and equals() methods. All the methods in Object class other than hashCode(), toString() and equals() are not overridden by the Annotation interface.

Which two statements are true about properly overridden hashCode() and equals() methods?

  1. hashCode() doesn't have to be overridden if equals() is.
  2. equals() doesn't have to be overridden if hashCode() is.
  3. hashCode() can always return the same value, regardless of the object that invoked it.
  4. equals() can be true even if it's comparing different objects.
  1. 1 and 2

  2. 2 and 3

  3. 3 and 4

  4. 1 and 3


Correct Option: C
Explanation:

Statements (3) and (4) are correct. Statements (1) and (2) are incorrect because hashCode() and equals() can't be overridden.

What is the difference between constructor and other methods?

  1. Constructor must not have the same name as the class name, whereas the methods can have any name.

  2. Constructor must have the same name as the class name whereas the methods can have any name.

  3. Constructor can have return type whereas the methods may or may not have return methods.

  4. Constructor and the methods can be called any number of times in the object life cycle.


Correct Option: B
Explanation:

Constructor should have the class name as its name because when the object is instantiated, it will call the default constructor. The methods can have any name.

Which of the following statements are correct? i. final methods can be overridden ii. final fields are constants iii. final methods cannot be overridden iv. final variables can be changed

  1. i and ii both are correct

  2. i and iv both are correct

  3. ii and iii both are correct

  4. ii and iii both are incorrect


Correct Option: C
Explanation:

Final methods are meant not to be overridden. Similarly final variables cannot be changed.

Which of the following statements is/are correct? :i. Overloaded methods are multiple methods which have the same name but different arguments. ii. Overridden methods are multiple methods which have the same name, same return type and same arguments. iii. Overloaded methods are multiple methods which have the same name, same return type and same arguments. iv. Overridden methods are multiple methods which have the same name but different arguments.

  1. ii and iv are correct

  2. i and ii are correct

  3. only iii is correct

  4. only i is correct


Correct Option: B
Explanation:

This answer is correct. Overloaded methods are multiple methods which have the same name but different argument list. It is part of polymorphism whereas overridden methods are those methods which have the same name, same return type and same argument list.

Which of the following is/are true regarding overriding methods? i. A method marked as final cannot be overridden. ii. A method marked as static can be overridden. iii. A method marked as final can be overridden. iv. A method marked as static cannot be overridden.

  1. i and iv are correct

  2. only i is correct

  3. only ii is correct

  4. only iv is correct


Correct Option: A
Explanation:

This answer is correct. A method marked as as final or static cannot be overridden.

Which of the following are true regarding dynamic method dispatch? i. Dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile time. ii. Dynamic method dispatch is a process in which a call to an overridden method is resolved at compile time rather than at runtime. iii. In Dynamic method dispatch process, a reference variable calls the overridden method of a superclass. iv. In Dynamic method dispatch process, a reference variable calls the overridden method of a subclass.

  1. i. and iv. are correct

  2. i. and iii. are correct

  3. ii. and iii. are correct

  4. ii. and iv. are correct


Correct Option: B
Explanation:

The dynamic dispatch process is a process which is applicable when  a call to the overridden method is resolved at runtime rather than at compile time. This will be used as part of polymorphism concept. In the dynamic dispatch process, a reference variable calls the overridden method of the superclass but not the subclass because it doesn't know anything about subclass.

Which of the following are true regarding super keyword? i. Any level can be accessed using super keyword. ii. In the constructor, if super() method is used, it should be the first code. iii. Super is the keyword which is used to access method or member variables from the superclass. iv. Super is the keyword which is used to access method or member variables from the subclass.

  1. i and ii both are correct

  2. ii and iii both are correct

  3. i and iv both are correct

  4. i and iii both are correct


Correct Option: B
Explanation:

This answer is correct. Always the super() method when used in constructor should be placed as first code. If it is placed at any other place, compilation errors will occur. Also, super keyword is used to access method and member variables from the superclass.

Which of the following is true regarding overriding main method? i. main method can be overridden because it is having access modifier as public. ii. main method can be overridden because it is having return type as void. iii. main method can be overridden because the method is static. iv. main method cannot be overridden.

  1. Only i is correct

  2. Only ii is correct

  3. Only iii is correct

  4. Only iv is correct


Correct Option: D
Explanation:

This answer is correct because any static method cannot be overridden and so main method cannot be overridden.

- Hide questions