Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Practice Exam 1 Question 32, answer possibly wrong  RSS feed

 
Darren Wilsch
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, this is my first post!!

Shouldn't the answer be F (the code does not compile), not D? The classes Car and TestDrive are in different files since both start at line number 1. The TestDrive class does not compile because the Car class is not imported and the classes are not in the same package since nether file begins with a package statement. Am I correct on this?
question32.jpg
[Thumbnail for question32.jpg]
question32
 
Charles O'Leary
Ranch Hand
Posts: 167
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darren,

Welcome to the Ranch!

Personally, I've found https://www.compilejava.net/ to be quite useful when I find myself asking the same/similar question.  If you haven't used it already, I encourage you to give it a try as well. 

Sybex page 5: 
You can even put two classes in the same file. When you do so, at most one of the classes in the file is allowed to be public.



Charles,

 
Charles O'Leary
Ranch Hand
Posts: 167
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sybex page 12:
You can tell the code is in the default package, because there's no package name. On the exam, you'll see the default package used a lot to save space in code listings. In real life, always name your packages to avoid naming conflicts

 
Paul Anilprem
Enthuware Software Support
Ranch Foreman
Posts: 3984
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oracle has made it clear that you should assume appropriate import statements if no import statement is shown in the code listing.

Missing package and import statements: If sample code do not include package or import statements, and the question does not explicitly refer to these missing statements, then assume that all sample code is in the same package, or import statements exist to support them.

Having said that, why do you think, "the classes are not in the same package since nether file begins with a package statement"?
If neither file has a package statement, both the class will be in "default" package. No need to have an import statement to access these classes.
 
João Victor Gomes
Ranch Hand
Posts: 83
7
Eclipse IDE Java Postgres Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you had something like


Then you would need to worry about it.
In your example, even if each class was in a different package, you wouldn't be able to import and use the class Car, since the code wouldn't compile because the class Car has a default access modifier, so it wouldn't be accessible by a class of a different package.
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darren Wilsch wrote:Shouldn't the answer be F (the code does not compile), not D?

No!

Darren Wilsch wrote:The classes Car and TestDrive are in different files since both start at line number 1. The TestDrive class does not compile because the Car class is not imported and the classes are not in the same package since nether file begins with a package statement. Am I correct on this?

You are not correct! No package statement means that both classes are in the default package. And if classes reside in the same package you don't need an import statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!