Shared Flashcard Set

Details

Java CS170 Chapter. 10
Quiz Questions
15
Computer Science
Undergraduate 2
12/08/2011

Additional Computer Science Flashcards

 


 

Cards

Term
What does a contain after the following loop runs?

int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, value = 5;

int pos = 0;
for (pos = 0; pos < size; pos++)
if (a[pos] > value) break;
for (int j = size; j > pos; j--)
a[j] = a[j - 1];
a[pos] = value;
size++;

Answer

1. { 1, 3, 5, 7, 0, 0 }

2. { 1, 3, 7, 5, 0, 0 }

3. { 1, 3, 5, 0, 0, 0 }

4. { 5, 1, 3, 7, 0, 0 }
Definition
1. { 1, 3, 5, 7, 0, 0 }
Term
Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)

int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6;

int value = cin.nextInt();
while (size < capacity && value > 0)
{
a[size] = value;
size++;
value = cin.nextInt();
}

Answer

1. Reads up to 3 values and places them in the array in the unused space.

2. Crashes at runtime because it tries to write beyond the array.

3. Reads one value and places it in the remaining first unused space endlessly.

4. Reads up to 3 values and inserts them in the array in the correct position.
Definition
Reads up to 3 values and places them in the array in the unused space.
Term
What does a contain after the following loop?

int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;

for (int i = pos; i < size - 1; i++)
a[i] = a[i + 1];
size--;
Answer

1. {3, 7, 7, 0, 0, 0}
2. {3, 7, 0, 0, 0, 0}
3. {1, 3, 0, 0, 0, 0}
4. {1, 1, 1, 0, 0, 0}
Definition
1. {3, 7, 7, 0, 0, 0}
Term
Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)

int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6;

int value = cin.nextInt();
while (size < capacity && value > 0)
{
a[size] = value;
size++;
}

Answer

1. Crashes at runtime because it tries to write beyond the array.

2. Reads one value and places it in the remaining three unused spaces in a.

3. Reads up to 3 values and inserts them in the array in the correct position.

4. Reads up to 3 values and places them in the array in the unused space.
Definition
2. Reads one value and places it in the remaining three unused spaces in a.
Term
What does a contain after the following loop?

int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;

for (int i = size; i > pos; i--)
a[i - 1] = a[i];
size--;

Answer

1. {3, 7, 7, 0, 0, 0}

2. {1, 1, 1, 0, 0, 0}

3. {0, 0, 0, 0, 0, 0}

4. {3, 7, 0, 0, 0, 0}
Definition
3. {0, 0, 0, 0, 0, 0}
Term
What does the following loop do?

int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;

a[pos] = a[size - 1];
size--;

Answer

1. Removes the 1 from the array, replacing it with a 0.

2. Removes the 1 from the array, replacing it with the 7. Array now unordered.

3. Removes the 1 from the array, keeping the remaining elements ordered.

4. Swaps the 1 and the 7 in the array
Definition
2. Removes the 1 from the array, replacing it with the 7. Array now unordered.
Term
Consider the following two method calls. Which of the method headers would work to handle both calls without a syntax error?

String ans = alphabetize("Bob", "Frank", "Alice");
String ans = alphabetize("Bob", "Frank", "Alice", "Sam", "Judy");

Answer

1. String alphabetize(String names){ }

2. String alphabetize(String[] names){ }

3. String alphabetize(String...names){ }

4. String alphabetize(String a, String b, String c, String d, String e) { }
Definition
String alphabetize(String...names){ }
Term
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}

What is the value of vals.length in the array above?
Answer

1. 3

2. 4

3. 0

4. 16
Definition
2. 4
Term
double[] as = new double[7];
double[] bs;

bs = as;

How many objects are present after the code fragment above is executed?
Answer

1. 7

2. 2

3. 1

4. 14
Definition
3. 1
Term
Which of the following statements creates alpha, a two-dimensional array of 10 rows and 5 columns, wherein each component is of the type int?

(i)
int[][] alpha = new int[10][5];
(ii)
int[][] alpha;
alpha = new int[10][];
for (int i = 0; i < 10; i++)
alpha[i] = new int[5];
Answer

1. None of these

2. Only (ii)

3. Both (i) and (ii)

4. Only (i)
Definition
2. Only (ii)
Term
Suppose we have these two array definitions in main(): String[] hospName = new String[100]; int[] hospCode = new int[100]; hospCode[I] stores the code for a specific hospital. hospName[J] stores the name of the hospital that has code J. Suppose the code 24 is stored at position 13 of hospCode. Which expression below references 24's corresponding name?
Answer

a. hospName[13]

b. hospCode[24]

c. hospCode[hospName[13]]

d. hospName[hospCode[13]]

e. hospName[hospCode[24]]
Definition
d. hospName[hospCode[13]]
Term
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}

How many rows are in the array above?
Answer

1. 0

2. 4

3. 3

4. 2
Definition
2. 4
Term
char[][] array1 = new char[15][10];

How many dimensions are in the array above?
Answer

1. 3

2. 0

3. 2

4. 1
Definition
3. 2
Term
Suppose that sales is a two-dimensional array of 10 rows and 7 columns wherein each component is of the type int , and sum and j are int variables. Which of the following correctly finds the sum of the elements of the fifth row of sales?
Answer

1.
sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[4][j];

2. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[5][j];

3.
sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[5][j];

4.
sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[4][j];
Definition
1.
sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[4][j];
Term
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}

What is in vals[2][1]?
Answer

1. 3.5

2. 1.3

3. 3.3

4. 5.3
Definition
4. 5.3
Supporting users have an ad free experience!