Beginner quizzes – Java control statements

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 Conditional Statements Read More: Java Iteration (Loop) Statements

conditional-statements-color

Quiz 1:  Easy “for” loop statement for beginners

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

public class ForLoop {

	int y = 3;

	void methodA(){
		int y = 2;
		for(int x = 0; x < 3; x ++ ) {
			System.out.print(" y" + y);
			y -- ;
		}
	}
	public static void main(String[] args){
		ForLoop lc = new ForLoop();
		lc.methodA();
	}
}

Select the correct answer.


Quiz 2: One simple loop

What happens when the following program is compiled and run?

public class Loop {

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

Select the correct answer.


Quiz 3: Easy while loop

What happens when the following program is compiled and run?

public class MyLoop {

	void methodA(){
		int x = 0;
		while(x < 3) {
			x ++ ;
			System.out.print("-x" + x);
		}
	}
	public static void main(String[] args){
		MyLoop myLoop = new MyLoop();
		myLoop.methodA();
	}
}

Select the correct answer.


Quiz 4:  Easy If else statement

What happens when the following program is compiled and run?

public class Statement {

	public void methodA(){
		int a = 3;
		int b = 2;
		if(a < b) System.out.print("a");
		else System.out.print("b");
	}
	public static void main(String[] args){
		Statement st = new Statement();
		st.methodA();
	}
}

Select the correct answer.


Quiz 5: Easy switch block of char

What happens when the following program is compiled and run?

public class Switch {

	public void switchTester(){
		char myCahr = 'a';
		switch(myCahr) {
			case 'a':
				System.out.print("a");
			default:
				System.out.print("b");
		}
	}
	public static void main(String[] args){
		Switch st = new Switch();
		st.switchTester();
	}
}

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. This is my favorite Java blog, because it explains Java topics easy with different exercises!

Leave a Reply

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

Sarmaroof © 2017