Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oracle Java SE 8 Programmer I Certification

Similar presentations


Presentation on theme: "Oracle Java SE 8 Programmer I Certification"— Presentation transcript:

1 Oracle Java SE 8 Programmer I Certification
FLASH CARDS Oracle Java SE 8 Programmer I Certification Exam 1Z0-808

2 ABBREVIATIONS & ACRONYMS
actype – actual object’s type at run time assop – assignment operator castype – data type specified inside the parens for an explicit cast comperr – compilation error ctor – constructor dim – dimension initer – initializer op – operator paramlist – list of formal parameters in a lambda expression preditype – data type specified inside the angle brackets reftype – reference type refvar – reference variable sout – any printing statement such as System.out.println(), etc. stat – statement ternop – ternary operator var – variable AIOOBE – ArrayIndexOutOfBoundsException CCE – ClassCastException ChE – checked exception CSR – the Catch-or-Specify Requirement DTPE – DateTimeParseException E – an exception (regardless of the type) IAE – IllegalArgumentException IOE – IOException IOOBE – IndexOutOfBoundsException LDT – any of the new date/time classes in Java 8 LOC – line of code NFE – NumberFormatException NPE – NullPointerException RTE – RuntimeException SIOOBE – StringIndexOutOfBoundsException TCF – try-catch-finally construct Author: Igor Soudakevitch, 2017

3 Exam Objectives: FLASH CARDS OVERVIEW Java Basics;
Working with Java Data Types; Using Operators and Decision Constructs; Creating and Using Arrays; Using Loop Constructs; Working with Methods and Encapsulation; Working with Inheritance; Handling Exceptions; Working with Selected Classes from Java API. 3 Source: Oracle Univ.

4 fc.1 JAVA BASICS

5 ANSWER: What components can a class body contain? FLASH CARD 1.1
fields; methods; constructors; initializer blocks; nested datatypes. 5

6 ANSWER: What is the scope of a class variable, a.k.a. static field?
FLASH CARD 1.2 What is the scope of a class variable, a.k.a. static field? ANSWER: A static field is available to any object of the class it is defined in. 6

7 FLASH CARD 1.3 Which variables require the programmer to initialize them before use: class, local or instance? ANSWER: Local. 7

8 FLASH CARD 1.4 What statement is used to gain access to datatypes outside the current package? ANSWER: import 8

9 FLASH CARD 1.5 What character is used to import all datatypes contained within a package? ANSWER: The wildcard character *. 9

10 FLASH CARD 1.6 Which variables are in scope for the entire program: class, local or instance? ANSWER: Class variables. 10

11 FLASH CARD 1.7 True or false: An instance method is allowed to reference a static variable. ANSWER: True. 11

12 ANSWER: Which Java feature makes use of access modifiers?
FLASH CARD 1.8 Which Java feature makes use of access modifiers? ANSWER: Encapsulation. 12

13 FLASH CARD 1.9 Suggest all possible ways to import the static variable NAME declared in a.b.C. ANSWER: import static a.b.C.NAME; or import static a.b.C.*; 13

14 ANSWER: Is this a constructor or a method? FLASH CARD 1.10
public void MyClass(String name) { this.name = name; } ANSWER: It is not a constructor because it defines a return type. 14

15 ANSWER: What statement must a non-void method contain? FLASH CARD 1.11
return 15

16 ANSWER: What is the scope of local variables? FLASH CARD 1.12
The current code block. 16

17 ANSWER: How can we get the first command-line argument?
FLASH CARD 1.13 How can we get the first command-line argument? ANSWER: E.g., args[0] 17

18 FLASH CARD 1.14 What is the scope of instant variables, a.k.a. non-static fields? ANSWER: Non-static fields are available within the current object only. 18

19 FLASH CARD 1.15 What is most typical signature for the main() method when it is being used as the program’s entry point? ANSWER: public static void main(String[] args) 19

20 FLASH CARD 1.16 What platform-independent format is any Java program compiled to? ANSWER: Bytecode. 20

21 FLASH CARD 1.17 Which variables may have a scope smaller than a method: class, local or instance? ANSWER: Local variables. 21

22 FLASH CARD 1.18 Which ones are subject to implicit initialization: fields or local variables or both? ANSWER: Fields. 22

23 FLASH CARD 1.19 What is the name of the method that must be invoked to guarantee that the garbage collector will run? ANSWER: Trick question! The System.gc() method (which is effectively equivalent to the call Runtime.getRuntime().gc()) merely suggests that the GC should run but makes no guarantees. 23

24 ANSWER: What Java feature uses objects to model real-world scenarios?
FLASH CARD 1.20 What Java feature uses objects to model real-world scenarios? ANSWER: Object orientation. 24

25 ANSWER: What packages are imported by default? FLASH CARD 1.21
Only one package is imported by default, and it’s java.lang. 25

26 FLASH CARD 1.22 What statement provides access to constants and static methods of a class? ANSWER: import static 26

27 ANSWER: What is the scope of method parameters? FLASH CARD 1.23
Method parameters are available with the current method only. 27

28 FLASH CARD 1.24 True or false: The finalize() method of an Object can be invoked multiple times. ANSWER: False: the finalize() method is never invoked more than once by a Java virtual machine for any given object. 28

29 FLASH CARD 1.25 What is the length of the String array that contains the command-line arguments in the following scenario? java Test arg1,arg2 ANSWER: One. 29

30 FLASH CARD 1.26 Given the following packages a.A, a.b.B, a.b.c.C , which of them does the statement import a.b.* actually import? ANSWER: Only a.b.B, because the import wildcard * does not extend to subpackages. 30

31 FLASH CARD 1.27 Given that an executable public class named HelloWorld is defined in the a.b.c package, what should be the pair of commands to compile the class without the –d switch and then run it? ANSWER: javac .\a\b\c\HelloWorld.java (where the .\ part is optional) (or just java HelloWorld.java, depending on the location) java a.b.c.HelloWorld 31

