250+ TOP MCQs on Introduction To Methods and Answers

Java MCQs on Methods of Java Programming Language.

1. What is the return type of a method that does not return any value?
a) int
b) float
c) void
d) double

Answer: c
Clarification: Return type of a 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

Answer: b
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.

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

Answer: d
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.

4. Which method can be defined only once in a program?
a) main method
b) finalize method
c) static method
d) private method

Answer: a
Clarification: main() method can be defined only once in a program. Program execution begins from the main() method by java runtime system.

5. Which of this 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

Answer: d
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.

6. What will be the output of the following Java program?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.         int volume;
  7.         void volume(int height, int length, int width) 
  8.         {
  9.              volume = width*height*length;
  10.         } 
  11.     }    
  12.     class Prameterized_method
  13.     {
  14.         public static void main(String args[])
  15.         {
  16.             box obj = new box();
  17.             obj.height = 1;
  18.             obj.length = 5;
  19.             obj.width = 5;
  20.             obj.volume(3,2,1);
  21.             System.out.println(obj.volume);        
  22.         } 
  23.      }

a) 0
b) 1
c) 6
d) 25

Answer: c
Clarification: None.
output:

$ Prameterized_method.java
$ Prameterized_method
6

7. What will be the output of the following Java program?

  1.     class equality 
  2.     {
  3.         int x;
  4.         int y;
  5.         boolean isequal()
  6.         {
  7.             return(x == y);  
  8.         } 
  9.     }    
  10.     class Output 
  11.     {
  12.         public static void main(String args[])
  13.         {
  14.             equality obj = new equality();
  15.             obj.x = 5;
  16.             obj.y = 5;
  17.             System.out.println(obj.isequal());
  18.         } 
  19.     }

a) false
b) true
c) 0
d) 1

Answer: b
Clarification: None.
output:

$ javac Output.java
$ java Output 
true

8. What will be the output of the following Java program?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.         int volume;
  7.         void volume() 
  8.         {
  9.              volume = width*height*length;
  10.         } 
  11.     }    
  12.     class Output 
  13.     { 
  14.         public static void main(String args[])
  15.         {
  16.             box obj = new box();
  17.             obj.height = 1;
  18.             obj.length = 5;
  19.             obj.width = 5;
  20.             obj.volume();
  21.             System.out.println(obj.volume);        
  22.         } 
  23.     }

a) 0
b) 1
c) 25
d) 26

Answer: c
Clarification: None.
output:

$ javac Output.java
$ java Output
25

9. In the following Java code, which call to sum() method is appropriate?

  1. class Output 
  2. {
  3.  
  4.         public static int sum(int ...x)
  5.         {
  6.              return; 
  7.         }
  8.         static void main(String args[]) 
  9.         {    
  10.              sum(10);
  11.              sum(10,20);
  12.              sum(10,20,30);
  13.              sum(10,20,30,40);
  14.         } 
  15. }

a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned

Answer: d
Clarification: sum is a variable argument method and hence it can take any number as an argument.

10. What will be the output of the following Java program?

  1.     class area 
  2.     {
  3.         int width;
  4.         int length;
  5.         int volume;
  6.         area() 
  7.         {
  8.            width=5;
  9.            length=6;
  10.         }
  11.         void volume() 
  12.         {
  13.              volume = width*length*height;
  14.         } 
  15.     }    
  16.     class cons_method 
  17.     {
  18.         public static void main(String args[])
  19.         {
  20.             area obj = new area();
  21.             obj.volume();
  22.             System.out.println(obj.volume);        
  23.         } 
  24.     }

a) 0
b) 1
c) 30
d) error

Answer: d
Clarification: Variable height is not defined.
output:

$ javac cons_method.java
$ java cons_method
error: cannot find symbol height

Leave a Reply

Your email address will not be published. Required fields are marked *