Bird
Raised Fist0
Javaprogramming~20 mins

Public access modifier in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Public Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict 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
💡 Hint

Remember that public methods can be accessed from any other class.

❓ Predict 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
💡 Hint

Public variables can be accessed from any package if the class is imported.

🔧 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
💡 Hint

Check the access levels of overridden methods.

📝 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
💡 Hint

Check where public can be used inside methods.

🚀 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
💡 Hint

Check the package and access levels of classes and their usage.

Practice

(1/5)
1. What does the public access modifier mean in Java?
easy
A. The class, method, or variable is accessible from anywhere in the program.
B. The class, method, or variable is accessible only within its own class.
C. The class, method, or variable is accessible only within its own package.
D. The class, method, or variable is accessible only to subclasses.

Solution

  1. Step 1: Understand the meaning of public

    The public modifier allows access from any other class in any package.
  2. Step 2: Compare with other access levels

    Other modifiers like private or protected restrict access, but public is fully open.
  3. Final Answer:

    The class, method, or variable is accessible from anywhere in the program. -> Option A
  4. Quick Check:

    Public means open access [OK]
Hint: Public means accessible from anywhere in the program [OK]
Common Mistakes:
  • Confusing public with private or protected
  • Thinking public restricts access to package only
  • Assuming public means only subclass access
2. Which of the following is the correct way to declare a public method in Java?
easy
A. void public myMethod() {}
B. public void myMethod() {}
C. private public void myMethod() {}
D. method public void myMethod() {}

Solution

  1. Step 1: Recall Java method declaration syntax

    The access modifier comes first, then the return type, then the method name and parentheses.
  2. Step 2: Check each option

    public void myMethod() {} follows correct syntax: public void myMethod() {}. Others have wrong order or invalid keywords.
  3. Final Answer:

    public void myMethod() {} -> Option B
  4. Quick Check:

    Access modifier first, then return type [OK]
Hint: Access modifier always comes before return type [OK]
Common Mistakes:
  • Placing 'public' after 'void'
  • Using invalid keywords like 'method'
  • Combining multiple access modifiers incorrectly
3. What will be the output of the following Java code?
public class Test {
    public int number = 5;
    public static void main(String[] args) {
        Test obj = new Test();
        System.out.println(obj.number);
    }
}
medium
A. 5
B. 0
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand the public variable access

    The variable number is public and initialized to 5, so it can be accessed from main.
  2. Step 2: Trace the program output

    The main method creates an object and prints obj.number, which is 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Public variable accessed correctly prints 5 [OK]
Hint: Public variables can be accessed directly from objects [OK]
Common Mistakes:
  • Assuming default value 0 instead of initialized 5
  • Thinking public causes errors
  • Confusing static and instance variables
4. Identify the error in the following code snippet:
class Example {
    public int value;
}

public class Main {
    public static void main(String[] args) {
        Example ex = new Example();
        ex.value = 10;
        System.out.println(ex.value);
    }
}
medium
A. Class Example must be declared public to access its public variable.
B. Main class must not be public.
C. No error; code will compile and print 10.
D. Variable value must be private to be accessed.

Solution

  1. Step 1: Check class and variable access

    Class Example is package-private but accessible within the same package. Its public variable value can be accessed.
  2. Step 2: Verify main method usage

    Main creates Example object, assigns 10 to value, and prints it. This is valid.
  3. Final Answer:

    No error; code will compile and print 10. -> Option C
  4. Quick Check:

    Public variable accessible within same package [OK]
Hint: Public variables accessible within same package without public class [OK]
Common Mistakes:
  • Thinking class must be public to access public members
  • Confusing variable access with class access
  • Assuming private needed for access
5. You want to create a Java class with a public method that can be called from any other class, but the class itself should only be accessible within its package. Which of the following is correct?
hard
A. Declare both the class and method as public.
B. Declare the class as private and the method as public.
C. Declare the class as public and the method as private.
D. Declare the class without public and the method as public.

Solution

  1. Step 1: Understand class access modifiers

    Classes without public are package-private, so accessible only within the package.
  2. Step 2: Understand method access modifiers

    Methods declared public inside a package-private class are accessible from other classes in the same package.
  3. Step 3: Analyze options

    Declare the class without public and the method as public. matches the requirement: class package-private, method public. Declare both the class and method as public. makes class public (accessible everywhere). Declare the class as public and the method as private. hides method. Declare the class as private and the method as public. is invalid because classes cannot be private.
  4. Final Answer:

    Declare the class without public and the method as public. -> Option D
  5. Quick Check:

    Class package-private + public method = accessible in package [OK]
Hint: Class without public is package-private; method public means accessible inside package [OK]
Common Mistakes:
  • Thinking class can be private
  • Assuming public class needed for public method
  • Confusing method access with class access
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