32 FLASH CARD 1.28 In what order must the class declaration(s), import statement(s) and package declaration be placed within a .java file? ANSWER: package declaration (optional) import statement(s) (optional) class declaration(s) 32

33 NEXT: EXAM OBJECTIVE 1 EXAM OBJECTIVE 1 33

34 fc.2 DATA TYPES 34

35 ANSWER: What are the eight primitive types in Java? FLASH CARD 2.1
byte, short, int, long, float, double, char, and boolean. 35

36 FLASH CARD 2.2 What’s the name of the process when the compiler automatically converts an int to an Integer? ANSWER: Autoboxing. 36

37 ANSWER: What are the default values for these instance variables?
FLASH CARD 2.3 What are the default values for these instance variables? String str; int num; double d; boolean bool; ANSWER: str = null; num = 0; d = 0.0; bool = false; 37

38 FLASH CARD 2.4 What type would be used for a method parameter if it’s meant to support any variable? ANSWER: java.lang.Object 38

39 ANSWER: Which of the following variables can null be assigned to?
FLASH CARD 2.5 Which of the following variables can null be assigned to? Object obj; String str; int num; boolean bool; ANSWER: Object obj and String str can be assigned null because they are reference types. As for int and boolean, they are primitives and so cannot be assigned null. 39

40 FLASH CARD 2.6 True or false: When passing an ArrayList named list to a method, re-assigning list = new ArrayList() inside the method affects the caller. ANSWER: False. 40

41 ANSWER: What characters can a variable name start with? FLASH CARD 2.7
For the exam: any Latin letter, dollar sign $, or underscore _. 41

42 ANSWER: What characters may a variable name contain? FLASH CARD 2.8
For the exam: a Latin letter, number, dollar sign, or underscore. 42

43 FLASH CARD 2.9 Which primitive type uses four bytes to represent a signed fractional value with 23 bits for the precision and 8 bits for the scale? ANSWER: float 43

44 FLASH CARD 2.10 True or false: When passing an ArrayList named list to a method, calls to list.add() are reflected in the caller. ANSWER: True. 44

45 FLASH CARD 2.11 To which purpose would you place underscore(s) in a numeric literal? ANSWER: To increase readability. 45

46 FLASH CARD 2.12 True or false: An abstract class that implements an interface is not required to implement any of the interface's abstract methods. ANSWER: True. 46

47 FLASH CARD 2.13 What are the only two possible values valid for a boolean variable? ANSWER: true and false. 47

48 FLASH CARD 2.14 Which primitive type uses eigh bytes to represent a signed fractional value with 53 bits for the precision and 11 bits for the scale?? ANSWER: double 48

49 ANSWER: What keyword is required to instantiate a class?
FLASH CARD 2.15 What keyword is required to instantiate a class? ANSWER: new 49

50 FLASH CARD 2.16 Which primitive type represents a signed integer within the range of –32,768 to 32,767? ANSWER: short 50

51 FLASH CARD 2.17 What exception is thrown when you use the parseInt() method with an unparsable argument? NumberFormatException RuntimeException └─ IllegalArgumentException └─ NumberFormat Exception IAE: Thrown to indicate that a method has been passed an illegal or inappropriate argument. NFE: Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format. ANSWER: 51

52 FLASH CARD 2.18 Given the non-abstract class Test and the instance field fieldA, what expression will retrieve the value of fieldA? ANSWER: new Test().fieldA; 52

53 ANSWER: ls 0xCAFE_BABE a valid numerical literal in Java 8?
FLASH CARD 2.19 ls 0xCAFE_BABE a valid numerical literal in Java 8? ANSWER: Yes, it is. 53

54 FLASH CARD 2.20 Which primitive type represents a signed integer within the range of –128 to 127? ANSWER: byte 54

55 ANSWER: What is the decimal value for the numerical literal 0xA0?
FLASH CARD 2.21 What is the decimal value for the numerical literal 0xA0? ANSWER: 160 55

56 FLASH CARD 2.22 Which type of variable, primitive or reference, stores a pointer to an object? ANSWER: Reference type. 56

57 FLASH CARD 2.23 What is the decimal value for the numerical literal 0b1111_1111? ANSWER: 255 57

58 ANSWER: Which primitive type represents a character encoded in UTF-16?
FLASH CARD 2.24 Which primitive type represents a character encoded in UTF-16? ANSWER: char 58

59 FLASH CARD 2.25 Which exception is thrown when you try to get a null value unboxed by Java? ANSWER: NullPointerException 59

60 ANSWER: ls 1.0_F a valid numerical literal in Java 8? FLASH CARD 2.26
No, it isn’t. 60

61 ANSWER: Choose the method that can be used to remove 3
FLASH CARD 2.27 Choose the method that can be used to remove 3 from the array declared as int[] arr = {1,2,3}; java.util.Arrays.remove(2) or java.util.Arrays.delete(3)? ANSWER: Trick question! Arrays are structurally immutable in the first place and, besides, the class java.util.Arrays defines neither remove() nor delete(). 61

62 FLASH CARD 2.28 Which prefix in a numerical literal indicates a hexadecimal number? ANSWER: 0x 62

63 FLASH CARD 2.29 Which action must be performed first to access an instance member in a class? ANSWER: The class must be instantiated. 63

64 ANSWER: ls 123_456L a valid numerical literal in Java 8?
FLASH CARD 2.30 ls 123_456L a valid numerical literal in Java 8? ANSWER: Yes, it is. 64

65 FLASH CARD 2.31 Which type of variable, primitive or reference, stores only its underlying value? ANSWER: The primitive type. 65

66 FLASH CARD 2.32 In what phase is an object when the GC is about to remove it from memory? ANSWER: The object is finalized. 66

