What is the result?

Given the code fragment:

What is the result?
A. 1 2 3 4followed by an ArrayIndexOutOfBoundsException
B. 1 2 3
C. 1 2 3 4
D. Compilation fails.

Download Printable PDF. VALID exam to help you PASS.

8 thoughts on “What is the result?

  1. instead of int[] arr=(1,2,3,4);
    it can be int [] arr={1,2,3,4};
    then the answer should be option B

  2. D:
    # javac Test11.java
    Test11.java:3: error: ‘)’ expected
    int[] arr = (1, 2, 3, 4);
    ^
    Test11.java:3: error: expected
    int[] arr = (1, 2, 3, 4);
    ^
    Test11.java:3: error: illegal start of expression
    int[] arr = (1, 2, 3, 4);
    ^
    Test11.java:3: error: ‘;’ expected
    int[] arr = (1, 2, 3, 4);
    ^
    Test11.java:3: error: illegal start of expression
    int[] arr = (1, 2, 3, 4);
    ^
    Test11.java:3: error: ‘;’ expected
    int[] arr = (1, 2, 3, 4);
    ^
    Test11.java:3: error: illegal start of expression
    int[] arr = (1, 2, 3, 4);
    ————————————————————-
    its needed to use curly braces {} for array initialization then it will work with following result:

    # javac Test11.java
    # java Test11
    1 2 3

  3. Answer D.

    It will fail to compile because of bad syntax of declaring and initializing the array (in the second line of the code).

    11
  4. instead of int[] arr=(1,2,3,4);
    it can be int [] arr={1,2,3,4};
    then the answer should be option B

  5. The answer should be D
    Compilation fails because the array is not declared properly
    it should be
    int[] arr = new int[]{1, 2, 3, 4};

    In case the array eas declared properly, this code should print 1 2 3 (answer B)

    6
    1

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.