Thanks for help~ Question 1 Which of the following are valid declarations/initia
ID: 3864867 • Letter: T
Question
Thanks for help~
Question 1
Which of the following are valid declarations/initializations of arrays? Select all that apply.
Select one or more:
(a)int primes = {2, 3, 4, 5, 7, 11};
(b)int[] scores = new int[30];
(c)int[] scores = int[30];
(d)char[] grades = new char[];
(e)char grades[] = {'a', 'b', 'c', 'd', 'f'};
(f)double[] elapsedTimes = {11.47, 12.04, 11.72, 13.88};
(g)int[] primes = new {2,3,5,7,11};
Question 2
Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr?
Select one:
(a)It stores 5 elements with legal indices between 0 and 5
(b)It stores 6 elements with legal indices between 0 and 5
(c)It stores 5 elements with legal indices between 0 and 4
(d)It stores 5 elements with legal indices between 1 and 5
(e)It stores 4 elements with legal indices between 1 and 4
Question 3
What type of error is in the code below?
int[] nums = {3, 2, 3, 6, 9, 10, 12, 32, 3, 12, 6};
for (int count = 1; count <= nums.length; count++)
System.out.println (nums[count]);
Select one:
(a)none of the above (no error)
(b)runtime error
(c)compile time error
(d)compile time and runtime error
Question 4
Fill in the missing code from the statement below. Write code only to complete this single statement- do not write additional statements.
The array flags should contain alternating boolean values (i.e., true at index 0, false at index 1, true at index 2, false at index 3, etc.)
for (int index = 0; index < flags.length; index++)
flags[index] = ??? your code here
Question 5
For the next three questions, assume an int array candy stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
What does the following code do?
Scanner scan = new Scanner(System.in);
int value1 = scan.nextInt();
int value2 = scan.nextInt();
candy[value1] += value2;
Select one:
(a)adds 1 to the number of bars sold by child value1 and child value2
(b)adds value1 to the number of bars sold by child value2
(c)adds 1 to the number of bars sold by child value1
(d)inputs a new value for the number of bars sold by both child value1 and child value2
(e)adds value2 to the number of bars sold by child value1
Question 6
Which of the following code could be used to compute the total number of bars sold by the children?
Select one:
(a)for (int j=0; j<12; j++) sum += candy[j];
(b)for (int j=0; j<12; j++) sum = candy[j];
(c)for (int j=0; j<12; j++) candy[j] = sum;
(d)for (int j=0; j<12; j++) sum += [j];
(e)for (int j=0; j<12; j++) [j] += sum;
Question 7
What does the following method do?
public int mystery() {
int value1 = 0;
int value2 = 0;
for (int j = 0; j < 12; j++)
if (candy[j] > value1) {
value1 = candy[j];
value2 = j;
}
return value2;}
Select one:
(a)It returns the index of the child who sold the most candy bars
(b)It returns the total number of children who sold more than 0 candy bars
(c)It returns the total number of children who sold 0 candy bars
(d)It returns the number of candy bars sold by the child who sold the most candy bars
(e)It returns the total number of candy bars sold
Question 8
The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only.
int foo = 0;
for (int j =0 ; j < list.length; j++)
if (list[j] > foo)
foo = list[j];
Select one:
(a)it stores every value in list, one at a time, in foo, until the loop terminates
(b)it counts the number of elements in list that are less than foo
(c)it stores the smallest value in list (the minimum) in foo
(d)it counts the number of elements in list that are greater than foo
(e)it stores the largest value in list (the maximum) in foo
Question 9
Which of the following is true about the code below?
int[] nums = new int[5];
for(int i=0; i<=5; i++) {
System.out.println(nums[i]);
}
Select one:
(a)there are no errors
(b)it will result in a compiler error only
(c)it will result in a compiler error and a run-time error
(d)it will result in a run-time error only
Question 10
If a and b are both int arrays, then a = b; will
Select one:
(a)copy the 0th element of b into the 0th element of a
(b)return true if a and b are aliases and return false otherwise
(c)create an alias
(d)copy all elements of b into a
(e)return true if each corresponding element of b is equal to each corresponding element of a (that is, a[0] is equal to b[0], a[1] is equal to b[1] and so forth) and return false otherwise
Question 11
Every array has a built in toString method that returns all of the elements in the array as one String with “ ” inserted between each element. This means you can put the name of the array inside a println statement and a nicely formatted list of values will print.
Select one:
True
False
Question 12
In Java, an array can only store one type of data. For instance, you cannot create an array that stores both double and String values.
Select one:
True
False
Question 13
An array, when instantiated, is fixed in size, but an ArrayList can dynamically change in size when new elements are added to it.
Select one:
True
False
Question 14
To swap the elements in positions 3 and 4 in an int array values, you would do:
values[3] = values[4];
values[4] = values[3];
Select one:
True
False
Explanation / Answer
Solution:
Question 1:
a) int primes = {2, 3, 4, 5, 7, 11};
Invalid. It should be declared as int[] otherwise it be considered as one integer variable and not an array.
b) int[] scores = new int[30];
Valid. It will intantiates an array object.
c) int[] scores = int[30];
Invalid. it should be new int[30].
d) char[] grades = new char[];
Invalid. It is necessary to specify the size of the grades array.
e) char grades[] = {'a', 'b', 'c', 'd', 'f'};
Valid. The grades array is declared to hold the values given.
f) double[] elapsedTimes = {11.47, 12.04, 11.72, 13.88};
Valid. The declaration specifies that elapsedTimes is an array holding double type data elements.
g) int[] primes = new {2,3,5,7,11};
Invalid. The initializer is using the new operator which is not allowed.
Question 2:
c)It stores 5 elements with legal indices between 0 and 4
Question 8:
a)it stores every value in list, one at a time, in foo, until the loop terminates
Question 12:
True
An array is a homogeneous datatype.
Question 13:
True
ArrayList can dynamically change its size when new elements are added to it because it is a class which uses an array where larger array is created by copying the old array into new one.
Question 14
False
both values[3] and values[4] contains the same value
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.