nerdexam
Oracle

1Z0-819 · Question #135

Given: class Mycar { } and javac C:\workspace4\Mycar.java What is the expected result of javac?

The correct answer is B. javac compiles Mycar.java without errors or warnings. Option B is correct because Mycar is syntactically valid Java - a class declaration with no body requires nothing else to compile successfully, and javac only checks syntax and types, not whether a main method exists. Why the distractors are wrong: A is wrong because Mycar…

Java Object-Oriented Approach

Question

Given: class Mycar { } and javac C:\workspace4\Mycar.java What is the expected result of javac?

Options

  • Ajavac fails to compile the class and prints the error message, C:\workspace4\Mycar.java:1: error: package java does not exist
  • Bjavac compiles Mycar.java without errors or warnings.
  • Cjavac fails to compile the class and prints the error message, C:\workspace4\Mycar.java:1: error: expected import java.lang
  • Djavac fails to compile the class and prints the error message, Error: Could not find or load main class Mycar.class

How the community answered

(47 responses)
  • A
    6% (3)
  • B
    74% (35)
  • C
    15% (7)
  • D
    4% (2)

Explanation

Option B is correct because Mycar is syntactically valid Java - a class declaration with no body requires nothing else to compile successfully, and javac only checks syntax and types, not whether a main method exists.

Why the distractors are wrong:

  • A is wrong because Mycar never attempts to import or use the java package, so no "package does not exist" error could arise.
  • C is wrong because java.lang is automatically imported in every Java file - you never need to write import java.lang, and the compiler would never demand it.
  • D is wrong because "Could not find or load main class" is a runtime error from the java launcher, not javac. Compilation succeeds without a main method; the error only appears if you later try to run the class with java Mycar.

Memory tip: Think of javac as a grammar checker - it only cares about syntax and types. The java runner is the one that needs a main method to know where to start execution. Confusing compile-time vs. runtime errors is exactly what distractors C and D are designed to exploit.

Topics

#java.lang automatic import#javac compilation#class declaration#compilation vs runtime

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice