Packages – user-defined packages, imports, interaction Quiz - ICSE Class 11 Computer Science
Free practice quiz: Packages – user-defined packages, imports, interaction (ICSE Class 11 Computer Science). 47 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 47 questions freeSample questions
Which of the following is NOT a benefit of using user-defined packages in Java?
- Namespace management, preventing naming conflicts
- Code organization and modularity, improving maintainability
- Encapsulation of related classes and interfaces
- Automatic optimization of program execution speed
Show answer
Answer: D. Automatic optimization of program execution speed
User-defined packages help in organizing code, preventing naming conflicts, and providing encapsulation. However, packages themselves do not directly optimize program execution speed. Optimization is typically handled by the Java compiler (JVM) and bytecode optimization techniques.
What is the consequence of omitting the `package` declaration in a Java source file that contains a class definition?
- The class will be part of the default package, and can only be accessed by other classes in the default package.
- The Java compiler will automatically assign a package name based on the file's directory.
- The code will not compile.
- The class will be public by default.
Show answer
Answer: A. The class will be part of the default package, and can only be accessed by other classes in the default package.
If a Java source file does not have a `package` declaration, the class defined in it belongs to the default package. Classes in the default package can only be accessed by other classes in the default package. They cannot be imported into or accessed from named packages. This is generally discouraged for larger projects.
Which of the following scenarios demonstrates proper package interaction for code reusability?
- Creating a utility class in the `main` package and importing it into a library package.
- Placing all project classes in the default package and importing them as needed.
- Developing a core set of functionalities in a `com.library.core` package and importing specific classes from it into various application packages.
- Copy-pasting code from one package to another to avoid import statements.
Show answer
Answer: C. Developing a core set of functionalities in a `com.library.core` package and importing specific classes from it into various application packages.
The scenario described in option 3 exemplifies proper package interaction for code reusability. By creating a dedicated package (`com.library.core`) for reusable functionalities and then importing specific classes from it into different applications, developers promote modularity and avoid code duplication. The other options represent poor practices: placing everything in `main` or the default package leads to unmanageable code, and copy-pasting is the antithesis of reusability.
Consider a file `src/com/company/utils/StringUtils.java` containing a public class `StringUtils` with a public static method `isEmpty(String s)`. If you are in `src/com/my_app/Main.java`, which is the correct way to call `isEmpty`?
- import com.company.utils.StringUtils; System.out.println(StringUtils.isEmpty("hello"));
- import com.company.utils.StringUtils; System.out.println(isEmpty("hello"));
- import static com.company.utils.StringUtils.isEmpty; System.out.println(isEmpty("hello"));
- Option 1 and Option 3 are correct.
Show answer
Answer: D. Option 1 and Option 3 are correct.
Option 1 correctly imports the `StringUtils` class, allowing access to its static method using the class name prefix: `StringUtils.isEmpty()`. Option 3 uses `import static` to import the `isEmpty` method directly, allowing it to be called without the class name prefix. Option 2 is incorrect because it imports the class but then tries to call the method as if it were a globally available function, which is not how Java works.
Consider `com.example.core` containing `Config.java` and `com.example.core.defaults` containing `DefaultConfig.java`. A file in `com.example.app` needs `Config`. What is the correct import statement?
- import com.example.core.defaults.DefaultConfig;
- import com.example.core.*;
- import com.example.core.Config;
- Both option 2 and option 3 are correct.
Show answer
Answer: D. Both option 2 and option 3 are correct.
Option 3 imports the specific `Config` class from `com.example.core`. Option 2 imports all top-level classes from `com.example.core`, which includes `Config`. Option 1 imports a class from a subpackage, which is irrelevant for accessing `Config` itself.
More quizzes
- Propositional Logic – WFF, truth tables, logic gates, adder circuits
- Software – System vs application software, OS overview
- Computer Hardware – System architecture, processor, memory types
- Elementary Data Structures – stacks, queues, dequeue
- Numbers – Number systems, base conversion, arithmetic in bases