This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Practice Exam 1 Question 32, answer possibly wrong  RSS feed

 
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
 
Ranch Hand
Posts: 225
5
  • 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: 225
5
  • 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


 
Enthuware Software Support
Rancher
Posts: 4210
35
  • 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.
 
Ranch Hand
Posts: 116
11
Eclipse IDE Hibernate Java Netbeans IDE Postgres Database Tomcat Server
  • 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.
 
Sheriff
Posts: 11338
177
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!