1.
Which of the following are valid Java identifiers?
Correct Answer(s)
A. A$B
B. _helloWorld
E. Public
Explanation
A, B, E. Option A is valid because you can use the dollar sign in identifiers. Option B is valid because you can use an underscore in identifiers. Option C is not a valid identifier because true is a Java reserved word. Option D is not valid because the dot (.) is not allowed in identifiers. Option E is valid because Java is case sensitive, so Public is not a reserved word and therefore a valid identifier. Option F is not valid because the first character is not a letter, $, or _.
2.
Determine the output of the following program.
1. public class WaterBottle {
2. private String brand;
3. private boolean empty;
4. public static void main(String[] args) {
5. WaterBottle wb = new WaterBottle();
6. System.out.print("Empty = " + wb.empty);
7. System.out.print(", Brand = " + wb.brand);
8. } }
Correct Answer
D. Empty = false, Brand = null
Explanation
D. Boolean fields initialize to false and references initialize to null, so empty is false and brand is null. Brand = null is output.
3.
Which of the following are true?
4: short numPets = 5;
5: int numGrains = 5.6;
6: String name = "Scruffy";
7: numPets.length();
8: numGrains.length();
9: name.length();
Correct Answer(s)
B. Line 5 generates a compiler error.
D. Line 7 generates a compiler error.
E. Line 8 generates a compiler error.
Explanation
Line 5 generates a compiler error because the variable "numGrains" is declared as an int, which can only store whole numbers, but it is assigned a decimal value of 5.6.
Line 7 generates a compiler error because the variable "numPets" is declared as a short, which is a primitive data type and does not have any methods. Therefore, calling the "length()" method on it will result in a compiler error.
Line 8 generates a compiler error because the variable "numGrains" is declared as an int, which is a primitive data type and does not have any methods. Therefore, calling the "length()" method on it will result in a compiler error.
4.
Given the following class, which of the following is true?
1: public class Snake {
2:
3: public void shed(boolean time) {
4:
5: if (time) {
6:
7: }
8: System.out.println(result);
9:
10: }
- 11: }
Correct Answer(s)
A. If String result = "done"; is inserted on line 2, the code will compile.
B. If String result = "done"; is inserted on line 4, the code will compile.
Explanation
If String result = "done"; is inserted on line 2, the code will compile because the variable "result" is declared and initialized before it is used in the println statement on line 8. Similarly, if String result = "done"; is inserted on line 4, the code will compile because the variable "result" is declared and initialized before it is used in the if statement on line 5.
5.
Given the following classes, which of the following can independently replace INSERT IMPORTS HERE to make the code compile?
- package aquarium;
- public class Tank { }
- package aquarium.jellies;
- public class Jelly { }
- package visitor;
- INSERT IMPORTS HERE
- public class AquariumVisitor {
- public void admire(Jelly jelly) { } }
Correct Answer(s)
C. Import aquarium.jellies.Jelly;
D. Import aquarium.jellies.*;
Explanation
The code requires the class "Jelly" to be imported in order to compile. The correct answer options "import aquarium.jellies.Jelly;" and "import aquarium.jellies.*;" both fulfill this requirement. "import aquarium.jellies.Jelly;" specifically imports the "Jelly" class, while "import aquarium.jellies.*;" imports all classes in the "jellies" package, including the "Jelly" class.
6.
Which of the following statements about lambda expressions in Java SE 8 is true?
Correct Answer
C. Lambda expressions can be used to create instances of functional interfaces.
Explanation
Lambda expressions in Java SE 8 are used primarily to create instances of functional interfaces, which are interfaces with a single abstract method. This makes the code more concise and readable. Contrary to the other options:
A is incorrect because lambda expressions can only be used with functional interfaces (interfaces with a single abstract method).
B is incorrect because lambda expressions can access both final and effectively final variables from the enclosing scope.
D is incorrect because lambda expressions can throw checked exceptions if their functional interface's abstract method declares them.
7.
Given the following class, which of the following calls print out Blue Jay?
- public class BirdDisplay {
- public static void main(String[] name) {
- System.out.println(name[1]);
- } }
Correct Answer
B. Java BirdDisplay Sparrow "Blue Jay"
Explanation
B. Option B is correct because arrays start counting from zero and strings with spaces must be in quotes. Option A is incorrect because it outputs Blue. C is incorrect because it outputs Jay. Option D is incorrect because it outputs Sparrow. Options E and F are incorrect because they output Error: Could not find or load main class Bird-Display.class.
8.
Which of the following legally fill in the blank so you can run the main() method from the command line?
- public static void main( )
Correct Answer(s)
A. String[] _names
C. String abc[]
D. String _Names[]
E. String... $n
Explanation
The correct answer is String[] _names, String abc[], String _Names[], String... $n. These options are all valid ways to declare a parameter for the main() method in Java. The main() method is the entry point for a Java program, and it must have a parameter of type String[] in order to accept command-line arguments. The other options listed are not valid because they do not have the correct syntax for declaring an array parameter.
9.
Which of the following are legal entry point methods that can be run from the command line?
Correct Answer
D. Public static void main(String[] args)
Explanation
In Java, the correct signature for the entry point method that the JVM looks for to start execution is public static void main(String[] args). This method must be public, static, and return void. The parameter must be a String array (String[] args). The other options either lack the correct access modifier, return type, or method signature required for the Java application to run from the command line.
10.
Which of the following statements are true?
Correct Answer(s)
B. An instance variable of type String defaults to null.
C. An instance variable of type double defaults to 0.0.
Explanation
An instance variable of type String defaults to null because when an instance variable is not assigned a value, its default value is null. Similarly, an instance variable of type double defaults to 0.0 because when an instance variable of type double is not assigned a value, its default value is 0.0.
11.
What is true about the following code?
- public class Bear {
- protected void finalize() {
- System.out.println("Roar!");
- }
- public static void main(String[] args) {
- Bear bear = new Bear();
- bear = null;
- System.gc();
- } }
Correct Answer(s)
B. Finalize() might or might not be called
E. Garbage collection might or might not run.
Explanation
The code includes a finalize() method, which is a method that is called by the garbage collector before an object is destroyed. However, the execution of the finalize() method is not guaranteed. It might or might not be called depending on various factors such as the availability of system resources. Similarly, the execution of garbage collection is also not guaranteed. It might or might not run depending on the JVM's decision and the availability of system resources. Therefore, both finalize() and garbage collection might or might not be called/run in this code.
12.
Select a statement that is true.
Correct Answer
E. None of the above.
13.
Which of the following are true?
Correct Answer(s)
A. An instance variable of type boolean defaults to false.
D. An instance variable of type int defaults to 0.
Explanation
An instance variable of type boolean defaults to false because boolean is a primitive data type in Java, and all primitive data types have default values. For boolean, the default value is false. An instance variable of type int defaults to 0 because int is also a primitive data type, and its default value is 0.
14.
Given the following class in the file /my/directory/named/A/Bird.java:
- INSERT CODE HERE
- public class Bird { }
- Which of the following replaces INSERT CODE HERE if we compile from /my/directory? (Choose all that apply)
Correct Answer
D. Package named.A;
Explanation
The correct answer is "package named.A;". This is because the class Bird is declared in the package named.A, so the correct package statement should be "package named.A;".
15.
Which of the following lines of code compile?
Correct Answer(s)
A. Int i1 = 1_234;
E. Double d4 = 1_234.0;
Explanation
A, E. Underscores are allowed as long as they are directly between two other digits. This means options A and E are correct. Options B and C are incorrect because the underscore is adjacent to the decimal point. Option D is incorrect because the underscore is the last character.
16.
What does the following code output?- 1: public class Salmon {
- 2: int count;
- 3: public void Salmon() {
- 4: count = 4;
- 5: }
- 6: public static void main(String[] args) {
- 7: Salmon s = new Salmon();
- 8: System.out.println(s.count);
- 9: } }
Correct Answer
A. 0
Explanation
The code outputs 0. The reason for this is that the constructor of the Salmon class is defined as a method instead of a constructor. As a result, when the object is created on line 7, the count variable is not initialized and its default value of 0 is printed on line 8.
17.
Given the following code, what will be the output when compiled and run?
public class TestClass {
public static void main(String[] args) {
int x = 5;
int y = 10;
int z = (x > y) ? x++ : y++;
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
}
}
Correct Answer
C. X = 6, y = 11, z = 10
Explanation
The ternary operator (x > y) ? x++ : y++ checks if x > y.
Since x (5) is not greater than y (10), the else part (y++) is executed.
z is assigned the current value of y, which is 10.
After that, y is incremented to 11.
x remains unchanged at 5, and y becomes 11.
Thus, the output will be x = 6, y = 11, z = 10.
18.
Which of the following are true statements?
Correct Answer(s)
B. Java code compiled on Windows can run on Linux.
E. Java is an object-oriented language.
Explanation
Java code compiled on Windows can run on Linux because Java programs are compiled into bytecode, which is platform-independent and can be executed on any system that has a Java Virtual Machine (JVM) installed. Java is an object-oriented language because it supports the concepts of encapsulation, inheritance, and polymorphism. However, Java does not allow operator overloading, does not have pointers to specific locations in memory, and is not a procedural or functional programming language.
19.
Which of the following are true?
- public class Bunny {
- public static void main(String[] args) {
- Bunny bun = new Bunny();
- } }
Correct Answer(s)
A. Bunny is a class.
E. Bun is a reference to an object.
Explanation
Bunny is a class because it is declared with the keyword "class" and followed by the class name. bun is a reference to an object because it is declared as an instance of the Bunny class using the new keyword.
20.
Which of the following are true?
Correct Answer(s)
C. Javac compiles a .java file into a .class file.
D. Java takes the name of the class as a parameter.
Explanation
The javac command is used to compile Java source code files (.java) into bytecode files (.class). Therefore, the statement "javac compiles a .java file into a .class file" is true. Additionally, when running a Java program, the name of the class containing the main method is passed as a parameter to the java command. Hence, the statement "Java takes the name of the class as a parameter" is also true.
21.
Which represents the order in which the following statements can be assembled into a program that will compile successfully?
- A: class Rabbit {}
- B: import java.util.*;
- C: package animals;
Correct Answer(s)
C. C, B, A
D. B, A
E. C, A
Explanation
The correct order in which the statements can be assembled into a program that will compile successfully is C, B, A. Firstly, the package declaration (C) should come before any import statements (B). Then, the class declaration (A) should come after the import statements. The second occurrence of B and A in the given answer is redundant and can be ignored.