Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 76
Question ID: UKOCP64791
Consider below code of Test.java file:
- package com.udayankhattry.ocp;
- public class Test {
- public static void main(String[] args) {
- var res = ""; //Line n1
- String [] arr = {"Dog", null, "Friendly"};
- for(String s : arr) { //Line n2
- res += String.join("-", s); //Line n3
- }
- System.out.println(res); //Line n4
- }
- }
What will be the result of compiling and executing Test class?
- A DogFriendly
- B Dog-Friendly
- C DognullFriendly
- D Dog-null-Friendly
- E An exception is thrown at runtime
- F Compilation error
Reveal correct answer
Correct answer: C
Explanation
UKOCP64791:
Local variable Type inference was added in JDK 10.
Reserved type name var is allowed in JDK 10 onwards for local variable declarations with initializers, enhanced for-loop indexes, and index variables declared in traditional for loops. For example,
var x = "Java"; //x infers to String
var m = 10; //m infers to int
The identifier var is not a keyword, hence var can still be used as variable name, method name or package name but it cannot be used as a class or interface name.
Given code compiles successfully.
At Line n1, variable 'res' infers to String.
Static overloaded method join(...) was added in JDK 1.8 and has below declarations:
1. public static String join(CharSequence delimiter, CharSequence... elements) {...}: It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
For example,
String.join(".", "A", "B", "C"); returns "A.B.C"
String.join("+", new String[]{"1", "2", "3"}); returns "1+2+3"
String.join("-", "HELLO"); returns "HELLO"
If delimiter is null or elements refer to null, then NullPointerException is thrown. e.g.,
String.join(null, "A", "B"); throws NullPointerException
String [] arr = null; String.join("-", arr); throws NullPointerException
But if single element is null, then "null" is considered. e.g.,
String str = null; String.join("-", str); returns "null"
String.join("::", new String[] {"James", null, "Gosling"}); returns "James::null::Gosling"
2. public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {...}: It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
For example,
String.join(".", List.of("A", "B", "C")); returns "A.B.C"
String.join(".", List.of("HELLO")); returns "HELLO"
If delimiter is null or elements refer to null, then NullPointerException is thrown. e.g.,
String.join(null, List.of("HELLO")); throws NullPointerException
List<String> list = null; String.join("-", list); throws NullPointerException
But if single element is null, then "null" is considered. e.g.,
List<String> list = new ArrayList<>(); list.add("A"); list.add(null); String.join("::", list); returns "A::null"
Please note: String.join("-", null); causes compilation error as compiler is unable to tag this call to specific join(...) method. It is an ambiguous call.
Let's check the iterations:
1st iteration: s refers to "Dog". String.join(".", s) returns "Dog" and res = "" + "Dog" = "Dog".
2nd iteration: s refers to null. String.join(".", s) returns "null" and res = "Dog" + "null" = "Dognull".
3rd iteration: s refers to "Friendly". String.join(".", s) returns "Friendly" and res = "Dognull" + "Friendly" = "DognullFriendly".
Loop finishes its execution and Line n4 prints DognullFriendly on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
