PASS - TAKER : Anonymos

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

Correct : 17

89% Complete (success)

89 %

False : 2

10% Complete (success)

10 %



Anonymos 2018-10-26T01:05:05

Correct

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.

Correct

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.

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.

Not Correct

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.

Correct

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.

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.

Correct

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.

Correct

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.

Correct

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.

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.

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.

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.

Correct

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

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

Correct

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.

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.

Correct

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

ArrayLists can have duplicate entries.

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.

Correct

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.