Java Questions & Answers – Methods Taking Parameters

This set of Java Questions and Answers for Entrance exams focuses 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?
a) main method
b) finalize method
c) static method
d) private method
View Answer

Answer: c
Explanation: If a static method is present in the program then it will be executed first, then main will be executed.

2. What is the process of defining more than one method in a class differentiated by parameters?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
View Answer

Answer: b
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 these can be used to differentiate two or more methods having the same name?
a) Parameters data type
b) Number of parameters
c) Return type of method
d) All of the mentioned
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

4. Which of these data type can be used for a method having a return statement in it?
a) void
b) int
c) float
d) both int and float
View Answer

Answer: d
Explanation: None.

5. Which of these statement is incorrect?
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
View Answer

Answer: d
Explanation: Even if a method is returning a value, it is not necessary to store that value.
Note: Join free Sanfoundry classes at Telegram or Youtube

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.         public static void main(String args[]) 
  14.         {
  15.             box obj = new box();
  16.             obj.height = 1;
  17.             obj.length = 5;
  18.             obj.width = 5;
  19.             obj.volume(3, 2, 1);
  20.             System.out.println(obj.volume);        
  21.         } 
  22.     }

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

Answer: c
Explanation: None
output:

advertisement
$ Prameterized_method.java
$ Prameterized_method
6

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

advertisement
  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
View Answer

Answer: b
Explanation: 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.         void volume(int x) 
  12.         {
  13.             volume = x;
  14.         }
  15.     }    
  16.     class Output 
  17.     { 
  18.         public static void main(String args[]) 
  19.         {
  20.             box obj = new box();
  21.             obj.height = 1;
  22.             obj.length = 5;
  23.             obj.width = 5;
  24.             obj.volume(5);
  25.             System.out.println(obj.volume);        
  26.         } 
  27.     }

a) 0
b) 5
c) 25
d) 26
View Answer

Answer: b
Explanation: None.
output:

$ javac Output.java
$ java Output
5

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

  1.     class Output 
  2.     {
  3.         static void main(String args[]) 
  4.         {    
  5.              int x , y = 1;
  6.              x = 10;
  7.              if(x != 10 && x / 0 == 0)
  8.                  System.out.println(y);
  9.              else
  10.                  System.out.println(++y);
  11.         } 
  12.     }

a) 1
b) 2
c) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: 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)

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

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

a) 0
b) 1
c) 25
d) 30
View Answer

Answer: d
Explanation: None.
output:

$ javac cons_method.java
$ java cons_method
30
Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.