Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Arrays Aptitude Questions and Answers | Set 3
C# Arrays Aptitude Questions | Set 3: This section contains aptitude questions and answers on C# Arrays.
Submitted by Nidhi, on April 01, 2020
1) What is the correct way to access each element of an array without using an index variable?
- Using do-while loop
- Using for loop
- Using while loop
- Using a foreach loop
Correct answer: 4
Using a for each loop
In C#.NET, using a foreach loop, we can access each element of an array without using the index variable.
2) There are the following options are given below, which of them are the correct way to initialize an array of three integers?
-
int []arr;
arr = new int[3];
arr[0]=1;
arr[1]=1;
arr[2]=1;
-
int []arr = {1,2,3};
-
int []arr;
arr = new int {1,2,3};
-
int []arr;
arr = new int[3] {1,2,3};
Options:
- Only A
- Only B
- Only D
- A, B, D
Correct answer: 4
A, B, D
A, B and D are the correct ways to initialize an array of an integer with 3 elements.
3) What is the correct output of given code snippets?
static void Main(string[] args)
{
int[] ARR = {1,2,3,4,5};
foreach (int X in ARR)
{
Console.Write(X + " ");
}
}
- 1 2 3 4 5
- 0 0 0 0 0
- Syntax Error
- Runtime Exception
Correct answer: 1
1 2 3 4 5
The above code will print "1 2 3 4 5" on console screen.
4) If we created an array of 5 integers, can we increase array size from 5 to 10?
- Yes
- No
Correct answer: 1
Yes
Yes, we can increase the size of the already created array.
5) What is the correct output of given code snippets?
static void Main(string[] args)
{
int[] ARR = {1,2};
ARR = new int[4];
foreach (int X in ARR)
{
Console.Write(X + " ");
}
}
- 1 2 0 0
- 0 0 0 0
- Syntax Error
- Runtime Error
Correct answer: 2
0 0 0 0
The above code will print "0 0 0 0" on console screen.