• OCA: Java 8 Programmer I Study Guide • OCP: Java 8 Programmer II Study Guide • OCA / OCP Java 8 Practice Tests • OCP Java 11 Programmer I Study Guide • OCP Java 11 Programmer II Study Guide • OCP Java 11 Developer Complete Study Guide • OCP Java 11 Practice Tests Win a book at the end!
Jeanne Boyarsky \s </speaker> <title> Becoming one of the first Java 17 \ certified programmers \ (and learning new features) </title> </session> """; continue on next line without a line break new escape character keeps trailing whitespace tab 12
"Ponce City" -> "GA"; case "Legoland" -> { int random = new Random().nextInt(); String city = random % 2 == 0 ? "GA" : "Carlsbad"; yield city; } default -> throw new IllegalArgumentException("Unknown"); }; System.out.println(output); Block yield throws exception so no return value needed 31
BOTTOM }; Position pos = Position.TOP; int stmt = switch(pos) { case TOP: yield 1; case BOTTOM: yield 0; }; int expr = switch(pos) { case TOP -> 1; case BOTTOM -> 0; }; Same! 33
Position pos = Position.TOP; int stmt = switch(pos) { case TOP: yield 1; }; int expr = switch(pos) { case BOTTOM -> 0; }; Does not compile because assigning value and not all values covered 34
'b'; int count = 0; switch (ch) { case 'a' -> count++; case 'b' -> count+=2; case 'c' -> count+=3; } System.out.println(count); 43 C. 5 D. Does not compile A. 1 B. 2
'b'; int count = 0; switch (ch) { case 'a' -> count++; case 'b' -> count+=2; case 'c' -> count+=3; } System.out.println(count); 44 C. 5 D. Does not compile A. 1 B. 2 B
to have the code print 2? char ch = 'b'; ______ value = switch (ch) { case 'a' -> 1; case 'b' -> 2L; case 'c' -> 3.0; default -> 4; }; System.out.println(value); 45 C. Either A or B D. None of the above A. int B. Object
to have the code print 2? char ch = 'b'; ______ value = switch (ch) { case 'a' -> 1; case 'b' -> 2L; case 'c' -> 3.0; default -> 4; }; System.out.println(value); 46 C. Either A or B D. None of the above A. int B. Object B
robot = "694"; if (robot instanceof String s) { System.out.print("x"); } if (robot instanceof Integer s) { System.out.print("y"); } System.out.println(robot); 47 C. y694 D. Does not compile A. x694 B. xy694
robot = "694"; if (robot instanceof String s) { System.out.print("x"); } if (robot instanceof Integer s) { System.out.print("y"); } System.out.println(robot); 48 C. y694 D. Does not compile A. x694 B. xy694 A
Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 49 C. 1, 2 and 3 D. Does not compile A. 1 B. 1 and 3
Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 50 C. 1, 2 and 3 D. Does not compile A. 1 B. 1 and 3 D
class Sword { int length; public boolean equals(Object o) { if (o instanceof Sword sword) return length == sword.length; return false; } } // assume hashCode properly implemented 51 C. equals() does not compile A. equals() is correct B. equals() is incorrect
class Sword { int length; public boolean equals(Object o) { if (o instanceof Sword sword) return length == sword.length; return false; } } // assume hashCode properly implemented 52 C. equals() does not compile A. equals() is correct B. equals() is incorrect A
numPages) { } New type Automatically get * final record * private final instance variables * public accessors/getters * constructor taking both fields * equals(), hashCode() and toString() 56
and entering", 289); System.out.println(book.title()); System.out.println(book.toString()); No “get” Outputs: Breaking and entering Book[title=Breaking and entering, numPages=289] 57
List<String> chapters) { } Book book = new Book("Breaking and entering", 289, chapters); chapters.add("2"); book.chapters().add("3"); System.out.println(book.chapters()); Prints [1,2,3] because shallow immutability 59
numPages, List<String> chapters) { public Book { chapters = List.copyOf(chapters); } } Must match record access modifier Compact constructor 60 Make immutable copy
Fall, Spring, Summer, Winter { } final class Fall extends Seasons {} final class Spring extends Seasons {} final class Summer extends Seasons {} final class Winter extends Seasons {} Seasons Fall Spring Summer Winter 61
Hour, Evening { boolean early(); } public non-sealed class Morning implements TimeOfDay { public boolean early() { return true; } } public non-sealed interface Hour extends TimeOfDay {} public record Evening(int hour) implements TimeOfDay { public boolean early() { return false; } } Records are implicitly final 63
{ } final class Fall extends Seasons {} final class Spring extends Seasons {} final class Summer extends Seasons {} final class Winter extends Seasons {} 64 Yes, but only when they are in the same file.
removed for this code to compile? public record BBQ(String type) {} public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.setType("pork")); System.out.println(bbq.getType()); System.out.println(bbq.equals(bbq)); } 65 C. 2 D. None of the above A. 0 B. 1
removed for this code to compile? public record BBQ(String type) {} public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.setType("pork")); System.out.println(bbq.getType()); System.out.println(bbq.equals(bbq)); } 66 C. 2 D. None of the above A. 0 B. 1 C
BBQ(String type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 67 C. Does not compile D. None of the above A. chicken B. CHICKEN
BBQ(String type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 68 C. Does not compile D. None of the above A. chicken B. CHICKEN C
the following code? public final record BBQ(String type) { { type = ""; } public BBQ(String type) { type = type.toUpperCase(); } public void type() { return ""; } public String toString() { return ""; } } 69 C. 3 D. 4 A. 1 B. 2
the following code? public final record BBQ(String type) { { type = ""; } public BBQ(String type) { type = type.toUpperCase(); } public void type() { return ""; } public String toString() { return ""; } } 70 C. 3 D. 4 A. 1 B. 2 C
BBQ(String type) implements Comparable<BBQ> { public int compareTo(BBQ bbq) { return type.compareTo(bbq.type); } } public static void main(String[] args) { BBQ beef = new BBQ("beef"); BBQ pork = new BBQ("pork"); System.out.println(pork.compareTo(beef)); } 71 C. 0 D. Does not compile A. Negative # B. Positive #
BBQ(String type) implements Comparable<BBQ> { public int compareTo(BBQ bbq) { return type.compareTo(bbq.type); } } public static void main(String[] args) { BBQ beef = new BBQ("beef"); BBQ pork = new BBQ("pork"); System.out.println(pork.compareTo(beef)); } 72 C. 0 D. Does not compile A. Negative # B. Positive # B
block? String sql = """select * from mytable \ where weather = 'snow'; """; A. Has incidental whitespace C. Both A and B B. Has essential whitespace D. Doesn’t compile 76
block? String sql = """select * from mytable \ where weather = 'snow'; """; A. Has incidental whitespace C. Both A and B B. Has essential whitespace D. Doesn’t compile 77 D
have this code print 2? char ch = 'b'; int value = switch (ch) { case 'a' -> 1; case 'b' -> yield 2; case 'c' -> 3; } System.out.println(value); 78 C. 3 D. 4 A. 1 B. 2
have this code print 2? char ch = 'b'; int value = switch (ch) { case 'a' -> 1; case 'b' -> yield 2; case 'c' -> 3; } System.out.println(value); 79 C. 3 D. 4 A. 1 B. 2 C
{ int length = 8; public void printLength(Object x) { if (x instanceof Integer length) length = 2; System.out.println(length); } } 80 C. 8 D. Does not compile A. 2 B. 3
{ int length = 8; public void printLength(Object x) { if (x instanceof Integer length) length = 2; System.out.println(length); } } 81 C. 8 D. Does not compile A. 2 B. 3 C
type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 82 C. Does not compile D. None of the above A. chicken B. CHICKEN
type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 83 C. Does not compile D. None of the above A. chicken B. CHICKEN B