CSC Exam Three with Accurate
Solutions
T/F: If an array can hold N values, the valid indexes for that array range from 0 to N -
correct Answer-false the valid range is 0 to N-1
T/F:If you try to access an element outside of the valid index range for an array, an
exception will be thrown. - correct Answer-true
T/F:Arrays can hold primitive types, but not objects - correct Answer-false
What is the output of the following code?
int[] list = new int[5]; System.out.println(list[3]); - correct Answer-0
What is the output of the following code?
int sum = 0; int[] values = {1, 2, 3, 4, 5}; for (int i = 0; i <= 5; i++) sum = sum + values[i];
System.out.println("Sum: " + sum); - correct Answer-no output a bounds error occurs,
the loop tries to access scores[5], but the last element in the array is scores[4].
What is the output of the following code?
int sum = 0; int[] values = {7, 6, 5, 4, 3, 2, 1}; for (int i = 1; i < values.length; i++) sum +=
values[i]; System.out.println("Sum: " + sum); - correct Answer-Sum: 21
which is the following is the preferred way to declare an array
double prices[] = new double[10];
double[] prices = new double[10]; - correct Answer-double[] prices = new double[10];
T/F: The Greek mathematician Eratosthenes came up with the idea of using a sieve
algorithm to find prime numbers. - correct Answer-true
T/F: To be sure you have found all possible prime numbers up to a certain number N,
you need to repeat the sieve algorithm all the way up to N. - correct Answer-false
T/F: Our implementation of a prime sieve uses an array of integers and sets the values
to 0 when they are found not to be prime. - correct Answer-false
What list of prime values is produced if the PrimeSieve program is executed with an
input value of 20? - correct Answer-2 3 5 6 11 13 17 19
T/F:An array that holds objects is, itself, an object. - correct Answer-true
, T/F: An array of objects is really an array of object references. - correct Answer-true
T/F: An array of objects cannot be created with an initialization list. - correct Answer-
false, each element in the list has to be an object of the appropriate type
What is the type of the variable adjectives in the ArrayOfStringDemo program? - correct
Answer-String[]
What is the element type of the adjectives array in the ArrayOfStringDemo program? -
correct Answer-String
What is the type of the variable athletes in the ArrayOfPersonDemo program? - correct
Answer-Person[]
What is the element type of the athletes array in the ArrayOfPersonDemo program? -
correct Answer-Person
T/F: Traversing an array or collection means accessing each element one at a time. -
correct Answer-true
T/F:A for-each loop cannot be used to set the values in an array. - correct Answer-true
T/F: A for-each loop cannot be used to print the elements in an array backwards. -
correct Answer-true, the for-each loop only processes array elements starting at index 0
T/F: An ArrayList is a collection that can be processed using a for-each loop. - correct
Answer-true
Which of the following correctly shows the general structure of a for-each loop header?
for(type variable-name : array-or-collection)
foreach (value array : condition; array-length-specifier)
for (initialization; condition; increment)
foreach (type variable-name : array-or-collection) - correct Answer-for(type variable-
name : array-or-collection)
What is the output of the following code?
int[] list = {2, 4, 6, 8, 10}; int num = 0; for (int val : list) { if (val != 6) num = num + val; }
System.out.println(num); - correct Answer-24
What are the contents of the items array after the following code is executed?
int[] items = {10, 20, 30, 40, 50}; for (int item : items) { item = item * 2; } - correct
Answer-10, 20, 30,40,50 the for loop does not change the values stored in the array
T/F: you cannot use command-line arguments in IDEs like Eclipse and NetBeans. -
correct Answer-false
Solutions
T/F: If an array can hold N values, the valid indexes for that array range from 0 to N -
correct Answer-false the valid range is 0 to N-1
T/F:If you try to access an element outside of the valid index range for an array, an
exception will be thrown. - correct Answer-true
T/F:Arrays can hold primitive types, but not objects - correct Answer-false
What is the output of the following code?
int[] list = new int[5]; System.out.println(list[3]); - correct Answer-0
What is the output of the following code?
int sum = 0; int[] values = {1, 2, 3, 4, 5}; for (int i = 0; i <= 5; i++) sum = sum + values[i];
System.out.println("Sum: " + sum); - correct Answer-no output a bounds error occurs,
the loop tries to access scores[5], but the last element in the array is scores[4].
What is the output of the following code?
int sum = 0; int[] values = {7, 6, 5, 4, 3, 2, 1}; for (int i = 1; i < values.length; i++) sum +=
values[i]; System.out.println("Sum: " + sum); - correct Answer-Sum: 21
which is the following is the preferred way to declare an array
double prices[] = new double[10];
double[] prices = new double[10]; - correct Answer-double[] prices = new double[10];
T/F: The Greek mathematician Eratosthenes came up with the idea of using a sieve
algorithm to find prime numbers. - correct Answer-true
T/F: To be sure you have found all possible prime numbers up to a certain number N,
you need to repeat the sieve algorithm all the way up to N. - correct Answer-false
T/F: Our implementation of a prime sieve uses an array of integers and sets the values
to 0 when they are found not to be prime. - correct Answer-false
What list of prime values is produced if the PrimeSieve program is executed with an
input value of 20? - correct Answer-2 3 5 6 11 13 17 19
T/F:An array that holds objects is, itself, an object. - correct Answer-true
, T/F: An array of objects is really an array of object references. - correct Answer-true
T/F: An array of objects cannot be created with an initialization list. - correct Answer-
false, each element in the list has to be an object of the appropriate type
What is the type of the variable adjectives in the ArrayOfStringDemo program? - correct
Answer-String[]
What is the element type of the adjectives array in the ArrayOfStringDemo program? -
correct Answer-String
What is the type of the variable athletes in the ArrayOfPersonDemo program? - correct
Answer-Person[]
What is the element type of the athletes array in the ArrayOfPersonDemo program? -
correct Answer-Person
T/F: Traversing an array or collection means accessing each element one at a time. -
correct Answer-true
T/F:A for-each loop cannot be used to set the values in an array. - correct Answer-true
T/F: A for-each loop cannot be used to print the elements in an array backwards. -
correct Answer-true, the for-each loop only processes array elements starting at index 0
T/F: An ArrayList is a collection that can be processed using a for-each loop. - correct
Answer-true
Which of the following correctly shows the general structure of a for-each loop header?
for(type variable-name : array-or-collection)
foreach (value array : condition; array-length-specifier)
for (initialization; condition; increment)
foreach (type variable-name : array-or-collection) - correct Answer-for(type variable-
name : array-or-collection)
What is the output of the following code?
int[] list = {2, 4, 6, 8, 10}; int num = 0; for (int val : list) { if (val != 6) num = num + val; }
System.out.println(num); - correct Answer-24
What are the contents of the items array after the following code is executed?
int[] items = {10, 20, 30, 40, 50}; for (int item : items) { item = item * 2; } - correct
Answer-10, 20, 30,40,50 the for loop does not change the values stored in the array
T/F: you cannot use command-line arguments in IDEs like Eclipse and NetBeans. -
correct Answer-false