Array - Study Mode

[#51] What is the correct way to initialize a two-dimensional array in Java?
Correct Answer

(A) int[][] myArray = {{1, 2}, {3, 4}}

[#52] Which of the following is a valid declaration and initialization of a String array in Java?
Correct Answer

(D) Both A and B

Explanation

Solution: Option A initializes a String array named "names" with three elements: "Alice", "Bob", and "Charlie". This is a valid and commonly used syntax for declaring and initializing a String array in Java. Option B: String names[] = new String[]{"Alice", "Bob", "Charlie"}
This syntax is also valid for declaring and initializing a String array in Java. However, it's less commonly used. Option C: String[3] names = {"Alice", "Bob", "Charlie"}
This syntax is invalid. In Java, when declaring an array, the size should not be specified in the square brackets.

[#53] What will be the output of the following Java code? class array_output
{
public static void main(String args[])
{
char array_variable [] = new char[10]
for (int i = 0
i < 10
++i)
{
array_variable[i] = 'i'
System.out.print(array_variable[i] + "")
}
}
}
Correct Answer

(D) i i i i i i i i i i

[#54] What will be the output of the following Java code? class multidimention_array
{
public static void main(String args[])
{
int arr[][] = new int[3][]
arr[0] = new int[1]
arr[1] = new int[2]
arr[2] = new int[3]
int sum = 0
for (int i = 0
i < 3
++i)
for (int j = 0
j < i + 1
++j)
arr[i][j] = j + 1
for (int i = 0
i < 3
++i)
for (int j = 0
j < i + 1
++j)
sum + = arr[i][j]
System.out.print(sum)
}
}
Correct Answer

(B) 10

[#55] What will be the output of the following Java code? class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}}
int sum = 0
for (int i = 0
i < 3
++i)
for (int j = 0
j < 3
++j)
sum = sum + array_variable[i][j]
System.out.print(sum / 5)
}
}
Correct Answer

(B) 9