Subscribe To MegiSoft RSS Feeds!     Your Browser OR  Google Feedburner

Intermediate Quizzes – Java Abstract Classes Exercises

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-blue

Quiz 1: Abstract classes in Java.

What happens when the following program is compiled and run?

public abstract class Base {

	int x = 2;

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

public class MySuper extends Base {

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

public class Sub extends MySuper {

	int calculate(){
		return x + 2;
	}
	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: Abstract classes and methods

What happens when the following program is compiled and run?

public abstract class Base {

	int z = 6;

	public Base() {
		z -= 2;
		System.out.print("-z" + method(3,8));
	}
	abstract int method(int i);
	abstract int method(int i, int i2);
}

public class Sub extends Base {

	int z = 2;

	public Sub() {
		System.out.print("-z" + method(4));
	}
	int method(int i){
		return(z * i + super.z);
	}
	int method(int i, int i2){
		return(method(i) + i + i2);
	}
	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
megisoft © 2016