67 ANSWER: Which primitive type is default for a signed integer?
FLASH CARD 2.33 Which primitive type is default for a signed integer? int JLS, §4.2, Primitive Types and Values: The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units. Official Oracle Tutorial: In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of Use the Integer class to use int data type as an unsigned integer. ANSWER: 67

68 FLASH CARD 2.34 Which type of variable, primitive or reference, shares a copy of an object pointer in an assignment operation? ANSWER: The reference type. 68

69 FLASH CARD 2.35 Which suffix specifies that a literal value should be stored as a long? ANSWER: L 69

70 FLASH CARD 2.36 Which specialized operation is invoked like a method to initialize an object? ANSWER: A constructor. 70

71 FLASH CARD 2.37 Which three locations are invalid when placing underscores in a numeric literal? ANSWER: Beginning or end of the number, contiguous to decimal point, or contiguous to F / L suffixes. 71

72 FLASH CARD 2.38 Which suffix specifies a literal value should be stored as a float? ANSWER: f 72

73 ANSWER: Which operation is used on Line X: autoboxing or unboxing?
FLASH CARD 2.39 List(double) list = new ArrayList<>(); list.add(1.0); // Line X Which operation is used on Line X: autoboxing or unboxing? ANSWER: Neither: the code fails compilation as Collections accept reference types only. Changing to List<Double> would mean that Line X uses autoboxing. 73

74 FLASH CARD 2.40 In what phase is an object when it goes out of scope and has no remaining live pointers? ANSWER: The object is dereferenced. 74

75 (not available in Demo Version)
NEXT: EXAM OBJECTIVE 2 EXAM OBJECTIVE 2 (not available in Demo Version) 75

76 OPERATORS & DECISION CONSTRUCTS
fc.3 OPERATORS & DECISION CONSTRUCTS 76

77 ANSWER: What is the value of the following expression? FLASH CARD 3.1
(5 == 5) ? "Wrong" : "Correct" ANSWER: Wrong 77

78 FLASH CARD 3.2 What does the == operator determine when comparing two String variables? ANSWER: Whether those String variables point to the same object. 78

79 FLASH CARD 3.3 What keyword is used to execute a statement (or a block of statements) in a conditional operator if the value of the conditional expression is false? ANSWER: else 79

80 FLASH CARD 3.4 Arrange the following operators in decreasing order of precedence: || ++ == * ANSWER: ++ * == || 80

81 FLASH CARD 3.5 What method tests reference types for semantic equivalence rather than the equality of the underlying objects? ANSWER: equals() 81

82 ANSWER: What characters are used to override operator precedence?
FLASH CARD 3.6 What characters are used to override operator precedence? ANSWER: The parentheses, (). technospeak: parens brackets: Am.Eng: usually denoting square brackets Br.Eng: usually denoting round brackets 82

83 ANSWER: What is the value of this code snippet? 0 <= 5 – 5 * 10
FLASH CARD 3.7 What is the value of this code snippet? 0 <= 5 – 5 * 10 ANSWER: false Follow-up: Is it an expression or statement? Will it compile when followed by a semicolon? 83

84 ANSWER: What is the <> called in the following LOC?
FLASH CARD 3.8 What is the <> called in the following LOC? List<String> list = new ArrayList<>(); ANSWER: The diamond operator. 84

85 FLASH CARD 3.9 Operators of what type enjoy the highest order of precedence in Java? ANSWER: Unary operators. 85

86 FLASH CARD 3.10 Operators of what type get evaluated last according to the Java rules of precedence? ANSWER: The assignment operators. 86

87 FLASH CARD 3.11 What keyword is used in a switch construct to exit a case block? ANSWER: break 87

88 ANSWER: Which one does the following code snippet produce:
FLASH CARD 3.12 Which one does the following code snippet produce: true or false? int a = 0; int b = (a < 0) : true ? "false"; ANSWER: Neither because the code fails compilation. 88

89 FLASH CARD 3.13 In which way do postfix unary operators differ from prefix unary operators? ANSWER: The variable acted upon by a postfix unary operator changes its value not until after the overall expression has been evaluated. 89

90 ANSWER: What does the following statement print? FLASH CARD 3.14
System.out.print(0_1^1_0); ANSWER: 11 90

91 What’s wrong with the following Kaplan Self-Test question?
FLASH CARD 3.15 What’s wrong with the following Kaplan Self-Test question? 91

92 ANSWER: FLASH CARD 3.15 , cont’d It should’ve been:
all integral-type primitives except long (i.e., byte, short, int, char); their corresponding wrappers; Strings; enums. 92

93 ANSWER: Which expression comes first in a ternary operator?
FLASH CARD 3.16 Which expression comes first in a ternary operator? ANSWER: A conditional expression that evaluates to a boolean or Boolean value. 93

94 FLASH CARD 3.17 What does the method equals() determine when comparing two StringBuilder objects? ANSWER: Since the StringBuilder class does not override Object’s equals(), this method tests whether those two objects are the same. 94

95 FLASH CARD 3.18 What construct can be used in place of else-if statements when comparing discrete values? ANSWER: switch 95

96 ANSWER: Where in a switch statement must the default block be placed?
FLASH CARD 3.19 Where in a switch statement must the default block be placed? ANSWER: The default block is optional and can be placed anywhere: in the beginning, at the end or in the middle of the switch statement. 96

97 FLASH CARD 3.20 If you want the newly incremented value to be used in an expression after it has been updated, which operator would you use? ANSWER: A prefix-form increment operator (e.g., ++a). 97

98 FLASH CARD 3.21 Why should you be careful when evaluating the right-hand side of expressions that contain && and ||? ANSWER: The right-hand side of short-circuit operators will not be evaluated if the value can be determined solely from the left-hand side. 98

99 ANSWER: How to negate a boolean? FLASH CARD 3.22
By prefixing it with the logical compliment operator ! 99

100 FLASH CARD 3.23 True or false: a case in a switch statement must be a constant expression. ANSWER: True. 100

