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's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.
 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 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