OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн книгу.explicit initialization; others use the default value for that type. Identifiers may contain letters, numbers, currency symbols, or
_
, although they may not begin with numbers. Also, you cannot define an identifier that is just a single underscore character _
. Numeric literals may contain underscores between two digits, such as 1_000
, but not in other places, such as _100_.0_
.
Understand how to create text blocks. A text block begins with """
on the first line. On the next line begins the content. The last line ends with """
. If """
is on its own line, a trailing line break is included.
Be able to use var correctly. A var
is used for a local variable. A var
is initialized on the same line where it is declared, and while it can change value, it cannot change type. A var
cannot be initialized with a null
value without a type, nor can it be used in multiple variable declarations.
Be able to determine where variables go into and out of scope. All variables go into scope when they are declared. Local variables go out of scope when the block they are declared in ends. Instance variables go out of scope when the object is eligible for garbage collection. Class variables remain in scope as long as the program is running.
Know how to identify when an object is eligible for garbage collection. Draw a diagram to keep track of references and objects as you trace the code. When no arrows point to a box (object), it is eligible for garbage collection.
Review Questions
The answers to the chapter review questions can be found in the Appendix.
1 Which of the following are legal entry point methods that can be run from the command line? (Choose all that apply.)private static void main(String[] args)public static final main(String[] args)public void main(String[] args)public static final void main(String[] args)public static void main(String[] args)public static main(String[] args)
2 Which answer options represent the order in which the following statements can be assembled into a program that will compile successfully? (Choose all that apply.)X: class Rabbit {} Y: import java.util.*; Z: package animals;X, Y, ZY, Z, XZ, Y, XY, XZ, XX, ZNone of the above
3 Which of the following are true? (Choose all that apply.)public class Bunny { public static void main(String[] x) { Bunny bun = new Bunny(); } }Bunny is a class.bun is a class.main is a class.Bunny is a reference to an object.bun is a reference to an object.main is a reference to an object.The main() method doesn't run because the parameter name is incorrect.
4 Which of the following are valid Java identifiers? (Choose all that apply.)__helloWorld$truejava.langPublic1980_s_Q2_
5 Which statements about the following program are correct? (Choose all that apply.)2: public class Bear { 3: private Bear pandaBear; 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(); } }The object created on line 9 is eligible for garbage collection after line 13.The object created on line 9 is eligible for garbage collection after line 14.The object created on line 10 is eligible for garbage collection after line 12.The object created on line 10 is eligible for garbage collection after line 13.Garbage collection is guaranteed to run.Garbage collection might or might not run.The code does not compile.
6 Assuming the following class compiles, how many variables defined in the class or method are in scope on the line marked 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: }234567None of the above
7 Which are true about this code? (Choose all that apply.)public class KitchenSink { private int numForks; public static void main(String[] args) { int numKnives; System.out.print(""" "# forks = " + numForks + " # knives = " + numKnives + # cups = 0"""); } }The output includes: # forks = 0.The output includes: # knives = 0.The output includes: # cups = 0.The output includes a blank line.The output includes one or more lines that begin with whitespace.The code does not compile.
8 Which of the following code snippets about var compile without issue when used in a method? (Choose all that apply.)var spring = null;var fall = "leaves";var evening = 2; evening = null;var night = Integer.valueOf(3);var day = 1/0;var winter = 12, cold;var fall = 2, autumn = 2;var morning = ""; morning = null;
9 Which of the following are correct? (Choose all that apply.)An instance variable of type float defaults to 0.An instance variable of type char defaults to null.A local variable of type double defaults to 0.0.A local variable of type int defaults to null.A class variable of type String defaults to null.A class variable of type String defaults to the empty string "".None of the above.
10 Which of the following expressions, when inserted independently into the blank line, allow the code to compile? (Choose all that apply.)public void printMagicData() { var magic = ______________; System.out.println(magic); }3_11_329_.03_13.0_5_291._22_234.0_09___6_1_3_5_0
11 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); } }01234Does not compile
12 Which statements about the following class are correct? (Choose all 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: } }Line 2 generates a compiler error.Line 3 generates a compiler error.Line 4 generates a compiler error.Line 7 generates a compiler error.The code prints 0.The code prints 2.0.The code prints 2.The code prints 3.
13 Given the following classes, which of the following snippets can independently be inserted in place of INSERT IMPORTS HERE and have the code compile? (Choose all that apply.)package aquarium; public class Water { boolean salty = false; } package aquarium.jellies; public class Water { boolean salty = true; } package employee; INSERT IMPORTS HERE public class WaterFiller { Water water; }import aquarium.*;import aquarium.Water;import aquarium.jellies.*;import aquarium.*;import aquarium.jellies.Water;import aquarium.*;import aquarium.jellies.*;import aquarium.Water;import aquarium.jellies.Water;None of these imports can make the code compile.
14 Which of the following statements about the code snippet are true? (Choose all that apply.)3: short numPets = 5L; 4: int numGrains = 2.0; 5: String name = "Scruffy"; 6: int d = numPets.length(); 7: int e = numGrains.length; 8: int f = name.length();Line 3 generates a compiler error.Line 4 generates a compiler error.Line 5 generates a compiler error.Line 6 generates a compiler error.Line 7 generates a compiler error.Line 8 generates a compiler error.
15 Which of the following statements about garbage collection are correct? (Choose all that apply.)Calling System.gc() is guaranteed to free up memory by destroying objects eligible for garbage collection.Garbage collection runs on a set schedule.Garbage collection allows the JVM to reclaim memory for other objects.Garbage collection runs when your program has used up half the available memory.An object may be eligible for garbage collection but never removed from the heap.An object is eligible for garbage collection once no references to it are accessible in the program.Marking a variable final means its associated object will never be garbage collected.
16 Which are true about this code? (Choose all that apply.)var blocky = """ squirrel \s pigeon \ termite"""; System.out.print(blocky);It outputs two lines.It outputs three lines.It outputs four lines.There is one line with trailing whitespace.There are two lines with trailing whitespace.If we indented each line five characters,