Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


What's true about package and import declarations in a Java file?

  1. Both can be placed anywhere in the file

  2. Both must be the first statements in the file

  3. The package declaration must be first, followed by imports

  4. Imports must be first, followed by the package declaration

The correct answer is: The package declaration must be first, followed by imports

In a Java file, the package declaration must be the first line of code, followed by any import declarations. This is because the package declaration determines which package the file belongs to, while the import declarations specify which external packages or classes are being used within the current file. Incorrect options include A, as both declarations must be placed at the top of the file, and D, as the package declaration must always come before any import declarations. Option B is also incorrect because while the package declaration must be first, there can be other lines of code before any import declarations. Option C is the only correct answer.