0
0
Javaprogramming~15 mins

Public access modifier in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What does the public access modifier mean in Java?
The <code>public</code> access modifier means the class, method, or variable can be accessed from any other class in any package.
touch_appClick to reveal answer
beginner
Can a public method be accessed from a different package?
Yes, a public method can be accessed from any other package in the Java program.
touch_appClick to reveal answer
intermediate
Which of these is true about public classes in Java?
A <code>public</code> class can be accessed from any other class in any package, and the file name must match the public class name.
touch_appClick to reveal answer
beginner
Example: What is the access level of this method?
public void greet() { System.out.println("Hello"); }
The method <code>greet()</code> is <code>public</code>, so it can be called from any other class anywhere in the program.
touch_appClick to reveal answer
intermediate
What happens if you do not specify any access modifier in Java?
If no access modifier is specified, the default is package-private, meaning the member is accessible only within the same package, unlike public which allows access from anywhere.
touch_appClick to reveal answer
What does the public access modifier allow?
AAccess from any other class in any package
BAccess only within the same class
CAccess only within the same package
DAccess only to subclasses
If a class is declared public, where can it be accessed?
AFrom any package
BOnly inside its own class
COnly inside its own package
DOnly by subclasses
What must match the file name in Java?
AAny class name
BThe <code>public</code> class name
CThe package name
DThe method name
Which access modifier restricts access to the same package only?
Apublic
Bprivate
Cprotected
Ddefault (no modifier)
Can a public method be accessed from a class in a different package?
AOnly if subclassed
BNo
CYes
DOnly if imported
Explain what the public access modifier does in Java and give an example.
Describe the difference between public and default (no modifier) access in Java.