Beginner quizzes – Java casting object

The following Java exercise(s) are designed for beginner level programmers. If the level is too hard, then I recommend reading my article about this topic, which offers a theoretical explanation including more exercises. Read More: Java Object Casting


Quiz 1: Upcasting an object

What happens when the following program is compiled and run?

public class Super {

	protected int i = 4;

	public int method(){
		return i * i;

public class Sub extends Super {

	int i = 3;

	public int method(){
		return i * i;
	public static void main(String[] args){
		Super s = new Sub();
		System.out.print("-x" + s.method());
		System.out.print("-x" + s.i);

Select the correct answer.

Quiz 2: Downcasting and upcasting objects in Java

What happens when the following program is compiled and run?

public class MySuper {

	int z = 3;

	int myMethod(){
		return z;

public class MySub extends MySuper {

	int z = 8;

	int myMethod(){
		return z;
	public static void main(String[] args){
		MySuper mySuper = new MySub();
		MySub mySub = (MySub) mySuper;
		System.out.print("-x" + mySuper.myMethod() + "-x" + mySuper.z);
		System.out.print("-x" + mySub.myMethod() + "-x" + mySub.z);

Select the correct answer.

Please, leave your questions, feedback and suggestions in the comments below! 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!


Add a Comment
  1. I like what you are up too. This kind of clever work!

  2. Wow, marvelous blog structure! How long have you been running a blog?

  3. i love your blog and its my favorite java website!!

  4. Love the quizzes! I am the beginner and spent over an hour thinking about few questions. Not easy but covers a lot!

  5. The paragon of undrestanding these issues is right here!

  6. First of all, I want to say that your website is awesome! I love all your quizzes and thanks for your great effort.

    1. You are always welcome!

      1. That’s a genuinely impressive answer.

    2. The quality of your posting is there for all to see

  7. I am a beginner Java programmer. Can you tell me please, how long it takes to start with the intermediate level quizzes?

    1. I think you need almost one year experience, but that is not a rule:)

      1. Thanks! Always good to find a real expert.

    2. If inoamrftion were soccer, this would be a goooooal!

  8. I learn everyday from your quizzes!

    1. I am glad to read that!

      1. I told my kids we’d play after I found what I neddee. Damnit.

    2. Big help, big help. And sutrleapive news of course.

  9. Thanks for posting this nice article!

    1. If your aritelcs are always this helpful, “I’ll be back.”

  10. Your articles and quizzes are just great. Keep up the good work!

    1. The answer of an expert. Good to hear from you.

  11. Please, more quizzes!

    1. I update and add content to this website regularly. New quizzes are on the way:)

      1. Just what I’ve been looking for. Thanks!

Leave a Reply

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

Sarmaroof © 2017