Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following declarations : int [] beta = { 10, 5, 7, 2, 0, 4, 3}; Wha

ID: 3702790 • Letter: C

Question

Consider the following declarations:

int [] beta = { 10, 5, 7, 2, 0, 4, 3};

What is stored in beta after the following statements executes:

for (int i = 1; i < beta.length; i++)

                  beta[i] = beta[i-1] * beta[i];

    

Suppose list is an array of six elements of type int. what is stored in the array list after the following Java code executes?

int[] list = new int[6];

list[0] = 5;

for (int i=1; i< list.length; i++) {

            list[i] = i + i * 5;

           

            if (i < 4)

                        list[i] = 2 * list[i] – list[i-1];

}

Suppose list is an array of 7 elements what does the following code produce and WHY.

int [] list = { 10, 25, 7, 12, 0, -4, 3};

int i=0;

for( int x: list){

    values[i] = 2* x - 3;

     i++;

}

Given the existence of an array called “list” declared as followes:

int[] list = {25,-45,88,3,10,-5,48,66,15,20,-24,100,16,-31,70,7};

Write a loop to sum all of the elements in the array.

Given the existence of an array called “list” declared as followes:

int[] list = {25,-45,88,3,10,-5,48,66,15,20,-24,100,16,-31,70,7};

Write a loop to print all of the negative elements in the array.

Explanation / Answer

Answers)

1) int [] beta = { 10, 5, 7, 2, 0, 4, 3};

What is stored in beta after the following statements executes:

int [] beta = { 10, 5, 7, 2, 0, 4, 3};

for (int i = 1; i < beta.length; i++){

beta[i] = beta[i-1] * beta[i];

System.out.println(beta[i]);

}

}

So we print as the data gets stored in the beta[i].

Output of the above program:

50

350

700

0

0

0

Thus 50, 350, 700, 0, 0, 0 are stored in beta[].

2) Suppose list is an array of six elements of type int. what is stored in the array list after the following Java code executes?

int[] list = new int[6];

list[0] = 5;

for (int i=1; i< list.length; i++) {

list[i] = i + i * 5;

if (i < 4){

list[i] = 2 * list[i] - list[i-1];

System.out.println(list[i]);

}

}

Output is:

7

17

19

So list stores 7,17,19 when the above code executes.

3) Suppose list is an array of 7 elements what does the following code produce and WHY.

  

int [] list = { 10, 25, 7, 12, 0, -4, 3};

int [] values= new int[10];;

int i=0;

for( int x: list){

values[i] = 2*x - 3;

System.out.println(values[i]);

i++;

}

}

The above code produces the output:

17

47

11

21

-3

-11

3

The above code takes the items stores in the list array and calculates 2*item value- 3 and then stores it into the values array. Thus values array contains 17,47,11,21,-3,-11,3

4) Given the existence of an array called “list” declared as follows:

int[] list = {25,-45,88,3,10,-5,48,66,15,20,-24,100,16,-31,70,7};

Write a loop to sum all of the elements in the array.

Answer)

public class Sum {

public static void main(String args[]) {

  

int[] list = {25,-45,88,3,10,-5,48,66,15,20,-24,100,16,-31,70,7};

int i=0,y=0;

for( int x: list){ // Calculating the sum for each of the elements and storing into the variable i.

y=x;

i=y+i;

}

System.out.println(i); // Printing the sum

}

}

Output:

363

5) Given the existence of an array called “list” declared as followes:

int[] list = {25,-45,88,3,10,-5,48,66,15,20,-24,100,16,-31,70,7};

Write a loop to print all of the negative elements in the array.

Answer)

public class Negative {

public static void main(String args[]) {

  

int[] list = {25,-45,88,3,10,-5,48,66,15,20,-24,100,16,-31,70,7};

for( int x: list){ // Looping to print all negative numbers

if(x<0){ // Checking negative by checking if a number is < 0

System.out.println(x); // Printing the number when the condition is met.

}

}

}

}

Output:

-45

-5

-24

-31