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

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

Want to excel in Java? Test your knowledge with our ultimate quiz based on 'Thinking in Java'. Engage with multiple-choice questions and in-depth explanations. Boost your understanding and prepare for your exam with confidence.

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


How does Java resolve a name clash between two imported packages?

  1. It automatically selects one

  2. It throws a runtime exception

  3. The programmer must specify which class to use

  4. It merges the two classes

The correct answer is: The programmer must specify which class to use

Name clash occurs when there are two classes with the same name in two different packages. When this happens, Java allows us to import both packages and use the classes within them. However, if we try to use a method or variable within the class without specifying which class it belongs to, Java will not know which class to use and this results in a compilation error. So, in order to resolve this ambiguity, the programmer has to explicitly specify which class they want to use in their code. This can be done by specifying the package name followed by the class name, separated by a dot. For example, "package1.ClassA" or "package2.ClassA". Therefore, option C is the correct answer. Options A, B, and D are incorrect as Java does not have any default behavior for resolving name clashes and will always require the programmer to specify the class to use.