0% found this document useful (0 votes)
3 views

array review questions

The document contains a series of questions and answers related to arrays in programming, focusing on array declarations, indexing, memory allocation, and initialization. It includes multiple-choice questions, fill-in-the-blank statements, and code snippets for practical understanding. The content is aimed at testing knowledge of array concepts and operations in programming languages.

Uploaded by

agnesloeser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

array review questions

The document contains a series of questions and answers related to arrays in programming, focusing on array declarations, indexing, memory allocation, and initialization. It includes multiple-choice questions, fill-in-the-blank statements, and code snippets for practical understanding. The content is aimed at testing knowledge of array concepts and operations in programming languages.

Uploaded by

agnesloeser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Array Review Questions

1. Suppose I have int b = new int[42]. What are the highest and lowest legal array indexes for b?
a. 0 and 41

b. 0 and 42

c. 1 and 41
d. 1 and 42
2. Consider the following statements:

int[ ] p = new int[100];


int[ ] s = p;
After these statements, which of the following statements will change the last value of p to 75?
a. p[99] = 75;

b. p[100] = 75;

c. s[99] = 75;
d. s[100] = 75;

e. Two or more of the answers will change i to 75.


3. An array is a list of data items that ____.
Is stored in a single variable and all have the same type

4. You reserve memory locations for an array when you ____.

a. int
b. []
c. new
d. array

5. For how many integers does the following statement reserve room? int[] value = new int[34];

a. 33
b. 34
c. 35
d. 0

6. What can be used as an array subscript?

a. int
b. double
c. String
d. char
7. If you declare an array as follows, how do you indicate the final element of the array?

int[] num = new int[6];

a. num[0]
b. num[5]
c. num[6]
d. num[7]

8. If you declare an integer array as follows, what is the value of num[2]?


int[] num = {101, 202, 303, 404, 505, 606};

a. 101
b. 202
c. 303
d. 404
9. Write a statement that declares an array named streetAddress that contains exactly eighty
elements of type char.

int[] streetAdress = new int[80];

10. Declare and instantiate an array named scores of twenty-five elements of type int .
int[] x = new int[25];

11. Declare an array named a of ten elements of type int and initialize the elements (starting
with the first) to the values 10 , 20 , ..., 100 respectively.

int[] x = {10,20,30,40,50,60,70,80,90,100};

12. Declare an array reference variable, week, and initialize it to an array containing the strings
"mon", "tue", "wed", "thu", "fri", "sat", "sun" (in that order).

int[] weekDays = {“mon”,“tue”,”wed”,”thu”,”fri”,”sat”,”sun”};

13. Given that an array named a with elements of type int has been declared, assign 3 to its first
element

x[0] = 3;

14. Assume that an array named a containing exactly 5 integers has been declared and
initialized.
Write a single statement that adds 10 to the value stored in the first element of the array.

a. a[1]=a[1]+10;
b. a[0]=a[0]+10;
c. a[0]=a[0]+a[1];
d. a[1]=a[1]+a[0];

15. Given that an array named a whose elements are of type int has been declared, assign the
value -1 to the last element in a.
16. Assume that an array of int s named a has been declared with 12 elements. The integer
variable k holds a value between 0 and 6 . Assign 15 to the array element whose index is k .

a. a[s]=15;
b. a[15]=s;
c. a[k]=15;
d. a[15]=k
17. An array of int s named a has been declared with 12 elements. The integer variable k holds a
value between 0 and 6 . Assign 9 to the element just after a[k] .

x[k + 1] = 9;

18. An array of int s named a has been declared with 12 elements. The integer variable k holds a
value between 2 and 8 . Assign 22 to the element just before a[k] .

19. Assume that the array arr has been declared. Write a statement that assigns the next to last
element of the array to the variable x , which has already been declared.

20. Given an array of ints named x and an int variable named total that has already been
declared, write some code that places the sum of all the elements of the array x into total.
Declare any variables that you need.

a. int total =0;


for (int i=0; i<x.length; i++)
{
total=total+x[length];
}

b. int total =0;


for (int i=0; i<x.length; i++)
{
total=total+x[i];
}
c. int total =0;
for (int x=0; i<x.length; x++)
{
total=total+x[x];
}

d. int total =0;


for (int i=0; i<x.length; i++)
{
total=total+x[i];
}
21. Which of these is an incorrect array declaration?
a) int arr[] = new int[5]

b) int [] arr = new int[5]


c) int arr[] arr = new int[5]

d) int arr[] = int [5] new

22. What will this code print?


int arr[] = new int [5];
System.out.print(arr);
a) 0
b) value stored in arr[0].
c) 00000
d) Garbage value

23. 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

24. 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
25. What is the output of this program?
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
26. What is the output of this program?
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
27. What is the output of this program?
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
28. What is the output of this program?

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
29. What is the output of this program?
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

You might also like