Challenge - 5 Problems
Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2: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()); } }
Attempts:
2 left
💻 code output
intermediate2: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.
Attempts:
2 left
🔧 debug
advanced2: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"); } }
Attempts:
2 left
📝 syntax
advanced1:00remaining
Correct package declaration syntax
Which of the following is the correct way to declare a package in a Java file?
Attempts:
2 left
🚀 application
expert3: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? } }
Attempts:
2 left
