Java Programming > Programmer I Chapter 2: Java building blocks > Flashcards
Programmer I Chapter 2: Java building blocks Flashcards
What this prints out?
public class Egg { public Egg() { number = 5; } public static void main(String[] args) { Egg egg = new Egg(); System.out.println(egg.number); } private int number = 3; { number = 4; } }
5
ch2 creating objects
how short and char are similar and different?
both are 16 bits,
short is signed and char is unsigned
what will this print?
short bird = ‘d’;
char mammal = (short)83;
System.out.println(bird);
System.out.println(mammal);
100 (code for ‘d’)
S (letter with code 83)
what will this print?
short reptile = 65535;
System.out.println(reptile);
does not compile - max value of short is 32767
what will this print?
char fish = (short)-1;
System.out.println(fish);
does not compile - char is signed
how to write binary, octal and hexademical literals?
binary - 0b10 (or 0B10)
octal - 017
hexademical - 0xff (or 0XFF)
what lines will compile?
- double a = _1000.00;
- double b = 1000.00_;
- double c = 1000_.00;
- double d = 1_00_0.0_0;
- double e = 1__________2;
- no
- no
- no
- yes
- yes
what lines will compile?
int value = null;
String s = null;
- no
2. yes
rules for legal identifier names?
- starts with a letter, _ or $
- can include numbers but not start with them
- a single underscore “_” is not allowed, since Java 9
- can’t be a reserved word
what variables are initialized?
void paintFence() { int i1, i2, i3 = 0; }
only i3 is initialized
what lines are legal?
1: boolean b1, b2;
2: String s1 = “1”, s2;
3: int num, String value;
4: double d1, double d2;
5: int i1; int i2;
6: int i3; i4;
1: yes
2: yes
3: no - different types can’t be declared in the same statement
4: no - even if both say double, still counts as different types
5: yes
6: no
what will this print out?
public void findAnswer(boolean check) { int answer; int otherAnswer; int value; if (check) { value= 1; answer = 1; } else { answer = 2; } System.out.println(answer); System.out.println(value); }
will not compile - value is not initialized in all if branches
what will this print out?
public void printAnswer(boolean value) { System.out.println(value); } public void checkAnswer() { boolean value; findAnswer(value); }
does not compile - value in checkAnswer is not initialized
what are default values for field values of these types?
boolean, byte, short, int, long, float, double, char, object reference
boolean - false byte, short, int, long - 0 float, double - 0.0 char - '\u0000' (NUL) object references - null
what is var? can it be used for field variables?
var - local variable type inference
can only be used for local variables (inside methods), not field variables
what is the type of apples?
var apples = (short)10; apples = (byte)5;
short, on line 2 the byte value is promoted to short
will this compile?
public void breakingDeclaration() { var silly = 1; }
yes, line breaks are allowed
will this compile?
public void doesThisCompile(boolean check) { var question; question = 1; var answer; if (check) { answer = 2; } else { answer = 3; } System.out.println(answer); }
no, can’t infer types for question and answer
what lines compile?
int a, var b = 3; var a = 2, b = 3;
none - can’t use var in multiple variable declarations
why will this not compile?
public int addition(var a, var b) {
return a + b;
}
a, b are not local variables. local variable type inference can only be used for local variables
is this code legal?
package var;
public class Var { public void var() { var var = "var"; } public void Var() { Var var = new Var(); } }
the code compiles.
var is a reserved type name (not a reserved keyword) and can be used as variable identifier and package name.
is this code legal?
public class var { public var() { } }
class declaration is illegal.
var is a reserved type name - it can’t be used to define a type: class, interface, enum.
but var can be used as variable identifier and package name.
what happens after running this method?
public static void main(String[] args) {
System.gc();
}
gc() requests the JVM to perform garbage collection. The execution is not guaranteed and JVM may ignore the request.
System.gc() is not guaranteed to do anything.
What are the conditions for an object to become eligible for garbage collection?
- The object no longer has any references pointing to it
2. All references to the object have gone out of scope
when are the objects become eligible for garbage collection?
1: public class Scope {
2: public static void main(String[] args) {
3: String one, two;
4: one = new String(“a”);
5: two = new String(“b”);
6: one = two;
7: String three = one;
8: one = null;
9: } }
“a” became unreachable at line 6
“b” goes out of scope the the methods ends
how many times can finalize() method run?
0 or 1 times, never more than once, even if the first attempt to collect the object failed.
finalize() is ran when garbage collector tries to collect the object for the first time.
Which of the following are valid Java identifiers? (Choose all that apply.) A. _ B. _helloWorld$ C. true D. java.lang E. Public F. 1980_s G. _Q2_
B, E, G.
Option A is invalid because a single underscore is no longer allowed as an identifier as of Java 9. Option B is valid because you can use an underscore within identifiers, and a dollar sign ($) is also a valid character. Option C is not a valid identifier because true is a Java reserved word. Option D is not valid because a period (.) is not allowed in identifiers. Option E is valid because Java is case sensitive. Since Public is not a reserved word,it is allowed as an identifier, whereas public would not be allowed. Option F is not valid because the first character is not a letter, dollar sign ($), or underscore (). Finally, option G is valid as identifiers can contain underscores () and numbers, provided the number does not start the identifier.
What lines are printed by the following program? (Choose all that apply.)
1: public class WaterBottle {
2: private String brand;
3: private boolean empty;
4: public static float code;
5: public static void main(String[] args) {
6: WaterBottle wb = new WaterBottle();
7: System.out.println(“Empty = “ + wb.empty);
8: System.out.println(“Brand = “ + wb.brand);
9: System.out.println(“Code = “ + code);
10: } }
A. Line 8 generates a compiler error. B. Line 9 generates a compiler error. C. Empty = D. Empty = false E. Brand = F. Brand = null G. Code = 0.0 H. Code = 0f
D, F, G. The code compiles and runs without issue, so options A and B are incorrect. A boolean field initializes to false, making option D correct with Empty = false being printed. Object references initialize to null, not the empty String, so option F is correct with Brand = null being printed. Finally, the default value of floating-point numbers is 0.0. Although float values can be declared with an f suffix, they are not printed with an f suffix. For these reasons, option G is correct and Code = 0.0 is printed.
Which of the following code snippets about var compile without issue when used in a method? (Choose all that apply.) A. var spring = null; B. var fall = "leaves"; C. var evening = 2; evening = null; D. var night = new Object(); E. var day = 1/0; F. var winter = 12, cold; G. var fall = 2, autumn = 2; H. var morning = ""; morning = null;
B, D, E, H. A var cannot be initialized with a null value without a type, but it can be assigned a null value if the underlying type is not a primitive. For these reasons, option H is correct, but options A and C are incorrect. Options B and D are correct as the underlying types are String and Object, respectively. Option E is correct, as this is a valid numeric expression. You might know that dividing by zero produces a runtime exception, but the question was only about whether the code compiled. Finally, options F and G are incorrect as var cannot be used in a multiple-variable assignment.
Which of the following statements about the code snippet are true? (Choose all that apply.)
4: short numPets = 5L;
5: int numGrains = 2.0;
6: String name = “Scruffy”;
7: int d = numPets.length();
8: int e = numGrains.length;
9: int f = name.length();
A. Line 4 generates a compiler error. B. Line 5 generates a compiler error. C. Line 6 generates a compiler error. D. Line 7 generates a compiler error. E. Line 8 generates a compiler error. F. Line 9 generates a compiler error.
A, B, D, E. Line 4 does not compile because the L suffix makes the literal value a long, which cannot be stored inside a short directly,making option A correct. Line 5 does not compile because int is
an integral type, but 2.0 is a double literal value, making option B correct. Line 6 compiles without issue. Lines 7 and 8 do not compile because numPets and numGrains are both primitives, and you can call methods only on reference types, not primitive values, making options D and E correct, respectively. Finally, line9 compiles because there is a length() method defined on String.
Which statements about the following class are true? (Choose all that apply.)
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: } }
A. Line 3 generates a compiler error. B. Line 6 generates a compiler error. C. Line 7 generates a compiler error. D. Line 10 generates a compiler error. E. The program prints 3 on line 10. F. The program prints 4 on line 10. G. The program prints 50.0 on line 11. H. The program prints 49.0 on line 11.
A, D. The class does not compile, so options E, F, G, and H are incorrect. You might notice things like loops and increment/decrement operators in this problem, which we will cover in the next two chapters, but understanding them is not required to answer this question. The first compiler error is online 3. The variable temp is declared as a float, but the assigned value is 50.0, which is a double without the F/f postfix. Since a double doesn’t fit inside a float, line 3 does not compile. Next,depth is declared inside the for loop and only has scope inside this loop. Therefore, reading the value on line 10 triggers a compiler error. Note that the variable Depth on line 2 is never used. Java is case sensitive, so Depth and depth are distinct variables. For these reasons, options A and D are the correct answers.
Which of the following are correct? (Choose all that apply.)
A. An instance variable of type float defaults to 0.
B. An instance variable of type char defaults to null.
C. An instance variable of type double defaults to 0.0.
D. An instance variable of type int defaults to null.
E. An instance variable of type String defaults to null.
F. An instance variable of type String defaults to the empty string “”.
G. None of the above
C, E. Option C is correct because float and double primitives default to 0.0, which also makes option A incorrect. Option E is correct because all non primitive values default to null, which makes option F incorrect. Option D is incorrect because int primitives default to 0. Option B is incorrect because char defaults to the NUL character, ‘\u0000’. You don’t need to know this value for the exam, but you should know the default value is not null since it is a primitive.
Which of the following are correct? (Choose all that apply.)
A. A local variable of type boolean defaults to null.
B. A local variable of type float defaults to 0.0f.
C. A local variable of type double defaults to 0.
D. A local variable of type Object defaults to null.
E. A local variable of type boolean defaults to false.
F. A local variable of type float defaults to 0.0.
G. None of the above
G. Option G is correct because local variables do not get assigned default values. The code fails to compile if a local variable is used when not being explicitly initialized. If this question were about instance variables, options B, D, and E would be correct. A boolean primitive defaults to false, and a float primitive defaults to 0.0f.
Which of the following are true? (Choose all that apply.) A. A class variable of type boolean defaults to 0. B. A class variable of type boolean defaults to false. C. A class variable of type boolean defaults to null. D. A class variable of type long defaults to null. E. A class variable of type long defaults to 0L. F. A class variable of type long defaults to 0. G. None of the above
B, E. Option B is correct because boolean primitives default to false. Option E is correct because long values default to 0L.
Which of the following statements about garbage collection are correct? (Choose all that apply.)
A. Calling System.gc() is guaranteed to free up memory by destroying objects eligible for garbage collection.
B. Garbage collection runs on a set schedule.
C. Garbage collection allows the JVM to reclaim memory for other objects.
D. Garbage collection runs when your program has used up half the available memory.
E. An object may be eligible for garbage collection but never removed from the heap.
F. An object is eligible for garbage collection once no references to it are accessible in the program.
G. Marking a variable final means its associated object will never be garbage collected
C, E, F. In Java, there are no guarantees when garbage collection will run. The JVM is free to ignore calls to System.gc(). For this reason, options A, B, and D are incorrect. Option C is correct, as the purpose of garbage collection is to reclaim used memory.Option E is also correct that an object may never be garbage collected, such as if the program ends before garbage collection runs. Option F is correct and is the primary means by which garbage collection algorithms determine whether an object is eligible for garbage collection. Finally, option G is incorrect as marking a variable final means it is constant within its own scope. For example, a local variable marked final will be eligible for garbage collection after the method ends, assuming there areno other references to the object that exist outside the method.
Which statements about the following class are correct? (Chooseall that apply.)
1: public class PoliceBox {
2: String color;
3: long age;
4: public void PoliceBox() {
5: color = “blue”;
6: age = 1200;
7: }
8: public static void main(String []time) {
9: var p = new PoliceBox();
10: var q = new PoliceBox();
11: p.color = “green”;
12: p.age = 1400;
13: p = q;
14: System.out.println(“Q1=”+q.color);
15: System.out.println(“Q2=”+q.age);
16: System.out.println(“P1=”+p.color);
17: System.out.println(“P2=”+p.age);
18: } }
A. It prints Q1=blue. B. It prints Q2=1200. C. It prints P1=null. D. It prints P2=1400. E. Line 4 does not compile. F. Line 12 does not compile. G. Line 13 does not compile. H. None of the above
C. The class does compiles without issue, so options E, F, and Gare incorrect. The key thing to notice is line 4 does not define aconstructor, but instead a method named PoliceBox(), since it hasa return type of void. This method is never executed during theprogram run, and color and age get assigned the default valuesnull and 0L, respectively. Lines 11 and 12 change the values for anobject associated with p, but then on line 13 the p variable ischanged to point to the object associated with q, which still hasthe default values. For this reason, the program prints Q1=null,Q2=0, P1=null, and P2=0, making option C the only correct answer.
Which of the following legally fill in the blank so you can run themain() method from the command line? (Choose all that apply.)
public static void main(_______________) {}
A. String... var B. String My.Names[] C. String[] 123 D. String[] _names E. String... $n F. var names G. String myArgs
A, D, E. From Chapter 1, a main() method must have a valididentifier of type String… or String[]. For this reason, option Gcan be eliminated immediately. Option A is correct because var isnot a reserved word in Java and may be used as an identifier.Option B is incorrect as a period (.) may not be used in anidentifier. Option C is also incorrect as an identifier may includedigits but not start with one. Options D and E are correct as anunderscore (_) and dollar sign ($) may appear anywhere in anidentifier. Finally, option F is incorrect, as a var may not be usedas a method argument.
Which of the following expressions, when inserted independentlyinto the blank line, allow the code to compile? (Choose all thatapply.)
public void printMagicData() {
double magic = ___;
System.out.println(magic);}
A. 3_1 B. 1_329_.0 C. 3_13.0_ D. 5_291._2 E. 2_234.0_0 F. 9\_\_\_6 G. _1_3_5_0 H. None of the above
A, E, F. An underscore () can be placed in any numeric literal, solong as it is not at the beginning, the end, or next to a decimalpoint (.). Underscores can even be placed next to each other. For these reasons, options A, E, and F are correct. Options B and Dare incorrect, as the underscore () is next to a decimal point (.).Options C and G are incorrect, because an underscore (_) cannotbe placed at the beginning or end of the literal.
Suppose we have a class named Rabbit. Which of the followingstatements are true? (Choose all that apply.)
1: public class Rabbit {
2: public static void main(String[] args) {
3: Rabbit one = new Rabbit();
4: Rabbit two = new Rabbit();
5: Rabbit three = one;
6: one = null;
7: Rabbit four = one;
8: three = null;
9: two = null;
10: two = new Rabbit();
11: System.gc();
12: } }
A. The Rabbit object created on line 3 is first eligible for garbagecollection immediately following line 6.
B. The Rabbit object created on line 3 is first eligible for garbagecollection immediately following line 8.
C. The Rabbit object created on line 3 is first eligible for garbagecollection immediately following line 12.
D. The Rabbit object created on line 4 is first eligible for garbagecollection immediately following line 9.
E. The Rabbit object created on line 4 is first eligible for garbagecollection immediately following line 11.
F. The Rabbit object created on line 4 is first eligible for garbagecollection immediately following line 12.
G. The Rabbit object created on line 10 is first eligible forgarbage collection immediately following line 11.
H. The Rabbit object created on line 10 is first eligible forgarbage collection immediately following line 12.
B, D, H. The Rabbit object from line 3 has two references to it: oneand three. The references are set to null on lines 6 and 8,respectively. Option B is correct because this makes the objecteligible for garbage collection after line 8. Line 7 sets the referencefour to null, since that is the value of one, which means it has noeffect on garbage collection. The Rabbit object from line 4 hasonly a single reference to it: two. Option D is correct because thissingle reference becomes null on line 9. The Rabbit objectdeclared on line 10 becomes eligible for garbage collection at theend of the method on line 12, making option H correct. CallingSystem.gc() has no effect on eligibility for garbage collection.
Which of the following statements about var are true? (Choose allthat apply.)
A. A var can be used as a constructor parameter.
B. The type of var is known at compile time.
C. A var cannot be used as an instance variable.
D. A var can be used in a multiple variable assignmentstatement.
E. The value of var cannot change at runtime.
F. The type of var cannot change at runtime.
G. The word var is a reserved word in Java.
B, C, F. A var cannot be used for a constructor or methodparameter or for an instance or class variable, making option Aincorrect and option C correct. The type of var is known atcompile-time and the type cannot be changed at runtime,although its value can change at runtime. For these reasons,options B and F are correct, and option E is incorrect. Option D isincorrect, as var is not permitted in multiple-variabledeclarations. Finally, option G is incorrect, as var is not a reservedword in Java.
Given the following class, which of the following lines of code canindependently replace INSERT CODE HERE to make the codecompile? (Choose all that apply.)
public class Price {public void admission() { INSERT CODE HERE System.out.print(amount);} }
A. int Amount = 0b11; B. int amount = 9L; C. int amount = 0xE; D. int amount = 1_2.0; E. double amount = 1_0_.0; F. int amount = 0b101; G. double amount = 9_2.1_2; H. double amount = 1_2_.0_0;
C, F, G. First off, 0b is the prefix for a binary value, and 0x is theprefix for a hexadecimal value. These values can be assigned tomany primitive types, including int and double, making options Cand F correct. Option A is incorrect because naming the variableAmount will cause the System.out.print(amount) call on the nextline to not compile. Option B is incorrect because 9L is a longvalue. If the type was changed to long amount = 9L, then it wouldcompile. Option D is incorrect because 1_2.0 is a double value. Ifthe type was changed to double amount = 12.0, then it wouldcompile. Options E and H are incorrect because the underscore() appears next to the decimal point (.), which is not allowed.Finally, option G is correct and the underscore and assignment
usage is valid
Which statements about the following class are correct? (Chooseall that apply.)
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: } }
A. Line 2 contains a compiler error. B. Line 3 contains a compiler error. C. Line 4 contains a compiler error. D. Line 7 contains a compiler error. E. The code prints 0. F. The code prints 2.0. G. The code prints 2. H. The code prints 3.
A, C, D. The code contains three compilation errors, so options E,F, G, and H are incorrect. Line 2 does not compile, as this isincorrect syntax for declaring multiple variables, making option Acorrect. The data type is declared only once and shared among allvariables in a multiple variable declaration. Line 3 compileswithout issue, as it declares a local variable inside an instanceinitializer that is never used. Line 4 does not compile becauseJava, unlike some other programming languages, does notsupport setting default method parameter values, making optionC correct. Finally, line 7 does not compile because fins is in scopeand accessible only inside the instance initializer on line 3,making option D correct.
Which statements about classes and its members are correct?(Choose all that apply.) A. A variable declared in a loop cannot be referenced outside theloop. B. A variable cannot be declared in an instance initializer block. C. A constructor argument is in scope for the life of the instanceof the class for which it is defined. D. An instance method can only access instance variablesdeclared before the instance method declaration. E. A variable can be declared in an instance initializer block butcannot be referenced outside the block. F. A constructor can access all instance variables. G. An instance method can access all instance variables.
A, E, F, G. The question is primarily about variable scope. Avariable defined in a statement such as a loop or initializer blockis accessible only inside that statement. For this reason, options Aand E are correct. Option B is incorrect because variables can bedefined inside initializer blocks. Option C is incorrect, as aconstructor argument is accessible only in the constructor itself,not for the life of the instance of the class. Constructors andinstance methods can access any instance variable, even onesdefined after their declaration, making option D incorrect andoptions F and G correct.
Which statements about the following code snippet are correct?(Choose all that apply.)
3: var squirrel = new Object();
4: int capybara = 2, mouse, beaver = -1;
5: char chipmunk = -1;
6: squirrel = “”;
7: beaver = capybara;
8: System.out.println(capybara);
9: System.out.println(mouse);
10: System.out.println(beaver);
11: System.out.println(chipmunk);
A. The code prints 2. B. The code prints -1. C. The code prints the empty String. D. The code prints: null. E. Line 4 contains a compiler error. F. Line 5 contains a compiler error. G. Line 9 contains a compiler error. H. Line 10 contains a compiler error.
F, G. The code does not compile, so options A, B, C, and D are allincorrect. The first compilation error occurs on line 5. Since charis an unsigned data type, it cannot be assigned a negative value,making option F correct. The second compilation error is on line9, since mouse is used without being initialized, making option Gcorrect. You could fix this by initializing a value on line 4, but thecompiler reports the error where the variable is used, not where itis declared.
Assuming the following class compiles, how many variablesdefined in the class or method are in scope on the line marked //SCOPE on line 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: }
A. 2 B. 3 C. 4 D. 5 E. 6 F. 7 G. None of the above
F. To solve this problem, you need to trace the braces {} and seewhen variables go in and out of scope. You are not required tounderstand the various data structures in the question, as this willbe covered in the next few chapters. We start with hairs, which
goes in and out of scope on line 2, as it is declared in an instanceinitializer, so it is not in scope on line 14. The three variables—water, air, twoHumps, declared on lines 3 and 4—are instancevariables, so all three are in scope in all instance methods of theclass, including spit() and on line 14. The distance methodparameter is in scope for the life of the spit() method, making itthe fourth value in scope on line 14. The path variable is in scopeon line 6 and stays in scope until the end of the method on line 16,making it the fifth variable in scope on line 14. The teeth variableis in scope on line 7 and immediately goes out of scope on line 7since the statement ends. The two variables age and i defined onlines 9 and 10, respectively, both stay in scope until the end of thewhile loop on line 15, bringing the total variables in scope to sevenon line 14. Finally, Private is in scope on 12 but out of scope afterthe for loop ends on line 13. Since the total in-scope variables isseven, option F is the correct answer
What is the output of executing the following class?
1: public class Salmon {
2: int count;
3: { System.out.print(count+”-“); }
4: { count++; }
5: public Salmon() {
6: count = 4;
7: System.out.print(2+”-“);
8: }
9: public static void main(String[] args) {
10: System.out.print(7+”-“);
11: var s = new Salmon();
12: System.out.print(s.count+”-“); } }
A. 7-0-2-1- B. 7-0-1- C. 0-7-2-1- D. 7-0-2-4- E. 0-7-1- F. The class does not compile because of line 3. G. The class does not compile because of line 4. H. None of the above
D. The class compiles and runs without issue, so options F and Gare incorrect. We start with the main() method, which prints 7- online 11. Next, a new Salmon instance is created on line 11. This callsthe two instance initializers on lines 3 and 4 to be executed inorder. The default value of an instance variable of type int is 0, so0- is printed next and count is assigned a value of 1. Next, theconstructor is called. This assigns a value of 4 to count and prints2-. Finally, line 12 prints 4-, since that is the value of count.Putting it altogether, we have 7-0-2-4-, making option D thecorrect answer.
Which statements about the following program are correct?(Choose all that apply.)
1: public class Bear {
2: private Bear pandaBear;
3: protected void finalize() {}
4: private void roar(Bear b) {
5: System.out.println(“Roar!”);
6: pandaBear = b;
7: }
8: public static void main(String[] args) {
9: Bear brownBear = new Bear();
10: Bear polarBear = new Bear();
11: brownBear.roar(polarBear);
12: polarBear = null;
13: brownBear = null;
14: System.gc(); } }
A. The object created on line 9 is eligible for garbage collectionafter line 13.
B. The object created on line 9 is eligible for garbage collectionafter line 14.
C. The object created on line 10 is eligible for garbage collectionafter line 12.
D. The object created on line 10 is eligible for garbage collectionafter line 13.
E. Garbage collection is guaranteed to run.
F. Garbage collection might or might not run.
G. Garbage collection is guaranteed not to run.
H. The code does not compile
A, D, F. The class compiles and runs without issue, so option H isincorrect. The program creates two Bear objects, one on line 9 andone on line 10. The first Bear object is accessible until line 13 viathe brownBear reference variable. The second Bear object is passedto the first object’s roar() method on line 11, meaning it isaccessible via both the polarBear reference and thebrownBear.pandaBear reference. After line 12, the object is stillaccessible via brownBear.pandaBear. After line 13, though, it is nolonger accessible since brownBear is no longer accessible. In otherwords, both objects become eligible for garbage collection after line 13, making options A and D correct. Finally, garbagecollection is never guaranteed to run or not run, since the JVMdecides this for you. For this reason, option F is correct, andoptions E and G are incorrect. The class contains a finalize()method, although this does not contribute to the answer. For theexam, you may see finalize() in a question, but since it’sdeprecated as of Java 9, you will not be tested on it
Which of the following are valid instance variable declarations?(Choose all that apply.)
A. var _ = 6000_.0; B. var null = 6_000; C. var $_ = 6_000; D. var $2 = 6_000f; E. var var = 3_0_00.0; F. var #CONS = 2_000.0; G. var %C = 6_000_L; H. None of the above
H. None of these declarations is a valid instance variabledeclaration, as var cannot be used with instance variables, onlylocal variables. For this reason, option H is the only correctanswer. If the question were changed to be about local variabledeclarations, though, then the correct answers would be optionsC, D, and E. An identifier must start with a letter, $, or _, sooptions F and G would be incorrect. As of Java 9, a singleunderscore is not allowed as an identifier, so option A would beincorrect. Options A and G would also be incorrect because theirnumeric expressions use underscores incorrectly. An underscorecannot appear at the end of literal value, nor next to a decimalpoint (.). Finally, null is a reserved word, but var is not, so optionB would be incorrect, and option E would be correct.