Overloading Methods & Argument Passing Questions & Answers - Sanfoundry
Overloading Methods & Argument Passing Questions & Answers - Sanfoundry
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.
Answer: c
Explanation: None.
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.
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.
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. }
12.
13. public static void main(String[]args)
14. {
15. San s=new San();
16. s.m1(20,20);
17. }
18. }
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.
1. class overload
2. {
3. int x;
4. int y;
5. void add(int a)
6. {
7. x = a + 1;
8. }
9. void add(int a, int b)
10. {
11. x = a + 2;
12. }
13. }
14. class Overload_methods
15. {
16. public static void main(String args[])
17. {
18. overload obj = new overload();
19. int a = 0;
20. obj.add(6);
21. System.out.println(obj.x);
22. }
23. }
a) 5
b) 6
c) 7
d) 8
View Answer
Answer: c
Explanation: None.
output:
advertisement
$ javac Overload_methods.java
$ java Overload_methods
7
1. class overload
2. {
3. int x;
4. int y;
5. void add(int a)
6. {
7. x = a + 1;
8. }
9. void add(int a , int b)
10. {
11. x = a + 2;
12. }
13. }
14. class Overload_methods
15. {
16. public static void main(String args[])
17. {
18. overload obj = new overload();
19. int a = 0;
20. obj.add(6, 7);
21. System.out.println(obj.x);
22. }
23. }
a) 6
b) 7
c) 8
d) 9
View Answer
Answer: c
Explanation: None.
output:
$ javac Overload_methods.java
$ java Overload_methods
8
1. class overload
2. {
3. int x;
4. double y;
5. void add(int a , int b)
6. {
7. x = a + b;
8. }
9. void add(double c , double d)
10. {
11. y = c + d;
12. }
13. overload()
14. {
15. this.x = 0;
16. this.y = 0;
17. }
18. }
19. class Overload_methods
20. {
21. public static void main(String args[])
22. {
23. overload obj = new overload();
24. int a = 2;
25. double b = 3.2;
26. obj.add(a, a);
27. obj.add(b, b);
28. System.out.println(obj.x + " " + obj.y);
29. }
30. }
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:
$ javac Overload_methods.java
$ java Overload_methods
4 6.4
1. class test
2. {
3. int a;
4. int b;
5. void meth(int i , int j)
6. {
7. i *= 2;
8. j /= 2;
9. }
10. }
11. class Output
12. {
13. public static void main(String args[])
14. {
15. test obj = new test();
16. int a = 10;
17. int b = 20;
18. obj.meth(a , b);
19. System.out.println(a + " " + b);
20. }
21. }
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:
$ javac Output.java
$ java Output
10 20
1. class test
2. {
3. int a;
4. int b;
5. test(int i, int j)
6. {
7. a = i;
8. b = j;
9. }
10. void meth(test o)
11. {
12. o.a *= 2;
13. o.b /= 2;
14. }
15. }
16. class Output
17. {
18. public static void main(String args[])
19. {
20. test obj = new test(10 , 20);
21. obj.meth(obj);
22. System.out.println(obj.a + " " + obj.b);
23. }
24. }
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:
$ javac Output.java
$ java Output
20 10
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and
Answers.
« Prev - Java Questions & Answers – Heap and » Next - Java Questions & Answers – Access Control
Garbage Collection –1
Related Posts:
advertisement
Recommended Articles:
1. Java Program to Find Arithmetic Sum by Passing Argument Using Method Overloading
2. Object Oriented Programming using Java Questions and Answers – Method Overloading
3. Java Questions & Answers – Generic Methods
4. Java Questions & Answers – Methods Taking Parameters
5. Java Program to Find Area of Square, Rectangle and Circle using Method Overloading
6. Java Questions & Answers – Methods
7. Object Oriented Programming using Java Questions and Answers – Constructor Overloading
8. Java Methods
9. Operator Overloading in C++ Using Friend Function
10. Java Questions & Answers – Java.lang – Miscellaneous Math Methods & StrictMath Class
advertisement
Additional Resources:
Java Programs on Classes and Objects
Java Applet Programs
Java Programs on Collections
Java Programming Examples
Event Handling in Java with Examples
Popular Pages:
Searching Algorithms in Java
Java Programming MCQ Questions
Sorting Algorithms in Java
Java Matrix Programs
Java String Programs: Code Examples & Solutions
Name
Subscribe
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get
free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos,
internships and jobs!
© 2011-2025 Sanfoundry. All Rights Reserved.