Module 01: Primitive Types Flashcards
Which of the following is not a primitive type?
- int
- double
- String
- boolean
- char
- String
Which of the following statements is true about variables?
- The memory associated with a variable of a primitive type holds an actual primitive value.
- When a variable is declared final, its value can only be changed through a direct reassignment.
- A variable originally created as an int can be changed to store a double through casting.
- All of these choices are true
- The memory associated with a variable of a primitive type holds an actual primitive value
What is the value of x after this code runs?
int x = 5;
x = 10;
x = 4;
4
What is the proper syntax to declare and initialize a variable called temperature to have the value 70.4?
- int temperature = 70.4;
- double temperature = 70.4;
- temperature = 70.4;
- dbl temperature = 70.4;
- temperature = (double) 70.4
- double temperature = 70.4;
What is the result of this expression?
(int) (5 + 2 / 3 + 1)
6
Which expression returns the 1’s place of an integer x?
- x % 10
- x / 10
- x % 100
- x + 1
- x % 10
What is the value of myInteger after this line of code is executed?
int myInteger = (int) 5.6;
5
What output will be produced by
System.out.println(“Hello”); System.out.println(“Karel”);
Hello
Karel
Refer to the following code segment:
- double myDouble = 1/4;*
- System.out.println(“1 / 4 = “ + myDouble);*
The output of the code is:
1 / 4 = 0.0
The student wanted the output to be:
1 / 4 = 0.25
Which change to the first line of their code segment would get the student the answer that they wanted?
- int myDouble = 1/4;
- double myDouble = (double) 1/4;
- double myDouble = (int) 1/4;
- double myDouble = (int) (1.0/4.0);
- double myDouble = (double) 1/4;
What is the result of this expression?
4 + 8 * 3 / 4 + 5 % 2
11
Joe’s Pizza is creating a program that will calculate the total amount of money an employee earns each week. Employees are paid by the hour and only work in 1 hour increments. Salaries start at minimum wage, but employees get a $0.50 raise after the first month. Which variables would be the best to store the hours and salary of the employees?
(1) double hours
int salary
(2) int hours
double salary
(3) boolean hours
double salary
(4) int hours
int salary
(5) double hours
boolean salary
(2) int hours
double salary
What will the following code print?
- int n = 5;*
- n ++;*
- n ++;*
- n += n;*
- System.out.println(n);*
14
Which of the following would equal 2?
I.
int x = 0;
x ++;
x += x;
II.
int y = 4;
y ++;
y /= 2;
III.
int z = 4;
z += 2;
z /= 2;
- I Only
- II Only
- III Only
- I and II Only
- I, II, and III
- I and II Only
Given a and b as properly initialized integers, which of the following will result in a correct calculation with a decimal answer?
- double y = (double) (a / b);
- double y = a / b * 1.0;
- double y = a / b;
- double y = 1.0 * a / b;
- double y = 1.0 * a / b;
Assume y is a properly initialized positive integer. Which of the following will always result in a value of 1?
- y ++;
- y –;
- y += y;
- y /= y;
- y -= y;
- y /= y;
The following code is intended to print 8.
- int x = 23;*
- double y = 3;*
- System.out.println((int)(x / y));*
What is printed and why?
- 7 because the values of x and y are integers so 23 / 3 evaluated to 7
- 7 because x / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7
- 8 because the values of x and y are integers so 23 / 3 evaluated to 8
- 8 because x / y calculates to 7.66 then the cast to an int results in the value getting rounded up to 8
- 7 because x / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7
What will the output of the following lines of code be?
int x = 10;
int y = x / 4;
System.out.print(“x + y = “);
System.out.print(x + y);
x + y = 12
Which of the following print:
Hello Java!
I.
System.out.println(“Hello Java!”);
II.
System.out.print(“Hello Java!”);
III.
System.out.print(“Hello”);
System.out.print(“Java!”);
IV.
System.out.println(“Hello”);
System.out.println(“Java!”);
- I and II only
- I, II, and III only
- I and III only
- I, II, and IV only
- I, II, III, and IV
- I and II only
How many lines will be printed with the following statement?
System.out.println(“Hello”);
System.out.println(“ World”);
System.out.print(“Welcome to”);
System.out.print(“Java.”);
3
Assume that a, b, and c are all integers with values of 90, 5, and 4 respectively. What would the value of x be in this expression?
int x = a / b / c;
4
A financial planner wants to calculate the average rate of return for clients. She does this by dividing the earnedIncome by the principal amount and displays the value as a double. Which of the following will correctly calculate and store the returnRate, assuming earnedIncome and principal are integers?
I. double returnRate = earnedIncome / principal;
II. double returnRate = (double) earnedIncome / principal;
III. double returnRate = (double) (earnedIncome / principal);
- I only
- II only
- III Only
- II and III only
- I, II, III
- II only
A teacher has calculated the gradeAverage as a double, but for report cards, she needs to report it rounded to the nearest whole number. Assuming that we round up from 0.5, which of the following will correctly round the gradeAverage?
- int rcGrade = (int) gradeAverage;
- int rcGrade = gradeAverage % 0.5;
- int rcGrade = (int) gradeAverage + 0.5;
- int rcGrade = (int) (gradeAverage + 0.5);
- int rcGrade = (int) gradeAverage - 0.5;
- int rcGrade = (int) (gradeAverage + 0.5);
What is the result of the following expression when x is 125?
x % 6
5
Given the following, what will be printed out?
int a = 2;
int b = 3;
int c = 4;
System.out.println(a * b + b / a + (a * c / 4.0) * c);
15.0
Which of the following values can correctly be saved in a boolean?
True
Yes
1
true
yes
true
Look at the following piece of code. Your task is to debug the code. For each correction there should be a comment on or before that line of code telling me how you plan to correct the error.

