Module 01: Primitive Types Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Which of the following is not a primitive type?

  1. int
  2. double
  3. String
  4. boolean
  5. char
A
  1. String
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following statements is true about variables?

  1. The memory associated with a variable of a primitive type holds an actual primitive value.
  2. When a variable is declared final, its value can only be changed through a direct reassignment.
  3. A variable originally created as an int can be changed to store a double through casting.
  4. All of these choices are true
A
  1. The memory associated with a variable of a primitive type holds an actual primitive value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the value of x after this code runs?

int x = 5;

x = 10;

x = 4;

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the proper syntax to declare and initialize a variable called temperature to have the value 70.4?

  1. int temperature = 70.4;
  2. double temperature = 70.4;
  3. temperature = 70.4;
  4. dbl temperature = 70.4;
  5. temperature = (double) 70.4
A
  1. double temperature = 70.4;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the result of this expression?

(int) (5 + 2 / 3 + 1)

A

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which expression returns the 1’s place of an integer x?

  1. x % 10
  2. x / 10
  3. x % 100
  4. x + 1
A
  1. x % 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the value of myInteger after this line of code is executed?

int myInteger = (int) 5.6;

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What output will be produced by

System.out.println(“Hello”); System.out.println(“Karel”);

A

Hello

Karel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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?

  1. int myDouble = 1/4;
  2. double myDouble = (double) 1/4;
  3. double myDouble = (int) 1/4;
  4. double myDouble = (int) (1.0/4.0);
A
  1. double myDouble = (double) 1/4;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the result of this expression?

4 + 8 * 3 / 4 + 5 % 2

A

11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

(2) int hours
double salary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will the following code print?

  • int n = 5;*
  • n ++;*
  • n ++;*
  • n += n;*
  • System.out.println(n);*
A

14

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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;

  1. I Only
  2. II Only
  3. III Only
  4. I and II Only
  5. I, II, and III
A
  1. I and II Only
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given a and b as properly initialized integers, which of the following will result in a correct calculation with a decimal answer?

  1. double y = (double) (a / b);
  2. double y = a / b * 1.0;
  3. double y = a / b;
  4. double y = 1.0 * a / b;
A
  1. double y = 1.0 * a / b;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Assume y is a properly initialized positive integer. Which of the following will always result in a value of 1?

  1. y ++;
  2. y –;
  3. y += y;
  4. y /= y;
  5. y -= y;
A
  1. y /= y;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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?

  1. 7 because the values of x and y are integers so 23 / 3 evaluated to 7
  2. 7 because x / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7
  3. 8 because the values of x and y are integers so 23 / 3 evaluated to 8
  4. 8 because x / y calculates to 7.66 then the cast to an int results in the value getting rounded up to 8
A
  1. 7 because x / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

A

x + y = 12

18
Q

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!”);

  1. I and II only
  2. I, II, and III only
  3. I and III only
  4. I, II, and IV only
  5. I, II, III, and IV
A
  1. I and II only
19
Q

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.”);

A

3

20
Q

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;

A

4

21
Q

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

  1. I only
  2. II only
  3. III Only
  4. II and III only
  5. I, II, III
A
  1. II only
22
Q

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?

  1. int rcGrade = (int) gradeAverage;
  2. int rcGrade = gradeAverage % 0.5;
  3. int rcGrade = (int) gradeAverage + 0.5;
  4. int rcGrade = (int) (gradeAverage + 0.5);
  5. int rcGrade = (int) gradeAverage - 0.5;
A
  1. int rcGrade = (int) (gradeAverage + 0.5);
23
Q

What is the result of the following expression when x is 125?
x % 6

A

5

24
Q

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

A

15.0

25
Q

Which of the following values can correctly be saved in a boolean?

True

Yes

1

true

yes

A

true

26
Q

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.

A

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

}

27
Q

What is a program class?

A

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

28
Q

What is the main method?

A

Main Method: point where java program starts execution

29
Q

What is increment and decrement?

A

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

30
Q

What are shortcuts?

A
31
Q

What are packages?

A

Packages are used to group code into a folder for easy use

32
Q

What is Scanner class?

A

A calss within java.util. It contains code specifically designed to help with user input

33
Q

What is variable.nextLine()?

A

Allows user to input String values

34
Q

What is variable.nextInt()?

A

Allows users to input int values

35
Q

What is variable.nextDouble()?

A

Allows users to input double

36
Q

What is the input skeleton for the scanner class?

A
  • 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
37
Q

What is casting?

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

38
Q

How do you cast a double to an int?

A
39
Q

How do you cast an int to a double?

A
40
Q

How does rounding when casting work?

A

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

41
Q

What are integer limits?

A

Integer.MIN_Value = -2,147,483,648

Integer.MIAX_Value = 2,147,483,648

int min = Integer.MIN_Value int

max = Integer.MAX_Value

42
Q
A