Subscribe To MegiSoft RSS Feeds!     Your Browser OR  Google Feedburner

Advanced Quizzes – Java Abstract Classes

The following Java exercise(s) are designed for intermediate level programmers. If the level is too hard, then I recommend to select an easier one or you might consider reading my article about this topic, which offers a theoretical explanation including more exercises. Read More: Java Abstract Classes

Article: Java abstract classes
Quizzes: Beginner  Intermediate  Advanced 

abstract-classes-red

Quiz 1: Overwriting abstract methods

What is written to the standard output as the result of executing the following code?

public abstract class Base {

	int x = 3;

	public Base() {
		x += 2;
		System.out.print("-x" + x);
		x ++ ;
	}
	abstract int calculate();
	abstract int calculate(int i);
}

public class SuperA extends Base {

	int x = 1;

	public SuperA() {
		System.out.print("-x" + calculate(2));
	}
	int calculate(){
		return x;
	}
	int calculate(int i){
		return(calculate() + i);
	}
}

public class Sub extends SuperA {

	Sub() {
		x += 3;
	}
	int calculate(){
		return x + 6;
	}
	public static void main(String[] args){
		Sub sub = new Sub();
		System.out.print("-x" + sub.calculate());
	}
}

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: Extending an abstract class in Java

What appears in the standard output when this program is invoked?

public abstract class Base {

	int y = 5;

	public Base() {
		y -= 2;
		System.out.print("-y" + method(4,7));
	}
	abstract int method(int i);
	abstract int method(int i, int i2);
}

public class Super extends Base {

	int y = 2;

	public Super() {
		System.out.print("-y" + method(4));
	}
	int method(int i){
		return(y * i + super.y);
	}
	int method(int i, int i2){
		return(method(i) + i + i2);
	}
}

public class Sub extends Super {

	int method(int i){
		return y * 2 + i;
	}
	public static void main(String[] args){
		Sub sub = new Sub();
	}
}

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 abstract classes
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

Leave a Reply

megisoft © 2016