Category: Java quizzes

Java quiz 36: Conditional statement with boolean

This is a simple conditional statement using a boolean variable. Java quiz 36: Conditional statement with boolean Level: Easy What happens if the following code is compiled and run? Author: Sar Maroof Select the correct answer. a. This program writes “yyy” to the standard output. b. This program writes “yxy” to the standard output. c. […]

Java quiz 35: Easy conditional statements

Here below is an easy conditional statement’s quiz. Java quiz 35: Easy conditional statements Level: Easy What happens if the following code is compiled and run? Author: Sar Maroof Select the correct answer. a. This program writes “1” to the standard output. b. This program writes “7” to the standard output. c. This program writes […]

Java quiz 34: A simple switch statement

This quiz demonstrates a simple Java switch statement for beginner. Java quiz 34: A simple switch statement Level: Easy What is the output of the following code? Author: Sar Maroof Select the correct answer. a. This program writes “Red Green” to the standard output. b. This program writes “Red” to the standard output. c. This […]

Java quiz 33: Conditional vs bitwise operator

This Java quiz demonstrates the difference between conditional operator || with the bitwise operator |. To understand the difference you can play with the code and change the operators to test it. Java quiz 33: Conditional operator vs bitwise operator Level: Normal What is the output of the following code? Author: Sar Maroof Select the […]

Java quiz 32: Two dimensional arrays

In this two dimensional array we try to write one of the elements to the standard output. Java quiz 32: Two dimensional arrays Level: Normal What is the output of the following code? Author: Sar Maroof Select the correct answer. a. This program writes “Emma” to the standard output. b. This program writes “null” to […]

Java quiz 31: Demonstration of arrays

Java quiz 31: Demonstration of arrays This Java quiz demonstrates some idea’s of arrays. Level: Normal What will the following code print when compiled and run? Author: Sar Maroof Select the correct answer. a. This program writes “0” to the standard output. b. This program writes “5” to the standard output. c. This program writes […]

Java quiz 30: Static, instance statements and blocks

Have you ever though which block, constructor or statement is called first when you compile and run your code? This quiz demonstrates that idea! Java quiz 30: Static, instane statements and blocks Level: Normal What is the output of the following code? Author: Sar Maroof Select the correct answer. a. This program writes “Toyota” to […]

Java Quiz 29: Working with objects and references

Java Quiz 29: Working with objects and references Level: Normal What is the output of the following code? Author: Sar Maroof Select the correct answer. a. This program writes “David, Emma” to the standard output. b. This program writes “David, Vera” to the standard output. c. This program writes “Vera, Vera” to the standard output. […]

Java quiz 28: Object values as parameters

Java quiz 28: Object values as parameters Level: Normal What is the output of the following code? Author: Sar Maroof a. This program writes “mh, ok” to the standard output. b. This program writes “mx, ox” to the standard output. c. This program writes “h, k” to the standard output. d. This program writes “m, […]

Java quiz 26: How unary operators work?

The following example shows different kind of unary operators. Java quiz 26: How unary operators work? Level: Normal What happens if the following code is compiled and run? Author: Sar Maroof Answer explanation The statement if(x == x–) returns true, because the value of x is decremented by one after the evaluation. The statement x […]

Java quiz 25: Object references using StringBuffer

The class StringBuffer is one of the classes that you often need by writing Java programs. This example demonstrates StringBuffer and object references. If you are not familiar with the class StringBuffer, Here is a link to the article. Java quiz 25: Object references using StringBuffer Level: Normal What happens if the following code is […]

Java quiz 24: The ArrayList of car brands

Here is an ArrayList of car brands. This program shows what happens if you add a new brand in a specific index. Java quiz 24: The ArrayList of car brands Level: Normal What happens if the following code is compiled and run? Author: Sar Maroof Answer explanation The statement carList.add(3, “Ford”); adds the brand Ford […]

Java quiz 23: Upcasting and downcasting objects

This example shows what happens to the overridden methods and variables by upcasting and downcasting objects in Java. Java quiz 23: Upcasting and downcasting objects Level: Hard Note: The classes Item and Book are two separate files in one package. What happens if the following code is compiled and run? Item.java Book.java Author: Sar Maroof Answer […]

Java quiz 22: Handling inner classes

This code shows handeling inner classes in Java. You have to store the whole code in one file with the name Outer. Java quiz 22: Handling inner classes Level: Normal What happens if the following code is compiled and run? Outer.java Author: Sar Maroof Answer explanation The statement new Outer(4, 5) calls the two argument […]

Java quiz 21: Working with operators

This code shows how the operators and the operands work in Java. Java quiz 21: Working with operators Level: Easy What is written to the standard output as the result of executing the following code? Author: Sar Maroof Answer explanation The first conditional statement if(x == (y – 4) && !isDefect) returns true, because x […]

Java quiz 20: Convert from a class to interfaces

In this quiz InterfaceA extends InterfaceB. The class MyClass implements InterfaceA. The question is whether it is allowed to convert from MyClass to both interfaces and what the consequences would be. Java quiz 20: Convert from a class to interfaces Level: Normal Note: The class MyClass and the interfaces InterfaceA and InterfaceB are three separate files […]

Java quiz 19: Conditional statements

Let’s start with today’s simple quiz for beginners. Java quiz 19: Conditional statements Level: Easy What is written to the standard output as the result of executing the following code? Author: Sar Maroof Answer explanation The statement if(y > (x + 4) || (y – 3) == x) returns false, because y is not greater […]

