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

public static void main(String[] args) { int i; long george[]; long jane[] = {1,

ID: 3919570 • Letter: P

Question

public static void main(String[] args) { int i; long george[]; long jane[] = {1, 2, 3, 5, 8, 13}; Scanner console = new Scanner(System.in); george = new long[console.nextInt()]; for (i = george.length - 1; (i >= 0); --i) { george[i] = console.nextLong() * 2; } i = console.nextInt(); swap(george, jane, i); System.out.println(george[i]); } public static void swap(long[] array1, long[] array2, int which) { long temp; temp = array1[which]; array1[which] = array2[which]; array2[which] = temp; } and the following user input values 5 4 3 2 1 0 2 what output is displayed?

Explanation / Answer

So we first created a java project and created a package with named chegg and inside that created a class named Demo1 to run this program.

package chegg;
import java.util.Scanner;
public class Demo1 {

public static void main(String[] args)
{

int i;
long george[];
long jane[] = {1, 2, 3, 5, 8, 13};
Scanner console = new Scanner(System.in);
george = new long[console.nextInt()];
for (i = george.length - 1; (i >= 0); --i)
{ george[i] = console.nextLong() * 2; }
i = console.nextInt();
swap(george, jane, i);
System.out.println(george[i]); }

public static void swap(long[] array1, long[] array2, int which)
{ long temp; temp = array1[which];
array1[which] = array2[which];
array2[which] = temp; }
}

Step 1: We first declared an integer type variable i .

// int i;

Step2; Then we declared an array named george which will hold long values.

//long george[];

step 3: Then we declare a scanner class object named cosole for taking input

//Scanner console = new Scanner(System.in);

step 4: After that we declared the size of array george by taking input from console.

//george = new long[console.nextInt()];

for ex: if we write 5 on console an array named george will be defined which can hold 5 long value.

i.e. george[5]; so index will go from i=0 to i=5-1 i.e.=4

Step 5: After that we take input from console from i=4 to i=0 that is we are filling array reversely and multiplying that value with 2.

for (i = george.length - 1; (i >= 0); --i)
{ george[i] = console.nextLong() * 2; }

ex: 4 3 2 0 1

so george[i]=value at console*2;

so george [4]=4*2=8;

george[3]=3*2=6;

george[2]=2*2=4;

george[1]=1*1=1;

george[0]=0*0=0

so now geoge[]={0,1,4,6,8};

step 6: now take new value of i from console

//i = console.nextInt();

ex : i=2;

step 7: sending george and jane array withthe new i value in swap fxn

//swap(george, jane, i);

now call goes to swap fxn where george[i] is swapped with jane[i] using temp variable

first george[i] is stored in a temp variable then jane[i] is assigned to george[i] to after that temp i.e. george[i] is assigned to jane[i] and value is swapped.

//public static void swap(long[] array1, long[] array2, int which)
{ long temp; temp = array1[which];
array1[which] = array2[which];
array2[which] = temp; }

step 8 : program returns back to System.out.println(george[i]); statement which will be swapped value i.e. jane[i]

ex: george[]=

jane[]=

so when i=2 so george[i] is swapped with jane[i]

so now george[2] will be jane[2] i.e. 3

so output will be 3

0 2 4 6 8