Which of the following is a correct implementation of method overloading void method

This section of our 1000+ Java MCQs focuses on overloading methods & argument passing in Java Programming Language.

1. What is the process of defining two or more methods within same class that have same name but different parameters declaration?
a] method overloading
b] method overriding
c] method hiding
d] none of the mentioned
View Answer

Answer: a
Explanation: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.

2. Which of these can be overloaded?
a] Methods
b] Constructors
c] All of the mentioned
d] None of the mentioned
View Answer

Answer: c
Explanation: None.

3. Which of these is correct about passing an argument by call-by-value process?
a] Copy of argument is made into the formal parameter of the subroutine
b] Reference to original argument is passed to formal parameter of the subroutine
c] Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
d] Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
View Answer

Answer: a
Explanation: When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.

Note: Join free Sanfoundry classes at Telegram or Youtube

advertisement

advertisement

4. What is the process of defining a method in terms of itself, that is a method that calls itself?
a] Polymorphism
b] Abstraction
c] Encapsulation
d] Recursion
View Answer

Answer: d
Explanation: None.

5. What will be the output of the following Java code?

Take Java Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

  1. class San
  2. {
  3.  public void m1 [int i,float f]
  4.  {
  5.   System.out.println[" int float method"];
  6.  }
  7.  
  8.  public void m1[float f,int i];
  9.   {
  10.   System.out.println["float int method"];
  11. {
    0
  12.  
  13. {
    2
  14.   {
  15. {
    4
  16. {
    5
  17. {
    0
  18. {
    7

a] int float method
b] float int method
c] compile time error
d] run time error
View Answer

Answer: c
Explanation: While resolving overloaded method, compiler automatically promotes if exact match is not found. But in this case, which one to promote is an ambiguity.

advertisement

6. What will be the output of the following Java code?

  1. {
    8
  2. {
    9
  3.  public void m1 [int i,float f]
    0
  4.  public void m1 [int i,float f]
    1
  5.  public void m1 [int i,float f]
    2
  6.  public void m1 [int i,float f]
    3
  7.  public void m1 [int i,float f]
    4
  8.  public void m1 [int i,float f]
    5
  9.  public void m1 [int i,float f]
    6
  10.  public void m1 [int i,float f]
    3
  11.  public void m1 [int i,float f]
    8
  12.  public void m1 [int i,float f]
    9
  13.  {
    0
  14.  {
    1
  15. {
    9
  16.  {
    3
  17.  public void m1 [int i,float f]
    3
  18.  {
    5
  19.  {
    6
  20.  {
    7
  21.  {
    8
  22.  public void m1 [int i,float f]
    5
  23.   System.out.println[" int float method"];
    0

a] 5
b] 6
c] 7
d] 8
View Answer

Answer: c
Explanation: None.
output:

advertisement

  System.out.println[" int float method"];
1

7. What will be the output of the following Java code?

  1. {
    8
  2. {
    9
  3.  public void m1 [int i,float f]
    0
  4.  public void m1 [int i,float f]
    1
  5.   System.out.println[" int float method"];
    6
  6.  public void m1 [int i,float f]
    3
  7.  public void m1 [int i,float f]
    4
  8.  public void m1 [int i,float f]
    5
  9.  }
    0
  10.  public void m1 [int i,float f]
    3
  11.  public void m1 [int i,float f]
    8
  12.  public void m1 [int i,float f]
    9
  13.  {
    0
  14.  {
    1
  15. {
    9
  16.  {
    3
  17.  public void m1 [int i,float f]
    3
  18.  {
    5
  19.  {
    6
  20.  
    1
  21.  {
    8
  22.  public void m1 [int i,float f]
    5
  23.  
    4

a] 6
b] 7
c] 8
d] 9
View Answer

Answer: c
Explanation: None.
output:

 
5

8. What will be the output of the following Java code?

  1.  
    6
  2.  
    7
  3.  public void m1 [int i,float f]
    0
  4.  
    9
  5.  public void m1[float f,int i];
    0
  6.  public void m1 [int i,float f]
    3
  7.  public void m1[float f,int i];
    2
  8.  public void m1 [int i,float f]
    5
  9.  public void m1[float f,int i];
    4
  10.  public void m1 [int i,float f]
    3
  11.  public void m1[float f,int i];
    6
  12.  public void m1 [int i,float f]
    5
  13.  public void m1[float f,int i];
    8
  14.  public void m1 [int i,float f]
    3
  15.   {
    0
  16.   {
    1
  17.  public void m1 [int i,float f]
    9
  18.  {
    0
  19.  {
    1
  20. {
    9
  21.  {
    3
  22.  public void m1 [int i,float f]
    3
  23.  {
    5
  24.   {
    9
  25.   System.out.println["float int method"];
    0
  26.   System.out.println["float int method"];
    1
  27.   System.out.println["float int method"];
    2
  28.   System.out.println["float int method"];
    3
  29.  public void m1 [int i,float f]
    5
  30.   System.out.println[" int float method"];
    0

a] 6 6
b] 6.4 6.4
c] 6.4 6
d] 4 6.4
View Answer

Answer: d
Explanation: For obj.add[a,a]; ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4
output:

  System.out.println["float int method"];
6

9. What will be the output of the following Java code?

  1.   System.out.println["float int method"];
    7
  2. {
    9
  3.   System.out.println["float int method"];
    9
  4. {
    00
  5. {
    01
  6.  public void m1 [int i,float f]
    3
  7. {
    03
  8. {
    04
  9. {
    05
  10.  {
    0
  11. {
    07
  12. {
    9
  13.  {
    3
  14.  public void m1 [int i,float f]
    3
  15. {
    11
  16. {
    12
  17. {
    13
  18. {
    14
  19. {
    15
  20. {
    16
  21.  
    4

a] 10 20
b] 20 10
c] 20 40
d] 40 20
View Answer

Answer: a
Explanation: Variables a & b are passed by value, copy of their values are made on formal parameters of function meth[] that is i & j. Therefore changes done on i & j are not reflected back on original arguments. a & b remain 10 & 20 respectively.
output:

{
18

10. What will be the output of the following Java code?

  1.   System.out.println["float int method"];
    7
  2. {
    9
  3.   System.out.println["float int method"];
    9
  4. {
    00
  5. {
    23
  6.  public void m1 [int i,float f]
    3
  7. {
    25
  8. {
    26
  9.  public void m1 [int i,float f]
    5
  10. {
    28
  11.  public void m1 [int i,float f]
    3
  12. {
    30
  13. {
    31
  14. {
    05
  15.  {
    0
  16. {
    07
  17. {
    9
  18.  {
    3
  19.  public void m1 [int i,float f]
    3
  20. {
    38
  21. {
    39
  22. {
    40
  23. {
    16
  24.  
    4

a] 10 20
b] 20 10
c] 20 40
d] 40 20
View Answer

Answer: b
Explanation: Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth[obj] sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth[] function of class test. a & b becomes 20 & 10 respectively.
output:

{
43

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

Which of the following is correct implementation of method overloading?

Simply put, we can implement method overloading in two different ways: implementing two or more methods that have the same name but take different numbers of arguments. implementing two or more methods that have the same name but take arguments of different types.

Which of the following is correct implementation of method overloading Mcq?

Explanation: The concept of method overloading is implemented in method “vol” with same name but different definitions and parameter list which is overloaded three times and each time the return type is different for each method and hence matches the method using types of parameters.

Which of the following is are correct for an overloaded method or a function in?

The correct answer is option4. Key Points Both functions and operators can be overloaded. C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

Which of the following statements is correct about the overloaded methods?

- Method overloading is done in super and sub classes. Ans 3 is correct. return type can be same or sub class of return type of the overridden method.

Chủ Đề