LearnFrenzy
Sign in Take a free test

Arrays in Java · Exercise 3 of 4

Arrays in Java Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program contains an array mistake that javac rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        int[5] marks;
        System.out.println("declared");
    }
}
A marks must be initialised
B The size belongs to the creation expression, not the type: write int[] marks = new int[5];
C 5 is too small
D Arrays cannot be declared in main
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        int[] scores = {70, 80, 90};
        for (int i = 0; i < scores.length(); i++) System.out.println(scores[i]);
    }
}
A i must start at 1
B For arrays, length is a field, not a method: use scores.length
C scores must be an ArrayList
D println cannot print ints
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        int[] ids = new int[3];
        ids[0] = "101";
        System.out.println(ids[0]);
    }
}
A Index 0 is invalid
B An int array can only hold ints; "101" is a String – write ids[0] = 101; or Integer.parseInt("101")
C ids must be final
D Arrays cannot hold numbers
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        int[] days;
        days = {31, 28, 31};
        System.out.println(days.length);
    }
}
A days must be final
B The short {…} form is allowed only in a declaration; in an assignment write days = new int[]{31, 28, 31};
C Arrays need at least 4 elements
D length is a method
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        int[] primes = new int[4]{2, 3, 5, 7};
        System.out.println(primes[3]);
    }
}
A primes must be long
B When an initialiser is given, the size comes from the values; write new int[]{2, 3, 5, 7} without the 4
C Only 3 primes are allowed
D primes[3] is out of range
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        int[][] grid = new int[][3];
        System.out.println(grid.length);
    }
}
A grid must be jagged
B The first (row) dimension is required; later dimensions may be omitted: new int[3][] or new int[2][3]
C 3 must be a variable
D 2D arrays need Arrays.fill
Question page & discussion
Question 7 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String>[] buckets = new ArrayList<String>[10];
        System.out.println(buckets.length);
    }
}
A 10 buckets is too many
B Java cannot create arrays of a parameterised type such as ArrayList<String> (generic array creation); use a List<List<String>> instead
C List must be imported twice
D buckets must be final
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        double[] prices = {9.5, 12.0};
        int[] rounded = prices;
        System.out.println(rounded[0]);
    }
}
A prices must be final
B double[] and int[] are unrelated types – there is no conversion between primitive array types; copy element by element with a cast
C rounded needs a size
D 9.5 cannot be rounded
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        String[] names = {"Asha", "Ravi"};
        for (String n : names) {
            System.out.println(names[n]);
        }
    }
}
A names must be an int array
B n is the element (a String), not an index; an array index must be an int – print n directly
C for-each cannot be used with Strings
D println needs two arguments
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    public static void main(String[] args) {
        int[] data = new int[10];
        long pos = 3;
        data[pos] = 7;
        System.out.println(data[3]);
    }
}
A data must be long[]
B Array indexes must be int (or a smaller type promoted to int); a long index needs a cast: data[(int) pos]
C 3 is out of range
D pos must be final
Question page & discussion
Up next · Exercise 4Java Array Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links