The following Java exercise(s) are designed to help you understand the article to which it belongs. If the following quizzes are too hard, then I recommend to read the article first, which offers a theoretical explanation including examples and exercises.
Java Packages and Access Modifiers: Article Quizzes |
Quiz 1: Using access modifiers by extending a superclass.
What is written to the standard output as the result of executing the following code?
Remember that the two classes MySuper and MySub are in two different packages!
package qz01.package1; public class MySuper{ public int i = 3; protected int x = 4; } ------------------------ package qz01.package2; import qz01.package1.MySuper; public class MySub extends MySuper{ int i = 5; int x = 7; void method(){ MySuper mySuper = new MySuper(); System.out.print(mySuper.i); System.out.print(mySuper.x); } public static void main(String[] args){ MySub ms = new MySub(); ms.method(); } }
Quiz 2: How access modifiers work for classes in different packages?
What is written to the standard output as the result of executing the following code?
Remember that the two classes ClassA and ClassB are in two different packages!
package quiz; class ClassA{ public int p = 6; protected int q = 7; int r = 5; private int s = 2; } ------------------------ package quiz.package1; import quiz.ClassA; public class ClassB extends ClassA{ int r = 8; private int s = 3; public static void main(String[] args){ ClassA ca = new ClassA(); ClassB cb = new ClassB(); System.out.print(ca.p + " "); System.out.print(cb.p + " "); System.out.print(cb.q + " "); System.out.print(cb.r + " "); System.out.print(cb.s + " "); } }
Quiz 3: Access modifiers?
What is written to the standard output as the result of executing the following code?
Remember that the two classes ClassA and ClassB are in two different packages!
package qz03.package1; class ClassA{ public String str = "Channel "; protected int x = 3; } ------------------------ package qz03.package2; import qz03.package1.ClassA; public class ClassB extends ClassA{ public static void main(String[] args){ ClassA ca = new ClassA(); ClassB cb = new ClassB(); System.out.print(ca.str); System.out.print(cb.x); } }
Java Packages and Access Modifiers: Article Quizzes |
Suggested Articles
![]() ![]() ![]() |
I didn’t understand the reason of first one! Can you please explain?
That hits the target pecfretly. Thanks!