Bird
Raised Fist0
Javaprogramming~20 mins

Creating packages in Java - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Java package usage
What is the output of the following Java code when run?
Java
package com.example.utils;

public class Helper {
    public static String greet() {
        return "Hello from Helper!";
    }
}

// In another file:
package com.example.app;

import com.example.utils.Helper;

public class Main {
    public static void main(String[] args) {
        System.out.println(Helper.greet());
    }
}
AHello from Helper!
BCompilation error: cannot find symbol Helper
CRuntime error: NoClassDefFoundError
DHello from Main!
Attempts:
2 left
💡 Hint
Check how the import statement allows access to classes in other packages.
❓ Predict Output
intermediate
2:00remaining
Effect of missing package declaration
What happens if you remove the package declaration from a Java file that is supposed to be in a package?
Java
package mypkg;

public class Test {
    public static void main(String[] args) {
        System.out.println("In package mypkg");
    }
}

// Now remove the line 'package mypkg;' and compile and run.
AThe program runs and prints 'In package mypkg'
BCompilation error: class not found in package
CRuntime error: ClassNotFoundException
DThe program runs but prints nothing
Attempts:
2 left
💡 Hint
Think about what happens when no package is declared in a Java file.
🔧 Debug
advanced
2:00remaining
Identify the cause of compilation error with packages
Why does the following code cause a compilation error?
Java
package com.example;

public class A {
    public static void main(String[] args) {
        B b = new B();
        b.sayHi();
    }
}

// In file B.java:
package com.example.other;

public class B {
    public void sayHi() {
        System.out.println("Hi from B");
    }
}
AClass B's method sayHi is static but called on instance
BClass B is not public
CClass A does not import com.example.other.B
DClass A and B must be in the same package
Attempts:
2 left
💡 Hint
Check if class A can see class B without import.
📝 Syntax
advanced
1:00remaining
Correct package declaration syntax
Which of the following is the correct way to declare a package in a Java file?
Apackage com.example.utils
Bpackage com.example.utils;
Cpackage com.example.utils()
Dpackage com.example.utils {}
Attempts:
2 left
💡 Hint
Remember the package declaration ends with a semicolon.
🚀 Application
expert
3:00remaining
Number of classes accessible without import
Given these two packages and classes, how many classes can class Main access directly without import statements?
Java
package alpha;
public class A {}
public class B {}

package beta;
public class C {}

package alpha;
public class Main {
    public static void main(String[] args) {
        // How many of A, B, C can be used here without import?
    }
}
A0 classes
B3 classes (A, B, and C)
C1 class (A only)
D2 classes (A and B)
Attempts:
2 left
💡 Hint
Classes in the same package can be accessed without import.

Practice

(1/5)
1. What is the main purpose of using packages in Java?
easy
A. To group related classes and organize code
B. To execute Java programs faster
C. To create graphical user interfaces
D. To store variables globally

Solution

  1. Step 1: Understand what packages do

    Packages help organize classes by grouping related ones together.
  2. Step 2: Identify the correct purpose

    Grouping related classes improves code organization and readability.
  3. Final Answer:

    To group related classes and organize code -> Option A
  4. Quick Check:

    Packages organize code = D [OK]
Hint: Packages group classes logically for better organization [OK]
Common Mistakes:
  • Thinking packages speed up program execution
  • Confusing packages with GUI creation
  • Assuming packages store variables globally
2. Which of the following is the correct way to declare a package named com.example.app in a Java file?
easy
A. package com_example_app;
B. Package com.example.app;
C. package com.example.app;
D. import com.example.app;

Solution

  1. Step 1: Check package declaration syntax

    The package statement must be lowercase and end with a semicolon.
  2. Step 2: Match the correct syntax

    package com.example.app; uses correct lowercase 'package' keyword and dot notation with semicolon.
  3. Final Answer:

    package com.example.app; -> Option C
  4. Quick Check:

    Correct package syntax = C [OK]
Hint: Use lowercase 'package' and end with semicolon [OK]
Common Mistakes:
  • Capitalizing 'Package' keyword
  • Using underscores instead of dots
  • Using 'import' instead of 'package'
3. Given the following Java file content, what will be the package of the class MyClass?
package org.test.project;

public class MyClass {
    public void display() {
        System.out.println("Hello from MyClass");
    }
}
medium
A. org.test.project
B. org.test
C. project
D. default package

Solution

  1. Step 1: Read the package declaration

    The first line declares the package as 'org.test.project'.
  2. Step 2: Identify the package of MyClass

    Since MyClass is declared after the package statement, it belongs to 'org.test.project'.
  3. Final Answer:

    org.test.project -> Option A
  4. Quick Check:

    Package line matches class package = B [OK]
Hint: Class package matches the declared package line [OK]
Common Mistakes:
  • Assuming partial package names
  • Thinking class is in default package without declaration
  • Confusing package with folder names
4. Identify the error in the following Java file related to package declaration:
package MyApp.Utilities;

public class Helper {
    // class code
}
medium
A. Missing semicolon after package statement
B. Package name should be lowercase
C. Class name should be lowercase
D. Package keyword should be capitalized

Solution

  1. Step 1: Check package naming conventions

    Package names should be all lowercase letters separated by dots.
  2. Step 2: Identify the error

    'MyApp.Utilities' uses uppercase letters, which is against convention.
  3. Final Answer:

    Package name should be lowercase -> Option B
  4. Quick Check:

    Package names lowercase = A [OK]
Hint: Package names must be lowercase letters only [OK]
Common Mistakes:
  • Using uppercase letters in package names
  • Forgetting semicolon after package
  • Capitalizing 'package' keyword
5. You want to create a package structure for a company named "TechCorp" with a project "Inventory" and a module "Reports". Which is the best package declaration for a Java class in the Reports module?
hard
A. package techcorp_inventory_reports;
B. package TechCorp.Inventory.Reports;
C. package tech_corp.inventory.reports;
D. package techcorp.inventory.reports;

Solution

  1. Step 1: Follow Java package naming conventions

    Package names should be lowercase and use dots to separate levels.
  2. Step 2: Evaluate options

    package techcorp.inventory.reports; uses all lowercase and dot notation correctly. Others use uppercase or underscores incorrectly.
  3. Final Answer:

    package techcorp.inventory.reports; -> Option D
  4. Quick Check:

    Lowercase with dots = A [OK]
Hint: Use lowercase names separated by dots for packages [OK]
Common Mistakes:
  • Using uppercase letters in package names
  • Using underscores instead of dots
  • Combining all names without dots
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard