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.  

Java Packages and Access Modifiers: Article  Quizzes

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! If you need more explanation by any of the answers, just let me know to update it for you!

Megisoft.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. The author of Megisoft is Sar Maroof who has more than a decade of experience with web development mainly with Java, MySql, HTML and more..
Java Packages and Access Modifiers: Article  Quizzes

Suggested Articles:


Sar Maroof's New Book "Java Quizmaster For Beginners"
Author: Sar Maroof
Many years of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.
 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

megisoft © 2016