101 FLASH CARD 3.24 True or false: the data structure in a for-each loop must be an array. ANSWER: False. In addition to arrays, it also can be any Object that implements the java.lang.Iterable interface. 101

102 FLASH CARD 3.25 Which statement transfers the flow of control from the nearest enclosing loop? ANSWER: break 102

103 (not available in Demo Version)
NEXT: EXAM OBJECTIVE 3 EXAM OBJECTIVE 3 (not available in Demo Version) 103

104 fc.4 ARRAYS 104

105 FLASH CARD 4.1 What characters are used to declare and initialize an array and give access to its elements? ANSWER: The square brackets, []. 105

106 ANSWER: How to get access to the first element in an array?
FLASH CARD 4.2 How to get access to the first element in an array? ANSWER: E.g., myArray[0]. 106

107 FLASH CARD 4.3 How many arrays are created in this code and how many dimensions is each one? int[] a, b[]; ANSWER: Two arrays. a is a one-dimensional array and b is a two-dimensional array. 107

108 ANSWER: What field stores the number of elements in any given array?
FLASH CARD 4.4 What field stores the number of elements in any given array? ANSWER: length 108

109 ANSWER: How to get access to the last element in an array?
FLASH CARD 4.5 How to get access to the last element in an array? ANSWER: E.g., myArray[myArray.length - 1]. 109

110 ANSWER: Which one is valid? int[] a; int b[]; FLASH CARD 4.6
Both are valid. 110

111 ANSWER: What’s wrong with this question from Kaplan Self-Test?
FLASH CARD 4.7 What’s wrong with this question from Kaplan Self-Test? ANSWER: The suggested initializing shortcut can be used only at the point of array declaration. 111

112 (not available in Demo Version)
NEXT: EXAM OBJECTIVE 4 EXAM OBJECTIVE 4 (not available in Demo Version) 112

113 fc.5 LOOPS 113

114 FLASH CARD 5.1 How many times will statements in a while(false) block execute? ANSWER: Zero times. 114

115 ANSWER: Which statement terminates the innermost iterative block?
FLASH CARD 5.2 Which statement terminates the innermost iterative block? ANSWER: (Unlabeled) break. 115

116 ANSWER: How many times will statements in a for(;;) loop execute?
FLASH CARD 5.3 How many times will statements in a for(;;) loop execute? ANSWER: An infinite number of times. 116

117 FLASH CARD 5.4 The bodies of which of the following statements will be executed at least once even if the boolean expression initially evaluates to false: while, do-while, for ANSWER: do-while 117

118 ANSWER: How many times is the do-while loop guaranteed to run?
FLASH CARD 5.5 How many times is the do-while loop guaranteed to run? ANSWER: Once. 118

119 FLASH CARD 5.6 How many times will statements in a for(int e=0; e<10; e+=2) loop execute? ANSWER: Five times. 119

