Java Array MCQs: 10 Solved Questions on Indexing, Defaults and References

Trace all 10 Java array questions. Learn to separate compile-time errors, bounds errors, defaults, nested indexing and shared-array mutation.

KnowledgeGate Team

Exam prep & CS education

Updated 31 Oct 20267 min read
Java Array MCQs: 10 Solved Questions on Indexing, Defaults and References

The questions test zero-based indexing, default values, reference aliasing, method calls and nested subscripts. For the wider programming route, start with Coding & DSA.

Java array MCQs: use a four-pass trace before choosing

Use the same four passes on every question:

  1. Check whether the code as printed is valid Java.

  2. Write the array length and legal indices.

  3. Mark which variables point to the same array object.

  4. Evaluate nested brackets from the inside out.

For int[] a = {4, 7, 9}, the legal indices are 0, 1, 2; a[1] = 7, while a[3] throws ArrayIndexOutOfBoundsException. Keep that run-time error separate from the compile-time error in X.length() and the null default in a new String[].

Java array defaults and initialization: Q3, Q4 and Q10

Q3. Default element in a newly allocated String array (UPLT 2018) (module page)

java
String[] var1;
var1 = new String[3];
System.out.println(var1[1]);
  • (a) an empty string

  • (b) null

  • (c) 0

  • (d) a garbage character

Answer: (b) null. Java reference-array elements default to null. String[] var1 = new String[3] creates three reference slots, all initialized to null, so var1[1] is neither "", 0 nor a garbage value.

Q4. The untouched third slot (RPSC Programmer - P2 2024)

What will be the value of arr[2] in the following code?

java
 String[] arr = new String[3];
 arr[0] = "Maths";
 arr[1] = "Science";
  • (a) Null

  • (b) "Science"

  • (c) "Maths"

  • (d) " "

Answer: (a) Null. The Java value is spelled null. After the assignments, the slots are arr[0] = "Maths", arr[1] = "Science", and arr[2] = null, which is different from the one-space string " ".

Q10. The same default-value trap in another paper (UPPSC Polytechnic Lecturer 2018)

java
String[] var1;
var1 = new String[3];
System.out.println(var1[1]);
  • (a) an empty string

  • (b) null / (null)

  • (c) 0 / शून्य

  • (d) a garbage character

Answer: (b) null / (null). This independent exam item repeats Q3's default-value check almost exactly. In the corrected Java form, every newly allocated String[] slot starts as null, so var1[1] prints null deterministically.

Java array declarations and length syntax: Q6 and Q7

Q6. One-dimensional double array as a method parameter (RPSC Programmer - P2 2024)

Code
Which of the following correctly declares a method that accepts one dimensional array of double as a parameter?
  • (a) public void fun(double *arr[])

  • (b) public void fun(double[] arr)

  • (c) public void fun(arr double[])

  • (d) public void fun(double arr)

Answer: (b) public void fun(double[] arr). Java can place brackets with the type or identifier, but it does not use a C-style pointer declarator. Option (d) accepts one scalar double, while (a) and (c) are invalid Java parameter syntax.

Q7. Array length is a field, not a method (Kendriya Vidyalaya Sangathan 2017)

What correction is required for the following Java code snippet to compile?

java
 int [] X = new int [10];
 for (int P = 0; P < X.length(); P++)
     X [P] = 5;
  • (a) X[P] = 5 should be X(P) = 5

  • (b) P <= X.length() should be P < X.length()

  • (c) X.length() should be X.length

  • (d) P++ should be P+1

Answer: (c) X.length() should be X.length. The corrected loop is for (int P = 0; P < X.length; P++) X[P] = 5;. It assigns 5 at indices 0 through 9, then stops when P becomes 10; an array has a length field, while a String has a length() method.

Arrays in C: Declaration, Initialization and Worked Examples also uses brackets, but Java arrays have a length field and reference semantics rather than C pointer rules.

Java zero-based indexing and array traversal: Q2, Q5 and Q9

StringBuffer is not an array, but both structures use zero-based positions. Q2 tests that transferable indexing rule; Q5 and Q9 apply indexing to arrays directly.

Q2. Zero-based mutation in a StringBuffer (RSSB SCI - Paper 2 2022)

What will be the output of the program?

java
 public class Test2 {
   public static void main (String [ ] args) {
     StringBuffer S1 = new StringBuffer("Complete");
     S1.setCharAt(1, 'i');
     S1.setCharAt(7, 'd');
     System.out.println(S1);
   }
 }
  • (a) Complete

  • (b) lomplede

  • (c) Cimpletd

  • (d) Coipletd

