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

Part 1: MCQ

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

Part 2: FRQ

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

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

(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.

// code

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.

(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.

// code

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.

An arraylist is a list of any type. This means we can have a list of any objects. And for reference, everything in java is an object. We could have a list of arraylists, arrays, integers, a custom object we created, or anything else we want. This is extemely useful for grouping together lists of things, allowing us to organize our code and perform specific operations. Arraylists are also mutable, meaning we can change the size of them, which is different than normal arrays. Arraylists also have built in methods which help us like easily adding or removing entries or getting properties like the size easily.

(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.



public class AverageGrade{
    private ArrayList<Integer> grades = new ArrayList<Integer>();
    public AverageGrade(){
        // Adds example grades for example
        this.grades.add(96);
        this.grades.add(93);
        this.grades.add(80);
        this.grades.add(84);
        this.grades.add(86);
        this.grades.add(75);
        this.grades.add(68);
    }

    public int calculateAverageGrade(){
        // intializes sum
        int sum = 0;
        // iterates through grades and adds it to the sum
        for (int i = 0; i < grades.size(); i++){
            sum += grades.get(i);
        }
        // returns the division of sums and the number of grades to find the average. 
        return sum/grades.size();
    }

    public static void main(String[] args){
        AverageGrade averageGrade = new AverageGrade();
        System.out.println(averageGrade.calculateAverageGrade());
    }
}

AverageGrade.main(null);
83

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.

(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.

// code

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 are the backbone of all coding logic, they are the main way that one can check a condition and change the behavior of code according too differing conditions. Else statements are integral to this as well, as they allow us to simply acount for conditions outside of the ones we anticipate. While loops are useful as they allow us to loop infintely until a condition is met, another way to reshape our code depending on varying conditions.

Examples:

If statements:

if (name == "Toby"){
    // Do something
}
else if (name == "Gene"){
    // Do something different
}
else if (name == "Drew"){
    // Do something different
}
else {
    // Do something different if the name is not something we're expecting to get
}

While Loop

while (num > target){
    num--;
    count++;
}
// this simple code would allow us to calculate how far away from a target a number is, by infinitely incrementing downwards until we reach our desired number. Obviously there are easier ways to do this but this is an example of how while loops can be useful for repetative tasks. 

(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.

public class IsPassing{
    public IsPassing(){}

    public static void printGradeStatus(int grade){
        if (grade >= 60){
            System.out.println("Pass");
        }
        else{
            System.out.println("Fail");
        }
    }
}

IsPassing.printGradeStatus(80);

Pass