Fiveable
Fiveable

public

Definition

Public is an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.

Analogy

Think of public access as a public park. Just like how anyone can enter and use the facilities in a public park, public members can be accessed and used by any part of the program.

Related terms

Private: Private is an access modifier that restricts visibility to only within the same class. Private members cannot be accessed from outside the class.

Protected: Protected is an access modifier that allows visibility within the same package or subclasses. Protected members are not accessible from unrelated classes.

Default (package-private): Default, also known as package-private, is an access level without any keyword specified. It allows visibility within the same package but not outside of it.

"public" appears in:

Subjects (1)

Practice Questions (19)

  • Consider the following method: public void printFibonacci(int n) { int a = 0; int b = 1; System.out.print(a + " " + b + " "); for (int i = 2; i < n; i++) { int sum = a + b; System.out.print(sum + " "); a = b; b = sum; } } Assume that the method printFibonacci(6) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printEvenNumbers(int n) { for (int i = 2; i <= n; i += 2) { System.out.print(i + " "); } } Assume that the method printEvenNumbers(8) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printPowersOfTwo(int n) { for (int i = 0; i <= n; i++) { int power = (int) Math.pow(2, i); System.out.print(power + " "); } } Assume that the method printPowersOfTwo(5) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printReverse(String str) { for (int i = str.length() - 1; i >= 0; i--) { System.out.print(str.charAt(i)); } } Assume that the method printReverse("Hello") appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printVowels(String str) { str = str.toLowerCase(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { System.out.print(ch + " "); } } } Assume that the method printVowels("OpenAI") appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printFactors(int n) { for (int i = 1; i <= n; i++) { if (n % i == 0) { System.out.print(i + " "); } } } Assume that the method printFactors(24) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printPrimeNumbers(int n) { for (int i = 2; i <= n; i++) { boolean isPrime = true; for (int j = 2; j <= Math.sqrt(i); j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { System.out.print(i + " "); } } } Assume that the method printPrimeNumbers(20) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void calculateSquareRoot(double n) { double squareRoot = Math.sqrt(n); printSquareRoot(n, squareRoot); } public void printSquareRoot(double n, double squareRoot) { System.out.print("Square root of " + n + " = " + squareRoot); } Assume that the method calculateSquareRoot(16) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method. public boolean isValidEmail(String email) { /* implementation not shown */ } Which of the following lines of code, if located in a method in the same class as isValidEmail, will compile without an error?
  • What happens when we try to run the following? class D { public D(int x) { } } class E extends D { public E() { } } public class C { public static void main(String[] args) { E e = new E(); } }
  • What happens when we try to run the following? class J { public J(String x) { } } class K { public K() { } } public class L { public static void main(String[] args) { K k = new K(); } }
  • Given the block of code below, answer the following question: ```java class Animal { // constructor not shown } class Dog extends Animal { // constructor not shown public void makeSound() { System.out.println("Woof! Woof!"); } } ``` Question: Consider the code above. If we create an object of the `Dog` class and call the `makeSound()` method on that object, what will be outputted?
  • How can we fix the following class so it compiles? public class Weather { private int currentTemp; public Weather(int temp) { currentTemp = temp; } public void resetTemp() { currentTemp = newTemp; } }
  • Why does the following class does not compile? public class Weather { private int currentTemp; public Weather(int temp) { currentTemp = temp; } public void resetTemp(double newTemp) { currentTemp = newTemp; } }
  • In the following code block, what is the scope of the variable "name"? public void printGreeting(String name) { System.out.println("Hi " + name); }
  • What output do we get if we run the following code? class X { private int a; private int y; public X() { this.a = 5; } public X(int y) { this(); this.y = y; System.out.print(a); System.out.print(this.y); } } public class Z { public static void main(String[] args) { X x = new X(3); } }
  • Does the following code compile successfully? ```java class X { private int a; private int y; public X() { this.a = 10; } public X(int y) { this(); this.y = y * 10; System.out.print(this.a + " "); System.out.print(this.y + " "); } } public class Z { public static void main(String[] args) { X x = new X(5); } } ```
  • What is the output of the following code? ```java class X { private int a; private int y; public X() { a = 15; } public X(int y) { this(); X.y = y; System.out.print(this.a + " "); System.out.print(this.y + " "); } } public class Z { public static void main(String[] args) { X x = new X(5); } } ```
  • What output do we get if we run the following code? ```java class X { private int a; private int y; public X() { this.a = 3; } public X(int y) { this("z"); this.y = y; System.out.print(this.a + " "); System.out.print(this.y + " "); } } public class Z { public static void main(String[] args) { X x = new X(7); } } ```


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.