Java quiz 31: Demonstration of arrays

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
< Java Quizzes   Green = Easy,  Blue = Normal,  Red = Hard
Select    31   32   33   34   35   36   37   38   39   40   41   42   43   44   By title 
>

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?

 
public class MyArray
{
  public static void main(String[] args)
  {
    int[] arrA = {3, 5, 0, 1, 6, 4, 2};
    int[] arrB = {2, 0, 6, 4, 5, 3, 1};
    System.out.print(arrB[(arrA = arrB)[3]]);
  }
}
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 “4” to the standard output.
d. This program writes “1” to the standard output.
e. This program writes “3” to the standard output.
f. Runtime exception.
g. This code fails to compile.

For the answer and the explanation you can join the discussion below!




Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

3 Comments

Add a Comment
  1. Answer explanation

    1. In the expression arrB[(arrA = arrB)[3]],
    the expression arrB is evaluated before the expression arrA = arrB)[3];
    2. The statement System.out.print(arrB[(arrA = arrB)[3]]); results as the statement
    System.out.print(arrB[(arrB)[3]]);
    3. arrB[3] is equal to 4. Remember that array index starts with 0.
    4. The statement arrB[4] is equal to 5. Therefore the correct answer is 5.

    The correct answer is: b.

  2. I am not sure whether this allowed in Java (arrA = arrB)[3]], and if that is allowed what does it mean exactly?

    1. The statement you mentioned is allowed in Java.

Leave a Reply to Kumar Cancel reply

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