Foojay Today

Arrays and Object Reference Java Challenge Code Quiz

September 29, 2021

Arrays are objects in Java.

Variables in Java actually store references to the object, instead of the real object.

When we pass an object reference to a method, we are changing the object that is in the heap of the memory.

Considering the explanation above, can you solve the following Java Challenge? What is going to happen when you run the following code?

public class ArrayChallenge2 {
    public static void main(String... doYourBest) {
        int[] anyArray = new int[5];
        anyArray[0] = 0;
        anyArray[1] = 2;
        anyArray[2] = 4;
        anyArray[3] = 6;
        anyArray[4] = 8;

        int[] otherArray = anyArray;
        doSum(anyArray);
        doSum(otherArray);

        Arrays.stream(anyArray).forEach(System.out::println);
    }
    private static void doSum(int[] anyArray) {
        for (int i = 0; i < anyArray.length; i++) {
            anyArray[i] = anyArray[i] + 2;
        }
    }
}

a) 0
2
4
6
8

b) 4
6
8
10
12

c) 2
4
6
8
10

d) 0
0
0
0
0

e) ArrayOutOfBoundsException will be thrown

Warning: Try to answer the correct alternative BEFORE seeing the answer! Only then you will refine your Java skills!

The correct answer is... what do you think?

Now let’s understand what happens in the code above!

The key concept here is that we are using object references instead of real objects!

After executing the following code:

int[] anyArray = new int[5]; // We are instantiating an Array object here.
int[] otherArray = anyArray; // We are assigning the reference to the same Array object.
// ….we have 2 variables pointing to the same object.

Arrays in Java are objects, so when we pass an Array to a method we are accessing the object. Any change made inside the doSum method is going to reflect the Array object. Therefore, we will be applying the sum of 2 on the same Array object twice.

At the line we are printing the result of this Java Challenge, we are transforming anyArray to a stream. Then we are using the forEach method from the Stream class to print each element from anyArray. We are using the concept of method reference which is a syntax sugar to use less code and do more in Java. In short, the System.out::println code will print every element from anyArray that is being iterated in the stream.

Arrays.stream(anyArray).forEach(System.out::println);

If you prefer, you can watch the FULL explanation video about several Java quizzes on the Java Challengers channel:

Don’t forget to stay constantly breaking your limits! Don’t hesitate to put a comment if you have any questions!

You can also check the original Java Challenge and take a look at the whole content of the Java Challengers initiative:
https://javachallengers.com/arrays-and-object-reference-java-challenge/

Topics:

Related Articles

View All
  • Asynchronous CompletableFuture San Francisco Adventure Java Challenge

    The Completable Future feature is powerful for better performance when running asynchronous methods.

    In Java 5, there is the Future interface, however, the Future interface doesn’t have many methods that would help us to create robust code.

    To solve the limitations of the Future interface, we have the CompletableFuture API with methods that will help us to build reliable high-performant software.

    Now that we had the intro about CompletableFuture, let’s go to the Java Challenge!

    Read More
    Jun 01, 2021
  • Advanced Polymorphic Overloading Java Challenge

    Overloading is one of the core Object-Oriented programming concepts which is highly important to master. It’s an important fundament that can make our code more readable and easier to maintain, therefore with fewer bugs when applied appropriately. In this Java …

    Read More
    Sep 28, 2021
  • Daemon Thread Java Code Quiz

    When we are working with Threads, it’s important to know when to use a daemon or a non-daemon Thread.

    Do you know if the main Thread is a daemon or not? And do you know what a daemon Thread is?

    That’s what you will find out by trying out the following Java Challenge!

    Read More
    Sep 08, 2021

Author(s)

  • Rafael del Nero

    Java Champion, a passionate and creative developer who possesses strong technical knowledge and delivers solid systems focused on Spring and Java EE specifications for cloud applications. More than 10 years' ... Learn more

Comments (0)

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

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard