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
class San
{
public void m1 [int i,float f]
{
System.out.println[" int float method"];
}
public void m1[float f,int i];
{
System.out.println["float int method"];
{
0{
2{
{
4{
5{
0{
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?
{
8{
9public void m1 [int i,float f]
0public void m1 [int i,float f]
1public void m1 [int i,float f]
2public void m1 [int i,float f]
3public void m1 [int i,float f]
4public void m1 [int i,float f]
5public void m1 [int i,float f]
6public void m1 [int i,float f]
3public void m1 [int i,float f]
8public void m1 [int i,float f]
9{
0{
1{
9{
3public void m1 [int i,float f]
3{
5{
6{
7{
8public void m1 [int i,float f]
5System.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?
{
8{
9public void m1 [int i,float f]
0public void m1 [int i,float f]
1System.out.println[" int float method"];
6public void m1 [int i,float f]
3public void m1 [int i,float f]
4public void m1 [int i,float f]
5}
0public void m1 [int i,float f]
3public void m1 [int i,float f]
8public void m1 [int i,float f]
9{
0{
1{
9{
3public void m1 [int i,float f]
3{
5{
6{
8public void m1 [int i,float f]
5
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?
public void m1 [int i,float f]
0public void m1[float f,int i];
0public void m1 [int i,float f]
3public void m1[float f,int i];
2public void m1 [int i,float f]
5public void m1[float f,int i];
4public void m1 [int i,float f]
3public void m1[float f,int i];
6public void m1 [int i,float f]
5public void m1[float f,int i];
8public void m1 [int i,float f]
3{
0{
1public void m1 [int i,float f]
9{
0{
1{
9{
3public void m1 [int i,float f]
3{
5{
9System.out.println["float int method"];
0System.out.println["float int method"];
1System.out.println["float int method"];
2System.out.println["float int method"];
3public void m1 [int i,float f]
5System.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?
System.out.println["float int method"];
7{
9System.out.println["float int method"];
9{
00{
01public void m1 [int i,float f]
3{
03{
04{
05{
0{
07{
9{
3public void m1 [int i,float f]
3{
11{
12{
13{
14{
15{
16
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:
{
1810. What will be the output of the following Java code?
System.out.println["float int method"];
7{
9System.out.println["float int method"];
9{
00{
23public void m1 [int i,float f]
3{
25{
26public void m1 [int i,float f]
5{
28public void m1 [int i,float f]
3{
30{
31{
05{
0{
07{
9{
3public void m1 [int i,float f]
3{
38{
39{
40{
16
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:
{
43Sanfoundry 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.