Java quiz 31: Demonstration of arrays

<< Java Quizzes   Green = Easy,  Blue = Normal,  Red = Hard
 Q25   Q26   Q27   Q28   Q29   Q30   Q31   Q32   Q33   Q34 
 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!

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 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

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

Sarmaroof © 2017