Test your Java skill – Intermediate level

The intermediate level (blue level) offers a bit more complicated Java tests than the beginners level. It is designed for Java programmers with one to two years experience.

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

test-your-java-skill-intermediate_480_324

Test 1: Java inheritance

Read More: Java Inheritance.
What happens when the following program is compiled and run?

public class MySuper {

	int x = 1;
	int y = 6;

	MySuper() {
		System.out.print("-x" + x);
		new MySuper(5);
	}
	MySuper(int y) {
		System.out.print("-y" + y);
		x = 4;
	}
}

public class MySub extends MySuper {

	int w = 4;

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

Select the correct answer.


Test 2: One loop with “if” statement.

Read More: Java Conditional Statements
Read More: Java Iteration (Loop) Statements

What happens when the following program is compiled and run?

public class MyClass {

	public static void main(String[] args){
		int y = 3;
		for(int x = 0; x < 3; x ++ ) {
			System.out.print("-x" + x);
			if(x == 1) {
				System.out.print("-x" + y);
			}
			if(x == 0) {
				int b = x + y;
				System.out.print("-x" + b);
			}
		}
	}
}

Select the correct answer.


Test 3: Instance and local variables

Read More: Java Methods.

What happens when the following program is compiled and run?

public class MyClass {

	int x = 5;
	int y = 7;

	public void myMethod(int x){
		System.out.print("-x" + x);
		x = this.x;
		System.out.print("-x" + x);
	}
	public static void main(String args[]){
		MyClass mc = new MyClass();
		int y = 2;
		mc.myMethod(y);
		System.out.print("-y" + y);
	}
}

Select the correct answer.


Test 4: An inner class

Read More: Java Nested And Inner Classes

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

class Outer {

	public int a;
	private int b;

	Outer() {
		a = 1;
		b = 4;
		InnerA innerA = new InnerA();
		innerA.methodA();
	}
	class InnerA {
		int a = 4;
		private void methodA(){
			System.out.print("-a" + a + "-b" + b);
		}
	}
	public static void main(String[] args){
		InnerA innerA = new Outer().new InnerA();
	}
}

Select the correct answer.


Test 5: Java methods

Read More: Java Methods.
What happens when the following program is compiled and run?

public class MyClass {

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

	MyClass() {
		x = "y";
	}
	void myMethod(int y){
		System.out.print(x + y);
	}
	public int myMethod(int i, int i2){
		return i * i2;
	}
	public static void main(String[] args){
		MyClass m = new MyClass();
		int y = m.myMethod(2,3);
		m.myMethod(y);
	}
}

Select the correct answer.


Test 6: 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 {

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

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 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!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

1 Comment

Add a Comment
  1. Real brain power on diylaps. Thanks for that answer!

Leave a Reply

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

Sarmaroof © 2017