Category: Java puzzles

Java puzzle 30: Simple garbage collection

Try to use the following principle to solve this puzzle! When there is no references pointing to an object, that object can be eligible for garbage collection. Java puzzle 30: Simple garbage collection Level: Normal Question: After which line number, the object bird created at line //1 is eligible for garbage collection? Author: Sar Maroof […]

Java puzzle 29: Loops and branching statements

This Java puzzle demonstrates a combination of branching statements. Java puzzle 29: Loops and branching statements Level: Normal What is the output of the following code? Author: Sar Maroof Answer explanation The statement x += 3; is unreachable. When the value of the variable i is equal to 2 the statement break; terminates the loop. […]

Java puzzle 28: Improving encapsulation

If the following code is compiled and run, it writes [Orange, Apple, Mango, Cherry] to the standard output. By adding cherry to the fruit’s list, it’s clear that the class TestFruit is able to modify the fruit’s list. How can we improve the encapsulation of the class Fruit to prevent other classes from removing and […]

Java puzzle 27: Implementing interfaces and casting

The output of this program is 3200.0, 1199.0. Write only one statement at line 8. As a result of your statement, the output of this code will be 3100.0, 1350.0 What is that statement? Java puzzle 27: Implementing interfaces and casting Level: Hard Author: Sar Maroof Answer explanation The statement should assign both values 3100.0 […]

Java puzzle 26: Invoking a method within a method

This code demonstrates invoking a method within another method. Java puzzle 26: Invoking a method within a method Level: Normal What is the output of this program and why? Author: Sar Maroof Answer explanation The statement myMethod(3, 1); invokes the method myMethod. The statement this.x += x; adds the value of the parameter x to […]

Java puzzle 25: return, break and continue

Questions: 1. What is the output of the following code? 2. What is the output if you replace the return; statement with the break; statement? 3. What is the output if you replace the return; statement with the continue; statement? Java puzzle 25: The difference between return, break and continue Level: Normal Author: Sar Maroof […]

Java puzzle 24: Anonymous class demonstration

An anonymous class is a class, which doesn’t have a name. It is used once to override methods of a class or an interface. For more about anonymous classes you can click here. Note: The class Anonymous and the interface Length are two separate files in the same package. Java puzzle 24: Anonymous class demonstration […]

Java puzzle 23: Inheritance, animal example

If this code is compiled and run, it writes Meat to the standard output. Wrtie only one statement at line 11 of the class WildAnimal. As a result of adding your statement, the program writes “Wolf 50.0 Meat” to the standard output. What is that statement? Note: The classes Animal and WildAnimal are two separate […]

Java puzzle 22: The do-while iteration

This quiz shows that the do block is executed first then the condition is evaluated. That is what happens by the do-while loop and it is different with the while-loop and the for-loop. Java puzzle 22: The do-while iteration Level: Normal What is the output of this program and why? Author: Sar Maroof Answer explanation By […]

Java puzzle 21: Using inheritance

If this code is compiled and run, it writes 5, 6 to the standard output. Write only one statement at line 8 of the class MySub. As a result of that the program writes 2, 7 to the standard output. What is that statement? Note: The classes MySuper and MySub are two separate files in one […]

Java puzzle 20: Classes and constructors

If this code is compiled and run, it writes nullx2 to the standard output. Write only one statement at line 8. As a result of your statement the program writes xx3 to the standard output. What is that statement? Java puzzle 20: Classes and constructors Level: Normal Author: Sar Maroof Answer explanation The statement should […]

Java puzzle 19: Overloading methods

If this code is compiled and run, it writes null3 to the standard output. Write only one statement at line 9. As a result of that statement the output of the program becomes x5. What is that statement? Java puzzle 19: Overloading methods Level: Normal Author: Sar Maroof Answer explanation To solve this puzzle we […]

Java puzzle 18: Classes and objects

If this program is compiled and run, it writes pp9 to the standard output. Write only one statement at line 27. As a result of that statement the output of this code becomes 456. What is that statement? Java puzzle 18: Classes and objects Level: Hard Author: Sar Maroof Answer explanation We need to reassign […]

Java puzzle 17: Classes and objects

The output of this code is 2y2. Add only one statement at line 20. As a result of your statement, the output of the program becomes 9×5. What is that statement? Java puzzle 17: Classes and objects Level: Hard Author: Sar Maroof Answer explanation The statement should reassign the variable i of the object mc. […]

Java puzzle 16: Conditional statements within a loop

