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

Your friend is looking at the previous code and he is wondering why after callin

ID: 3736126 • Letter: Y

Question

Your friend is looking at the previous code and he is wondering why after calling foo()  a is not modified but the array b is modified. How can you explain this? Is it expected?


Output:

Write a method isSorted() which takes in an array of integers and returns true if the numbers are sorted from smallest to largest.

Then, write a method called reverseAll(). This method will take in an array of Strings and modify the array so that all the Strings in the array are reversed. Because this method modifies the contents of the array being passed in, it does not return anything. An example:

Explanation / Answer

Question:1

In calling function parameters passing is divided into two parts. one is call by value, another is call by reference.

call by value means we are passing just value. It doesn't care which operations that can be done. It's just passing value i.e., the value assigning to another variable...

call by reference means we are passing reference of variable. it means we passing address of variable instead of address. It means we are sharing address of variable, if any modifications done in the function that also changed, because of same memory address.

generally variables are passing through call by value, arrays can be pass through the call by reference.

that's why "a" doesn't changed(because of call by value), "b" changed due to call by reference.

Question-2:

import java.util.*;

import java.io.*;

public class Output{

public static void main(String[] args){

int arr[] = {0,2,3,4};

System.out.println("isSorted("+Arrays.toString(arr)+") is: "+isSorted(arr));

int arr1[] = {5,2,3,4};

System.out.println("isSorted("+Arrays.toString(arr1)+") is: "+isSorted(arr1));

String str[] = {"apple", "banana", "racecar", "abc"};

String rev[] = reverseAll(str);

System.out.println(" "+Arrays.toString(rev));

}

public static boolean isSorted(int[] arr){

int len = arr.length;

for(int i=0; i<len-1; i++){

if(arr[i]>arr[i+1])

return false;

}

return true;

}

public static String[] reverseAll(String[] arr){

int len = arr.length;

String n_arr[] = new String[len];

for(int i=0; i<len; i++){

int s_len = arr[i].length();

n_arr[i] = "";

for(int j=0; j<s_len; j++)

n_arr[i] += arr[i].charAt(s_len-j-1);

}

return n_arr;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote