72). Java Arrays
[A]malloc
[B]alloc
[C]new
[D]new malloc
Show Answer
Correct Answer: new
Notes:
Answer: c
Explanation: Operator new allocates a block of memory specified by the size of an array, and gives the reference of memory allocated to the array variable.
73). Which of these is an incorrect array declaration?
[A]int arr[] = new int[5]
[B]int [] arr = new int[5]
[C]int arr[] = new int[5]
[D]int arr[] = int [5] new
Show Answer
Correct Answer: int arr[] = int [5] new
Notes:
Answer: d
Explanation: Operator new must be succeeded by array type and array size.
74). What will be the output of the following Java code?
int arr[] = new int [5];
System.out.print(arr);
[A]0
[B]value stored in arr[0]
[C]00000
[D]Class name@ hashcode in hexadecimal form
Show Answer
Correct Answer: Class name@ hashcode in hexadecimal form
Notes:
Answer: d
Explanation: If we trying to print any reference variable internally, toString() will be called which is implemented to return the String in following form:
classname@hashcode in hexadecimal form
75). Which of these is an incorrect Statement?
int arr[] = new int [5];
System.out.print(arr);
[A]It is necessary to use new operator to initialize an array
[B]Array can be initialized using comma separated expressions surrounded by curly braces
[C]Array can be initialized when they are declared
[D]None of the mentioned
Show Answer
Correct Answer: It is necessary to use new operator to initialize an array
Notes:
Answer: a
Explanation: Array can be initialized using both new and comma separated expressions surrounded by curly braces example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};
76). Which of these is necessary to specify at time of array initialization?
int arr[] = new int [5];
System.out.print(arr);
[A]Row
[B]Column
[C]Both Row and Column
[D]None of the mentioned
Show Answer
Correct Answer: Row
Notes:
Answer: a
Explanation: None.
77). What will be the output of the following Java code?
class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
[A]0 2 4 6 8
[B]1 3 5 7 9
[C]0 1 2 3 4 5 6 7 8 9
[D]1 2 3 4 5 6 7 8 9 10
Show Answer
Correct Answer: 0 2 4 6 8
Notes:
Answer: a
Explanation: When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
output:$ javac array_output.java
$ java array_output
0 2 4 6 8
78). 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);
}
}
Show Answer
Correct Answer: 10
Notes:
Answer: b
Explanation: arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1 element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of array is given i + j value in loop. sum contains addition of all the elements of the array.
output:
$ javac multidimention_array.java
$ java multidimention_array
10
79). What will be the output of the following Java code?
class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}
Show Answer
Correct Answer: 1
Notes:
Answer: d
Explanation: Array arr contains 10 elements. n contains 6 thus in next line n is given value 3 printing arr[3]/2 i:e 3/2 = 1 because of int Value, by int values there is no rest. If this values would be float the result would be 1.5.
output:
$ javac evaluate.java
$ java evaluate
1
80). 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] + "");
}
}
}
[A]1 2 3 4 5 6 7 8 9 10
[B]0 1 2 3 4 5 6 7 8 9 10
[C]i j k l m n o p q r
[D]i i i i i i i i i i
Show Answer
Correct Answer: i i i i i i i i i i
Notes:
Answer: d
Explanation: None.
output:
$ javac array_output.java
$ java array_output
i i i i i i i i i i
81). 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);
}
}
Show Answer
Correct Answer: 9
Notes:
Answer: b
Explanation: None.
output:
$ javac array_output.java
$ java array_output
9