120 ANSWER: How to jump out of an arbitrarily choosen loop? FLASH CARD 5.7
Use return or a labeled break. How many times will this code print 2nd? for (int i = 0; i < 3; i++) { label: for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { System.out.println("3rd"); break label // line L ;} System.out.println("2nd"); } System.out.println("1st"); And what if we comment out Line L? ANSWER: 120

121 FLASH CARD 5.8 How many times will statements in a for(int i=0; ++i<3;) loop execute? ANSWER: Twice. 121

122 (not available in Demo Version)
NEXT: EXAM OBJECTIVE 5 EXAM OBJECTIVE 5 (not available in Demo Version) 122

123 METHODS & ENCAPSULATION
fc.6 METHODS & ENCAPSULATION 123

124 ANSWER: True or false: Overloaded methods have identical signatures.
FLASH CARD 6.1 True or false: Overloaded methods have identical signatures. ANSWER: False. 124

125 FLASH CARD 6.2 List all the seven parts of a concrete method declaration going left to right. ANSWER: optional access modifier; optional specifier(s); return type; method name; optional parameter list; optional exception clause; method body. 125

126 FLASH CARD 6.3 Which of the following may be different between two overloaded methods: method name, return type, access modifier, specifier(s), exception list? ANSWER: Return type, access modifier, specifier(s) and exception list. 126

127 FLASH CARD 6.4 Arrange the order in which Java is looking for matches among overloaded methods: autoboxing, exact match, widening primitives, varargs. ANSWER: Exact match, widening primitives, autoboxing, varargs. 127

128 FLASH CARD 6.5 Suggest a LOC to print the number of elements passed in this method: public void printMe(int... a){ // your code goes here } ANSWER: System.out.println(a.length); 128

129 ANSWER: True or false: all methods in Java are virtual. FLASH CARD 6.6
False: only non-final non-private instance methods are virtual in Java. 129

130 ANSWER: List all the acces modifiers available in Java. FLASH CARD 6.7
private protected public 130

131 FLASH CARD 6.8 Given the expression Test.member1(), what is member1 if it has been declared in the Test class? ANSWER: A static method. 131

132 ANSWER: What is meant by method overloading? FLASH CARD 6.9
Two or more methods within a class share the same name but their parameter lists are different. 132

133 FLASH CARD 6.10 What is provided by the compiler if the class defines no constructors at all? ANSWER: A default constructor. 133

134 ANSWER: What does a subclass’s default constructor contain?
FLASH CARD 6.11 What does a subclass’s default constructor contain? ANSWER: A call to the superclass’s no-arg constructor. 134

135 Parameters vs arguments
FLASH CARD 6.12 What is stored in the method parameter? A copy of the method argument. Parameters vs arguments Official Oracle Tutorial: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order. ANSWER: 135

136 ANSWER: What keyword is used to invoke the superclass’s constructor?
FLASH CARD 6.13 What keyword is used to invoke the superclass’s constructor? ANSWER: super 136

137 ANSWER: What statement is mandatory in a non-void method?
FLASH CARD 6.14 What statement is mandatory in a non-void method? ANSWER: return 137

138 ANSWER: What keyword is used to invoke an overloaded constructor?
FLASH CARD 6.15 What keyword is used to invoke an overloaded constructor? ANSWER: this 138

139 FLASH CARD 6.16 What type of construct is optional when used as a parameter and can have more than one value? ANSWER: The varargs construct. 139

140 FLASH CARD 6.17 What keyword is used to declare a method that doesn’t return a value? ANSWER: void 140

141 ANSWER: How many parameters does default constructor declare?
FLASH CARD 6.18 How many parameters does default constructor declare? ANSWER: Zero. 141

142 FLASH CARD 6.19 What is the term for a method that is used to change a field’s value? ANSWER: A setter (or “mutator”). 142

143 FLASH CARD 6.20 When following JavaBeans naming conventions, what would be the mutator method for the property declared as String name; ANSWER: public void setName(String name) { this.name = name; } 143

144 FLASH CARD 6.21 When following JavaBeans naming conventions, what would be the accessor method for the property declared as boolean boo; ANSWER: public boolean isBoo() { return boo; } or public boolean getBoo() { return boo; } 144

145 FLASH CARD 6.22 What is the term for a method that is used to retrieve a field’s value? ANSWER: A getter (or “accessor”). 145

146 ANSWER: How do we specify a varargs parameter? FLASH CARD 6.23 With …
146

147 ANSWER: What is wrong with the following declaration? FLASH CARD 6.24
private abstract int run(int... a, int[]... b) {} ANSWER: private abstract combo is illegal; only one vararg parameter is allowed per method; in addition, it must be the last parameter in the list; abstract methods cannot have bodies. 147

148 FLASH CARD 6.25 Which access modifier makes a member available to all other classes? ANSWER: public 148

149 ANSWER: According to JLS, what is included in a method’s signature?
FLASH CARD 6.26 According to JLS, what is included in a method’s signature? ANSWER: The method’s name and the list of its parameters. 149

150 FLASH CARD 6.27 Given the method signature run(String str, int... abc), how many arguments are mandatory? ANSWER: One. 150

151 FLASH CARD 6.28 Which modifier is used to declare fields or methods as class members? ANSWER: static 151

152 FLASH CARD 6.29 What access modifier should be the default for fields when applying encapsulation principles to a class? ANSWER: private 152

153 FLASH CARD 6.30 What is the effective access level of a member that does not have an access modifier applied? ANSWER: Default (or package-private, or package-level) access. 153

154 FLASH CARD 6.31 Which access modifier makes a member available only to classes within the same package or subclasses? ANSWER: protected 154

155 ANSWER: What code can read protected String str? FLASH CARD 6.32
Code in the same package or code that subclasses the class containing that instance variable str. EXTRA: So, how many LOCs wont’ compile? package one; public class Parent{ protected String name = "Parent"; } package two; import one.Parent; class Child extends Parent{ public static void main(String[] args) { Parent p = new Parent(); Child c = new Child(); System.out.println( p.name ); System.out.println( c.name ); } class Test { ANSWER: 155

156 FLASH CARD 6.33 If an argument is a primitive type, then what is the effect of modifications to the value of the object? alternative wording: Will the changes be reflected in the caller if the passed-in arguments are of primitive types? ANSWER: Any changes will be discarded when the method returns. 156

157 FLASH CARD 6.34 When handling a field object from an accessor method, what should be returned to meet encapsulation principles? ANSWER: A copy of the object. 157

158 ANSWER: What’s wrong with the following Kaplan Self-Test question?
FLASH CARD 6.35 What’s wrong with the following Kaplan Self-Test question? class ClassA{ static class member1{ static { System.out.println("Hi!"); } } class member2{ { System.out.println("Hello!"); } void member3(){ System.out.println("Whazzup?");} public static void main(String[] args) { new ClassA.member1(); // Hi! new ClassA().new member2(); // Hello! new ClassA().member3(); // Whazzup? ANSWER: 158

159 (not available in Demo Version)
NEXT: EXAM OBJECTIVE 6 EXAM OBJECTIVE 6 (not available in Demo Version) 159

160 fc.7 INHERITANCE 160

161 FLASH CARD 7.1 True or false: A reference to a class may be automatically used as a reference to a subclass without an explicit cast. ANSWER: False. 161

162 FLASH CARD 7.2 When implementing a method declared in an abstract class, what three approaches are available to deal with the specified checked exceptions? ANSWER: throw the same exception; throw a subclass of the specified exception; throw nothing. 162

163 ANSWER: What keyword is used to subclass a parent class?
FLASH CARD 7.3 What keyword is used to subclass a parent class? ANSWER: extends 163

164 FLASH CARD 7.4 Give one reason why default methods were added to interfaces in Java 8. ANSWER: To maintain backward compatibility. 164

165 ANSWER: Explain a covariant return type. FLASH CARD 7.5
Covariant return types are used whenever a child class overrides a method in a parent class. In this case the return type is permitted to be a subclass of the return type of the method defined in the superclass. 165

166 ANSWER: What datatypes can contain optional implementations?
FLASH CARD 7.6 What datatypes can contain optional implementations? ANSWER: Interfaces and abstract classes. 166

167 ANSWER: How many classes can be derived from a parent class?
FLASH CARD 7.7 How many classes can be derived from a parent class? ANSWER: An infinite number. 167

168 ANSWER: Which feature of Java lets you reuse code in existing classes?
FLASH CARD 7.8 Which feature of Java lets you reuse code in existing classes? ANSWER: Inheritance. 168

169 FLASH CARD 7.9 If you have a constructor that calls another constructor in the same class, which should be used first, this() or super()? ANSWER: Actually, there’s no ‘first’: only one such call may be made, and it has to be this(). 169

170 ANSWER: How many interfaces can a class implement? FLASH CARD 7.10
An infinite number. 170

171 FLASH CARD 7.11 What keyword would be used for a class that extends an interface? ANSWER: This is a trick question: classes do not extend interfaces, they implement them. 171

172 ANSWER: How would you override a private instance method?
FLASH CARD 7.12 How would you override a private instance method? ANSWER: It is impossible. A private method can be only hidden. 172

173 FLASH CARD 7.13 What restriction applies to the target type when performing implicit reference type casting? ANSWER: The target type must be a valid supertype. 173

174 ANSWER: What’s wrong with the following Kaplan Self-Test question?
FLASH CARD 7.14 What’s wrong with the following Kaplan Self-Test question? ANSWER: Starting with Java 8, interfaces may contain fully implemented static and/or default methods. 174

175 FLASH CARD 7.15 In polymorphism, what does the run-time type of the object determine? ANSWER: Which implementation of the overridden method gets executed. 175

176 FLASH CARD 7.16 What is the difference between overloading, overriding, and hiding a method? ANSWER: Overloaded methods share the same name but different signatures and have no polymorphic relationship. Overridden methods share the same signature and are replaced at runtime in all places they are called. Hidden methods share the same signature but are only replaced in the subclasses for which they are defined. 176

177 ANSWER: Which two reference types cannot be instantiated?
FLASH CARD 7.17 Which two reference types cannot be instantiated? ANSWER: Interfaces and abstract classes. 177

178 FLASH CARD 7.18 True or false: If a parent class does not include a no-arg constructor, the child class may not declare one. ANSWER: False. 178

179 ANSWER: What class is the implicit supertype for all classes?
FLASH CARD 7.19 What class is the implicit supertype for all classes? ANSWER: java.lang.Object 179

180 ANSWER: What operator is used to test the IS-A relationship?
FLASH CARD 7.20 What operator is used to test the IS-A relationship? ANSWER: instanceof 180

181 ANSWER: What’s wrong with the following Kaplan Self-Test question?
FLASH CARD 7.21 What’s wrong with the following Kaplan Self-Test question? class Father { void runF(){ System.out.println("Hello from Father"); }} class Son extends Father { void runS(){ System.out.println("Hello from Son"); }} class Test{ public static void main(String[] args) { Father obj = new Son(); obj.runF(); // Hello from Father // obj.runS(); // INVALID: 'cannot find method runS() // in variable obj of type Father' }} ANSWER: 181

182 FLASH CARD 7.22 When overriding a non-void method, what is the restriction imposed on the returned type? ANSWER: The returned type of the overriding method must be compatible with that of the overridden method. 182

183 ANSWER: Given the statement
FLASH CARD 7.23 Given the statement TypeA obj = new TypeB(); what is the reference type of the obj variable? ANSWER: TypeA. 183

184 FLASH CARD 7.24 Which keyword is used to declare a class that provides interface implementation? ANSWER: implements 184

185 FLASH CARD 7.25 Starting with Java 8, what four kinds of members are valid in interfaces? ANSWER: abstract methods; default methods; static methods; constants. 185

186 ANSWER: What is accomplished by reference type downcasting?
FLASH CARD 7.26 What is accomplished by reference type downcasting? ANSWER: Access to the subtype members that are not normally available to the declared reference type. 186

187 ANSWER: What is method overriding? FLASH CARD 7.27
It’s when a subtype’s method has the same signature but different implementation. 187

188 FLASH CARD 7.28 Which keyword is used to specify that a class cannot be extended, a field is a constant value, or a method cannot be overridden? ANSWER: final 188

189 FLASH CARD 7.29 True or false: The first line of every constructor is an implicit call to the parent constructor by invoking super(). ANSWER: False. 189

190 FLASH CARD 7.30 Which keyword is used to access superclass members within a subclass? ANSWER: super 190

191 ANSWER: What is the difference between super and super()?
FLASH CARD 7.31 What is the difference between super and super()? ANSWER: super() calls the constructor of the parent class and is used in the first line of every child constructor that has no this(), whereas super is a keyword used to reference a member of the parent class. Corollary and known trap on the exam: super() cannot appear in methods. 191

192 FLASH CARD 7.32 True or false: For inherited methods of the parent class, both super and this can be used interchangeably within a child class to access the method, assuming the child class does not override it. ANSWER: True. 192

193 FLASH CARD 7.33 Which three modifiers are implicit for fields declared in an interface? ANSWER: public, static and final. 193

194 ANSWER: Are instance initializers run before or after the constructor?
FLASH CARD 7.34 Are instance initializers run before or after the constructor? ANSWER: Before. 194

195 FLASH CARD 7.35 When do you need to explicitly call super() in at least one of your constructors? ANSWER: Whenever the parent class does not define a no-arg constructor. 195

196 (not available in Demo Version)
NEXT: EXAM OBJECTIVE 7 EXAM OBJECTIVE 7 (not available in Demo Version) 196

197 fc.8 EXCEPTIONS 197

198 FLASH CARD 8.1 Is a class that extends RuntimeException a checked or unchecked exception? ANSWER: Unchecked. 198

199 FLASH CARD 8.2 True or false: A program must handle or declare java.lang.Error. ANSWER: False. 199

200 ANSWER: Which of the following datatypes are checked exceptions?
FLASH CARD 8.3 Which of the following datatypes are checked exceptions? ArrayIndexOutOfBoundsException ClassCastException ExceptionInInitializerError FileNotFoundException IOException ANSWER: FNFE and IOE. 200

201 FLASH CARD 8.4 How many finally blocks are allowed per try statement in a method? ANSWER: One at most. 201

202 FLASH CARD 8.5 How many catch blocks are allowed per regular try statement in a method? ANSWER: Any number; zero is also possible but only if a finally block has been provided. 202

203 ANSWER: How do exceptions propagate? FLASH CARD 8.6 Up the call stack.
203

204 FLASH CARD 8.7 Suppose, the try block, catch block, and finally block all throw an exception. Which one of these exceptions does the caller get? ANSWER: The one from finally. 204

205 FLASH CARD 8.8 Which exception superclass represents abnormal conditions that are internal to an application and often recoverable? ANSWER: java.lang.Exception 205

206 FLASH CARD 8.9 What are advantages of the class hierarchy of Throwable, Error, Exception, and RuntimeException that is used in exception handling? ANSWER: Such an hierarchy allows to group and differentiate between error types. 206

207 FLASH CARD 8.10 Which exception superclass represents abnormal conditions that are internal to an application and not recoverable? ANSWER: java.lang.RuntimeException 207

208 FLASH CARD 8.11 Which keyword is used to generate an exception from within a method? ANSWER: throw 208

209 ANSWER: Which exception classes are checked by the compiler?
FLASH CARD 8.12 Which exception classes are checked by the compiler? ANSWER: The class java.lang.Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. 209

210 ANSWER: When does code in the catch block execute? FLASH CARD 8.13
When the exception specified in the catch statement gets thrown in the associated try block. 210

211 ANSWER: Which exception class handles input-output errors?
FLASH CARD 8.14 Which exception class handles input-output errors? ANSWER: IOException 211

212 FLASH CARD 8.15 Which type of exceptions must be specified or caught for methods to compile? ANSWER: Checked exceptions. 212

213 FLASH CARD 8.16 Which exception class indicates that the array index is either negative or not less than the length of the array? ANSWER: ArrayIndexOutOfBoundsException 213

214 FLASH CARD 8.17 Which exception class indicates that a character index is either negative or not less than the length of a String? ANSWER: StringIndexOutOfBoundsException 214

215 FLASH CARD 8.18 Which exception class indicates that an index is invalid for a String or array? ANSWER: IndexOutOfBoundsException 215

216 FLASH CARD 8.19 Is java.lang.Exception the most generic exception class? What about its supertype, Throwable? ANSWER: Exceptions are meant to be handled while Throwable also includes the Error class. 216

217 FLASH CARD 8.20 In which way the Java exception handling mechanism affects programs’ business logic and error handling code? ANSWER: It keeps them separate. 217

218 FLASH CARD 8.21 True or false: If a try statement has catch blocks for NullPointerException and RuntimeException, the catch blocks can be in either order. ANSWER: False. 218

219 ANSWER: What reference types can a catch statement accept?
FLASH CARD 8.22 What reference types can a catch statement accept? ANSWER: java.lang.Throwable and its subclasses. 219

220 ANSWER: What happens when an exception gets thrown? FLASH CARD 8.23
The default program flow is halted and the exception is handled depending on how the Catch-or-Specify Requirement has been implemented in the code. 220

221 FLASH CARD 8.24 Which superclass represents abnormal conditions that are external to the application and not recoverable? ANSWER: java.lang.Error 221

222 FLASH CARD 8.25 Which keyword specifies that an exception might be thrown by a method? ANSWER: throws 222

223 ANSWER: True or false: If an interface has the method
FLASH CARD 8.26 True or false: If an interface has the method default void run() throws Exception {} , the implementing class is not allowed to have the method void run() throws IOException { } ANSWER: OK, this question isn’t about exceptions; it’s about interface methods being public. True. 223

224 ANSWER: What happens when an exception gets caught within a method?
FLASH CARD 8.27 What happens when an exception gets caught within a method? ANSWER: The execution of the program continues after the last catch or in a finally block. 224

225 ANSWER: What is the supertype for ClassCastException? FLASH CARD 8.28
java.lang.RuntimeException 225

226 FLASH CARD 8.29 Which exception is thrown if you attempt to divide a number by zero in your program? ANSWER: ArithmeticException – but only in 25% of cases: System.out.println( 10/0 ); // Exception in thread "main“ // java.lang.ArithmeticException: / by zero System.out.println( 10/0.0 ); // prints Infinity System.out.println( 10.0/0 ); // ditto System.out.println( 10.0/0.0 ); // ditto 226

227 FLASH CARD 8.30 If a try statement has both a catch block and a finally block, which order do they run in if an exception is not thrown? ANSWER: try followed by finally. 227

228 (not available in Demo Version)
NEXT: EXAM OBJECTIVE 8 EXAM OBJECTIVE 8 (not available in Demo Version) 228

229 SELECTED CLASSES FROM JAVA API
fc.9 SELECTED CLASSES FROM JAVA API 229

230 FLASH CARD 9.1 What method is used to inject new characters into a string literal? ANSWER: There’s NO such method: the String objects are immutable. 230

231 FLASH CARD 9.2 Which StringBuilder method injects new characters within a string literal? ANSWER: insert() 231

232 FLASH CARD 9.3 Assuming all necessary imports are in place, suggest the simplest replacement for the following code block: Predicate<ArrayList<String>> p; p = (ArrayList<String> list) -> { return true; }; ANSWER: E.g.: Predicate p = list -> true; 232

233 FLASH CARD 9.4 What are the three primary local date and time classes introduced in Java 8 and what do they represent? ANSWER: LocalDate is just the system date without time. LocalTime is just the system time. LocalDateTime is the system date and time. All three do not include time zone. 233

234 FLASH CARD 9.5 Which method indicates whether an object is present in an ArrayList? ANSWER: contains() 234

235 FLASH CARD 9.6 Write the fully qualified name of the interface provided in the standard Java API library that declares a one-arg functional method that returns a boolean. ANSWER: java.util.function.Predicate 235

236 FLASH CARD 9.7 Which two methods return an index value for the specified object in an ArrayList? ANSWER: indexOf() and lastIndexOf() 236

237 ANSWER: How do you create a Period of a year, a month and a day?
FLASH CARD 9.8 How do you create a Period of a year, a month and a day? ANSWER: Period.of(1,1,1); 237

238 ANSWER: What’s wrong with this lambda expression? FLASH CARD 9.9
(ExamTaker et) -> return et.isPassed(); ANSWER: Curly braces are missing. 238

239 FLASH CARD 9.10 Whereas StringBuilder manipulation methods affect the underlying string literal, what do String manipulation methods do? ANSWER: They create and return a new String object. 239

240 FLASH CARD 9.11 Which method indicates whether multiple objects are elements in an ArrayList? ANSWER: containsAll() 240

241 ANSWER: Which method takes out multiple elements from an ArrayList?
FLASH CARD 9.12 Which method takes out multiple elements from an ArrayList? ANSWER: removesAll() 241

242 ANSWER: What is the class used for formatting a LocalDate?
FLASH CARD 9.13 What is the class used for formatting a LocalDate? ANSWER: java.time.format.DateTimeFormatter 242

243 FLASH CARD 9.14 How do you create an object with the current system date and time? ANSWER: LocalDateTime.now(); 243

244 ANSWER: What’s wrong with the following Kaplan Self-Test question?
FLASH CARD 9.15 What’s wrong with the following Kaplan Self-Test question? ANSWER: The answer would be valid when the LE is enclosed in parens + isPresent() implies the JavaBeans Conventions  return would be mandatory, too. 244

245 FLASH CARD 9.16 What method is typically used to convert a String into another type such as LocalDateTime, etc.? ANSWER: parse() 245

246 FLASH CARD 9.17 Which method appends multiple elements to the end of an ArrayList? ANSWER: addAll() 246

247 FLASH CARD 9.18 What expression would retrieve the last element from an ArrayList object named myList? ANSWER: myList.get(myList.size()-1); 247

248 FLASH CARD 9.19 Which StringBuilder method overwrites characters in a string literal? ANSWER: replace() 248

249 ANSWER: Which method takes out an element from an ArrayList?
FLASH CARD 9.20 Which method takes out an element from an ArrayList? ANSWER: remove() 249

250 FLASH CARD 9.21 Given that this lambda must print Hi! and return a boolean, which parts in the following expression are optional? () -> { System.out.println("Hi!"); return true; } ANSWER: None. 250

251 FLASH CARD 9.22 What method do you call on an ArrayList to replace the element at a given index with a different one? ANSWER: set() 251

252 FLASH CARD 9.23 Which StringBuilder method adds to the end of a string literal? ANSWER: append() 252

253 FLASH CARD 9.24 Which method removes all elements from an ArrayList: deleteAll(), removeAll() or clear()? ANSWER: clear() 253

254 ANSWER: How can we add an hour to a LocalTime object? FLASH CARD 9.25
LocalTime.plusHours(1); 254

255 FLASH CARD 9.26 What common design pattern feature is shared by the LocalDate, LocalTime and LocalDateTime classes? ANSWER: All these datatypes are immutable. 255

256 ANSWER: What are components in a lambda expression? FLASH CARD 9.27
The formal parameters list, lambda token and lambda body. 256

257 FLASH CARD 9.28 Is it possible to use add() to insert an element at the head of an ArrayList? ANSWER: Yes, it is; just use the overloaded add(int index, E element). 257

258 FLASH CARD 9.29 In what cases would it be possible to call a no-arg constructor to create a LocalDate object ? ANSWER: It is never possible; in fact, the LDT classes have no public constructors at all. 258

259 ANSWER: What’s wrong with the following Kaplan Self-Test question?
FLASH CARD 9.30 What’s wrong with the following Kaplan Self-Test question? ANSWER: While the new LDT classes (available since 1.8) do live in the java.time package, the Date supertype is defined in java.util and extended by Time and another Date (both in java.sql). 259

260 FLASH CARD 9.31 What is the structural difference between an array and ArrayList object? ANSWER: Arrays are fixed in size while ArrayLists can grow as needed. 260

261 FLASH CARD 9.32 What expression can retrieve the first element in an ArrayList object named myList? ANSWER: myList.get(0) 261

262 FLASH CARD 9.33 What does the size() method of the java.lang.StringBuilder class represent? ANSWER: Nothing… because StringBuilder has no such method! 262

263 ANSWER: Which method assigns a value to an element in an ArrayList?
FLASH CARD 9.34 Which method assigns a value to an element in an ArrayList? ANSWER: set() 263

264 FLASH CARD 9.35 Which utility method in the standard Java library accepts varargs of datatype T and returns a List<T> object? ANSWER: Arrays.asList() 264

265 FLASH CARD 9.36 Which are immutable: String, StringBuilder, StringBuffer, arrays, or ArrayList? ANSWER: Only String. 265

266 FLASH CARD 9.37 Which of the following String methods can be freely chained between themselves: contains(), length(), or replace()? ANSWER: Only replace() because the other two return primitives (a boolean and int, respectively.) 266

267 FLASH CARD 9.38 Which of the following StringBuilder methods can be freely chained between themselves: append(), delete(), or insert()? ANSWER: Any one of them because all three return a StringBuilder object. 267

268 FLASH CARD 9.39 Which of the following StringBuilder methods can be freely chained between themselves: reverse(), substring(), or toString()? ANSWER: Only reverse() because the other two return a String object. 268

269 ANSWER: What is an advantage of StringBuilder over StringBuffer?
FLASH CARD 9.40 What is an advantage of StringBuilder over StringBuffer? ANSWER: StringBuilder is faster since it doesn’t have to be thread-safe. 269

270 ANSWER: How many objects are created by the following statement?
FLASH CARD 9.41 How many objects are created by the following statement? String[] str = new String[10]; ANSWER: Only one since the default value for reference type elements in an array is null. 270

271 NEXT: EXAM OBJECTIVE 9 EXAM OBJECTIVE 9 271


Download ppt "Oracle Java SE 8 Programmer I Certification"

Similar presentations


Ads by Google