Challenge - 5 Problems
Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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()); } }
Attempts:
2 left
💡 Hint
Check how the import statement allows access to classes in other packages.
✗ Incorrect
The class Helper is in package com.example.utils and is imported correctly in com.example.app.Main. Calling Helper.greet() prints the returned string.
❓ Predict Output
intermediateEffect 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.
Attempts:
2 left
💡 Hint
Think about what happens when no package is declared in a Java file.
✗ Incorrect
If no package declaration is present, the class is in the default package and runs normally.
🔧 Debug
advancedIdentify 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"); } }
Attempts:
2 left
💡 Hint
Check if class A can see class B without import.
✗ Incorrect
Class A is in package com.example and tries to use class B from com.example.other without importing it, causing a compilation error.
📝 Syntax
advancedCorrect package declaration syntax
Which of the following is the correct way to declare a package in a Java file?
Attempts:
2 left
💡 Hint
Remember the package declaration ends with a semicolon.
✗ Incorrect
The package declaration must end with a semicolon and not have parentheses or braces.
🚀 Application
expertNumber 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? } }
Attempts:
2 left
💡 Hint
Classes in the same package can be accessed without import.
✗ Incorrect
Classes A and B are in the same package alpha as Main, so they can be accessed directly. Class C is in beta and requires import.
