C#.NET arrays — Which of the following is the correct way to obtain the total number of elements in the array? int[] intMyArr = { 25, 30, 45, 15, 60 }; Consider these candidates: 1) intMyArr.GetMax 2) intMyArr.Highest(0) 3) intMyArr.GetUpperBound(0) 4) intMyArr.Length 5) intMyArr.GetMaxElements(0)

Difficulty: Easy

Correct Answer: 3, 4

Explanation:


Introduction / Context:
In C#.NET, arrays expose members that help you discover their shape. Two commonly discussed members are Length (the total element count) and GetUpperBound(dimension) (the highest valid index along a dimension). This question asks which options are valid approaches to determine how many elements the given one-dimensional array actually contains.



Given Data / Assumptions:

  • The array is intMyArr = { 25, 30, 45, 15, 60 } which contains 5 integers.
  • We evaluate five candidate API calls listed in the stem.
  • We must identify the methods/properties that can be used to compute the count.


Concept / Approach:
For any array a, a.Length returns the total number of elements. For a one-dimensional array, a.GetUpperBound(0) returns the last valid index (zero-based). Therefore, a.GetUpperBound(0) + 1 equals the count. Names like GetMax, Highest, or GetMaxElements are not valid .NET array APIs.



Step-by-Step Solution:

Check 4) a.Length → directly gives 5 (the element count).Check 3) a.GetUpperBound(0) → returns 4; you can compute count using 4 + 1 = 5.Check 1), 2), 5) → not valid members of System.Array for C# arrays.


Verification / Alternative check:
Printing intMyArr.Length yields 5. Printing intMyArr.GetUpperBound(0) yields 4, confirming that adding one gives the count.



Why Other Options Are Wrong:

  • 1, 2 and 1, 5: These identifiers do not exist for arrays.
  • 3, 5 and 4, 5: Include a non-existent API (5).


Common Pitfalls:
Confusing final index (upper bound) with element count; remember to add one when using GetUpperBound for 1-D arrays.



Final Answer:
3, 4

More Questions from Arrays

Discussion & Comments

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