Q.1: Which of the following are legal entry point methods that can be run from the command line?
A)
1. public final static void main(String[] args)
2. public static void main(String[] args)
Q.2: Given the following classes, which of the following snippets can independently be inserted in the place of INSERT IMPORTS HERE and have the code compile?
package aquarium;
public class Water {
boolean salty = false;
}
package aquarium.jellies;
public class Water {
boolean salty = true;
}
package employee;
INSERT IMPORTS HERE
Water water;
}
A)
1. import aquarium.*;
2. import aquarium.Water;import aquarium.jellies.*;
3. import aquarium.*;import aquarium.jellies.Water;
Q.3: Given the following two class files, what is the maximum number of imports that can be removed and have the code still compile?
// Water.java
package aquarium;
public class Water { }
// Tank.java
package aquarium;
import java.lang.*;
import java.lang.System;
import aquarium.Water;
import aquarium.*;
public class Tank {
public void print(Water water) { System.out.println(water); } }
A) 4
Q.4: Which answer options represent the order in which the following statements can be assembled into a program that will compile successfully?
X: class Rabbit {}
Y: import java.util.*;
Z: package animals;
A)
1. Z, Y, X
2. Y, X
3. Z, X
Q.5: What is the output of executing the following class?
1. public class Salmon {
int count;
{ System.out.print(count+”-“); }
{ count++; }
public Salmon() {
count = 4;
System.out.print(2+”-“);
}
public static void main(String[] args) {
System.out.print(7+”-“);
var s = new Salmon();
System.out.print(s.count+”-“); } }
A) 7-0-2-4-
Q.6: Which of the following statements are true?
public class Bunny {
public static void main(String[] x) {
Bunny bun = new Bunny();
} }
A) 1. Bunny is a class.
2. bun is a reference to an object.
Q.7: Which statements about the following class are true?
1. public class River {
int Depth = 1;
float temp = 50.0;
public void flow() { \
for (int i = 0; i < 1; i++) {
int depth = 2;
depth++;
temp–;
}
System.out.println(depth);
System.out.println(temp); }
public static void main(String… s) {
new River().flow();
} }
A)
1. Line 3 generates a compiler error.
2. Line 10 generates a compiler error.
Q.8: What data type (or types) will allow the following code snippet to compile?
byte apples = 5;
short oranges = 10; _____ bananas = apples + oranges;
A)
1. Int
2. Long
3. Double
Q.9: Given the following class, which of the following lines of code can independently replace INSERT CODE HERE to make the code compile?
public class Price {
public void admission() {
INSERT CODE HERE
System.out.print(amount); } }
A)
1. int amount = 0xE;
2. int amount = 0b101;
3. double amount = 9_2.1_2;
Q.10: Which statements are true about the following code?
var num1 = Long.parseLong(“100”);
var num2 = Long.valueOf(“100”);
System.out.println(Long.max(num1, num2));
A)
1. The output is 100.
2. num1 is a primitive.
Q.11: Which of the following statements about the code snippet are true?
1. short numPets = 5L;
int numGrains = 2.0;
String name = “Scruffy”;
int d = numPets.length();
int e = numGrains.length; int f = name.length();
A)
1. Line1 generates a compiler error.
2. Line 2 generates a compiler error.
3. Line 4 generates a compiler error.
4. Line 5 generates a compiler error.
Q.12: Which of the following statements is correct?
A)
A class variable of type String defaults to null.
Q.13: Which of these are true about this code?
public class KitchenSink {
private int numForks;
public static void main(String[] args) {
int numKnives;
System.out.print(“””
“# forks = ” + numForks +
” # knives = ” + numKnives +
# cups = 0″””);
}
}
A)
1. The output includes # cups = 0.
2. The output includes one or more lines that begin with whitespace.
Q.14: Which of the following are valid Java identifiers?
A)
1. _helloWorld$
2. Public
3. _Q2_
Q.15: Which of the following expressions, when inserted independently into the blank line, allow the code to compile?
public void printMagicData() {
var magic = ______________;
System.out.println(magic); }
A)
1. 3_1
2. 2_234.0_0
3. 9___6
Q.16: Which statement about the following class is correct?
1. public class PoliceBox {
String color;
long age;
public void PoliceBox() {
color = “blue”;
age = 1200;
}
public static void main(String []time) {
var p = new PoliceBox();
var q = new PoliceBox();
p.color = “green”;
p.age = 1400;
p = q;
System.out.println(“Q1=”+q.color);
System.out.println(“Q2=”+q.age);
System.out.println(“P1=”+p.color);
System.out.println(“P2=”+p.age);
} }
A)
It prints P1=null.
Q.17: Which of the following statements are true about this code?
var blocky = “””
squirrel \s
pigeon \
termite”””;
System.out.print(blocky);
A)
1. It outputs two lines.
2. There is one line with trailing whitespace.
Q.18: Which of the following statements about var are true?
A)
1. The type of a var is known at compile time.
2. A var cannot be used as an instance variable. 3. The type of a var cannot change at runtime.
Q.19: What lines are printed by the following program?
1. public class WaterBottle {
private String brand;
private boolean empty;
public static float code;
WaterBottle wb = new WaterBottle();
System.out.println(“Empty = ” + wb.empty);
System.out.println(“Brand = ” + wb.brand);
System.out.println(“Code = ” + code);
} }
A)
1. Empty = false
2. Brand = null
3. Code = 0.0
Q.20: Which of the following code snippets about var compile without issue when used in a method?
A)
1. var fall = “leaves”;
2. var night = Integer.valueOf(3);
3. var day = 1/0; 4. var morning = “”; morning = null;
Q.21: Which statements about the following class are correct?
1. public class ClownFish {
int gills = 0, double weight=2;
{ int fins = gills; }
void print(int length = 3) {
System.out.println(gills);
System.out.println(weight);
System.out.println(fins);
System.out.println(length);
} }
A)
1. Line 2 generates a compiler error.
2. Line 4 generates a compiler error.
3. Line 7 generates a compiler error.
Q.22: Assuming the following class compiles, how many variables defined in the class or method are in scope on line 14?
1. public class Camel {
{ int hairs = 3_000_0; }
long water, air=2;
boolean twoHumps = true;
public void spit(float distance) {
var path = “”;
{ double teeth = 32 + distance++; }
while(water> 0) {
int age = twoHumps ? 1 : 2; \
short i=-1;
for(i=0; i<10; i+
}
// SCOPE
}
}
}
A) 7
Q.23: Which of the following statements about garbage collection are correct?
A)
1. Garbage collection allows the JVM to reclaim memory for other objects.
2. An object may be eligible for garbage collection but never removed from the heap.
3. An object is eligible for garbage collection once no references to it are accessible in the program.
Other Links:
See other websites for quiz:
Check on QUIZLET