nerdexam
Oracle

1Z0-819 · Question #15

Given: ``java public interface ExampleInterface { } `` Which two statements are valid to be written in this interface? (Choose two.)

The correct answer is A. public abstract void methodB(); D. public String methodD(). In Java, all interface methods are implicitly public abstract, so explicitly declaring them with those modifiers (option A) is perfectly legal. Option D is equally valid - omitting abstract (or public) is allowed because the compiler adds them automatically, making public…

Java Object-Oriented Approach

Question

Given:
public interface ExampleInterface {
}
Which two statements are valid to be written in this interface? (Choose two.)

Options

  • Apublic abstract void methodB();
  • Bfinal public void methodA(){System.out.println("G");}
  • Cpublic abstract void methodC();
  • Dpublic String methodD();
  • Epublic int x = 1;
  • Ffinal public void methodF(){System.out.println("F");}

How the community answered

(39 responses)
  • A
    74% (29)
  • B
    5% (2)
  • C
    3% (1)
  • E
    3% (1)
  • F
    15% (6)

Explanation

In Java, all interface methods are implicitly public abstract, so explicitly declaring them with those modifiers (option A) is perfectly legal. Option D is equally valid - omitting abstract (or public) is allowed because the compiler adds them automatically, making public String methodD(); a valid abstract method signature.

Options B and F are wrong because final methods are not permitted in interfaces. The entire purpose of an interface is to define a contract that implementing classes must fulfill, which requires overriding - final would prevent overriding, creating a direct contradiction. These also have method bodies without the default keyword, which only became legal in Java 8 for default and static methods (never final).

Option C (public abstract void methodC()) is structurally identical to A and is arguably also valid Java - this appears to be a flaw in the question, or C was intended to use protected which is illegal (interface methods must be public). Option E (public int x = 1) is actually valid Java since interface fields are implicitly public static final, but the exam likely intends it as a distractor expecting you to flag the missing final or static keywords.

Memory tip: Think of an interface as a promise poster - you can list abstract promises (void doThis();) and declare named constants, but you cannot hang a final sign (no final methods) because someone must always be able to fulfill each promise by overriding it.

Topics

#Interfaces#Method modifiers#Abstract methods#Syntax rules

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice