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…
Question
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)- A6% (3)
- B74% (35)
- C15% (7)
- D4% (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
Mycarnever attempts to import or use thejavapackage, so no "package does not exist" error could arise. - C is wrong because
java.langis automatically imported in every Java file - you never need to writeimport 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 thejavalauncher, notjavac. Compilation succeeds without amainmethod; the error only appears if you later try to run the class withjava 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
Community Discussion
No community discussion yet for this question.