Category: Java puzzles

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? Java puzzle 21: Using inheritance Level: Normal Author: Sar Maroof Answer […]

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