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;

Question 1 Answer explanation

It is b because for the others you cannot start with a number for declaring variables or have a space or any sport of non-alpha numeric characters.

// 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);

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.

Answer Explanation

It is C because it is dividing 2 integers which truncates the decimal of 0.5 when dividing.

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.

Answer Explanation #3

It is A because char represents a single part in a string and form strings.

Question 4

Answer the following questions based on the code cell:

  • a) What kind of types are person1 and person2?
  • Answer: They are the type “Person.”
  • b) Do person1 and person3 point to the same value in memory?
  • Answer: They don’t since they are different objects.
  • c) Is the integer “number” stored in the heap or in the stack?
  • Answer: It is stored in the stack memory.
  • d) Is the value that “person1” points to stored in the heap or in the stack?
  • Answer: It is stored in the heap memory.
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.
public class Account{
    public String name; // this is a refrence type as seen by the captial S and some methods include the .equals method and this is stored in the heap
    public double balance; // this is a primitve stored in stack so (LIFO)
    public int Accountnumber;// this is a primitve stored in stack so (LIFO)
    public static int lastAccountNumber;// this is a primitve stored in stack so (LIFO)
    private static double intrestRate=0.01; // 1 percent and is a primitve stored in stack so (LIFO)
    Account(String name, double balance){
        this.name=name;
        this.balance=balance;
        this.Accountnumber=lastAccountNumber;
        lastAccountNumber+=1;
    }
    public void calculateIntrest(){
        this.balance+=balance*intrestRate;
    }
    public double getbalance(){
        return this.balance;
    }

}

Account person1= new Account("jon",100000);
person1.calculateIntrest();
System.out.println(person1.getbalance());

101000.0