Java quiz 18: A while loop & conditional statements

Here we have a combination of a while loop and a conditional statement. Java quiz 18: A while loop and conditional statements Level: Normal What is written to the standard output as the result of executing the following code? Author: Sar Maroof Answer explanation The boolean variable isActive is not initialized. Therefore its value is […]

Java quiz 17: Working with ArrayLists

ArrayList is used in Java to collect data. If you are not familiar with ArrayLists, you can read this article about ArrayList. Java quiz 17: Working with ArrayLists Level: Hard What is written to the standard output as the result of executing the following code? Author: Sar Maroof Answer explanation The statement ml1.list2 = ml1.method(ml1.list); […]

Java quiz 16: Ternary operator

Here is an example that shows how the ternary operator works in Java. Java quiz 16: Ternary operator Level: easy What is the output of this code? Author: Sar Maroof Answer explanation The statement int y = (x < 3) ? 4 : 8; means The statement if(x < 3) returns false, because x is […]

Java quiz 15: Loop within loop

For many of the projects on which I worked, I needed to use loops within loops. In the beginning seems very hard, but if you understand the process you become gradually familiar with it. Java quiz 15: Loop within loop Level: Hard What is the output of this code? Author: Sar Maroof Answer explanation If […]

Java quiz 14: Comparing strings

As a Java programmer you need very often to work with strings. Here is a simple quiz that shows the way Java handles strings. Java quiz 14: Comparing strings Level: Normal What happens when the following program is compiled and run?   Author: Sar Maroof Answer explanation The first conditional statement if (str1 == str2) […]

Java quiz 13: Working with while loop

This program is a simple while loop. Working with while loop is needed regularly for every Java programmers. Java quiz 13: Working with while loops Level: Easy What is written to the standard output as the result of executing the following code?   Author: Sar Maroof Answer explanation The initial value of i is 1. […]

Java quiz 12: Conditional statements

Here we have a sequence of conditional statements. Java quiz 12: A sequence of conditional statements Level: Normal What is written to the standard output as the result of executing the following code?   Author: Sar Maroof Answer explanation The condition if(!isActive) returns true, because the boolean variable isActive is false. The statement str += […]

Java quiz 11: While loop

The while loop is one of the types of loops in Java. Every advanced Java programmer see what happens in this program,  but unfortunately that doesn’t apply to beginners. Therefore, as a beginner programmer, you need to practice regularly to master that kind of loops. Java quiz 11: Practicing with while loop Level: Easy What is […]

Java quiz 10: Calling constructor from constructor

In this program we call a constructor within another constructor. Remember what happens when you create an object of a class that extends another class. Java quiz 10: Calling constructor from constructor Level: Normal Note: The classes MySuper and MyClass are two separate files in one package. What is written to the standard output as the […]

Java quiz 09: How to handle exceptions?

As a programmer you need to realize what kind of exceptions might occur in your code. By handling the exceptions, you would avoid problems for the users of your program. In the following example, passing some parameters to the method myMethod could cause exceptions. Therefore it is necessary to handle the exceptions that might occur. […]

Java quiz 08: Array elements

Here is a simple array of Strings with the length of 5. We assigned the values of all the elements of the array. If you are not familiar with arrays, you can read my article “Java arrays” before answering this quiz. Java quiz 8: Array elements Level: Normal What is written to the standard output […]

Java quiz 07: Working with arrays

Here is a simple array of Strings with the length of 4. We assigned values of some of the elements of the array. If you are not familiar with arrays, you can read my article “Java arrays” before answering this quiz. Java quiz 7: Working with Arrays Level: Normal What is written to the standard […]

Java quiz 06: Handling exceptions

Handling Exceptions is one of the important concepts in Java programming language. I try to make that concept clear in this simple program. If you are not familiar with Exceptions, I recommend to read my article “Java exception handling using easy exercises” before trying this quiz. Java quiz 6: Handling exceptions Level: Normal What is […]

Java quiz 05: Conditional statements else-if

I post three different levels of puzzles, quizzes and assignments to make sure that all the members benefit from my posts. Here is an easy quiz for beginners. To solve this quiz you need to understand the difference between the if sequences and the else if sequences, click here if you need to read my article […]

Java quiz 04: Overriding methods

Hi guys, Here is a Java quiz. If you can solve this quiz and understand the right explanation of the answer, you are really good in Java! Of course, I mean before you read the answer explanation below :-). Java quiz 4: Overriding methods Level: Hard Note: The classes Item and Book are two separate files in […]

Java quiz 03: Static and instance variables

It is important for every Java programmers to understand the difference between class(static) variables and instance variables. Therefore I wrote this quiz, which clarifies those differences. Java quiz 3: The difference between static and instance variables Level: Normal What is written to the standard output as the result of executing the following code? Author: Sar […]

Java quiz 02: Simple conditional statements

Here is a Java quiz about conditional statements. Note that there is a difference between a sequence of if statements and a sequence of else if statments. If you don’t know that, please, read my article first by clicking here. Java quiz 2: Simple conditional statements Level: Easy What is written to the standard output […]

Java quiz 01: Passing parameters to constructors

I would emphasize that understanding constructors helps you by initializing instance variables. In the following code, I try to make that concept clear. I use a trick to make it difficult to guess the result if you don’t understand the constructor block. Java quiz 1: Passing parameters to constructors Level: Normal What is the output […]

Sarmaroof © 2017