Chapter 1: Building Blocks Flashcards
Which of the following are legal entry point methods that can be run from the command line?
A. public static void main(String[] args)
B. public static main(String[] args)
C. public static final void main(String[] args)
A and C
Which order can the following be arranged in?
X: class Rabbit { }
Y: import java.util.*;
Z: package animals;
Z, Y, X = package, import, class
Y, X = import, class
Z, X = package, class
Which of the following are true?
public class Bunny {
public static void main(String[] x) {
Bunny bun = new Bunny();
}}
A. Bunny is a class
B. main is a class.
C. bun is a reference to an object
D. the main() method doesn’t run because the parameter is incorrect
A: Bunny is a class
C: bun is a reference to an object
Which of the following are valid Java identifiers?
A. _
B. _helloWorld$
C. true
D. java.loang
E. Public
F. 1980_s
G. Q2
B: _helloWorld$
E: Public
G: Q2
When are the objects of line 9 and 10 eligible for garbage collection (GC)?
2: public class Bear {
3: private Bear bearX;
4: private void roar (Bear x) {
5: bearX = x;
6: }
7: main() { // legit main
8: Bear bear1 = new Bear();
9: Bear bear2 = new Bear();
10: bear1.roar( bear2 );
11: bear2 = null;
12: bear1 = null;
13: System.gc(); } }
Object, line 9, is eligible for GC after line 13
Object, line 10, is eligible for GC after line 13
They are both eligible after line 13 as at that point, they are no longer reachable.
Inside the line 5 method, bearX is assigned the reference to the bear2 object. Now both bear2 and bearX are referring to the same Bear object created on line 9.
So the Bear object created on line 9 is still reachable through bearX, even after bear2 is set to null.
bearX a back-up reference, stored in bear1. Remember, each Bear object will have a private Bear field, called bearX.
How many variables defined in the class or method are in scope on the line marked 14?
1: public class Camel {
2: { int hairs = 3_000_0; }
3: long water, air = 2;
4: boolean twoHumps = true
5: public void spit (float distance) {
6: var path = “”;
7: { double teeth = 32 + distance++; }
8: while (water > 0 ) {
9: int age = twoHumps ? 1 : 2;
10: short i = -1;
11: for (i = 0; i<10; i++) {
12: var Private = 2;
13: }
14: // SCOPE
15: }
16: }
17:}
7
trace the brackets and see when variables go in and out of scope
so lines 2 and 7 have variables that are only available for a single line
the for loop variables are only in scope for the loop
What does this output?
Is there one or more lines that begin with whitespace?
public class X {
private int numA;
main() { int numB; System.out.print(""" "# A = " + numA + "# B = " + numB + # C = 0"""); } }
The output includes: # C = 0
The output includes one or more lines that begin with whitespace.
What value do local variables default to?
What value would a class variable of String default to?
local variables need to be instantiated after being declared, they won’t default to any particular number
a class variable of String defaults to null
What is the max number of imports that can be removed and still have code 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 {
System.out.println(water); } }
}
4
Which lines generate a compiler error?
1: public class ClownFish {
2: int gills = 0, double weight = 2;
3: { int fins = gills; }
4: void print( int length = 3 ) {
5: System.out.println(gills);
6: System.out.println(weight);
7: System.out.println(fins);
8: System.out.println(length);
9: } }
Lines 2, 4, 7 generates a compiler error
When is an object eligible for GC?
When no references are available to it
What is true about var?
the type of var is known at compile time
var cannot be used as an instance variable
the type of a var cannot change at runtime
What is true about the following code?
var num1 = Long.parseLong(“100”);
var num2 = Long.valueOf(“100”);
System.out.println(Long.max(num1, num2));
The output is 100
num1 is a primitive, num2 is not
1: public class River {
2: int Depth = 1;
3: float temp = 50.0;
4: public void flow() {
5: for (int i = 0; i < 1; i++) {
6: int depth = 2;
7: depth++;
8: temp–;
9: }
10: System.out.println(depth);
11: System.out.println(temp); }
12: public static void main (String… s) {
13: new River().flow();
14: } }
Line 3 generates a compiler error
Line 10 generates a compiler error