public static void main(String args [])
{
int num = 2;
// Since int p is an integer, it cannot be a desimal (then it will need to be a double), so it should be int p = 5 or double p = 5.0
int p = 5;
int d = 5;
int m = 2;
double a = 7;
double b = 3.5;
// There needs to be a semicolon at the end of a line of code for the compiler to know that it is the end of a command
double c, f, g, h;
c = a/b;
f = d/m;
g = d%m;
h = d/(double)m;
// There needs to be a quotation mark on the right side of the equal than sign in order for the compiler to know it is a string
System.out.println(“ c = “ + c);
// Assuming that each letter needs to be on a separate line, “println” instead of “print” needs to be used
System.out.println(“ f = “ + f);
// There needs to be a command to add the value of “g” in the string
System.out.println(“ g = “ + g);
System.out.print(“ h =” + h);
}
What is a program class?
Program Class: all of the code is inside (wrap it in a pair of curly brackets)
The name of the class should match the name of the Java file

What is the main method?
Main Method: point where java program starts execution

What is increment and decrement?
Common in programming to want to add one or subtract one to a variable
Instead of having to write:
counter = counter + 1;
//We can write, counter++; As a shortcut!
Example:
int x = 0;
//Add one
x++; > 1
// Subtract one x–; > 0
What are shortcuts?

What are packages?
Packages are used to group code into a folder for easy use
What is Scanner class?
A calss within java.util. It contains code specifically designed to help with user input
What is variable.nextLine()?
Allows user to input String values
What is variable.nextInt()?
Allows users to input int values
What is variable.nextDouble()?
Allows users to input double
What is the input skeleton for the scanner class?
- java.util: package
- Used to group code into a folder for easy use
- Scanner: class within java.until
- Scanner input = new Scanner(System.in): Initializes scanner

What is casting?
- Casting is turning something of one type into another type!
- Add the type you want in between the parentheses to cast to that type
int x = (int)5.3 > 5
// Add the type you want in between the parentheses to cast to that type
How do you cast a double to an int?

How do you cast an int to a double?

How does rounding when casting work?
Casting can also be used to round double values to the closest integer using (int)(x + 0.5)
- x will then be closest to the nearest integer
- ints round down
- If the decimal is 0.5 or greater, this will round up
- If the decimal is less than 0.5, it will round down
double x = 2.0;
int y = (int)(x+0.5) > 2
double x = 2.8;
int y = (int)(x+0.5) > 3
Casting can also be used to round negative double value to the closest integer using (int)(x - 0.5):
double x = -2.0;
int y = (int)(x-0.5); > -2
// -2.5
double x = -2.8;
int y = (int)(x-0.5); > -3
// -3.3
What are integer limits?
Integer.MIN_Value = -2,147,483,648
Integer.MIAX_Value = 2,147,483,648
int min = Integer.MIN_Value int
max = Integer.MAX_Value