This section of our 1000+ Java MCQs focuses on Methods of Java Programming Language.
1. What is the return type of a method that does not returns any value?
a) int
b) float
c) void
d) double
View Answer
Explanation: Return type of an method must be made void if it is not returning any value.
2. What is the process of defining more than one method in a class differentiated by method signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
View Answer
Explanation: 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.
3. Which of the following is a method having same name as that of it’s class?
a) finalize
b) delete
c) class
d) constructor
View Answer
Explanation: 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.
4. Which method can be defined only once in a program?
a) main method
b) finalize method
c) static method
d) private method
View Answer
Explanation: main() method can be defined only once in a program. Program execution begins from the main() method by java’s run time system.
5. Which of these statement is incorrect?
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
View Answer
Explanation: 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.
6. What is the output of this program?
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);
}
}
a) 0
b) 1
c) 6
d) 25
View Answer
Explanation: None.
output:
$ Prameterized_method.java
$ Prameterized_method
6
7. What is the output of this program?
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());
}
}
a) false
b) true
c) 0
d) 1
View Answer
Explanation: None.
output:
$ javac Output.java
$ java Output
true
8. What is the output of this program?
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);
}
}
a) 0
b) 1
c) 25
d) 26
View Answer
Explanation: None.
output:
$ javac Output.java
$ java Output
25
9. In the below code, which call to sum() method is appropriate?
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);
}
}
a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
View Answer
Explanation: sum is a variable argument method and hence it can take any number as argument.
10. What is the output of this program?
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);
}
}
a) 0
b) 1
c) 30
d) error
View Answer
Explanation: Variable height is not defined.
output:
$ javac cons_method.java
$ java cons_method
error: cannot find symbol height
To practice all features of Java programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.