Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 13
Question ID: UKOCP14012
Given code:
- package com.udayankhattry.ocp;
- interface Writer {
- void print(Object obj);
- }
- public class Test {
- public static void main(String[] args) {
- Writer obj = System.out::print;
- obj.print("BE HONEST");
- }
- }
What is the result?
- A Compilation error
- B BE HONEST
- C An exception is thrown at runtime
- D None of the other options
Reveal correct answer
Correct answer: B
Explanation
UKOCP14012:
This is the case of "Reference to an Instance Method of a Particular Object".
Both print(Object) method and print(Object) method accepts same argument.
System.out::println means println method is called on System.out reference variable.
obj.print("BE HONEST"); invokes System.out.println("BE HONEST"); and this prints BE HONEST on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
