In Java, which of the following array declarations are possible: (1) an array of integers, (2) an array of String objects, (3) a multidimensional array (array of arrays), (4) a single array mixing integers and Strings?

Difficulty: Easy

Correct Answer: 1, 2 and 3 only

Explanation:


Introduction:
This question tests understanding of Java array typing rules: homogeneity, object arrays, and multidimensional arrays. Knowing what is and is not type-safe in Java is essential for avoiding ClassCastException and compile-time errors.


Given Data / Assumptions:

  • Statements under consideration: 1 (int[]), 2 (String[]), 3 (e.g., int[][]), 4 (one array mixing ints and Strings).
  • Java is statically typed; array element types are fixed at creation.


Concept / Approach:

Java arrays are homogeneous: every element has the same declared type. int[] holds primitives of type int only; String[] holds references to String objects only. Multidimensional arrays (like int[][] or String[][]) are fully supported. A single array mixing primitives and reference types (e.g., ints and Strings) is not allowed. An Object[] could hold both, but then integers would be boxed as Integer and the statement specifically says an array of integers and Strings together, which is not a single strongly typed array of those two distinct types.


Step-by-Step Solution:

Step 1: Evaluate (1): int[] is valid in Java.Step 2: Evaluate (2): String[] is valid in Java.Step 3: Evaluate (3): Multidimensional arrays (e.g., int[][]) are valid.Step 4: Evaluate (4): A single array mixing ints and Strings is not valid as a Java array type; Java requires a single element type.


Verification / Alternative check:

You can compile small snippets: int[] a = {1, 2}; String[] s = {"a", "b"}; int[][] m = {{1,2},{3,4}}; // all OKObject[] mix = {1, "x"}; // compiles, but this is not an 'array of integers and strings'; it is an Object[] with boxing


Why Other Options Are Wrong:

'All' – wrong because (4) is invalid. '1 and 2 only' – ignores valid multidimensional arrays. '1, 3 and 4 only' – includes invalid (4) and excludes (2).


Common Pitfalls:

Confusing Object[] (heterogeneous) with a typed array; forgetting autoboxing implications; mixing primitives and references in a single typed array.


Final Answer:

1, 2 and 3 only

More Questions from Microprocessors

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion