Java MCQ with Answers - Arrays Practice Questions (AP CSA)


<-- Back to Text Files Next to Two-dimensional Arrays-->



Arrays in Java

Declaring and Initializing Arrays

An array is a fixed-size, ordered collection of elements of the same type. An array can be declared and initialized in one step using curly-brace syntax: int[] numbers = {34, 21, 9, 75, 100};, or allocated with a given size using new: int[] numbers = new int[5];. Numeric elements default to 0 when allocated with new. The size of an array is fixed after creation and is accessed via the length property (not a method): numbers.length.

Indexing and Accessing Elements

Java arrays are zero-indexed: the first element is at index 0 and the last element is at index numbers.length - 1. Accessing an index outside this range throws an ArrayIndexOutOfBoundsException at runtime. Use numbers[i] to read or write the element at index i.

Traversing Arrays

A standard for loop with an index variable running from 0 to numbers.length - 1 is the most common way to read or modify array elements. A while loop with the condition i <= num.length - 1 is equivalent to i < num.length. Because arrays are reference types, when an array is passed to a method, changes made to the array's elements inside the method affect the original array.

Common Array Algorithms

Frequently tested algorithms include: computing a sum or average, finding the maximum or minimum value, counting elements that satisfy a condition, and modifying elements in place. Pay close attention to the loop step size — a loop with i += 2 visits only even-indexed elements, and a loop that iterates from numbers.length - 1 down to 0 traverses the array in reverse.

Tips for AP-CSA Exam

In the AP Computer Science A (AP CSA) exam, questions on this topic test the ability to trace loop variables and array element values step by step, identify off-by-one errors, distinguish the length property of arrays from the length() method of Strings, and determine the effect of passing an array to a method.


Q1. Which one of the following statements will you use to print only the last value '100' from the given array named numbers?

int[] numbers = {34, 21, 9, 75, 100};

(A)	System.out.println(numbers.length());

(B)	System.out.println(numbers.length);

(C)	System.out.println(numbers.length()-1);

(D)	System.out.println(numbers.length - 1);

Answer:

View Output

(D)	System.out.println(numbers.length - 1);


Q2. What value will be printed when the following statement is executed?

int[] numbers = {34, 21, 9, 75, 100};
System.out.println(numbers[3]); 

(A)	9

(B)	75

(C)	100

(D)	Compilation error

Answer:

View Output

(B)	75


Q3. What will the call to method calculate() return when the array numbers has the following values: {40, 12, 45, 78}?

public static double calculate(int[] numbers)
{
	double value = 0;
	for (int i = 0; i < numbers.length; i+=2)
	{
		value = value + numbers[i];
	}
	return value / numbers.length;
}

(A)	43.75

(B)	58.33

(C) 21.25

(D)	22.50

Answer:

View Output

(C) 21.25



Q4. What will the call to method calculate() return when the array numbers has the following values: {32, 15, 56, 85, 23}?

public static int calculate(int[] numbers)
{
	int finalValue = 0;
	for (int j = numbers.length-1; j >=0; j--)
	{
		if (numbers[j]% 5 ==0) finalValue = finalValue + numbers[j];
	}
	return finalValue;
}

(A)	100

(B)	211

(C)	188

(D)	111

Answer:

View Output

(A)	100


Q5. Assume following to be the contents of the arrays numberOne and numberTwo:

int[] numbersOne = {19,0,36,75,0} ;
int[] numbersTwo = {32,0,56,0,0,45} ;

What will be the value returned after the method checkArrays() finishes execution?

public static double checkArrays(int[] numbersOne, int[] numbersTwo)
{
	double value = 0.0;
	int count=0;
	for (int i = 0; i < numbersOne.length; i++)
	{
		if (numbersOne[i]!=0) 
			{
				value = value + numbersOne[i];
				count++;
			}
	}
	for (int i = 0; i < numbersTwo.length; i++)
	{
		if (numbersTwo[i]!=0) 
			{
				value = value + numbersTwo[i];
				count++;
			}
	}
		
	return (value/count);
}	

(A)	23.90

(B)	26.30

(C)	60.00

(D)	43.83

Answer:

View Output

(D)	43.83


Q6. Assume following to be the contents of the arrays numberOne and numberTwo:

int[] numbersOne = {19,6,36,75,44} ;
int[] numbersTwo = {32,51,16,92,40} ; 

What will be the contents of the array numbersOne after the method manipulateArrays() finishes execution?

public static void manipulateArrays(int[] numbersOne, int[] numbersTwo)
{
	for (int i = 0; i < numbersOne.length; i++)
	{
		if (numbersOne[i]<=50) 
		{
			numbersOne[i] = numbersOne[i] + numbersTwo[i];
		}
	}

}	

(A)	[19,6,36,167,44]

(B)	[51,57,52,167,84]

(C)	[51,57,52,75,84]

(D)	[19,6,36,75,44]

Answer:

View Output

(C)	[51,57,52,75,84]


Q7. Assume following to be the contents of the arrays numberOne and numberTwo:

int[] numbersOne = {19,6,36,75,44} ;
int[] numbersTwo = {32,51,16,92,40} ; 

What will be the contents of the array numbersOne after the method manipulateArrays() finishes execution?

