Shared Flashcard Set

Details

Java CS170 Chapter. 9
Quiz Questions
14
Computer Science
Undergraduate 2
12/08/2011

Additional Computer Science Flashcards

 


 

Cards

Term
The last element in the array created by the statement int[] ar = { 1, 2, 3 }; is :
Answer

a. ar[3]

b. ar[2]

c. ar(3)

d. ar(2)
Definition
b. ar[2]
Term
int[] x = new int[10]; x[0] = 34; x[1] = 88; println(x[0] + " " + x[1] + " " + x[10]);
What is the output of the code fragment above?
Answer

1. 0 34 88

2. 34 88 0

3. This program throws an exception.

4. 34 88 88
Definition
3. This program throws an exception.
Term
Consider the following declaration.
int[] alpha = new int[3];
Which of the following input statements correctly input values into alpha?
(i)
alpha = cin.nextInt(); alpha = cin.nextInt(); alpha = cin.nextInt();
(ii)
alpha[0] = cin.nextInt(); alpha[1] = cin.nextInt(); alpha[2] = cin.nextInt();
Answer

1. Only (ii)

2. None of these

3. Only (i)

4. Both (i) and (ii)
Definition
1. Only (ii)
Term
Consider the following method definition.
public int strange(int[] list, int item) { int count; for (int j = 0; j < list.length; j++) if (list[j] == item) count++; return count; }
Which of the following statements best describe the behavior of this method?
Answer

1. None of these

2. This method returns the number of times item is stored in list.

3. This method returns the number of values stored in list.

4. This method returns the sum of all the values of list.
Definition
2. This method returns the number of times item is stored in list.
Term
To create the array variable named ar, you would use :
Answer

a. int ar;

b. int() ar;

c. int[] ar;

d. None of these
Definition
c. int[] ar;
Term
A method that has the signature void m(int[] ar) :
Answer

a. can change the values in the array ar

b. cannot change the values in the array ar

c. can only change copies of each of the elements in the array ar

d. None of these
Definition
a. can change the values in the array ar
Term
If you pass the array ar to the method m() like this, m(ar); the element ar[0] :
Answer

a. will be changed by the method m()

b. cannot be changed by the method m()

c. may be changed by the method m(), but not necessarily

d. None of these
Definition
c. may be changed by the method m(), but not necessarily
Term
int[] num = new int[100];

for (int i = 0; i < 50; i++)
num[i] = i;

num[5] = 10;
num[55] = 100;

What is the index number of the last component in the array above?
Answer

1. 99

2. 0

3. 100

4. 101
Definition
1. 99
Term
Here is a loop that is supposed to sum each of the values in the array named ar :
double sum = 0.0; for (int i=0; i < ar.length; i++) // ??? System.out.println(sum);
What goes on the line containing ???? :
Answer

a. i = sum + ar[i];

b. sum + ar[i];

c. sum = sum + i;

d. sum += ar[i];
Definition
d. sum += ar[i];
Term
What is stored in alpha after the following code executes?
int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) { alpha[j] = j + 5; if (j % 2 == 1) alpha[j - 1] = alpha[j] + 2; }
Answer

1. alpha = {8, 6, 7, 8, 9}

2. alpha = {5, 6, 10, 8, 9}

3. alpha = {5, 6, 7, 8, 9}

4. alpha = {8, 6, 10, 8, 9}
Definition
4. alpha = {8, 6, 10, 8, 9}
Term
What is stored in alpha after the following code executes?
int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) { alpha[j] = j + 1; if (j > 2) alpha[j - 1] = alpha[j] + 2; }
Answer

1. alpha = {1, 2, 3, 4, 5}

2. alpha = {1, 5, 6, 7, 5}

3. alpha = {4, 5, 6, 7, 9}

4. None of these
Definition
4. None of these
Term
Suppose you have the following declaration.


int[] beta = new int[50];

Which of the following is a valid element of beta.

(i) beta[0]
(ii) beta[50]

Answer

1. Both (i) and (ii)

2. Only (i)

3. None of these

4. Only (ii)
Definition
2. Only (i)
Term
Which of the following is the array subscripting operator in Java?
Answer

1. new

2. {}

3. []

4. .
Definition
3. []
Term
Consider the following declaration.
Scanner cin = new Scanner(System.in); int[] beta = new int[3]; int j;
Which of the following input statements correctly input values into beta?
(i)
beta[0] = cin.nextInt(); beta[1] = cin.nextInt(); beta[2] = cin.nextInt();
(ii)
for (j = 0; j < 3; j++) beta[j] = cin.nextInt();
Answer

1. Only (i)

2. Only (ii)

3. None of these

4. Both (i) and (ii)
Definition
4. Both (i) and (ii)
Supporting users have an ad free experience!