Subscribe To MegiSoft RSS Feeds!     Your Browser OR  Google Feedburner

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

Article: Java casting object
Quizzes: Beginner  Intermediate  Advanced 

casting-objects-green

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.



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!

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! 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..
Article: Java casting object
Quizzes: Beginner  Intermediate  Advanced 


Suggested Articles:



Click Here For Info about: Sar Maroof's New Java Book
Author: Sar Maroof
More than a decade of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster - Mind IT
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.

 Subscribe To Megisoft RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

25 Comments

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!

    2. Deep thought! Thanks for cobiuinrttng.

Leave a Reply

megisoft © 2016