0 ratings0% found this document useful (0 votes) 80 views14 pagesMethod Overloading - Java OOPs Questions and Answers - Sanfoundry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Co
Object Oriented Programming using Java Questions and
Answers - Method Overloading
‘This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on
“Method Overloading’.
1. What will be the output of the following Java code?
class cle
public int dis0()
return 20;
public Long
a)10
bat
©) Compilation Error
d) Null
View Answerconcept of function or method overloading and thereby a compilation error is thrown.
Output
$ Savae Cale.jave
$ java cale
Unconpiiabe source code
2. What will be the output of the following Java code?
advertisement
class calet
‘
public int i390)
‘
)
public Long disp(int 3)
4
return 3;
>
public static votd main(stringl) ares)
cate 0b) = new Cate(:
systen,out.printn(ob.485p(23));
)
a)10
bat
©) Compilation Error
4) Null
View Answername inside the same class if they accept different arguments. In this case, as function disp() occurs twice with different
parameters, the program is executed successfully and returns 11 as mentioned in the function body,
Output:
§ javac Cale. Jove
$ Save cate
3. What will be the output of the following Java code?
advertisement
class Display
‘
private static void cisplay(int a)
4
systen.out.printin(a);
)
private static void display(dnt a, int b)
4
system.out.printin(a 1" b)s
>
public static votd nain(
eisplay(5);
() args)
>
ays
bya
©) Compilation Error
a4function overloading. In the above program, there are two display() functions which have different function signatures. As
the function with two parameters in called in the main() method, the value 5 is printed,
Output:
advertisement
$ javac Display. java
$ Save Display
4, What will be the output of the following java code?
class Float
static void display(int a, float b)
‘
System.out.printin(a +" +b):
>
public static void nain(
‘
sisplay(a, 4)
at] ares)
>
2)1.040
b)140
©) Compilation Error
14
View Answer
Answer: b
Explanation: Overloaded methods are differentiated based on the number and type of the parameters passed as
arguments to the function when method overloading is used in a program. In this case, the integer value 4 is converte$ javae Flost.jove
$ java Float
5. What will be the output of the following Java code?
clase Printvalve
private static void cteplay(dnt 2)
a
System. out.printin("string");
>
private static void cteplay(string a)
‘
systen.out.printin(“tat");
?
public static votd maint
4
sisplay(*helio”
a) ares)
>
a) string
by int
) Compilation Error
d) Hello
View Answer
Answer: b
Explanation: Method overloading or function overloading is a feature of object oriented programming where
polymorphism is implemented. In the above program, there are two display() functions which have different function
signatures but have the same number of parameters. Since a string is passed in the main() method, the method with
string arguments is executed,
Output
$ javac Printvalue.java
$ java Printvalue
6. What will be the output of the following Java code?
class patarype
‘
static vold print(int a)
‘
systen.out.printin( string");
>
static vold print(string 2, ant ©)print ("8");
>
ajo
by int
string
d) Compilation error
View Answer
Answer: d
Explanation: If the argument lists of both the given functions are different in terms of number of parameters and their
types, the polymorphism aspect of object oriented programming is invoked. This phenomenon is called method or
function overloading, In this case, the main() method calls a single string value but there is no function print() with exatly
one string value, hence an error is thrown,
Output
$ javac vatatype. java
$ jave oatarype
Unconpilable source code
‘7. What will be the output of the following Java code?
lass stringandinteger
static vold print(int a)
systen.out.printin( string");
>
static void print(string 2, ant c)
‘
Systen.out.printin( int");
>
public static void main(String] args)
‘
print ("3",5)5
?
ajo
by int
string
) compilation error
View Answer
Answer: b
Explanation: Multiple methods can have same name inside the same class in a program if they accept different§ javae Stringanétnteger. java
$ java Stringandtnteger
ant
8, What will be the output of the following Java code?
clase Stringanainteger2
static void print(int 3)
System. out.printin("string");
>
static veld print(string 2, ant ©)
‘
systen.out.printin(“tat");
?
public static votd maint
4
a) ares)
print(ase,"int*);
>
ajo
by int
string
d) compilation error
View Answer
Answer: d
Explanation: Method overloading is a concept in Java that allows to have many functions with same name but different
parameters. The order of datatype also matters. in this case, an integer and string are passed, but even though the
second print() function contains the same datatypes, they are not in the same order. Hence, an error is thrown,
Output
$ javac Stringanernteger2. java
$ java Stringandinteger2
Uncomptiable source code
9. What will be the output of the following Java code?
class Sun
‘
static vold cale(int 2)
‘
systen.out.printn(ara):
>
static vold print(double 2, Ant c)155
b)20.0
20
4) Compilation Error
View Answer
Answer: b
Explanation: If the data types are compatible, then Java will perform the conversion automatically known as Automatic
Type Conversion. In this case, since the main() method calls the second print() function, the integer is directly converted to,
double datatype by the compiler and does the arithmetic after that.
Output
$ javac Sum.jave
$ java sum
10. What will be the output of the following Java code?
class sunz
‘
static void print (int 2)
‘
systen.out.printin(ata);
)
static void print(Flost 2, int ©)
‘
system.out.printin(ate);
>
public static votd main(string!) ares)
«
print(35.0,5)5
>
a) 155
b)20.0
20
) Compilation Error
View Answer
Answer: d
Explanation: In the above program, the concept of function or method overloading is used. As there are two parameters
used to call the function the main() method, the second function apparently seems to be executed but that’s not the case
because the value of 15.0 is read as a double datatype and not float. Hence an error is thrown by the compiler.
Output11. What will be the output of the following Java code?
class Flosthas
‘
static vold print (Float 2, double »)
4
system.out.printin(ara):
>
static vold print(fleat a, float ¢)
System.out.printin(are):
>
public static void main(st
a
ng] ares)
a) 155
b) 20.0
920
4) Compilation Error
View Answer
Answer: d
Explanation: Method overloading or function overloading Is a feature of abject oriented programming where
polymorphism is implemented. In the above program, there are two print() functions and none of them are executed due
to mismatch of datatypes in the main() method.
Output:
$ javee Floataad. java
$ java Floseada
Unconpiiable source code
12. What will be the output of the following Java code?
class Flostade?
‘
static void calc(Float 2, double b)
‘
systen.out.printia(ata);
)
static vold calc(Float a, float ©)
4
system.out.printin(ate):
>
public static void main(st
‘
cale(18.0F,5.0);920
dd) Compilation Error
View Answer
Answer: a
Explanation: Multiple methods can have same name inside the same class in a program if they accept different
arguments in terms of their datatype or the number of arguments, This feature is known as method overloading. The
function cale() in this case is called in the main() method and is successfully executed,
Output:
$ javae Floataad2. java
$ java Flostadez
30.0
13. What will be the output of the following Java code?
lass otntede
‘
static void calc(Float 3, double b)
‘
systen.out.printin(ata);
)
static vold colc(Float a, nt )
4
systen.out.printin(ate);
>
public static votd nain(
‘
(1 args)
cale(15.0F, 10);
>
2) 30.0
b)20.0
9250
4) Compilation Error
View Answer
Answer: ¢
Explanation: Method overloading is a concept in Java that allows to have many functions with same name but different
parameters. The order of datatype also matters, Here, the float and integer values, 15.0f and 10 are passed to the calc)
function, Hence, 25.0 is attained as the output.
Output
$ Savae Pointadd. java
$ java Potnckda