***Need help ASAP*** Please help solve this by using JAVA and read the direction
ID: 3701703 • Letter: #
Question
***Need help ASAP*** Please help solve this by using JAVA and read the directions carefully! Thanks in advance! Exam 2 Program Design and Abstraction 2. (5 points) What is the output of this program when run? public class WhatsPrinted5 public static int func(int y) f return y*2 public static void main(String argsl) ( int x=10; func(x); System.out.println(x); 3. (5 points) What is the output of this code? int [ ] arr= {-1, -2, -5, 2, 5); forint i 1; í ati] - ali] arrli-1]; iarra length; :1++){ ystem.out.println(Arrays.tostring(arr)):Explanation / Answer
2)
Output will be : 10
Reason : func(x) will return a value which is not stored in the main method, hence value of x will not change. It will simply print the original value of x.
3)
Output will be : [-1, 2, -10, -20, -100]
Reason: The compiler provides the memory location from 0th index upto n-1. Here the loop starts from 1 and traverse upto length of array.
In each iteration the value of array at ith index, changes
For example for i = 1 arr[i] = -2,
new value of arr[i] = arr[i] * arr[i-1], arr[1] = arr[1] * arr[0]
arr[1] = -2 * -1 = 2.
4)
Output: [1, 1, 1, 3, 2]
Reason:
int[] array = new int[5];
for(int i=0;i<array.length;i++)
{
array[i] = i;
}
The above lines will initialize the array as {0,1,2,3,4}
array[index] = 1, this line will change the value of 0th element to be 1
index = array[array[index] + 1], now the index becomes 2
array[index] = array[array[index] - 1], since the value of index is 2, so arrary[2] = array[array[2]-1]
array[2] = array[2-1] = array[1] = 1
array[array.length-1] = 2, this line will change the value of array[4] = 2
hence the final array becomes : [1, 1, 1, 3, 2]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.