Array structure Questions with Answers

7 practice questions on Array structure (C#) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Medium

What is output for following set of code?

static void Main(string[] args)
 {
char A = 'K';
char B = Convert.ToChar(76);
     A++;
     B++;
Console.WriteLine(A+ "  " +B);
Console.ReadLine();
 }
  1. M L
  2. U L
  3. L M
  4. A B

Answer: C. L M

Explanation

++ increments the value of character by 1. A and B are given values K and 76, when we use increment operator their values increments by 1, A and B becomes L and M.
Output: L, M.

Step-by-step solution

  1. Convert.ToChar(76) gives the character L, because 76 is its character code.
  2. A starts as K and B as L; A++ makes A into L, and B++ makes B into M.
  3. Console.WriteLine(A + two spaces + B) prints the characters in order: L then M.
  4. So the answer is option C, L M.

Why the other options are wrong

  • A. M L has the letters reversed.
  • B. U L is wrong; A becomes L and B becomes M after the increments.
  • D. A B is wrong; the ++ operator moves both letters on by one.

Tip: ++ on a char moves it to the next character code; char + string joins the text.

Question 2Medium

What is advantage of using 2D jagged array over 2D rectangular array?

  1. Easy intialization of elements.
  2. Allow unlimited elements as well as also rows which had 0? or are empty in nature.
  3. All of the mentioned.
  4. None of the mentioned

Answer: B. Allow unlimited elements as well as also rows which had 0? or are empty in nature.

Explanation

A jagged array lets each row have its own length, including zero, so it suits sparse data where many rows have few or no elements.

Step-by-step solution

  1. A jagged array is an array of arrays. Each row can have a different length.
  2. Rows can be of any length, including zero or empty rows. The storage does not have to be rectangular.
  3. So the advantage is that jagged arrays allow rows of different and empty sizes.

Why the other options are wrong

  • A. Jagged arrays need row-by-row setup, so easy initialisation is not their advantage.
  • C. Option A is not a real advantage, so 'All of the mentioned' is wrong.
  • D. Option B is a real advantage, so 'None of the mentioned' is wrong.

Tip: A rectangular array has the same number of columns in every row. A jagged array does not.

Question 3Medium

If a is an array of 5 integers then which of the following is the correct way to increase its size to 10 elements?

  1. int[] a = new int[5]; int[] a = new int[10];
  2. int[] a = int[5]; int[] a = int[10];
  3. int[] a = new int[5]; a.Length = 10;
  4. int[] a = new int[5]; a = new int[10];

Answer: D. int[] a = new int[5]; a = new int[10];

Explanation

int[] a = new int[5];
int[] a = new int[10];
its declaring it again, int[] a = int[5];
int[] a = int[10];
neways wrong array is never declared.
int[] a = new int[5];
a = new int[10];
is first declare int the array then giving it the new length..

Step-by-step solution

  1. int[] a = new int[5]; creates an array of 5 elements.
  2. An array cannot be resized in place; its Length property is read-only.
  3. Options A and B declare a again or use invalid syntax; option C assigns to the read-only Length.
  4. a = new int[10] replaces a with a new 10-element array, so the answer is option D.

Why the other options are wrong

  • A. Declaring a again with the same name is a compile error, so no 10-element array is given to a.
  • B. int[5] is not valid C# syntax for creating an array.
  • C. Length is read-only, so assigning 10 to it fails.

Tip: Arrays have a fixed size; to grow one, create a new array and copy the values (Array.Resize does both).

Question 4Medium

Which of the following are not types of Array in c#?

  1. Single-Dimensional
  2. MultiDimensional
  3. Jagged Array
  4. Jazzed Array

Answer: D. Jazzed Array

Explanation

In c# It is the simplest form of array, it's kind of a row with n columns where each column is accessed with the zero based index. The above two code examples show single dimension arrays. In C#, declaration of array is a bit different from C or C++. Here square brackets are placed after type name, then array-name, keyword new and then type with square brackets containing size of array.
Arrays can be multidimensional. The most widely used are two dimensional arrays, often Matrices form 2D arrays. In 2D array, 2 zero based index are used to access a particular value in the array. Array of arrays are called jagged arrays. The statement might be confusing but consider the example of saving marks of few students who are studying different number of subjects.
Student-1 marks 65, 60, 76
Student-2 marks 78, 92, 68, 90, 55
Student-3 marks 45, 59, 88, 72

Step-by-step solution

  1. Single‑dimensional arrays have one index per element.
  2. Multidimensional arrays have more than one index per element.
  3. Jagged arrays are arrays of arrays.
  4. Jazzed Array.

Why the other options are wrong

  • A. Single-dimensional is a real C# array type, with one index per element, so it is not the answer.
  • B. MultiDimensional arrays are a real C# type with more than one index, so this is not the answer.
  • C. Jagged arrays are a real C# type, arrays of arrays, so they are not the answer.

Tip: Only Single‑Dimensional, Multidimensional and Jagged are valid array types in C#.

Question 5Medium

What would be the signature of a method myfunc() that has a double array as a formal parameter.

returns an array of integers.

  1. public void myfunc(double[] a)
  2. public [] myfunc(double a)
  3. public intmyfunc(double a)
  4. public int[] myfunc(double[] a)

Answer: D. public int[] myfunc(double[] a)

Explanation

Only D has return type as integer array and parameter as double so it is the correct option.

Step-by-step solution

  1. Method name is myfunc with double array parameter.
  2. Return type must be int array, so use public int[].
  3. Parameter is double array, written as double[] a.
  4. Combine return type and parameter: public int[] myfunc(double[] a).
  5. Final answer: public int[] myfunc(double[] a)

Why the other options are wrong

  • A. Returns void but should return int array.
  • B. Invalid syntax for return type and parameter.

Tip: Remember: return type and parameter types must be precise.

Question 6Medium

What is output of following set of code?

static void Main(string[] args)
{
    Program p = new Program();
    p.display(2, 3, 8);
    int[] a = { 2, 56, 78, 66 };
    Console.WriteLine("example of array");
    Console.WriteLine("elements added are");
    p.display(a);
    Console.ReadLine();
}
public void display(params int[] b)
{
    foreach (int i in b)
    {
        Console.WriteLine("ARRAY IS HAVING:{0}", i);
    }
}
  1. Compile time error
  2. Run time error
  3. Code runs successfully but prints nothing
  4. Code runs successfully and prints given on console

Answer: D. Code runs successfully and prints given on console

Explanation

Object p makes a call to invoke function display()
and hence consecutively prints the output.
Array is declared with elements again object p makes a call to display() and hence, consecutively prints the output with given elements.

Output:

ARRAY IS HAVING:2
ARRAY IS HAVING:3
ARRAY IS HAVING:8
Elements added are:
ARRAY IS HAVING:2
ARRAY IS HAVING:56
ARRAY IS HAVING:78
ARRAY IS HAVING:66

Step-by-step solution

  1. The call p.display(2, 3, 8) passes three values to the params int array, so three lines print.
  2. The array a with four values is passed to display, so four more lines print (the 'for each' typo reads as foreach).
  3. The code is valid and runs with console output, so the correct answer 3 is correct.

Why the other options are wrong

  • A. The code is valid C#, so there is no compile error.
  • B. No run-time error occurs; both calls work.
  • C. The loop prints every element, so there is output.

Tip: Params lets a method take any number of values, or accept an array passed directly.

Question 7Medium

In C# Rank property is used to find the dimension of an array. What would be the output of following C# code?

using System;

namespace JaggedArrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declaration and initialization of jagged array
            int[][] arr = new int[3][];

            Console.WriteLine(arr.Rank);
            Console.ReadKey();
        }
    }
}
  1. 0.
  2. 1
  3. 2
  4. Compilation error

Answer: B. 1

Explanation

arry is not two dimensional arrays it is one dimensional jagged array.

Step-by-step solution

  1. int[][] arr = new int[3][] creates a jagged array, which is a one-dimensional array of arrays.
  2. Rank counts the dimensions of the outer array.
  3. So arr.Rank is 1, the correct answer.

Why the other options are wrong

  • A. Rank is at least 1 for any array, so 0 is wrong.
  • C. The outer array has one dimension; it is not a two-dimensional array.
  • D. The code compiles, so there is no compilation error.

Tip: Jagged arrays (int[][]) have Rank 1; rectangular arrays (int[,]) have Rank 2.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics