Quizzes – Java packages and access modifiers

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.  

packages_access_modifier-gray

 

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();
	}
}

Select the correct answer.


 

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 + " ");
	}
}

Select the correct answer.



 

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);
	}
}

Select the correct answer.


Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.


Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

1 Comment

Add a Comment
  1. That hits the target pecfretly. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017