This set of Java Questions and Answers for Entrance exams on “Methods Taking Parameters”.
1. Which of these is the method which is executed first before execution of any other thing takes place in a program? Answer: c 2. What is the process of defining more than one method in a class differentiated by parameters? Answer: b 3. Which of these can be used to differentiate two or more methods having the same name? Answer: d 4. Which of these data type can be used for a method having a return statement in it? Answer: d 5. Which of these 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. What will be the output of the following Java program? a) 1 10. What will be the output of the following Java program? a) 0
a) main method
b) finalize method
c) static method
d) private method
Clarification: If a static method is present in the program then it will be executed first, then main will be executed.
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) Parameters data type
b) Number of parameters
c) Return type of method
d) All of the mentioned
Clarification: None.
a) void
b) int
c) float
d) both int and float
Clarification: None.
a) Two or more methods with same name can be differentiated on the basis of their parameters data type
b) Two or more method having same name can be differentiated on basis of number of parameters
c) Any already defined method in java library can be defined again in the program with different data type of parameters
d) If a method is returning a value the calling statement must have a variable to store that value
Clarification: Even if a method is returning a value, it is not necessary to store that value.
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;
}
void volume(int x)
{
volume = x;
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(5);
System.out.println(obj.volume);
}
}
b) 5
c) 25
d) 26
Clarification: None.
output:
$ javac Output.java
$ java Output
5
class Output
{
static void main(String args[])
{
int x , y = 1;
x = 10;
if(x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
b) 2
c) Runtime Error
d) Compilation Error
Clarification: main() method must be made public. Without main() being public java run time system will not be able to access main() and will not be able to execute the code.
output:
$ javac Output.java
Error: Main method not found in class Output, please define the main method as:
public static void main(String[] args)
class area
{
int width;
int length;
int height;
area()
{
width = 5;
length = 6;
height = 1;
}
void volume()
{
volume = width * height * length;
}
}
class cons_method
{
public static void main(String args[])
{
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
b) 1
c) 25
d) 30
Clarification: None.
output:
$ javac cons_method.java
$ java cons_method
30