0
0
Javaprogramming~15 mins

Public access modifier in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Public Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2: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();
    }
}
ANo output
BCompilation error: greet() is not accessible
CRuntime error: NullPointerException
DHello from A
Attempts:
2 left
💻 code output
intermediate
2: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);
    }
}
ACompilation error: number is not accessible
B42
CRuntime error: ClassNotFoundException
D0
Attempts:
2 left
🔧 debug
advanced
2: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");
    }
}
AChild's show() has weaker access than Parent's public show(), causing a compilation error
BChild's show() method is missing the @Override annotation
CParent's show() method should be protected, not public
DNo compilation error; code runs fine
Attempts:
2 left
📝 syntax
advanced
2:00remaining
Identify the syntax error related to public modifier

Which option contains a syntax error related to the use of the public modifier?

Apublic class Example { public void method() {} }
Bclass Example { public int x; private void method() {} }
Cpublic void method() { public int x = 5; }
Dpublic interface Sample { void run(); }
Attempts:
2 left
🚀 application
expert
3: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();
    }
}
AProgram runs and prints both messages
BCompilation error: Cannot access DefaultClass from another package
CCompilation error: DefaultClass is not public and cannot be accessed from TestAccess
DRuntime error: IllegalAccessException
Attempts:
2 left