Skip to the content.
Intro Primitive Types Reference Types Stack and Heap Code Example Quiz

Quiz Questions for APCSA Unit 1

Questions and Code Cells for the Quiz on Unit 1

CSA-Quiz

Unit 1: Primitive Types

Question 1

Which of the following is a valid declaration of a variable of type int in Java?
a) int 123variable;
b) int variable123;
c) int variable#123;
d) int variable 123;

Answer: b) int variable123;

// Q1 Hack: Define variables according to Java naming conventions.
// For instance, is it snake_case, camelCase, or PascalCase?

int variable123 = 123;
System.out.println(variable123);

int myVariable123 = 123;
System.out.println(myVariable123);

123
123

Question 2

What is the value of the following expression in Java: 5 / 2?
a) 2.5
b) 3
c) 2
d) 2.0

Answer: c) 2

// Q2.1 Hack: Show in code difference between integer and floating point division.
// Q2.2 Hack: Show in code the differnt number types in Java and how they behave.
// Behave means definition and assignment.

int num1 = 9;
int num2 = 12;
int intResult = num1 / num2; 
System.out.println("Integer Division: " + num1 + " / " + num2 + " = " + intResult);

// Floating-point division
double doubleResult = (double) num1 / num2;
System.out.println("Floating Point Division: " + num1 + " / " + num2 + " = " + doubleResult);
Integer Division: 9 / 12 = 0
Floating Point Division: 9 / 12 = 0.75

Question 3

Which primitive type is used to represent a single character in Java?
a) char
b) String
c) int
d) byte

Answer: a) char

// Q3.1 Hack: Show in code all the the non-number Java primitive data types and how they behave.
// Q3.2 Hack: Show in code the String data type and how it behaves.

char character = 'X';
        System.out.println("Character: " + character); // Output: A

        // Demonstrating char behavior
        char nextLetter = (char) (character + 1); // incrementing char
        System.out.println("Next Character: " + nextLetter); // Output: B

        // Boolean data type
        boolean isJavaFun = true; // represents true/false values
        System.out.println("Is Java fun? " + isJavaFun); // Output: true

        // Demonstrating boolean behavior
        boolean isFishTasty = false;
        System.out.println("Is fish tasty? " + isFishTasty); // Output: false

        // Using boolean in a conditional
        if (isJavaFun) {
            System.out.println("Let's keep coding in Java!");
        }
Character: X
Next Character: Y
Is Java fun? true
Is fish tasty? false
Let's keep coding in Java!

Question 4

Answer the following questions based on the code cell:

  • 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:

a) What kind of types are person1 and person2?

Answer: person1 and person2 are reference types pointing to Person objects.

b) Do person1 and person3 point to the same value in memory?

Answer: Yes, person1 and person3 point to the same value in memory.

c) Is the integer number stored in the heap or in the stack?

Answer: The integer number is stored in the stack.

d) Is the value that person1 points to stored in the heap or in the stack?

Answer: The value that person1 points to is stored in the heap.

public class Person {
    String name;
    int age;
    int height;
    String job;

    public Person(String name, int age, int height, String job) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.job = job;
    }
}

public static void main(String[] args) {
    Person person1 = new Person("Carl", 25, 165, "Construction Worker");
    Person person2 = new Person("Adam", 29, 160, "Truck Driver");
    Person person3 = person1;
    int number = 16;
    System.out.println(number);
}
main(null); // This is required in Jupiter Notebook to run the main method.

Question 5

(a) Define primitive types and reference types in Java. The application is for banking, where you need to represent customer information.

(b) Add comments for primitive types and reference types. In terms of memory allocation, discuss concepts like instance, stack, and heap where it adds value.

(c) To assist in requirements, here are some required elements:

  • Create multiple customers from the public class Account.
  • Consider key class variables that a Bank may require: name, balance, accountNumber.
  • Create a two argument constructor using name and balance.
  • Consider in constructor how you will create a unique account number using static int lastAccountNumber
  • Define a method calculateInterest that works with getting and setting double balance using private static double interestRate.

(a) Primitive Types vs. Reference Types in Java

Primitive types are predefined data types (like int and double) that store simple values, while reference types (like String) store references to objects. In a banking application, double balance and int accountNumber are primitive types, whereas String name is a reference type.

(b) Memory Allocation Concepts

Stack memory stores primitive types and reference variable addresses, while Heap memory stores dynamically allocated objects. Each object instance has its data stored in the heap, and static variables like lastAccountNumber are stored in the method area.

public class Account {
    private static int lastAccountNumber = 0;
    private String name;
    private double balance;
    private int accountNumber;
    private static double interestRate = 0.05;

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
        this.accountNumber = ++lastAccountNumber;
    }

    public double getBalance() { return balance; }
    public void setBalance(double balance) { this.balance = balance; }
    public double calculateInterest() { return balance * interestRate; }
    public void displayAccountDetails() {
        System.out.println("Account Number: " + accountNumber);
        System.out.println("Customer Name: " + name);
        System.out.println("Account Balance: $" + balance);
    }
}