public static void manipulateArrays(int[] numbersOne, int[] numbersTwo)
{
	int temp=0;
	for (int i = 0, j=numbersTwo.length-1; i < numbersOne.length && j>=0; i++, j--)
	{
		temp = numbersOne[i];
		numbersOne[i] = numbersTwo[j];
		numbersTwo[j] = temp;
	}
}	

(A)	[32,51,16,92,40]

(B)	[40,92,16,51,32] 

(C)	[19,6,36, 92,40]

(D)	[19,6,16,92,40]

Answer:

View Output

(B)	[40,92,16,51,32] 


Q8. Consider the following method named modifyPrime(). Additionally, given an array named numbers which is initialized to {15, 22, 70, 17, 12}. Which of the following represents the contents of the array numbers after a call to modifyPrime(numbers, 3) is executed?

public void modifyPrime(int[] num, int found)
{
   num[found] = num[found] * 10;
}

(A)	[150, 220, 700, 170, 120]

(B) [15, 22, 700, 17, 12]

(C)	[15, 22, 700, 170, 12]

(D)	[15, 22, 70, 170, 12]

Answer:

View Output

(D)	[15, 22, 70, 170, 12]


Q9. Consider the following method named modifyArray(). Array named numbers is initialized to {15,22,70,17,12,23,89,9,43,67,34}. Which of the following represents the contents of the array numbers after a call to modifyArray(numbers) is executed?

public static void modifyArray(int[] num)
{
	for (int i = 0, j = num.length-1; i!=j; i++, j--)
	{
		System.out.print(num[i]+",");
		num[i] = num[j];
	}
}

(A)	[34,67,43,9,89,23,89,9,43,67,34] 

(B) [15,22,70,17,12,23,89,9,43,67,34]

(C)	[15,22,70,17,12,23,12,17,70,22,15]

(D)	[34,67,43,9,89,23,12,17,70,22,15] 

Answer:

View Output

(A)	[34,67,43,9,89,23,89,9,43,67,34] 


Q10. What will the call to method sumArray() return when the array numbers has the following values: {50, 20, 70, 10, 50, 20, 80, 90, 40}?

public static int sumArray(int[] num)
{
	int sum=0;
	for (int i = 0; i< num.length; i++)
	{
		sum = sum + num[i]/10;
	}
	return sum;
}

(A)	0 

(B) 43

(C)	430

(D)	4300 

Answer:

View Output

(B) 43


Q11. The following method named countZerosInArray() is intended to find the number of zeros in the given array of integers, numbers. This method will give the right answer when?

public static int countZerosInArray(int[] num)
{
	int count=0;
	int i=0;
	while (i<= num.length-1)
	{
		if (num[i]!= 0) count++;
		i++;
	}
	return count;
}

(A)	Always. The method is correctly designed to calculate the number of zeros in the given array.

(B) Works correctly only when all the values in the array numbers are zeros

(C)	Works correctly only when all the values in the array numbers are non-zeros

(D)	Never. The method is not correctly designed to calculate the number of zeros in the given array.

(E)	Works correctly only when the array contains a collection of zeros and non-zeros values. 

Answer:

View Output

(D)	Never. The method is not correctly designed to calculate the number of zeros in the given array.


Q12. The following method named getFives() is invoked with the array values as: {100, 201, 70, 105, 55, 108, 82, 195, 160}. What does the method intend to do?

public static int getFives(int[] num)
{
	int value=0;
	int i=0;
	while (i<= num.length-1)
	{
		if (num[i]%5 == 0)
			{
				if (value < num[i]) value = num[i];
			}
		i++;
	}
	return value;
}

(A)	The method returns the count of integers in the array that are divisible by 5.

(B) The method returns the count of integers in the array that are not divisible by 5.

(C)	The method returns the value of the maximum integer in the array that is not divisible by 5.

(D)	The method returns the value of the maximum integer in the array that is divisible by 5.

(E)	The method returns the sum of all the integers in the array that are divisible by 5.

Answer:

View Output

(D)	The method returns the value of the maximum integer in the array that is divisible by 5.


Q13. The following method named getSumArray() is invoked with the array values as:{10, 20, 70, 10, 55, 10, 82, 5}. What will be the final values in the array sumArray after the execution of the method completes?

public static void getSumArray(int[] num)
{
	int[] sumArray = new int[num.length/2];
	for (int i = 0, j=0; i< num.length-1; i+=2, j++)
	{
		sumArray[j] = num[i]+ num[i+1];
	}
}

(A)	[10, 20, 70, 10]

(B) [55, 10, 82, 5]

(C)	[15, 102, 80, 65]

(D)	[65, 30, 152, 15]

(E)	[30, 80, 65, 87] 

Answer:

View Output

(E)	[30, 80, 65, 87] 


Q14. The following method named getIndex() is invoked with the array values as: {2, 14, 39, 39, 64, 70, 86, 98} and returns a value of 2. What does the method intend to do?

public static int getIndex(int[] num)
{
	int i=0;
	for (i = 0; i< num.length-1; i++)
	{
		if (num[i] == num[i+1]) return i;
			
	}
	return -1;
}

(A)	The method returns the index of the first number that is greater than 30.

(B) The method returns the index of the first number that is odd.

(C)	The method returns the index of the first number that is duplicated.

(D)	The method returns the index of the first number that is a multiple of 3.

(E)	The method returns the index of the first number that is a multiple of 2.

Answer:

View Output

(C)	The method returns the index of the first number that is duplicated.


<-- Back to Text Files Next to Two-dimensional Arrays-->