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:
Show answer & explanation
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:
- Access modifier:
public - Return type:
void - Static keyword:
static - Method name:
main - Parameter: either
String[]with a name (e.g.,String[] args) orString...varargs with a name (e.g.,String... args)
Analysis:
- A)
public static void main(String...a)- Valid. Varargs with namea. - 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.