Here is the puzzle of today, which demonstrates a conditional statement within a loop. Java puzzle 16: Conditional statements within a loop Level: Normal What is the output of this program and why? Author: Sar Maroof Answer explanation The loop is repeated 4 times, because the initial value of the variable i is 0 and […]

Java puzzle 15: Objects and methods

If this program is compiled and run, it writes 2x to the standard output. Write only one statement on line 14. As a result of your statement the program should write 7xy to the standard output. What is that statement? Java puzzle 15: Objects and methods Level: Normal Author: Sar Maroof Answer explanation The program […]

Java puzzle 14: Understanding while loops

If this code is compiled and run, it writes 777 to the standard output. By adding only one statement on the line 10 the code writes 641 to the standard output. What is that statement? Java puzzle 14: Understanding while loops Level: Normal Author: Sar Maroof Answer explanation The program writes 777 to the standard […]

Java puzzle 13: Invoking methods

In this Java puzzle the method myMethod is invoked, which returns an integer variable. Two integer parameters are passed to the method. Java puzzle 13: Invoking a method Level: Easy What is the output of this program and why? Author: Sar Maroof Answer explanation The parameters 3 and 5 are passed to the method myMethod. […]

Java puzzle 12: Passing parameters to constructors

In this Java puzzle we pass a parameter to a constructor. I congratulate you if you can solve this puzzle! Java puzzle 12: Passing a parameter to a constructor Level: Hard What is the output of this program and why? Author: Sar Maroof Answer explanation The statement new MyClass().method(3); instantiates an object from the class […]

Java puzzle 11: Operators

The combination of operators and conditional statements appear in almost every program. Every Java programmer should master all types of conditional statements. Therefore, I focus to offer that in different ways in my puzzles and quizzes. Please, always check out the explanation of the answer if you have doubts. Java puzzle 11: Operators and conditional […]

Java puzzle 10: Switch statement

Java puzzle 10: A switch statement Level: Normal What is the output of this program and why? Author: Sar Maroof Answer explanation Answer explanation The initial value of x is 2. So, case 1 doesn’t match the value of x. The statement x++; under case 2 is executed and the value of x is incremented […]

Java puzzle 09: Demonstrating a for loop

The process of loops is one of the most difficult concept for beginner programmers. To master loops you need to practice and parctice again. Therefore, I demonstrate many loops for beginners to solve. This loop is a for loop, which is often used in almost every Java program. You might need a pen and a […]

Java puzzle 07: Variables in Java

There are different types of variables in Java. Many beginners does not distinguish between them. Here is a new puzzle. What is the problem in this code? Java puzzle 7: Variable types in Java Level: Easy Author: Sar Maroof Answer explanation In this program the class variable isForeigner is not initialized, which is allowed in […]

Java puzzle 06: Controlling loops

In this Java puzzle we try to control the results of a loop. If you compile and run this program, it writes 123456 to the standard output. By adding only one statement to the line 9, the program will write 1346 to the standard output. What is that statement? More than one solution is possible! […]

Java puzzle 05: Creating objects

In this simple Java puzzle, we create two objects namely empA and empB. We assigned the name Jack to the variable name of the object empA, and assigned the name Emma to the variable name of the object empB. What is the output of this program and why? Java puzzle 5: Creating objects Level: Normal […]

Java puzzle 04: Switch statement

If you are or want to become a good programmer in the future and you don’t understand this puzzle series, you got to do something about it. Every advanced programmer can see most of the results of this series. The third one was very hard, I understand if you ignored it. Programming is writing code […]

Java puzzle 03: Inheritance

This Java puzzle is a mystery! By creating an object of the subclass MySub in the main method, the constructor of the superclass MySuper is called. The constructor of MySuper invokes the method myMethod. The statement System.out.print(“-x” + m);  should write -x3, because the value of m is 3. The statement System.out.print(“-x” + n); should […]

Java puzzle 02: Conditional statements

Here is the second simple Java puzzle If this simple program is compiled and run, it writes the letter “c” to the standard output. By adding one statement to the line 9, the program writes “ab” to the standard output. What is that statement? Java puzzle 2: Conditional statements Level: Normal Author: Sar Maroof Answer […]

Java puzzle 01: A simple loop

If this simple program is compiled and run, the following is written to the standard output. -x1-x2-x3-x4-x5-x6-x7-x8-x9-x10 Add only one statement to the line 8. As a result of that the program writes “-x1-x7” to the standard output. Java puzzle 1: A simple loop Level: Normal Author: Sar Maroof Answer explanation The loop of this […]

Sarmaroof © 2017