Multiple choice technology programming languages

In order for a source code file, containing the public class Test, to successfully compile, which of the following must be true?

  1. It must have a package statement

  2. It must be named Test.java

  3. It must import java.lang

  4. It must declare a public class named Test

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Java requires the source filename to match the public class name. If a file contains 'public class Test', it must be named Test.java for successful compilation. Package statements are optional, java.lang is auto-imported, and the class must already be declared (that's not a separate requirement).

AI explanation

Java requires that a source file containing a public class be named exactly after that class, so a file with public class Test must be saved as Test.java in order to compile. A package statement, importing java.lang (which is auto-imported anyway), and simply declaring a public class don't by themselves satisfy this filename-matching requirement.