Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Quiz 17: Working With Arrays

DZone's Guide to

Java Quiz 17: Working With Arrays

See the answer to our previous quiz on garbage collection eligibility and try your hand at today's quiz, which deals with arrays.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Before we start with this week's quiz, here is the answer to Java Quiz 16: Eligible Objects for Garbage Collection.

When there are no references pointing to an object, that object can be eligible for garbage collection.

  1. At line 5, the object car points to another object, but that doesn't make the object on line 1 eligible for garbage collection. The reason is that after the third statement ct.myMethod(car);, the object c is also pointed to the object car. The object car2 at line 4 is also pointed to the object car. The object car2 at line 7 is now pointed to null, but the reference c is still pointed to the object at line 1.

    After line 8, the reference c is pointed to null. Therefore, the object car at line 1 is eligible for garbage collection after line 8.

  2. You can demonstrate that by using the following statement before and after line 8 System.out.println(ct.c.brand);. The first one writes BMW to the standard output, while the second statement causes NullPointerException.

Here Is the Java Quiz for Today!

What happens when the following program is compiled and run?

public class MyClass
{
    public static void main(String[] args)
    {
        int[] arrA = {3, 5, 0, 1, 6, 4, 2};
        int[] arrB = {2, 6, 5, 4, 3, 1, 0};
        int[] arrC = {6, 1, 4, 0, 5, 3, 2};
        System.out.print(arrA[(arrC = arrB)[(arrA = arrC)[4]]]);
    }
}


Select the correct answer.

  • A. This program writes "2" to the standard output.

  • B. This program writes "6" to the standard output.

  • C. This program writes "0" to the standard output.

  • D. This program writes "5" to the standard output.

  • E. This program writes "4" to the standard output.

  • F. Compilation error.

  • G. Runtime error.

The correct answer and its explanation will be included in the next quiz in two weeks! For more Java quizzes, puzzles, and assignments, take a look at my site!

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,garbage collection ,arrays ,quiz

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}