Category: Last published

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 will the following code print when compiled and run? 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. […]

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 replace the operators with the one of your choice to test it. Java quiz 33: Conditional vs bitwise operator Level: Normal What is the output of the following code? […]

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 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 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 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 5: Annotations

Annotations are introduced in Java 5 to provide a mechanism that allows you to write info about your code directly in the code. You can use annotations  within packages, classes, methods, variables and parameters. Annotations syntaxes and elements The character @ is used as prefix of the annotation name. In that way you indicate the […]

Java 5: Generics

The generics is introduced in Java 5 to provide a type-safety at compile time and to avoid the ClassCastException at runtime. By using generics for example you can prevent adding an Integer to a Java collection of Strings. Generic wildcards. The Unknown Wildcard List<?> means the list type is unknown. This could be a List<Integer>, […]

Java 5: Enum types

The enum type is a new feature in Java 5. Whenever you have a fixed set of constants you can use the enum type. An example is the seasons of the year, the days of the week and the planets of the solar system. We use the uppercase letters for the names of an enum […]

Java 5: Autoboxing and unboxing

The Conversion between the primitive data types and their corresponding object wrappers directly is a new feature in Java 5. The conversion from primitive data types to their object wrappers is called autoboxing, but converting of the other way around is called unboxing. For more info click here to read Oracle’s explanation. Example 1: Autoboxing and unboxing […]

Java 5: Static import

To access a static member in earlier Java versions we needed to write the class name first followd by the name of the static member. For example to access the static method max of the class Math, we wrote the statement as follows: Math.max(3, 6). The static import allows you to access the method max […]

Java 5: Varargs

Varargs allows you to avoid writing a method several times for multiple arguments. Before this feature, the solution was overloading methods. If the number of the arguments is unknown, you can use Varargs as shown in the exercise below. For more info click here to read Oracle’s explanation. Note: By applying this feature to different types of […]

Java 5: Enhanced for loops

Enhanced for loops is introduced in Java 5. This feature allows you to handle loops in a simple. In the example below we compare the new for loop with the older one! For more info click here to read Oracle’s explanation. Example 1: Enhanced for loops code What is the output of the following code? Author: Sar […]

Java 7: Underscores in numeric literals

The underscores in numeric literals is included in JSE7. You can use the underscore character between digits in a numerical literals similar to a comma or a space as a separater. This feature is used to improve the readability of the code. For more info click here to read Oracle’s explanation. Notice: Underscores have to be located […]

Java 7: Type inference

Type inference is included in JSE7. As long as the compiler can infer the arguments type from the context, you can use an empty set of parameters type <>. We call this pair of angle brackets the diamond. See the example below! For more info click here to read Oracle’s explanation. Example 1: Type inference code What is […]

Java 7: Try with resources

The feature try with resources is included in JSE7. A resource is an object, could be a file or a connection, which must be closed when the program doesn’t use it anymore. By using this feature you ensure to close the resources after using them. I used the path “/home/gardun/Documents/mydoc” because I work on Linux, […]

Java 7: Catching multiple exceptions

In the following example we apply catching multiple exception types, which is a new feature in JSE7, In the earlier Java versions you could catch each exception type in a separate block, but in Java 7 you can catch multiple types of exceptions in one block. For more info click here to read Oracle’s explanation. Example 1: […]

Java 7: Using strings in switch statements

This is a simple feature in JSE7, which allows to pass a String to switch statements. That was not allowed in earlier Java versions. Let’s see how it works! For more info click here to read Oracle’s explanation. Example 1: The seasons in a switch statement What is the output of the following code? Author: […]

Java 7: Binary literals

This feature in JSE7 allows the primitive variables byte, short, int and long to express the binary number system by adding the prefix 0b or 0B to the number as shown in the following example. For more info click here to read Oracle’s explanation. Example 1: Binary literal code What is written to the standard […]

Java 8: forEach loop

Before you start to learn this Java 8 feature, I would recommend you first to read the feature Lambda expressions. Example 1: forEach loop using lambda expression What is written to the standard output as the result of executing the following code? Author: Sar Maroof Answer explanation The answer is: Porsche Ferrari Bugatti Mercedes-Benz Lamborghini

Java 8: Default methods

The feature default methods is included in JSE8. This feature allows you to add new defalut methods to your interfaces and ensure that the classes, which implement the interface remain compatible with the new code. It is not mandatory to override default methods unlike abstract methods. For more info click here to read Oracle’s explanation. Example 1: Default […]

Java 8: Method references

The feature method references is included in JSE8. The lambda expression is used sometimes to create anonymous methods. When a lambda expression only invoke an existing method, it is better to refer the method by using its name. For more info click here to read Oracle’s explanation. Kinds of method references Reference to a static method: […]

Java 8: Lambda expressions

Lambda expression is included in JSE8. It provides implementation of a functional interface, which is an interface that contains only one abstract method, but it may contain one or more default methods or static methods. This new feature is important, because it helps to write less code by programming. For more info click here to […]

Exercise 03: Default values of variables

Java exercise 03: Default values of variables Level: Very easy What is written to the standard output as the result of executing the following code? Author: Sar Maroof Answer explanation The variables height, width, wage, isBig and the object text are not initialized. If variables are not initialized the value of int = 0, double […]

Exercise 02: Simple for loop

The following code demonstrates a simple Java for loop. The level of this serie exercises is very easy. They are all designed for beginners. Java exercise 02: A simple for loop Level: Very easy What happens if the following code is compiled and run? Author: Sar Maroof Answer explanation The statement for(int i = 0; […]

Exercise 01: Simple conditional statement

I started this serie Java exercises, because some members wrote to me that the easy quizzes, puzzles and assignments are not easy for them. Therefore, I realized that this serie might help the beginners even better. In this serie I keep the code as simple as I can and hope that you guys can enjoy […]

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 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 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 […]

My recommendation for young programmers?

Dear visitors and members of my blog! Many young generations are very motivated to learn programming, but later some of them can’t keep that motivation. The reason is they discover that programming is not easy, but I assure that it is. The method to learn programming is not reading programming code as if you read […]

Sarmaroof © 2017