Challenge - 5 Problems
Public Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2:00remaining
Output of public method access from another class
What is the output of this Java program?
Java
class A { public void greet() { System.out.println("Hello from A"); } } public class B { public static void main(String[] args) { A obj = new A(); obj.greet(); } }
Attempts:
2 left
💻 code output
intermediate2:00remaining
Accessing public variable from another package
Given two packages pkg1 and pkg2, what is the output of the following code?
Java
package pkg1; public class Data { public int number = 42; } package pkg2; import pkg1.Data; public class Test { public static void main(String[] args) { Data d = new Data(); System.out.println(d.number); } }
Attempts:
2 left
🔧 debug
advanced2:30remaining
Why does this public method cause a compilation error?
Consider the following code. Why does it cause a compilation error?
Java
class Parent { public void show() { System.out.println("Parent show"); } } public class Child extends Parent { void show() { System.out.println("Child show"); } }
Attempts:
2 left
📝 syntax
advanced2:00remaining
Identify the syntax error related to public modifier
Which option contains a syntax error related to the use of the public modifier?
Attempts:
2 left
🚀 application
expert3:00remaining
Effect of public modifier on class accessibility
Given the following code in two files, what will happen when compiling and running?
// File: pkg1/PublicClass.java
package pkg1;
public class PublicClass {
public void display() {
System.out.println("Visible everywhere");
}
}
// File: pkg2/DefaultClass.java
package pkg2;
class DefaultClass {
public void show() {
System.out.println("Visible only in pkg2");
}
}
// File: pkg2/TestAccess.java
package pkg2;
import pkg1.PublicClass;
public class TestAccess {
public static void main(String[] args) {
PublicClass pc = new PublicClass();
pc.display();
DefaultClass dc = new DefaultClass();
dc.show();
}
}Attempts:
2 left
