nerdexam
Oracle

1Z0-819 · Question #168

public interface EulerInterface { double getEulerValue(); } public class EulerLambda { public static void main(String[] args) { EulerInterface myEulerInterface = () -> 2.71828…

The correct answer is C. The code does not compile. Option C is correct because Java only permits one public type per .java source file. As written, both EulerInterface and EulerLambda are declared public - placing them in the same file violates this rule and the compiler rejects it immediately. Why the distractors are wrong: B…

Working with Streams and Lambda Expressions

Question

public interface EulerInterface { double getEulerValue(); } public class EulerLambda { public static void main(String[] args) { EulerInterface myEulerInterface = () -> 2.71828; System.out.println("Value of Euler = " + myEulerInterface.getEulerValue()); } } What is the result?

Options

  • AIt throws a runtime exception.
  • BValue of Euler = 2.71828
  • CThe code does not compile.
  • DValue of Euler = "2.71828"

How the community answered

(20 responses)
  • A
    10% (2)
  • B
    5% (1)
  • C
    80% (16)
  • D
    5% (1)

Explanation

Option C is correct because Java only permits one public type per .java source file. As written, both EulerInterface and EulerLambda are declared public - placing them in the same file violates this rule and the compiler rejects it immediately.

Why the distractors are wrong:

  • B is what would print if the code were fixed (e.g., split into two files or one declaration made package-private) - the lambda syntax itself is valid since EulerInterface qualifies as a functional interface (one abstract method), so () -> 2.71828 is a legal implementation.
  • A is wrong because the error is a compile-time constraint, never reaching the JVM where a runtime exception could be thrown.
  • D is wrong on two counts: double values don't print with quotes, and again, the code never runs.

Memory tip: When you see multiple public type declarations shown together in a single code block, always check the "one public type per file" rule before analyzing logic - it's a frequent gotcha on Java certification exams.

Topics

#lambda expressions#functional interfaces#compilation rules#type inference

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice