Java Fundamentals  «Prev  Next»


Packages and Compilation Units - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 
1. Which of the following are true about a Java source code file?
Please select all the correct answers.
  A. It must contain at least one public class or interface.
  B. It must take the name of a class or interface that is declared within it.
  C. It must end with the .java extension.
  D. It must begin with a package statement.

2. Which of the following are true about packages and importing?
Please select all the correct answers.
  A. If an import statement is used, it must be the first non-blank line of a source code file.
  B. A package is used to define a separate naming context.
  C. A package may contain more than one class or interface of the same name.
  D. The java.lang package is automatically imported and does not need to be imported using an import statement.

3. Suppose that the source code file MyCode.java contains the following source code. What errors are associated with this code?
public class MyClass {
}

public interface MyInterface {
}

Please select all the correct answers.
  A. MyClass should implement MyInterface.
  B. The code does not have a main() method.
  C. A public class or interface must be defined in a file with the same name plus the .java extension.
  D. No more than one public class or interface may be in the same source code file.

4. What is wrong with the following source code file?
import java.util.*;
package myPackage;

class MyClass extends MyInterface {
}

interface MyInterface {
}
Please select all the correct answers.
  A. Only one class or interface may be defined in a source code file.
  B. At least one class or interface must be public.
  C. A class cannot extend an interface.
  D. The package statement should be the first non-blank line in the file.