wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

IPS_JAVA_Fall25_ Quiz9

Total questions: 12

Worksheet time: 6mins

Name
Class
Date
1.

Which of the following is the correct way to declare an array of integers in Java?

a)

int array[];

b)

int[] array;

c)

array int[];

d)

int array;

2.

What is the default value of an element in a newly created integer array in Java?

a)

1

b)

0

c)

null

d)

-1

3.

How do you access the third element of an array named `numbers` in Java?

a)

numbers[2]

b)

numbers[3]

c)

numbers(3)

d)

numbers{2}

4.

Which statement correctly initializes an array of 5 doubles in Java?

a)

double[] arr = new double[5];

b)

double arr = new double[5];

c)

double arr[5];

d)

double[] arr = {5};

5.

What is the length of the array `int[] arr = {1, 2, 3, 4, 5};`?

a)

4

b)

5

c)

6

d)

0

6.

Which of the following statements assigns the value 10 to the first element of an array named `data`?

a)

data[0] = 10;

b)

data[1] = 10;

c)

data(0) = 10;

d)

data = 10[0];

7.

Given `int[] arr = new int[3];`, which code segment will fill the array with the values 1, 2, and 3?

a)

arr[0]=1; arr[1]=2; arr[2]=3;

b)

arr[1]=1; arr[2]=2; arr[3]=3;

c)

arr[0]=3; arr[1]=2; arr[2]=1;

d)

arr[0]=2; arr[1]=3; arr[2]=1;

8.

If you want to print all elements of an array `nums` of length 4, which loop is most appropriate?

a)

for(int i=0; i

b)

for(int i=1; i<=nums.length; i++) { System.out.println(nums[i]); }

c)

for(int i=0; i<=nums.length; i++) { System.out.println(nums[i]); }

d)

for(int i=1; i

9.

Which of the following code snippets correctly creates and initializes an array of 3 strings with the values "A", "B", and "C"?

a)

String[] arr = {"A", "B", "C"};

b)

String arr = new String[3]{"A", "B", "C"};

c)

String[] arr = new String("A", "B", "C");

d)

String arr[] = new String[3];

10.

Given `int[] values = {2, 4, 6, 8};`, what is the value of `values[values.length - 1]`?

a)

2

b)

4

c)

6

d)

8

11.

Suppose you have an array `int[] arr = {5, 10, 15, 20, 25};`. Write a code segment to calculate the sum of all elements in the array.

a)

int sum = 0; for(int i=0; i

b)

int sum = arr[0] + arr[1] + arr[2];

c)

int sum = arr.length;

d)

int sum = arr[0] * arr[1] * arr[2];

12.

Given an array `int[] nums = {3, 7, 2, 9, 4};`, which code finds the maximum value in the array?

a)

int max = nums[0]; for(int i=1; i max) max = nums[i]; }

b)

int max = nums[0]; for(int i=0; i

c)

int max = nums[0]; for(int i=1; i

d)

int max = nums[0]; for(int i=0; i max) max = nums[i]; }