Question 1: Primitive Types vs Reference Types (Unit 1)

Part 1: MCQ

I don’t think this is how MCQs work lol

(a) What kind of types are person1 and person2? Answer: Reference types? (b) Do person1 and person3 point to the same value in memory? Answer: Yes (c) Is the integer “number” stored in the heap or in the stack? Answer: Stack (d) Is the value that “person1” points to stored in the heap or in the stack? Answer: Heap

Part 2: FRQ

(a) Define primitive types and reference types in Java. Provide examples of each.

Primitive types: Stores actual data, consists of booleans and numeric types (i.e. int, float, double) Reference types: Stores reference of an object in memory, used for classes

(b) Explain the differences between primitive types and reference types in terms of memory allocation and usage in Java programs.

Primitive types store the actual data of the object, like the value of an integer, whereas reference types store a reference to the object in memory.

(c) You have a method calculateInterest that takes a primitive double type representing the principal amount and a reference type Customer representing the customer information. Write the method signature and the method implementation. Include comments to explain your code.

// Idk what you mean by method implementation because I have no idea what this method is supposed to do
public static void calculateInterest(double num, Customer person) { // takes double "num" and Customer "person"
    // do something? return nothing cuz void
}

Question 2: Iteration over 2D arrays (Unit 4)

Situation: You are developing a game where you need to track player scores on a 2D grid representing levels and attempts.

(a) Explain the concept of iteration over a 2D array in Java. Provide an example scenario where iterating over a 2D array is useful in a programming task.

Iteration over 2D arrays usually works by looping through each row and every number within that row, using two loops. This is particularly useful for data that can be organized onto a 2D grid, for example, storing the positions of a chess board.

(b) You need to implement a method calculateTotalScore that takes a 2D array scores of integers representing player scores and returns the sum of all the elements in the array. Write the method signature and the method implementation. Include comments to explain your code.

// return type of int to return sum
// parameter type int[][] for scores 2d array
public static int calculateTotalScore(int[][] scores) {
    // Define sum
    int sum = 0;
    
    // Define number of rows
    int rows = scores.length;

    // Loop through each row of the array
    for (int row = 0; row < rows; row ++) {
        // Get number of columns within that row
        int cols = scores[row].length;

        // Loop through each column position within the row
        for (int col = 0; col < cols; col ++) {
            // Add the number at the specify
            sum += scores[row][col];
        }
    }

    // After iterating through 2d array return the sum
    return sum;
}

// Make a test array to show the code works, expected output is 31
public int[][] testArray = {
    {1, 2, 3},
    {3, 4, 1},
    {9, 2, 6}
};

// By golly, it works!
calculateTotalScore(testArray);
31

Question 3: ArrayList (Unit 6)

Situation: You are developing a student management system where you need to store and analyze the grades of students in a class.

(a) Define an arrayList in Java. Explain its significance and usefulness in programming.

ArrayLists are mutable arrays in Java, meaning that they can be changed after initialization, particularly its length. This is useful when you do not know the size of a data set beforehand, so that the array can adapt based on the input.

(b) You need to implement a method calculateAverageGrade that takes an array grades of integers representing student grades and returns the average of all the elements in the array. Write the method signature and the method implementation. Include comments to explain your code.

// return type double to return an average w/ decimals
// parameter type int[] to contain grades (though I think this should be a double)

public static double calculateAverageGrade(int[] grades) {
    // Define number of elements in the array and a variable for the sum
    int n = grades.length;
    int sum = 0;

    // Iterate through the array to calculate sum
    for (int i = 0; i < n; i ++) {
        sum += grades[i];
    }

    // Return the average calculated by sum/number of elements as a double for decimals
    return (double) sum/n;
}

// Expected output: 57.8
int[] grades = {30, 44, 99, 79, 37};

calculateAverageGrade(grades);
57.8

Question 4: Math Class (Unit 2)

Situation: You are developing a scientific calculator application where users need to perform various mathematical operations.

(a) Discuss the purpose and utility of the Math class in Java programming. Provide examples of at least three methods provided by the Math class and explain their usage.

Allows for more complex math operations like absolute value and radicals. I think the most important use of Math is the random feature, because I like rng based games.

(b) You need to implement a method calculateSquareRoot that takes a double number as input and returns its square root using the Math class. Write the method signature and the method implementation. Include comments to explain your code.

// import the Math class!
import java.lang.Math;

// return type double for returning a square root
// parameter type double
public static double calculateSquareRoot(double number) {
    return (double) Math.sqrt(number);
}

// Test with a number idk
calculateSquareRoot(784.2726);
28.004867434072956

Question 5: If, While, Else (Unit 3-4)

Situation: You are developing a simple grading system where you need to determine if a given score is passing or failing.

(a) Explain the roles and usage of the if statement, while loop, and else statement in Java programming. Provide examples illustrating each.

If statements allow to check for conditions, else statements run code that do not meet the specified conditions. A while loop, allows you to run code continuously until a condition is met. An example of a while loop is for a timer, that counts down while the timer is on, or true. An if statement can be used to check if a number is bigger than another and the else would do something with it idk man.

(b) You need to implement a method printGradeStatus that takes an integer score as input and prints “Pass” if the score is greater than or equal to 60, and “Fail” otherwise. Write the method signature and the method implementation. Include comments to explain your code.

// no return type because it will print
// integer parameter type for score
public static void printGradeStatus(int score) {
    // Check if greater than or equal to 60
    if (score >= 60) {
        // if yes, output pass
        System.out.println("pass");
    }
    else {
        // if not, output fail
        System.out.println("fail");
    }
}

// Tests
printGradeStatus(60);
printGradeStatus(30);
pass
fail