Java Methods Quiz - MCQ - Multiple Choice Questions


Welcome to the Java Methods Quiz! Methods in Java are blocks of code that perform a specific task and are central to the concept of Java's Object-Oriented Programming. This quiz has 10+ multiple-choice questions to test your knowledge of Java methods.

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. What is the return type of a method that does not return any value?

a) null
b) void
c) 0
d) empty

Answer:

b) void

Explanation:

A method that doesn't return any value has a return type of void.

2. Which of the following is a correct method declaration in Java?

a) public int myMethod(int a, int b)
b) public myMethod(int a, int b): int
c) myMethod(int a, int b) -> int
d) myMethod: int(a, b)

Answer:

a) public int myMethod(int a, int b)

Explanation:

Methods in Java are declared with an access modifier, followed by a return type, the method name, and parameters enclosed in parentheses.

3. In Java, every method must be part of ...?

a) an object
b) a class
c) a package
d) a project

Answer:

b) a class

Explanation:

In Java, every method must be part of a class.

4. What are the variables defined in a method header called?

a) arguments
b) parameters
c) values
d) both a and b

Answer:

d) both a and b

Explanation:

The terms "arguments" and "parameters" can both refer to variables defined in a method header, though the context may differentiate their exact meanings.

5. Which of these is NOT a valid method name in Java?

a) myMethod
b) _myMethod
c) 2myMethod
d) myMethod2

Answer:

c) 2myMethod

Explanation:

In Java, method names cannot begin with a number.

6. If a method does not access instance variables, it can be declared as...?

a) void
b) public
c) private
d) static

Answer:

d) static

Explanation:

If a method does not access instance variables, it can be declared as static. This means the method belongs to the class, not any specific instance of the class.

7. What does the method signature consist of?

a) Method name only
b) Method name and parameters
c) Method name, parameters, and return type
d) Method name and return type

Answer:

b) Method name and parameters

Explanation:

The method signature consists of the method name and its parameters. The return type is not considered a part of the method signature.

8. Which of the following is not a purpose of methods in Java?

a) Increase code reusability
b) Provide a structured and organized approach
c) Store data
d) Break a complex problem into simpler ones

Answer:

c) Store data

Explanation:

Methods are used to perform operations and are not primarily designed for storing data. That's what variables and data structures are for.

9. When are method parameters evaluated?

a) At compile time
b) At runtime
c) At load time
d) After method completion

Answer:

b) At runtime

Explanation:

Method parameters are evaluated at runtime, which means when the method is called.

10. Which of the following is a correct way to call a static method named calculate from a class named MathUtility?

a) calculate.MathUtility()
b) new MathUtility().calculate()
c) MathUtility.calculate()
d) calculate()

Answer:

c) MathUtility.calculate()

Explanation:

Static methods are called on the class itself, not on an instance of the class.

11. What is method overloading in Java?

a) Calling a method from another method.
b) Renaming a method at runtime.
c) Having multiple methods with the same name but different parameters in a class.
d) Overriding a superclass method in a subclass.

Answer:

c) Having multiple methods with the same name but different parameters in a class.

Explanation:

Method overloading allows a class to have multiple methods with the same name, differentiated by the number or type of their parameters.


Hopefully, this quiz offered a refreshing way to consolidate your understanding of Java methods. As you delve deeper into Java, always remember the importance of practice and continuous learning. Happy coding!


Comments