Method Overloading

POST TEST


Question 1:
What will be the output of the following Java program?
class Abc{
public static void main(String[]args){
String[] elements = { "for", "tea", "too" };
String first = (elements.length > 0) ? elements[0]: null;
}
}

Compilation error
An exception is thrown at run time
The variable first is set to null
The variable first is set to elements[0]


Question 2:
Which of these is supported by method overriding in Java?
Abstraction
Encapsulation
Polymorphism
none of the above


Question 3
What is the output of the following 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);
}
}

int float method
float int method
compile time error
None of above.


Question 4:
What is the output of this program?
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);
}
}

5
6
7
None of above.


Question 5:
What is the output of this program?
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);
}
}

6 6
6.4 6.4
6.4 6
4 6.4


Question 6:
What is the output of this program?
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);
}
}

10 20
20 10
20 40
40 20


Question 7:
What is the output of this program?
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);
} }

10 20
20 10
20 40
40 20


Question 8:


What is process of defining two or more methods within same class that have same name but different parameters declaration?
method overloading.
method overriding.
method hiding
None of the above.

Number of score out of 8 = Score in percentage =