PASS - TAKER : Anonymous

Java SE 8 - OCA Strings, Arrays, ArrayLists, Dates, and Lambdas

Correct : 11

57% Complete (success)

57 %

False : 8

42% Complete (success)

42 %

Anonymous 2018-11-06T20:03:40


1. Given: {code} Which two substrings will be included in the result? (Choose two.)

The String operations are working on a new (lost) String not String s. The StringBuilder operations work from left to right.


2. Given: {code} And, if the code compiles, the command line: java Hilltop eyra vafi draumur kara What is the result?

The horses array's first four elements contain Strings, but the fifth is null, so the toUpperCase() invocation for the fifth element throws a NullPointerException.

Not Correct

3. Given: {code} What is the result?

The Unicode declaration must be enclosed in single quotes: '\u004e'. If this were done, the answer would be A, but that equality isn't on the OCA exam.


4. Given: {code} What is the result? (Choose all that apply.)

ClassCastException is thrown at line 7 because o1 refers to an int[] [], not an int[]. If line 7 were removed, the output would be 4.


5. Given: {code} What is the result?

ArrayList elements are automatically inserted in the order of entry; they are not automatically sorted. ArrayLists use zero-based indexes, and the last add() inserts a new element and shifts the remaining elements back.

Not Correct

6. Given: {code} Which two are true about the objects created within main(), and which are eligible for garbage collection when line 14 is reached?

da refers to an object of type "Dozens array," and each Dozens object that is created comes with its own "int array" object. When line 14 is reached, only the second Dozens object (and its "int array" object) are not reachable.


7. Given: {code} What is the result?

A two-dimensional array is an "array of arrays." The length of ba is 2 because it contains 2 one-dimensional arrays. Array indexes are zero-based, so ba[1] refers to ba's second array.


8. Given: {code} Which are true? (Choose all that apply.)

Although String objects are immutable, references to Strings are mutable. The code s1 += "d"; creates a new String object. StringBuilder objects are mutable, so the append() is changing the single StringBuilder object to which both StringBuilder references refer.


9. Given: {code} If the garbage collector does NOT run while this code is executing, approximately how many objects will exist in memory when the loop is done?

StringBuilders are mutable, so all of the append() invocations are acting on the same StringBuilder object over and over. Strings, however, are immutable, so every String concatenation operation results in a new String object. Also, the string " " is created once and reused in every loop iteration.

Not Correct

10. Given: {code} What is the result?

Although main()'s b1 is a different reference variable than go()'s b1, they refer to the same Box object.

Not Correct

11. Given: {code} What is the result?

The substring() invocation uses a zero-based index and the second argument is exclusive, so the character at index 8 is NOT included. The toUpperCase() invocation makes a new String object that is instantly lost. The toUpperCase() invocation does NOT affect the String referred to by s.

Not Correct

12. Given: {code} Which lines of code (if any) break encapsulation? (Choose all that apply.)

When encapsulating a mutable object like an ArrayList, your getter must return a reference to a copy of the object, not just the reference to the original object.

Not Correct

13. Given: {code} What is the result?

Predicate lambdas take exactly one parameter; the rest of the code is correct.


14. Given: {code} What is the result?

Invoking the plusDays() method creates a new object, and both LocalDate and DateTimeFormatter have format() methods.


15. Given: {code} What is the result?

The only error in this code is attempting to add a String to an ArrayList of Integer wrapper objects. Line 7 uses autoboxing, and lines 6 and 9 demonstrate using a wrapper class's two constructors.

Not Correct

16. Given that adder() returns an int, which are valid Predicate lambdas? (Choose all that apply.)

A passes two parameters. C, a return, must be in a code block, and code blocks must be in curly braces. D, a block, must have a return statement. G, the result, is not a boolean.


17. Given: {code} What is the result?

ArrayLists can have duplicate entries.

Not Correct

18. Given: {code} Which are true? (Choose all that apply.)

A total of four LocalDate objects are created, but the one created using the of() method is abandoned on the next line of code when its reference variable is assigned to the new LocalDate object created via the first plusDays() invocation. The reference variables are swapped a bit, which accounts for the dates not printing in chronological order.


19. Given that e refers to an object that implements Predicate, which could be valid code snippets or statements? (Choose all that apply.)

A, C, D, E, F, and G are correct; they all require a boolean. B is incorrect. A switch doesn't take a boolean.