Test your Java skill – Advanced level

The advanced level (red level) offers more complicated Java tests than the intermediate level. It is designed for advanced Java programmers with several years of experience.

If you find the tests too difficult, I would recommend clicking on the link under the test title to read my article about that specific topic and try again.

test-your-java-skill-advanced_480_324

Test 1: Three for loops with labels

Read More: Java Iteration (Loop) Statements

What happens when the following program is compiled and run?

public class LoopLabel {

	public static void main(String[] args){
		
		int b = 3;
		
		one:for(int x = 0; x < 2; x ++ ) {
			System.out.print("-x" + x);
			for(int y = 0; y < 2; y ++ ) {
				System.out.print("-y" + y);
				for(int z = 0; z < 7; z ++ ) {
					z = z + b;
					System.out.print("-z" + z);
					if(z > 3) {
						break one;
					}
				}
			}
		}
	}
}

Select the correct answer.


Test 2: Constructors of subclasses and superclasses.

Read More: Java Inheritance.

What happens when the following program is compiled and run?

class MySuper {

	protected int b = 4;

	MySuper() {
		this(6);
		System.out.print("-b" + (b - 2));
	}
	MySuper(int c) {
		b = c;
		System.out.print("-b" + b);
	}
}

final class MySub extends MySuper {

	int b;
	int c = 5;

	MySub() {
		System.out.print("-b" + c);
	}
	MySub(int c) {
		int d = (super.b) + c;
		System.out.print("-b" + d);
	}
	void myMethod(){
		System.out.print("-b" + b);
	}
	public static void main(String[] args){
		MySub mySub = new MySub(2);
		mySub.myMethod();
	}
}

Select the correct answer.


Test 3: Imagining how the computer thinks

What happens when the following program is compiled and run?

public class MyClass {

	String x = "x";
	int y = 3;

	MyClass() {
		x = "y";
	}
	String myMethod(int y){
		int z = myMethod(13,12);
		String x = "z";
		if(z < y) {
			x = "w";
			System.out.print("-" + x + y);
			return x;
		}else {
			x = "n";
			String s = "z";
			System.out.print("-" + s + y);
			return x;
		}
	}
	public int myMethod(int i, int i2){
		int y = (i + i2) / 5;
		return y;
	}
	public static void main(String[] args){
		MyClass m = new MyClass();
		String s = m.myMethod(7);
		int y = m.myMethod(2,3);
		m.myMethod(y);
		System.out.print("-" + s + y);
	}
}

Select the correct answer.


Test 4: A combination of a “while” loop with an “if else” statement.

Read More: Java Iteration (Loop) Statements
What happens when the following program is compiled and run?

public class MyLoop {

	int x = 0;
	int y = 0;
	boolean isEqual;

	void methodA(){
		while(x < 4) {
			++ x;
			y += 2;
			System.out.print("-x" + x);
			if(y > x) {
				System.out.print("-y" + y);
				y -= 2;
				isEqual = false;
			}else {
				x -- ;
				-- y;
				System.out.print("-x" + x + "-y" + y);
				isEqual = true;
				break;
			}
		}
	}
	void methodA(int i){
		int x = 4 + i;
		methodA();
		if(isEqual) {
			System.out.print("-y" + x);
		}
	}
	public static void main(String[] args){
		MyLoop myLoop = new MyLoop();
		myLoop.methodA(2);
	}
}

Select the correct answer.


Test 5: Inheritance, object up casting.

Read More: Java Inheritance.
Read More: Java Object Casting

What happens when the following program is compiled and run?

class MySuper {

	protected int x = 3;
	protected int c = 7;
	protected String s1 = "-n";
	protected String s2 = "-m";

	MySuper() {
		new MySuper(s1);
	}
	MySuper(String s) {
		System.out.print(s + x);
	}
	void method(String s){
		System.out.print(s + c);
	}
}

class MySubA extends MySuper {

	int x = 6;
	int c = 1;
	String s1 = "-k";

	MySubA(String s) {
		new MySubA(s1,s2);
	}
	MySubA(String s1, String s2) {
		System.out.print(super.s1 + x + super.s2 + c);
	}
	void method(String s){
		s = super.s1;
		System.out.print(s + c);
	}
	public static void main(String[] args){
		MySuper mySubA = new MySubA("-n");
		System.out.print(mySubA.c);
		mySubA.method("-w");
	}
}

Select the correct answer.


Test 6: A class with two inner classes.

Read More: Java Nested And Inner Classes

What happens when you try to compile and run the following program?

class Outer {

	public int x;
	private int y;

	Outer() {
		new InnerA(3);
		x = 6;
	}
	private void methodOuter(){
		System.out.print("-x" + x);
	}
	class InnerA {
		InnerA() {
			System.out.print("-y" + y);
			y = 5;
		}
		InnerA(int i) {
			y = i + 3;
			x = i + 1;
			new InnerA();
		}
		class InnerB {
			InnerB() {
				y = 11;
			}
			private void methodB(){
				System.out.print("-x" + x);
				Outer o = new Outer();
				o.methodOuter();
			}
		}
	}
	public static void main(String[] args){
		Outer.InnerA.InnerB innerB = new Outer().new InnerA(4).new InnerB();
		innerB.methodB();
	}
}

Select the correct answer.


Test 7: Abstract classes and overwriting methods

What happens when the following program is compiled and run?

public abstract class Base {

	int x = 5;
	int y;

	public Base() {
		x ++ ;
		y += 3;
		System.out.print("-x" + method(4));
	}
	abstract int method();
	abstract int method(int i);
}

public class Super extends Base {

	int x = 1;

	public Super() {
		x += 2;
		y ++ ;
		System.out.print("-x" + method());
	}
	int method(){
		x ++ ;
		return(x + y);
	}
	int method(int i){
		return(method() + 2 * i);
	}
}

public class Sub extends Super {

	Sub() {
		x += 5;
		System.out.print("-x" + x + "-x" + y);
	}
	int method(){
		return x + (2 * y) + 2;
	}
	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!
Sarmaroof.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.

Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.
 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

2 Comments

Add a Comment
  1. Thank you very much!

  2. People norllamy pay me for this and you are giving it away!

Leave a Reply

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

Sarmaroof © 2017