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


Correct Option: B

AI Explanation

To answer this question, we need to understand the requirements for compiling a source code file in Java.

Option A) It must have a package statement - This option is incorrect because a source code file can be compiled without a package statement. The package statement is used to organize classes into different packages, but it is not a requirement for compilation.

Option B) It must be named Test.java - This option is correct because Java source code files must be named with the same name as the public class they contain. In this case, the public class is named Test, so the file must be named Test.java.

Option C) It must import java.lang - This option is incorrect because the import statement for java.lang is automatically added by the Java compiler. It is not necessary to explicitly import java.lang in your source code file.

Option D) It must declare a public class named Test - This option is incorrect because it is not necessary to declare a public class named Test in order for the source code file to compile. However, if the source code file does contain a public class, it must be named Test to match the file name.

Therefore, the correct answer is B) It must be named Test.java. This option is correct because Java source code files must be named with the same name as the public class they contain.

Find more quizzes: