- Home
- Programming
- OCA Java SE8
11.
Which statement about import statements is true?
- A.The class will not compile if it contains unused import statements.
- B.Unused import statements can be removed from the class without causing a class to become unable to be compiled.
- C.The class will not compile if a duplicate import statement is present
- D.If a class contains an import statement for a class used in the program that cannot be found, it can still compile.
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
A class will compile if it has unused or redundant import statements, making Option A and C incorrect. Option D is also incorrect as the compiler must be able to locate the class of the import statement. The correct answer is Option B. Removing unused import statements does not cause a class to become unable to be compiled. |
12.
What is the result of compiling and executing the following class?
1: public class ParkRanger {
2: int birds = 10;
3: public static void main(String[] data) {
4: int trees = 5;
5: System.out.print(trees+birds);
6: }
7: }
1: public class ParkRanger {
2: int birds = 10;
3: public static void main(String[] data) {
4: int trees = 5;
5: System.out.print(trees+birds);
6: }
7: }
- A.It does not compile.
- B.It compiles but throws an exception at runtime.
- C.It compiles and outputs 5.
- D.It compiles and outputs 15.
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
The code does not compile because of line 5, making Option A the correct answer. For this question, it helps to understand variable scope. The main() method is static and does not have access to any class instance variables. The birds variable is not static and requires a class instance variable to access. Therefore, the code does not compile when the static method attempts to access a non-static variable without an instance of the class. |
13.
Which statements about Java are true?
I. The java command can execute .java and .class files.
II. Java is not object oriented.
III. The javac command compiles directly into native machine code.
I. The java command can execute .java and .class files.
II. Java is not object oriented.
III. The javac command compiles directly into native machine code.
- A.I only
- B.III only
- C.II and III
- D.None are true.
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
The java command can only execute compiled .class files, so I is false. Java is most certainly object oriented, one of the key design principles, so II is also false. The javac command compiles into bytecode, which must be run in a Java virtual machine (JVM), and is not native machine code, so III is false as well. Since none of the statements are true, Option D is the correct answer. |
14.
Which of the following lines of code is not allowed as the first line of a Java class file?
- A.import widget.*;
- B.// Widget Manager
- C.package sprockets;
- D.int facilityNumber;
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
A class can start with a comment, an optional package statement, or an import statement if there is no package statement. It cannot start with a variable definition, making Option D the correct answer. |
15.
Which one of the following statements is true about using packages to organize your
code in Java?
- A.Every class is required to include a package declaration.
- B.To create a new package, you need to add a package.init file to the directory.
- C.Packages allow you to limit access to classes, methods, or data from classes outside the package.
- D.It is not possible to restrict access to objects and methods within a package.
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Classes may be defined without a package declaration and are placed in the default package, so Option A is incorrect. Option B is a completely false statement as no such file is required in Java. Option C is correct as it is one of the primary reasons for organizing your application into packages. Option D is incorrect as package-private allows access to methods and variables to be limited to those classes within the same package. |