Any2Quiz
Quizzes / ICSE / Class 11 / Computer Science

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.

This ICSE Class 11 Computer Science practice quiz has 47 multiple-choice questions on Packages – user-defined packages, imports, interaction, and 47 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.

Start quiz: 47 questions, free

Sample questions

  1. Which of the following is NOT a benefit of using user-defined packages in Java?

    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.

  2. What is the consequence of omitting the `package` declaration in a Java source file that contains a class definition?

    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.

  3. Which of the following scenarios demonstrates proper package interaction for code reusability?

    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.

  4. 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`?

    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.

  5. 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?

    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.

Take the full quiz

More questions in this quiz

Answers and explanations are in the full quiz.

  1. You have a package `geometry` with a class `Circle` in `com.shapes.geometry`. You want to use the `Circle` class in a file within `com.myapp.calculations`. If the `geometry` package is correctly placed in the classpath, which import statement is required in `com.myapp.calculations`?
  2. What does the `package` declaration at the beginning of a Java file signify?
  3. Consider a scenario where you have a package `utilities` with a class `Formatter`. You also have another package `formatters` with a class `DetailedFormatter` that extends `Formatter`. If you are in a class within `com.example.app` and you want to use both `Formatter` and `DetailedFormatter`, which is the most appropriate set of import statements?
  4. What is the primary purpose of the `java.lang` package in Java?
  5. If a class `MyClass` is declared in `com.example.core` and is made `public`, how can it be accessed from a class in `com.example.app` without any import statements in `com.example.app`?
  6. Consider a package `util` containing `DateHelper.java` and `StringUtil.java`. If you have a file in the same package and want to use the `formatDate` method from `DateHelper`, what is the correct import statement?
  7. Which keyword is used to create a package in Java?
  8. You have a package `com.app.utilities` with a class `Helper`. You also have a class `MyUtil` in the same package. If you want to use `Helper`'s methods in `MyUtil`, which of these is the correct approach?
  9. Consider a package structure: `com.company.services` containing `ApiService.java`. Another package `com.company.client` contains `ApiClient.java`. `ApiClient` needs to use `ApiService`. What is the correct statement for `ApiClient.java` to import `ApiService`?
  10. When using a wildcard import like `import com.example.utils.*;`, which of the following is true?
  11. You have a package `org.data.models` containing `User.java` and `Product.java`. You want to use both these classes in a file within the `org.app.ui` package. Which import statement is the most concise and correct?
  12. What is the purpose of the `import static` declaration in Java?
  13. Consider the package structure `com.mycompany.core` with a class `DataProcessor`. If you need to use `DataProcessor` in a file in the `com.mycompany.reporting` package, and you are compiling from the root directory of `com`, what is the correct way to specify the classpath for the compiler?
  14. Which access modifier is implicitly used for members of a class if no access modifier is specified?
  15. You have a package `com.example.core` with a class `ConfigLoader`. You want to use this class in `com.example.ui.MainApplication`. If `ConfigLoader` is declared as `public`, what is the correct way to access it in `MainApplication`?
  16. What happens if you try to import a non-existent package or class in Java?
  17. If a class `Utility` is in package `com.tools.helpers` and is declared as `public`, what is the correct way to refer to it in a file that has `package com.application.main;`?
  18. What is the difference between `import com.example.utils.StringUtils;` and `import com.example.utils.*;`?
  19. Which of the following is a valid declaration for a class named `MathUtils` within the package `com.numeric`?
  20. What is the purpose of the `final` keyword when applied to a package in Java?
  21. Consider a class `Configuration` in package `org.config`. To use this class in another class `AppRunner` in package `org.app`, with `AppRunner` needing to call a public static method `getProperty(String key)` from `Configuration`, which import statement is most appropriate in `AppRunner.java`?
  22. If you have a class `Validator` in `com.validation` and you want to use it in `com.app.process`, and you also have another class `Logger` in `com.app.utils`, what is the correct combination of import statements for a class in `com.app.process` to use both `Validator` and `Logger`?
  23. What is the default access level of a class if no access modifier is specified?
  24. Suppose you have a package `data_access` with a class `DatabaseConnector`. You want to use this class in a file within package `business_logic`. If `DatabaseConnector` has a constructor `DatabaseConnector(String url)` and a method `executeQuery(String query)`, how would you instantiate and use `DatabaseConnector` in `business_logic`?
  25. What is the primary mechanism by which Java's package system prevents naming conflicts between classes from different libraries or projects?
  26. What is the relationship between a package and a JAR file in Java?
  27. What does the `protected` access modifier allow?
  28. Which of the following best describes the purpose of the `import` statement in Java?
  29. Consider a package `com.utils` with a class `StringFormatter`. If you have a file in the same package, what is the correct way to use `StringFormatter`?
  30. What is the purpose of the `package` keyword in Java?
  31. If a class `MyClass` is in `com.example.core` and a class `AnotherClass` is in `com.example.app`, and `AnotherClass` needs to use `MyClass`, which of the following is NOT a valid way to achieve this?
  32. What is the scope of a class declared with package-private access?
  33. If you have a package `org.data` with a class `Record` and a subpackage `org.data.csv` with a class `CsvReader`, and you are in a file with `package org.app;`, which import statement will allow you to use both `Record` and `CsvReader`?
  34. What is the fundamental reason for using `import static`?
  35. Consider a class `DataParser` in `com.parser.data`. If you try to use it in a file in `com.importer` without any import statement, how must you refer to `DataParser`?
  36. Which statement about `java.lang` package is correct?
  37. If a class `A` in package `pkg1` needs to access a `public` method `m()` of class `B` in package `pkg2`, what must be done in class `A`?
  38. What is the primary advantage of using packages for code organization?
  39. If `com.data.models` contains `User.java` and `com.data.services` contains `UserService.java`, and `UserService` needs to create a `User` object, what is the most efficient import statement for `UserService.java`?
  40. Which of the following is a correct way to make a class `MyService` in `com.services` accessible from `com.app` without using an `import` statement?
  41. What does a `package` declaration at the beginning of a Java file indicate about the location of that file within the project's directory structure?
  42. If `com.util.strings` contains `StringUtil` and `com.util.numbers` contains `NumberUtil`, and you need to use both in `com.app.main`, which is the most common and readable way to import them?

More quizzes

Browse all Computer Science quizzes

More to play

Daily ChallengeA new quiz every day. Keep your streak.HangmanGuess the word, letter by letter.Story QuestLearn through short interactive stories.All quizzesCBSE, ICSE, competitive exams and more.