Mphasis Previous Years Solved Sample Placement Papers
-
To declare an array, define the variable type with ___.
A: Square brackets []
B: Curly brackets {}
C: Round brackets ()
D: None of the above
Ans: AExplanation:
To declare an array, define the variable type with square brackets [].
-
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { string[] mobiles = { "iPhone", "Samsung", "Vivo"}; Console.WriteLine(mobiles); } } }
A: "iPhone", "Samsung", "Vivo"
B: {"iPhone", "Samsung", "Vivo"}
C: string[]
D: System.String[]
Ans: DExplanation:
In the above code, the statement
Console.WriteLine(mobiles);
will print the type of the variable that isSystem.String[]
. -
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { string[] mobiles = {"iPhone", "Samsung", "Vivo"}; Console.WriteLine(mobiles[-1]); } } }
A: None
B: Warning
C: Exception
D: System.String[]
Ans: CExplanation:
The array index starts from 0 in C#. In the above code, we are trying to access an element with index -1, resulting in an exception:
System.IndexOutOfRangeException
. -
Which statement is correct about the following C# statement?
int[] x = {10, 20, 30};
A: 'x' is a reference to the array created on stack
B: 'x' is a reference to an object created on stack
C: 'x' is a reference to an object of a 'System.Array' class
D: None of the above
Ans: CExplanation:
In the above given C# statement, the variable
x
is a reference to an object of aSystem.Array
class. -
Which array property is used to get the total number of elements in C#?
A: Len
B: Length
C: Elements
D: MaxLen
Ans: BExplanation:
The
Length
property is used to get the total number of elements in C#. -
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { string[] mobiles = {"iPhone", "Samsung", "Vivo"}; Console.WriteLine(mobiles[0] + mobiles[2]); } } }
A: iPhoneVivo
B: iPhone+Vivo
C: Exception
D: iPhone Vivo
Ans: AExplanation:
The statement
mobiles[0] + mobiles[2]
concatenates the 0th and 2nd elements, resulting in"iPhoneVivo"
. -
Which array method is used to sort an array alphabetically or in ascending order in C#?
A: sort()
B: sorting()
C: Sort()
D: Sorting()
Ans: CExplanation:
The
Sort()
method is used to sort an array alphabetically or in ascending order in C#. -
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { int[,] ARR = {{1,2},{3,4}}; Console.WriteLine(ARR.GetLength(0) + "," + ARR.GetLength(1)); } } }
A: 4,4
B: 2,2
C: Compilation Error
D: Runtime Error
Ans: BExplanation:
The code prints the dimensions of the 2D array. The first dimension (rows) and the second dimension (columns) both have lengths of 2, so the output is
2,2
. -
Which is the correct syntax to declare an array with 2 rows and 3 columns in C#?
A:
int arr[2][3] = new int[2][3];
B:
int arr[2,3] = new int[2,3];
C:
int[,] arr = new int[2,3];
D:
Ans: Cint [,]arr = new [2,3]int;
Explanation:
The correct way to declare an array with 2 rows and 3 columns in C# is:
int[,] arr = new int[2,3];