What is the output of the following lines of code? int[] y = {6, 8, 10, 12, 14,
ID: 3632773 • Letter: W
Question
What is the output of the following lines of code?int[] y = {6, 8, 10, 12, 14, 16};
System.out.println(y[4] + " " + y.length);
y[0] = 2 * y[1];
System.out.println(Arrays.toString(y));
What is the output of the following lines of code?
int[] foo = {1, 2, 3, 4};
int[] bar = foo;
int[] baz = new int[foo.length];
for (int i = 0; i < foo.length; i++) {
baz[i] = foo[i];
}
foo[2] = 5;
System.out.println(foo[2] + " " + bar[2] + " " + baz[2]);
After giving the output, explain briefly why it makes sense.
Consider the following method:
public static void mystery(int[] values) {
int val = values[0];
for (int i = 0; i < values.length - 1; i++) {
values[i] = values[i + 1];
values[i]++;
}
values[values.length - 1] = val + 1;
}
Explain briefly what this method does. To figure it out, you may find it helpful to trace through the execution of one or more method calls like the following, each of which operates on one of the arrays from the problems above:
mystery(y);
mystery(foo);
Explanation / Answer
please rate - thanks
What is the output of the following lines of code?
int[] y = {6, 8, 10, 12, 14, 16};
System.out.println(y[4] + " " + y.length);
y[0] = 2 * y[1];
System.out.println(Arrays.toString(y));
14 6
[16, 8, 10, 12, 14, 16]
What is the output of the following lines of code?
int[] foo = {1, 2, 3, 4};
int[] bar = foo; //bar is now foo "points to it"
int[] baz = new int[foo.length];
for (int i = 0; i < foo.length; i++) {
baz[i] = foo[i]; //baz has same values as foo
}
foo[2] = 5; // change foo[2] so also changing bar[2]
System.out.println(foo[2] + " " + bar[2] + " " + baz[2]);
After giving the output, explain briefly why it makes sense.
5 5 3
changed bar so changed foo, but never changed value of baz since that is an array unto itself, whereas the other 2 share the same memory
Consider the following method:
public static void mystery(int[] values) {
int val = values[0];
for (int i = 0; i < values.length - 1; i++) {
values[i] = values[i + 1];
values[i]++;
}
values[values.length - 1] = val + 1;
}
Explain briefly what this method does. To figure it out, you may find it helpful to trace through the execution of one or more method calls like the following, each of which operates on one of the arrays from the problems above:
mystery(y);
mystery(foo);
does a circular shift left of the values in the array, and adds 1 to each
so if initial array is
1 22 43 54 65 76
new array will be
23 44 55 66 77 2
where new element 1 becomes original element 2 with 1 added to it 22 became 23
new element 2 becomes original element 3 with 1 added to it 43 became 44
etc
if any question message me
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.