Question 26:

d

What is it requiring?

Pretty straight forward, asking what will print out as a result of calling the method start(). However, why is this question easy to mess up?

Most common mistake: You MUST be careful when see which string is brought in.

Step by Step Breakdown Code:

public class CBq26 {
    public static void changeIt(int[] arr, int val, String word) {
        arr = new int[5];
        val = 0;
        word = word.substring(0, 5);

        for (int k = 0; k < arr.length; k++) {
            arr[k] = 0;
        }
    }

    public static void start() {
        int[] nums = {1, 2, 3, 4, 5};
        int value = 6;
        String name = "blackboard";

        changeIt(nums, value, name); 

        for (int k = 0; k < nums.length; k++) {
            System.out.print(nums[k] + " ");
        }
        
        System.out.print(value + " ");
        System.out.print(name);
    }

    public static void main(String[] args) {
        start();
    }
}

CBq26.main(null);
1 2 3 4 5 6 blackboard

Step by Step Analysis:

  1. The code doesn’t use any variables that belong to the entire class, only those within the “start” method. These variables only exist within that method.

  2. The “changeIt” method doesn’t print anything, and it doesn’t change the values of the variables passed to it. So, the variables stay the same after “changeIt” is called.

  3. Options a, c, and d can be ruled out because “name” isn’t modified after being defined in the “start” method.

  4. In the “start” method, variables are just printed, not modified. The “nums” array, the “value” variable, and the “name” variable are printed out, but their values aren’t changed.

What if we changed String name to String word?

public class CBq26 {
    public static void changeIt(int[] arr, int val, String word) {
        arr = new int[5];
        val = 0;
        word = word.substring(0, 5);

        for (int k = 0; k < arr.length; k++) { //array nums is changed to {0, 0, 0, 0, 0}
            arr[k] = 0;
        }
    }

    public static void start() {
        int[] nums = {1, 2, 3, 4, 5}; //array nums is passed through the method changeIt()
        int value = 6;
        String word = "blackboard"; //string "blackboard" is passed through the method changeIt()

        changeIt(nums, value, word);

        for (int k = 0; k < nums.length; k++) {
            System.out.print(nums[k] + " ");
        }
        
        System.out.print(value + " ");
        System.out.print(word);
    }

    public static void main(String[] args) {
        start();
    }
}

CBq26.main(null);
1 2 3 4 5 6 blackboard

Popcorn hack:

Why does the word “blackboard” and still not change?

public class CBq26 {
    public static void changeIt(int[] arr, int val, String word) {
        arr = new int[5];
        val = 0;
        word = word.substring(0, 5);
        System.out.println("Word changed locally: " + word);

        for (int k = 0; k < arr.length; k++) {
            arr[k] = 0;
        }
    }

    public static void start() {
        int[] nums = {1, 2, 3, 4, 5};
        int value = 6;
        String name = "blackboard";
        String word = "blackboard"; 

        changeIt(nums, value, name);

        for (int k = 0; k < nums.length; k++) {
            System.out.print(nums[k] + " ");
        }
        
        System.out.print(value + " ");
        System.out.print(name);
    }

    public static void main(String[] args) {
        start();
    }
}

CBq26.main(null);
Word changed locally: black
1 2 3 4 5 6 blackboard

Remember, string is immutable, meaning it can’t be changed after created.

This concept is also applied with the array “nums”, where although it is changed, it is only changed locally (respective to the method).

public class CBq26 {

    static int[] arr; //declare arr as a static variable

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};

        arr = new int[nums.length]; //initialize arr with the same length as nums

        for (int k = 0; k < arr.length; k++) {
            arr[k] = 0;
        }

        for (int k = 0; k < arr.length; k++) {
            System.out.print(arr[k] + " "); //print arr array elements individually
        }
    }
}

CBq26.main(null);
0 0 0 0 0 

Hacks

  1. Finish popcorn hacks
  2. Create a Java program that defines a Car class with static and instance variables, and implement methods for displaying car information and obtaining the total number of cars, then demonstrate these functionalities in the main method.