Question 1:
Which of these is necessary to specify at time of array initialization?
A.
Row
B.
Column
C.
Both Row and Column
D.
None of the mentioned
Answer: _________
Question 2:
Which of these is an incorrect Statement?
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
Answer: _________
Question 3:
In Java, arrays are objects of which class?
A.
Array
B.
Collection
C.
ArrayList
D.
None of the above
Answer: _________
Question 4:
What is the default initial value of numeric elements in an array of type int in Java?
A.
0
B.
1
C.
-1
D.
Null
Answer: _________
Question 5:
What does the length attribute of an array in Java represent?
A.
The number of elements in the array
B.
The maximum value of elements in the array
C.
The length of the variable name
D.
The number of dimensions
Answer: _________
Question 6:
How do you access an element in a one-dimensional array named myArray at index 3 in Java?
A.
myArray[3]
B.
myArray(3)
C.
myArray.get(3)
D.
myArray.getElement(3)
Answer: _________
Question 7:
What happens if you try to access an array element at an index that is out of bounds in Java?
A.
It returns null.
B.
It throws an ArrayIndexOutOfBoundsException.
C.
It returns 0.
D.
It returns -1.
Answer: _________
Question 8:
Which method is used to find the length of an array in Java?
A.
length()
B.
size()
C.
getSize()
D.
length
Answer: _________
Question 9:
What is the highest index of an array with 10 elements in Java?
A.
9
B.
10
C.
0
D.
1
Answer: _________
Question 10:
In Java arrays are
A.
objects
B.
object references
C.
primitive data type
D.
None of the above
Answer: _________
Question 11:
When you pass an array to a method, the method receives ________ .
A.
A copy of the array.
B.
A copy of the first element.
C.
The reference of the array.
D.
The length of the array.
Answer: _________
Question 12:
What is the purpose of the Arrays.sort() method in Java?
A.
To find the maximum element in an array.
B.
To search for an element in an array.
C.
To sort the elements of an array.
D.
To reverse the elements of an array.
Answer: _________
Question 13:
Which method is used to add an element to the end of an ArrayList in Java?
A.
addElement()
B.
insertElement()
C.
append()
D.
add()
Answer: _________
Question 14:
What happens when you try to access an element at an index that is out of bounds in an ArrayList in Java?
A.
It returns null.
B.
It throws an ArrayIndexOutOfBoundsException.
C.
It returns -1.
D.
It throws an IndexOutOfBoundsException.
Answer: _________
Question 15:
Which method is used to remove all elements from an ArrayList in Java?
A.
removeAllElements()
B.
clear()
C.
remove()
D.
deleteAll()
Answer: _________
Question 16:
In Java, how do you find the maximum element in an array?
A.
Use a loop to compare each element with the maximum value found so far.
B.
Use the max() method.
C.
Use the findMax() method.
D.
Use the maximum() method.
Answer: _________
Question 17:
Which of the following statements is true about ArrayLists in Java?
A.
ArrayLists can only store primitive data types.
B.
ArrayLists can have duplicate elements.
C.
ArrayLists are fixed in size.
D.
ArrayLists can only store Strings.
Answer: _________
Question 18:
What is the purpose of the Arrays.fill() method in Java?
A.
To fill an array with a specified value.
B.
To find the number of elements in an array.
C.
To copy elements from one array to another.
D.
To sort the elements of an array.
Answer: _________
Question 19:
In Java, what is the purpose of the Arrays.equals() method?
A.
To check if two arrays are equal.
B.
To concatenate two arrays.
C.
To find the common elements between two arrays.
D.
To remove duplicate elements from an array.
Answer: _________
Question 20:
How do you find the number of rows in a two-dimensional array named matrix in Java?
A.
matrix.rows()
B.
matrix.size()
C.
matrix.length
D.
matrix.length()
Answer: _________
Question 21:
What is the default initial value of elements in an array of type boolean in Java?
A.
TRUE
B.
FALSE
C.
1
D.
0
Answer: _________
Question 22:
What is the correct way to copy the elements of one array to another in Java?
A.
Use a for loop to iterate through the elements and copy them one by one.
B.
Use the clone() method of arrays.
C.
Use the copy() method of arrays.
D.
None of the above
Answer: _________
Question 23:
What is the purpose of the System.arraycopy() method in Java?
A.
To copy elements from one array to another.
B.
To compare two arrays.
C.
To reverse the elements of an array.
D.
To find the length of an array.
Answer: _________
Question 24:
Which of the following is true about the size of an ArrayList in Java?
A.
The size is fixed and cannot change.
B.
The size can only increase but not decrease.
C.
The size can change dynamically.
D.
The size is determined by the length of the elements.
Answer: _________
Question 25:
How do you check if an element exists in an array or ArrayList in Java?
A.
Use the find() method.
B.
Use the search() method.
C.
Use the contains() method.
D.
Use the exists() method.
Answer: _________
Question 26:
Which of these operators is used to allocate memory to array variable in Java?
A.
malloc
B.
alloc
C.
new
D.
new malloc
Answer: _________
Question 27:
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 arr[5]
D.
int arr[] = int [5] new
Answer: _________
Question 28:
What would be the result of attempting to compile and run the following code? public class HelloWorld{
public static void main(String[] args){
double[] x = new double[]{1, 2, 3}
System.out.println("Value is " + x[1])
}
}
A.
The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.
B.
The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3}
C.
The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0}
D.
The program compiles and runs fine and the output
Answer: _________
Question 29:
Which will legally declare, construct, and initialize an array?
A.
int [] myList = {}
B.
int [] myList = (5, 8, 2)
C.
int myList [] [] = {4,9,7,0}
D.
int myList [] = {4, 3, 7}
Answer: _________
Question 30:
What will be the output of the program? public class Test{
public static void main(String [] args){
String s1 = args[1]
String s2 = args[2]
String s3 = args[3]
String s4 = args[4]
System.out.print(" args[2] = " + s2)
}
} and the command-line invocation is C:Java> java Test 1 2 3 4
A.
args[2] = 2
B.
args[2] = 3
C.
args[2] = null
D.
An exception is thrown at runtime.
Answer: _________
Question 31:
What is the value of a[1] after the following code is executed? int[] a = {0, 2, 4, 1, 3}
for(int i = 0
i < a.length
i++)
a[i] = a[(a[i] + 3) % a.length]
A.
0
B.
1
C.
2
D.
3
Answer: _________
Question 32:
Predict the output: public class Test{
public static void main(String... args){
int[] index = new int[5]
System.out.println(index instanceof Object)
}
}
A.
true
B.
false
C.
Compilation fails with an error at line 3
D.
Compilation fails with an error at line 4
Answer: _________
Question 33:
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
Answer: _________
Question 34:
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
Answer: _________
Question 35:
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)
}
}
A.
3
B.
0
C.
6
D.
1
Answer: _________
Question 36:
Which one of the following is a valid statement?
A.
char [] c = new char ()
B.
char [] c = new char [5]
C.
char [] c = new char (4)
D.
char [] c = new char []
Answer: _________
Question 37:
What is the result of compiling and running the following code? public class Test{
public static void main(String[] args){
int[] a = new int[0]
System.out.print(a.length)
}
}
A.
0
B.
Compilation error, arrays cannot be initialized to zero size.
C.
Compilation error, it is a.length() not a.length
D.
None of the above
Answer: _________
Question 38:
What will be the output? public class Test{
public static void main(String[] args){
int[] x = new int[3]
System.out.println("x[0] is " + x[0])
}
}
A.
The program has a compile error because the size of the array wasn't specified when declaring the array.
B.
The program has a runtime error because the array elements are not initialized.
C.
The program runs fine and displays x[0] is 0.
D.
The program has a runtime error because the array element x[0] is not defined.
Answer: _________
Question 39:
What is the output of the following code? public class Test{
public static void main(String args[]){
double[] myList = {1, 5, 5, 5, 5, 1}
double max = myList[0]
int indexOfMax = 0
for(int i = 1
i < myList.length
i++){
if(myList[i] > max){
max = myList[i]
indexOfMax = i
}
}
System.out.println(indexOfMax)
}
}
A.
0
B.
1
C.
2
D.
3
Answer: _________
Question 40:
What is output of the following code: public class Test{
public static void main(String[] args){
int[] x = {120, 200, 016 }
for(int i = 0
i < x.length
i++)
System.out.print(x[i] + " ")
}
}
A.
120 200 16
B.
120 200 14
C.
120 200 016
D.
016 is a compile error. It should be written as 16.
Answer: _________
Question 41:
Determine output: public class Test{
public static void main(String[] args){
int[] x = {1, 2, 3, 4}
int[] y = x
x = new int[2]
for(int i = 0
i < x.length
i++)
System.out.print(y[i] + " ")
}
}
A.
1 2 3 4
B.
0 0 0 0
C.
1 2
D.
0 0
Answer: _________
Question 42:
Analyze the following code and choose the correct answer. int[] arr = new int[5]
arr = new int[6]
A.
The code has compile errors because the variable arr cannot be changed once it is assigned.
B.
The code has runtime errors because the variable arr cannot be changed once it is assigned.
C.
The code can compile and run fine. The second line assigns a new array to arr .
D.
The code has compile errors because we cannot assign a different size array to arr .
Answer: _________
Question 43:
What will be the output? public class Test{
public static void main(String[] args){
int[] a = new int[4]
a[1] = 1
a = new int[2]
System.out.println("a[1] is " + a[1])
}
}
A.
The program has a compile error because new int[2]
B.
The program has a runtime error because a[1] = null
C.
a [ 1 ] is 0
D.
a [ 1 ] is 1
Answer: _________
Question 44:
How do you find the index of an element in an ArrayList named list in Java?
A.
list.getIndex(element)
B.
list.indexOf(element)
C.
list.find(element)
D.
list.search(element)
Answer: _________
Question 45:
How do you declare a multi-dimensional array in Java?
A.
int[][] matrix
B.
int matrix[][]
C.
int matrix[2][2]
D.
All of the above
Answer: _________
Question 46:
Which of the following statements is true about arrays in Java?
A.
Arrays can change in size after initialization.
B.
Arrays can store elements of different types.
C.
Arrays can have a negative length.
D.
Arrays are objects of type Array.
Answer: _________
Question 47:
How do you declare and initialize an array of integers with 5 elements in Java?
A.
int[] numbers = new int[5]
B.
int[5] numbers
numbers = {1, 2, 3, 4, 5}
C.
int numbers[5] = {1, 2, 3, 4, 5}
D.
All of the above
Answer: _________
Question 48:
How do you declare a dynamic array (ArrayList) of integers in Java?
A.
ArrayList numbers = new ArrayList ()
B.
ArrayList numbers = new ArrayList ()
C.
ArrayList numbers[] = new ArrayList [5]
D.
int[] numbers = new int[5]
Answer: _________
Question 49:
In Java, how do you remove an element at a specific index from an ArrayList named list?
A.
list.remove(index)
B.
list.delete(index)
C.
list.pop(index)
D.
list.splice(index)
Answer: _________
Question 50:
How do you declare a one-dimensional array in Java?
A.
int[] myArray
B.
int myArray[]
C.
Array myArray
D.
All of the above
Answer: _________
Question 51:
What is the correct way to initialize a two-dimensional array in Java?
A.
int[][] myArray = {{1, 2}, {3, 4}}
B.
int[2][2] myArray = {{1, 2}, {3, 4}}
C.
int[2][2] myArray
myArray[0][0] = 1
myArray[0][1] = 2
myArray[1][0] = 3
myArray[1][1] = 4
D.
None of the above
Answer: _________
Question 52:
Which of the following is a valid declaration and initialization of a String array in Java?
A.
String[] names = {"Alice", "Bob", "Charlie"}
B.
String names[] = new String[]{"Alice", "Bob", "Charlie"}
C.
String[3] names = {"Alice", "Bob", "Charlie"}
D.
Both A and B
Answer: _________
Question 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] + "")
}
}
}
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
Answer: _________
Question 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)
}
}
A.
11
B.
10
C.
13
D.
14
Answer: _________
Question 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)
}
}
A.
8
B.
9
C.
10
D.
11
Answer: _________
Answer Key
1:
A
2:
A
3:
D
4:
A
5:
A
6:
A
7:
B
8:
D
9:
A
10:
A
Solution: In java an array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
11:
C
12:
C
13:
D
14:
B
15:
B
16:
A
17:
B
18:
A
19:
A
20:
C
21:
B
22:
A
23:
A
24:
C
25:
C
26:
C
27:
D
28:
D
Solution: new double []{1, 2, 3} is correct. This is the syntax I have not covered in this edition, but will be covered in the future edition. In this question, double [] x = new double []{1, 2, 3} is equivalent to double [] x = {1, 2, 3}
29:
D
Solution: The only legal array declaration and assignment statement is Option D Option A is wrong because it initializes an int array with String literals. Option B is wrong because it uses something other than curly braces for the initialization. Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array.
30:
D
Solution: An exception is thrown because in the code String s4 = args [ 4 ]
, the array index (the fifth element) is out of the bounds. The exception thrown is the ArrayIndexOutOfBoundsException .
31:
B
Solution: Here's an explanation of the code: 1. The array a is initialized with values: {0, 2, 4, 1, 3}. 2. The for loop iterates over each element of the array using the variable i . 3. Inside the loop, the value of a[i] is updated using the expression a[(a[i] + 3) % a.length] . (i) For i = 0 , a[i] is 0. So, a[(0 + 3) % 5] becomes a[3 % 5] which is a[3] (value 1). (ii) For i = 1 , a[i] is 2. So, a[(2 + 3) % 5] becomes a[5 % 5] which is a[0] (value 0). (iii) For i = 2 , a[i] is 4. So, a[(4 + 3) % 5] becomes a[7 % 5] which is a[2] (value 4). (iv) For i = 3 , a[i] is 1. So, a[(1 + 3) % 5] becomes a[4 % 5] which is a[4] (value 3). (v) For i = 4 , a[i] is 3. So, a[(3 + 3) % 5] becomes a[6 % 5] which is a[1] (value 2). 4. After the loop finishes, the modified array a becomes {0, 1, 4, 3, 2}. 5. Finally, the code prints the value of a[1] using System.out.println(a[1]) , which outputs 1 . Therefore, the output of the code is 1 .
32:
A
Solution: An array is always an instance of Object.
33:
D
34:
A
35:
D
36:
B
Solution: The syntax for declaring and creating an array variable in java is: dataType[] arrayRefVar = new dataType[arraySize]
Thus, option (A) and option (C) is syntactically wrong as parentheses( ( ) ) is used instead of square brackets( [ ] ). Option (D) is incorrect as the size of the array is missing.
37:
A
Solution: Step 1: Declare a public class named Test . Step 2: Define the main method with the public and static modifiers and a parameter of type String array named args . Step 3: Declare an array variable a of type int[] . Step 4: Initialize the array a with a size of 0 using the new keyword. Step 5: Print the length of the array a using the length property. Therefore, when you compile and run the code, the output will be: 0 This output corresponds to the length of the array a , which is 0.
38:
C
Solution: Program is syntactically correct, so no error. In java, if the array is not initialized at the time of declaration and creation then all the elements of the array are initialized to 0 by default.
39:
B
Solution: In the given program. Line 7 : if(myList[i] > max) execute only on time when i =1
when i = 1 then myList[i] = 5 and max = 1( so the statement is true and if block will be executed ). Then, max = myList[i] = 5 and indexOfMax = i = 1. After that if statement always false. so indexOfMax value remain 1. Therefore the value of indexOfMax is 1 at end of the for loop.
40:
B
Solution: 016 is an octal number. The prefix 0 indicates that a number is in octal.
41:
C
42:
C
43:
C
Solution: After executing the statement a = new int [ 2 ], a refers to int [ 2 ]. The default value for a [ 0 ] and a [ 1 ] is 0.
44:
B
45:
D
46:
D
Solution: In Java, arrays are objects. They are instances of classes that extend the abstract class java.lang.Object. However, arrays in Java are implemented as objects of a specific type called "Array". This means that arrays in Java are indeed objects, but they are not instances of the Array class
rather, they are instances of classes that are automatically generated by the Java Virtual Machine (JVM) when arrays are created. Option A: Arrays can change in size after initialization. In Java, once an array is initialized with a specific size, that size cannot be changed. Arrays have a fixed length determined at the time of creation. While you can create a new array with a different size and copy elements from the old array to the new one to achieve a similar effect, the original array itself cannot change its size. Option B: Arrays can store elements of different types. In Java, arrays are homogeneous data structures, meaning they can only store elements of the same type. When you declare an array, you specify the type of elements it can hold. All elements in the array must be compatible with this type. Option C: Arrays can have a negative length. In Java, array lengths must be non-negative integers. It is not possible to declare an array with a negative length. Attempting to do so will result in a compilation error. Therefore, the correct answer is Option D: Arrays are objects of type Array. Arrays in Java are indeed objects, but they are instances of classes that are automatically generated by the Java Virtual Machine (JVM) when arrays are created. They are not instances of the Array class itself, but they are objects nonetheless.
47:
A
48:
B
49:
A
50:
A
Solution: In Java, a one-dimensional array is declared by specifying the type of elements followed by square brackets and then the array name. The correct syntax is: type[] arrayName
Option A follows this syntax and is the correct way to declare a one-dimensional array in Java. Option B is also a valid syntax but is less commonly used. Both Option A and Option B are equivalent. Option C is incorrect as it attempts to use generics syntax, which is not allowed for arrays in Java. Option D is incorrect because Option C is not a valid way to declare an array.
51:
A
52:
D
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:
D
54:
B
55:
B