QUESTION 16 Remember that the % operator evaluates to the remainder after intege
ID: 3864231 • Letter: Q
Question
QUESTION 16
Remember that the % operator evaluates to the remainder after integer division e.g. 8 % 4 is 0, 11 % 4 is 3. What is the value of x after the following?
int x = 0;
for (int i = 1; i < 6; ++i)
if (i % 2 == 0)
x = x + i;
A.
0
B.
15
C.
6
D.
21
QUESTION 17
Assume that mynums is an array of 5 integers. What will be the contents of mynums after the following code fragment?
for (int i = 0; i < 5; i++)
mynums[i] = i + 1;
A.
1 2 3 4 5
B.
0 1 2 3 4
C.
1 2 3 4
D.
1 1 1 1 1
QUESTION 18
What is the effect of the following code fragment?
for (double element : values)
element = 0;
A.
The elements of the array named values are initialized to zero
B.
The elements of the array named element are initialized to zero
C.
The first element of the array named values is initialized to zero
D.
The elements of the array named values are not modified
QUESTION 19
The following statement accesses the element at index 4 in an array:
x = a[4];
What is the equivalent operation using an array list?
A.
x = a.get(4);
B.
x = a.get();
C.
x = a.get[4];
D.
x = a[4];
QUESTION 20
Assume that values is an array of random integers. Which condition must be used in the indicated area so the code fragment below will set max to the largest value in values?
int max = values[0];
for (int val: values)
{
if (/* put condition here */)
max = val;
}
A.
max > val
B.
val > max
C.
values[val] > max
D.
max > values[val]
A.
0
B.
15
C.
6
D.
21
Explanation / Answer
Please give the thumbs up, If it is helpful for you. Thank you!!
ANSWER 16) C. 6
Explanation:
for i=1, if statement fails, So x=x+1 will not execute.
for i=2, if statement pass, So x=x+1 will execute. Hence, x=2
for i=3, if statement fails, So x=x+1 will not execute.
for i=4, if statement pass, So x=x+1 will execute. Hence, x=2+4 => 6
for i=5, if statement fails, So x=x+1 will not execute.
ANSWER 17) A. 1 2 3 4 5
Explanation:
this will add one to each element in the array.
ANSWER 18)
A. The elements of the array named values are initialized to zero.
Explanation:
for (double element : values)
element = 0;
Each element of values are assigned to 0.
ANSWER 19) A. x = a.get(4);
Explanation:
The ArrayList.get(int) method is a thin wrapper over a direct array access.
ANSWER 20) B. val > max
Explanation: Iterate over each element of array values, if any val is greater than max then assign that value to max.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.