Answer: (c) Cimpletd. StringBuffer uses zero-based positions. Index 1 in Complete is o, and index 7 is the final e; replacing them with i and d produces Cimpletd.

Q5. Enhanced for traversal preserves array order (RPSC Programmer - P2 2024)

What is the output of the following code?

java
 public class ex {
     public static void main(String[] args) {
         int[] a = {1, 2, 3, 4, 5};
         print(a);
     }
     public static void print(int[] a) {
         for(int n : a) {
             System.out.print(n + " ");
         }
     }
 }
  • (a) Compilation error

  • (b) 5 4 3 2 1

  • (c) Runtime error

  • (d) 1 2 3 4 5

Answer: (d) 1 2 3 4 5. The enhanced loop visits n = 1, then 2, 3, 4, and 5, preserving array order without reversing or sorting. The program prints a trailing space after 5, which the answer choice does not show.

Q9. Evaluate nested subscripts from inside to outside

What will be output for the following code?

java
class Main
{
         public static void main(String args[])
        {
                int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
                int n = 6;
               n = arr[arr[n] / 2];
               System.out.print(n);
         }
}
  • (a) 3

  • (b) 0

  • (c) 6

  • (d) 1

Answer: (a) 3. Start with n = 6. First, arr[n] = arr[6] = 6; integer division gives 6 / 2 = 3; then arr[3] = 3, and that final value is assigned to n.

Java array references and method mutation: Q1 and Q8

Q1. Element copy versus reference assignment (UGC NET Computer Science, January 2017)

Three snapshots show array copied into p, q aliasing p, and index 4 producing output 18:20.

Given the array of integers ‘array’ shown below:

Code
array: 13 | 7 | 27 | 2 | 18 | 33 | 9 | 11 | 22 | 8

What is the output of the following JAVA statements?

int[] p = new int [10];

int[] q = new int [10];

for (int k=0; k< 10; k++)

    p[k]=array [k];

q=p;

p[4]=20;

System.out.println(array[4]+":"+q[4]);
  • (a) 20 : 20

  • (b) 18 : 18

  • (c) 18 : 20

  • (d) 20 : 18

Answer: (c) 18 : 20. The loop copies all ten values into a distinct p array, but q = p discards q's first reference and makes q and p aliases for the copied array. Therefore p[4] = 20 also means q[4] = 20, while the original array[4] remains 18; the exact printed form is 18:20.

Three snapshots of the Q1 arrays: the copy into p, then q aliasing p, then index 4 holding 18 in array but 20 in q.

Q8. Passing an array to a method passes the reference value (UGC NET Computer Science, July 2018)

What is the output of the following JAVA program?

java
class simple

{

    public static void main(String[ ] args)

    {

        simple obj = new simple();

        obj.start();

    }

    void start()

    {

        long [] P = {3, 4, 5};

        long [] Q = method (P);

        System.out.print (P[0] + P[1] +P[2]+":");

        System.out.print (Q[0] + Q[1] + Q[2]);

    }

    long [ ] method (long [ ] R)

    {

        R[1] = 7;

        return R;

    }

} //end of class
  • (a) 12:15

  • (b) 15:12

  • (c) 12:12

  • (d) 15:15

Answer: (d) 15:15. R receives the same array reference as P, changing the one object from {3, 4, 5} to {3, 7, 5}. The returned reference becomes Q, and each sum is 3 + 7 + 5 = 15.

What these Java array MCQs are actually testing

Q3, Q4 and Q10 test defaults; Q6 and Q7 test syntax; Q2, Q5 and Q9 test positions; Q1 and Q8 test aliasing and mutation.

Trap

Exact result to remember

new String[3]

Three null references

X.length

The array field

q = p

Both variables alias the copied array

arr[arr[n] / 2] with n = 6

arr[6] = 6, 6 / 2 = 3, then arr[3] = 3

Where a question is printed with typing errors, fix the obvious slips first and then trace the intended code.

Java array MCQs: the short version and next step

Remember: arrays are zero-indexed, reference slots default to null, arrays use the length field, assignments can create aliases, and methods can mutate shared arrays.

Now re-trace Q1 after changing only p[4] = 20 to array[4] = 30. The copy into p already preserved 18 at index 4, and q still aliases p, so the two reads are array[4] = 30 and q[4] = 18. The new output is 30:18.

Continue with the Java Course, Concepts, MCQs and Coding Questions, then practise one-operation-at-a-time tracing with Stacks and Queues MCQs: 12 Solved (GATE).