Java MCQs on Methods of Java Programming Language.
1. What is the return type of a method that does not return any value? Answer: c 2. What is the process of defining more than one method in a class differentiated by method signature? Answer: b 3. Which of the following is a method having same name as that of it’s class? Answer: d 4. Which method can be defined only once in a program? Answer: a 5. Which of this statement is incorrect? Answer: d 6. What will be the output of the following Java program? a) 0 7. What will be the output of the following Java program? a) false 8. What will be the output of the following Java program? a) 0 9. In the following Java code, which call to sum() method is appropriate? a) only sum(10) Answer: d 10. What will be the output of the following Java program? a) 0
a) int
b) float
c) void
d) double
Clarification: Return type of a method must be made void if it is not returning any value.
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
Clarification: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example – int volume(int length, int width) & int volume(int length , int width , int height) can be used to calculate volume.
a) finalize
b) delete
c) class
d) constructor
Clarification: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
a) main method
b) finalize method
c) static method
d) private method
Clarification: main() method can be defined only once in a program. Program execution begins from the main() method by java runtime system.
a) All object of a class are allotted memory for the all the variables defined in the class
b) If a function is defined public it can be accessed by object of other class by inheritation
c) main() method must be made public
d) All object of a class are allotted memory for the methods defined in the class
Clarification: All object of class share a single copy of methods defined in a class, Methods are allotted memory only once. All the objects of the class have access to methods of that class are allotted memory only for the variables not for the methods.
class box
{
int width;
int height;
int length;
int volume;
void volume(int height, int length, int width)
{
volume = width*height*length;
}
}
class Prameterized_method
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(3,2,1);
System.out.println(obj.volume);
}
}
b) 1
c) 6
d) 25
Clarification: None.
output:
$ Prameterized_method.java
$ Prameterized_method
6
class equality
{
int x;
int y;
boolean isequal()
{
return(x == y);
}
}
class Output
{
public static void main(String args[])
{
equality obj = new equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal());
}
}
b) true
c) 0
d) 1
Clarification: None.
output:
$ javac Output.java
$ java Output
true
class box
{
int width;
int height;
int length;
int volume;
void volume()
{
volume = width*height*length;
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume();
System.out.println(obj.volume);
}
}
b) 1
c) 25
d) 26
Clarification: None.
output:
$ javac Output.java
$ java Output
25
class Output
{
public static int sum(int ...x)
{
return;
}
static void main(String args[])
{
sum(10);
sum(10,20);
sum(10,20,30);
sum(10,20,30,40);
}
}
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
Clarification: sum is a variable argument method and hence it can take any number as an argument.
class area
{
int width;
int length;
int volume;
area()
{
width=5;
length=6;
}
void volume()
{
volume = width*length*height;
}
}
class cons_method
{
public static void main(String args[])
{
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
b) 1
c) 30
d) error
Clarification: Variable height is not defined.
output:
$ javac cons_method.java
$ java cons_method
error: cannot find symbol height