212). 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
Show Answer
Correct Answer: method overloading
Notes:
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.
213). Which of these can be overloaded?
[A]Methods
[B]Constructors
[C]All of the mentioned
[D]None of the mentioned
Show Answer
Correct Answer: All of the mentioned
Notes:
Answer: c
Explanation: None.
214). 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
Show Answer
Correct Answer: Copy of argument is made into the formal parameter of the subroutine
Notes:
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.
215). 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
Show Answer
Correct Answer: Recursion
Notes:
Answer: d
Explanation: None.
216). What will be the output of the following Java code?
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");
}
public static void main(String[]args)
{
San s=new San();
s.m1(20,20);
}
}
[A]int float method
[B]float int method
[C]compile time error
[D]run time error
Show Answer
Correct Answer: compile time error
Notes:
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.
217). What will be the output of the following Java code?
class overload
{
int x;
int y;
void add(int a)
{
x = a + 1;
}
void add(int a, int b)
{
x = a + 2;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6);
System.out.println(obj.x);
}
}
Show Answer
Correct Answer: 7
Notes:
Answer: c
Explanation: None.
output:$ javac Overload_methods.java
$ java Overload_methods
7
218). What will be the output of the following Java code?
class overload
{
int x;
int y;
void add(int a)
{
x = a + 1;
}
void add(int a , int b)
{
x = a + 2;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
obj.add(6, 7);
System.out.println(obj.x);
}
}
Show Answer
Correct Answer: 8
Notes:
Answer: c
Explanation: None.
output:
$ javac Overload_methods.java
$ java Overload_methods
8
219). What will be the output of the following Java code?
class overload
{
int x;
double y;
void add(int a , int b)
{
x = a + b;
}
void add(double c , double d)
{
y = c + d;
}
overload()
{
this.x = 0;
this.y = 0;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 2;
double b = 3.2;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + " " + obj.y);
}
}
[A]6 6
[B] 6.4 6.4
[C]6.4 6
[D] 4 6.4
Show Answer
Correct Answer: 4 6.4
Notes:
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
220). What will be the output of the following Java code?
class test
{
int a;
int b;
void meth(int i , int j)
{
i *= 2;
j /= 2;
}
}
class Output
{
public static void main(String args[])
{
test obj = new test();
int a = 10;
int b = 20;
obj.meth(a , b);
System.out.println(a + " " + b);
}
}
[A] 10 20
[B]20 10
[C] 20 40
[D]40 20
Show Answer
Correct Answer: 10 20
Notes:
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
221). What will be the output of the following Java code?
class test
{
int a;
int b;
test(int i, int j)
{
a = i;
b = j;
}
void meth(test o)
{
o.a *= 2;
O.b /= 2;
}
}
class Output
{
public static void main(String args[])
{
test obj = new test(10 , 20);
obj.meth(obj);
System.out.println(obj.a + " " + obj.b);
}
}
[A] 10 20
[B]20 10
[C]20 40
[D]40 20
Show Answer
Correct Answer: 20 10
Notes:
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