OA. free
Free
MathWorks Core Computer Science Core Computer Science Medium

Question 35: (Java Question) Function Signature For which of the following cases will the...

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Question 35: (Java Question) Function Signature

For which of the following cases will the program compile successfully?

public class MyMain {
    // Select your code here {
    System.out.println("This is a class");
    }
}

Replace // Select your code here with any of the following:

Choose one option.
Show answer & explanation
Answer: C. `public static void main(String... a)`

Option C uses the correct varargs syntax String... (three dots, no space) which is valid Java. Option A has a typo (String...a without space is still valid but less conventional). Option B uses invalid syntax String,*. Option D is missing a parameter name, making it syntactically invalid. The JVM requires the main method to accept an array or varargs of Strings with a parameter name.

Step-by-step Derivation:
In Java, valid main method signatures must have:

  1. Access modifier: public
  2. Return type: void
  3. Static keyword: static
  4. Method name: main
  5. Parameter: either String[] with a name (e.g., String[] args) or String... varargs with a name (e.g., String... args)

Analysis:

  • A) public static void main(String...a) - Valid. Varargs with name a.
  • B) public static void main(String,* a) - Invalid. Syntax error: String,* is not valid Java syntax.
  • C) public static void main(String... a) - Valid. Standard varargs signature (with space before ellipsis).
  • D) public static void main(String[]) - Invalid. Missing parameter name; method signature requires a variable name.

Correct answers: A and C both compile, but C is the most standard form.