Java Methods Quiz

Explore the basics of Java methods with these easy, scenario-based questions designed for anyone starting out. Strengthen your understanding of method concepts commonly found in introductory programming resources.

  1. Purpose of a Method

    Which of the following best describes the main purpose of a method in Java?

    1. To store data permanently
    2. To declare a program's main entry point only
    3. To display the entire program output
    4. To group code into a reusable block

    Explanation: The correct answer is to group code into a reusable block, as methods help organize and reuse logic. Storing data permanently relates to storage mechanisms such as files or databases, not methods. Displaying the entire program output is not the core function of methods. Methods are not exclusively for declaring the main entry point.

  2. Calling a Method

    How do you call a method named greet in Java?

    1. greet();
    2. call greet;
    3. invoke greet{}
    4. greet[];

    Explanation: The correct syntax for calling a method is greet(); in Java. greet[]; uses brackets incorrectly, call greet; is not valid Java syntax, and invoke greet{} does not follow Java's method calling convention.

  3. Method Declaration

    Which of these lines correctly declares a method that returns nothing and is named showMessage?

    1. showMessage void()
    2. void showMessage() {}
    3. return showMessage()
    4. int showMessage[]

    Explanation: void showMessage() {} is the right way to declare a method that returns no value. int showMessage[] incorrectly suggests an array. showMessage void() reverses keyword order. return showMessage() is missing key elements of a declaration.

  4. Return Type

    If a Java method is declared as int getValue(), what kind of value must it return?

    1. A string
    2. An integer value
    3. A character
    4. No value

    Explanation: int getValue() means the method must return an integer. A character is returned by methods of type char. No value would be for void methods. A string should be returned by methods of type String.

  5. Method Parameters

    Which option shows a method with two parameters in Java?

    1. void myMethod int a, int b {}
    2. void myMethod(int a, int b) {}
    3. void myMethod[] {}
    4. myMethod(void a, b) {}

    Explanation: The correct answer lists both parameters in the parentheses. The second option misses parentheses. The third is not proper syntax, and the last uses brackets instead of parentheses.

  6. Method Naming

    Which is a valid Java method name?

    1. void-main
    2. new class
    3. calculateTotal
    4. 1stMethod

    Explanation: Method names cannot begin with numbers or include spaces, so calculateTotal is correct. 1stMethod begins with a digit, void-main includes a hyphen (not allowed), and new class contains a space.

  7. The main Method

    What is the required signature of the main method in Java?

    1. public void Main(string[] args)
    2. void static main(String args)
    3. static main(String[] args)
    4. public static void main(String[] args)

    Explanation: The main method must be public, static, void, and receive a String array. The other options alter the order, use wrong casing, or omit required elements.

  8. Method Return Statement

    Which keyword sends a value back from a Java method?

    1. return
    2. output
    3. yield
    4. sendBack

    Explanation: In Java, return sends a value back from a method. output is used for displaying information, sendBack and yield are not keywords in Java.

  9. Default Values

    If a Java method is declared with parameters but none are given when called, what will happen?

    1. Compilation error occurs
    2. It uses zero as default
    3. Java automatically asks the user for input
    4. It runs with empty parameters

    Explanation: A compilation error occurs because Java does not provide default values for method parameters unless specified with overloading. The other options do not describe Java's actual behavior.

  10. Overloading Methods

    What does it mean to overload a Java method?

    1. Creating multiple methods with the same name but different parameter lists
    2. Calling a method multiple times in a program
    3. Defining a method with extra variables inside
    4. Using a method from another class

    Explanation: Overloading lets you have methods with the same name but different parameters. Calling a method multiple times is invoking, not overloading. Extra variables inside a method do not affect its overloading. Using a method from another class relates to access, not overloading.