Arrays Questions
Practice Arrays MCQs with answers and explanations. Page 1 of 2.
Category
C# Programming
Topic
Arrays
Page
1 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
C#.NET — Consider the declaration:
int[, ] intMyArr = { {7, 1, 3}, {2, 9, 6} };
Which statements about intMyArr are correct?
Open
View answer
C#.NET — For the declaration below, what does intMyArr represent?
int[][][] intMyArr = new int[2][][];
Open
View answer
C#.NET — Complete the foreach so that all elements of the jagged array are printed:
int[][] a = new int[2][];
a[0] = new int[4] { 6, 1, 4, 3 };
a[1] = new int[3] { 9, 2, 7 };
foreach (int[] i in a)
{
/* Add loop here */
Console.Write(j + " ");
Console.WriteLine();
}
Open
View answer
C#.NET — Which declarations correctly define a 2×3 rectangular array?
Open
View answer
C#.NET — Array initialization of 4 integers: identify all syntactically correct ways (size and values must be 25, 30, 40, 5).
Consider the following five code fragments (numbered 1–5). Which ones correctly define and initialize an array of exactly four integers with values 25, 30, 40, and 5, without indexing errors?
int[] a = {25, 30, 40, 5};
int[] a;
a = new int[3];
a[0] = 25;
a[1] = 30;
a[2] = 40;
a[3] = 5;
int[] a;
a = new int{25, 30, 40, 5};
int[] a;
a = new int[4]{25, 30, 40, 5};
int[] a;
a = new int[4];
a[0] = 25;
a[1] = 30;
a[2] = 40;
a[3] = 5;
Open
View answer
C#.NET — Multidimensional arrays: compute .Length for a 3×2×3 array.
Given:
int[ , , ] a = new int[3, 2, 3];
Console.WriteLine(a.Length);
What value is printed?
Open
View answer
C#.NET — Jagged arrays: determine which statements are correct for the following code.
Code:
int[][] intMyArr = new int[2][];
intMyArr[0] = new int[4]{6, 1, 4, 3};
intMyArr[1] = new int[3]{9, 2, 7};
Which statements are true?
intMyArr is a reference to a 2-D jagged array.
The two rows of intMyArr are stored in adjacent memory locations.
intMyArr[0] refers to the 0th 1-D array and intMyArr[1] refers to the 1st 1-D array.
intMyArr refers directly to intMyArr[0] and intMyArr[1].
intMyArr refers to intMyArr[1] and intMyArr[2].
Open
View answer
C#.NET — What does the declaration below represent? Choose the correct statements.
Code:
int[] intMyArr = {11, 3, 5, 9, 4};
Which are true?
intMyArr is a reference to an object of System.Array.
intMyArr is a reference to an object of a class that the compiler derives from System.Array.
intMyArr is a reference to an array of integers.
intMyArr is a reference to an object created on the stack.
intMyArr is a reference to an array created on the stack.
Open
View answer
C#.NET — “Resizing” an array variable from 5 to 10 elements: select the correct approach.
If a is an array of 5 integers, choose the correct way to make a refer to a 10-element array (ignoring data preservation).
Open
View answer
C#.NET — Pick the correct fact about arrays: rank, length, default values, and sorting.
Open
View answer
C#.NET — Arrays: storage location and type hierarchy for
int[] a = {11, 3, 5, 9, 4}.
Which statements are true?
The array elements are created on the stack.
The reference variable a is created on the stack (as a local).
The array elements are created on the heap.
Declaring the array creates a new array class derived from System.Array.
Whether the elements are on the stack or heap depends on the array size.
Open
View answer
C#.NET — Jagged array bounds: evaluate GetUpperBound on the second inner array.
Code:
int[][] a = new int[2][];
a[0] = new int[4]{6, 1, 4, 3};
a[1] = new int[3]{9, 2, 7};
Console.WriteLine(a[1].GetUpperBound(0));
Open
View answer
C#.NET — Trace nested loops and 2-D array assignments to predict output.
Program:
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int i, j;
int[ , ] arr = new int[2, 2];
for (i = 0; i < 2; ++i)
{
for (j = 0; j < 2; ++j)
{
arr[i, j] = i * 17 + i * 17;
Console.Write(arr[i, j] + " ");
}
}
}
}
}
Open
View answer
C#.NET — Arrays in .NET: rectangular vs jagged, adjacency, and System.Array methods.
Which statements are correct?
Arrays can be rectangular or jagged.
Rectangular arrays have “similar rows” stored in adjacent memory locations.
Jagged arrays do not have access to methods of System.Array.
Rectangular arrays do not have access to methods of System.Array.
Jagged arrays have dissimilar row lengths and rows are not stored adjacently like a single rectangular block.
Open
View answer
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)
Open
View answer
Pointer–pointer arithmetic and precedence in C: Determine the outputs printed by this program (track each change exactly).
#include<stdio.h>
int main()
{
static int arr[] = {0, 1, 2, 3, 4};
int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
int **ptr = p;
ptr++;
printf("%d, %d, %d
", ptr - p, *ptr - arr, **ptr);
*ptr++;
printf("%d, %d, %d
", ptr - p, *ptr - arr, **ptr);
*++ptr;
printf("%d, %d, %d
", ptr - p, *ptr - arr, **ptr);
++*ptr;
printf("%d, %d, %d
", ptr - p, *ptr - arr, **ptr);
return 0;
}
Open
View answer
Pointer equivalence for arrays in C: If the array arr begins at address 65486, what will printf output for these two expressions?
#include<stdio.h>
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u
", arr, &arr);
return 0;
}
Open
View answer
Evaluate pointer arithmetic on a 2x2 static matrix. What is printed by each expression?
#include<stdio.h>
int main()
{
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int p[] = {(int)a, (int*)a + 1, (int*)a + 2};
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
printf("%d, %d, %d, %d
", ((p + i) + j), ((j + p) + i), ((i + p) + j), ((p + j) + i));
}
}
return 0;
}
Open
View answer
Array length computation in C: For the array below, what does sizeof(arr)/sizeof(arr[0]) evaluate to?
#include<stdio.h>
int main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7};
printf("%d
", sizeof(arr) / sizeof(arr[0]));
return 0;
}
Open
View answer
Trace the effect of pointer updates in a function that receives an array parameter.
#include<stdio.h>
void fun(int, int[]);
int main()
{
int arr[] = {1, 2, 3, 4};
int i;
fun(4, arr);
for(i = 0; i < 4; i++)
printf("%d,", arr[i]);
return 0;
}
void fun(int n, int arr[])
{
int *p = 0;
int i = 0;
while(i++ < n)
p = &arr[i];
*p = 0;
}
What output is expected (apply standard, safe intent by correcting the off-by-one to the nearest reasonable interpretation)?
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.