1. Consider the following array: int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; Wha
ID: 3741077 • Letter: 1
Question
1. Consider the following array:
int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };
What is the value of total after the following loops complete?
a) int total = 0;
for (int i = 0; i < 10; i++) { total = total + a[i]; }
b) int total = 0;
for (int i = 0; i < 10; i = i + 2) { total = total + a[i]; }
c) int total = 0;
for (int i = 1; i < 10; i = i + 2) { total = total + a[i]; }
d) int total = 0;
for (int i = 2; i <= 10; i++) { total = total + a[i]; }
e) int total = 0;
for (int i = 1; i < 10; i = 2 * i) { total = total + a[i]; }
f) int total = 0;
for (int i = 9; i >= 0; i--) { total = total + a[i]; }
g) int total = 0;
for (int i = 9; i >= 0; i = i - 2) { total = total + a[i]; }
h) int total = 0;
for (int i = 0; i < 10; i++) { total = a[i] - total; }
2. What is wrong with each of the following code segments?
a)
b)
3. Write code that fills an array values with each set of numbers below.
a) 1 2 3 4 5 6 7 8 9 10
b) 0 2 4 6 8 10 12 14 16 18 20
c) 1 4 9 16 25 36 49 64 81 100
d) 0 0 0 0 0 0 0 0 0 0
e) 1 4 9 16 9 7 4 9 11
f) 0 1 0 1 0 1 0 1 0 1
g) 0 1 2 3 4 0 1 2 3 4
4.Rewrite the following loops without using the enhanced for loop construct. Here, values is an array of floating-point numbers.
a) for (double x : values) { total = total + x; }
b) for (double x : values) { if (x == target) { return true; } }
c) int i = 0; for (double x : values) { values[i] = 2 * x; i++; }
Explanation / Answer
Question 1a)
Answer:
25
Question 1b)
Answer:
13
Question 1c)
Answer:
12
Question 1d)
Answer:
It will throw an exception
java.lang.ArrayIndexOutOfBoundsException: 10
Question 1e)
Answer:
11
Question 1f)
Answer:
25
Question 1g)
Answer:
12
Question 1h)
Answer:
-1
Question 2
Answer:
int[] values = new int[10];
for (int i = 1; i <= 10; i++)
{
values[i-1] = i * i;
}
Question 3
a) 1 2 3 4 5 6 7 8 9 10
int a[] = new int[10];
for(int i=0;i<a.length;i++) {
a[i]=i+1;
}
b) 0 2 4 6 8 10 12 14 16 18 20
int a[] = new int[10];
for(int i=0,j=0;i<a.length;i++,j+=2) {
a[i]=j;
}
c) 1 4 9 16 25 36 49 64 81 100
int a[] = new int[10];
for(int i=0;i<a.length;i++) {
a[i]=(i+1)*(i+1);
}
d) 0 0 0 0 0 0 0 0 0 0
int a[] = new int[10];
for(int i=0;i<a.length;i++) {
a[i]=0;
}
e) 1 4 9 16 9 7 4 9 11
int a[] ={1 ,4 ,9 ,16, 9, 7, 4, 9, 11};
f) 0 1 0 1 0 1 0 1 0 1
int a[] = new int[10];
for(int i=0;i<a.length;i++) {
if(i%2==0)
a[i]=0;
else
a[i]=1;
}
g) 0 1 2 3 4 0 1 2 3 4
int a[] = new int[10];
for(int i=0,j=0;i<a.length;i++,j++) {
if(i==5) {
j=0;
}
a[i]=j;
}
Question 4
a) for (double x : values) { total = total + x; }
for(int i =0;i<values.length;i++) {
total+=values[i];
}
b) for (double x : values) { if (x == target) { return true; } }
for(int i =0;i<values.length;i++) {
if (values[i] == target) { return true; }
}
c) int i = 0; for (double x : values) { values[i] = 2 * x; i++; }
for(int i =0;i<values.length;i++) {
values[i] = 2 * values[i];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.