Java Arrays

Last Updated :
Discuss
Comments

Question 1

Which of the following is the correct way to declare an array in Java?


  • int arr[5];

  • int arr = new int(5);

  • int[] arr = new int[5];

  • array<int> arr = new int[5];

Question 2

What will be the output of the following code?

Java
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[2]);
  • 1

  • 2

  • 3

  • 4

Question 3

What is the default value of an integer array in Java?


  • null

  • 0

  • garbage value


  • depends on JVM

Question 4

Which of the following is used to determine the length of an array in Java?

  • arr.length();

  • arr.size;

  • arr.length;

  • arr.count();

Question 5

What is the time complexity for accessing an element in an array by index?

  • O(1)

  • O(n)

  • O(log n)

  • O(n^2)

Question 6

Which of the following creates an array of 5 integers?

  • int arr[] = new int[5];

  • int arr[5];

  • int arr() = 5;

  • int[5] arr;


Question 7

How do you access the third element of an array arr?


  • arr(3)

  • arr[3]

  • arr[2]

  • arr{3}

Question 8

How many rows and columns are in a 2D array int[][] arr = new int[3][4];?


  • 3 rows, 4 columns

  • 4 rows, 3 columns


  • 3 elements

  • 7 elements

Question 9

Which of these best defines a multidimensional array in Java?


  • int arr[][]


  • int[] arr[]


  • Both a and b

  • int arr(2)(3)

There are 9 questions to complete.

Take a part in the ongoing discussion