Arrays & Strings

1.What will be the output of the following Java code?
public class Main { public static void main(String[] args) { int[] arr = new int[5]; System.out.println(arr[2]); } }
  • A. 0
  • B. Garbage value
  • C. Compilation error
  • D. Runtime exception

0

0

2.What is the correct way to declare and initialize a Java array?
  • A. int arr = new int(5);
  • B. int arr[] = {1, 2, 3, 4, 5};
  • C. int arr = {1, 2, 3, 4, 5};
  • D. array arr = new int[5];

int arr[] = {1, 2, 3, 4, 5};

int arr[] = {1, 2, 3, 4, 5};

3.What will happen if you access an array index out of its bounds?
  • A. Compilation error
  • B. The program will run but give incorrect results
  • C. ArrayIndexOutOfBoundsException will be thrown
  • D. The program will execute normally

ArrayIndexOutOfBoundsException will be thrown

ArrayIndexOutOfBoundsException will be thrown

4.How do you determine the length of an array in Java?
  • A. arr.length()
  • B. arr.size()
  • C. arr.length
  • D. arr.count()

arr.length

arr.length

5.What will be the output of this code?
public class Main { public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5 }; System.out.println(arr[arr.length - 1]); } }
  • A. 1
  • B. 5
  • C. Compilation error
  • D. ArrayIndexOutOfBoundsException

5

5