Unit 1: Primitive Types

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/49

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

50 Terms

1
New cards

What will this code segment output?

System.out.println("Hello");

System.out.println("World");

Hello

World

2
New cards

Which code segment will print "Hello Karel" to the screen in Java?

System.out.println("Hello Karel");

3
New cards

What will this code segment output?

public class Printing

{

public static void main(String[] args)

{

System.out.println("*****");

System.out.println("****");

System.out.println("***");

System.out.println("**");

System.out.println("*");

}

}

*****

****

***

**

*

4
New cards

What is the correct syntax for writing the main method in Java?

public static void main(String[] args)

{

}

5
New cards

Which of the following is a proper way to declare and initialize a variable in Java?

int myNumber = 10;

6
New cards

Consider the following code snippet.

public static void main(String args[]){

final int z;

z = 20;

z = 30;

System.out.println(z);

}

What value of z is actually printed?

This code gives an error.

7
New cards

What are the memory values associated with the variables x, y, and z after the code snippet below executes?

int x = 7;

double y = 2.0;

boolean z = false;

x = x + 3;

z = true;

x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true.

8
New cards

Which of the following variable names follows best practices for naming a variable?

numApples

9
New cards

What does the keyword final do?

It prevents variables from being altered.

10
New cards

Which Java Data Type would be the best suited to represent whether or not a student has completed their homework?

boolean

11
New cards

Which Java Data Type would be best suited to represent the amount of money in Barack Obama's bank account.

double

12
New cards

Which Java Data Type would be the best suited to represent the number of days left until the AP Computer Science Exam?

int

13
New cards

What is the result of this expression?

150 % 100

50

14
New cards

Which of the below is NOT a Java arithmetic operator?

#

15
New cards

What will be the output of the following code snippet?

public class Calculator

{

public static void main(String[] args)

{

int first = 7;

int second = 2;

int result = first / second;

System.out.println(result);

}

}

3

16
New cards

What will be the output of the following code snippet and why?

public static void main(String[] args)

{

int num = 2;

int dividend = 5;

dividend /= num;

System.out.println(dividend);

}

The output will be 2 because when two integers are divided in Java, the decimal portion is always truncated.

17
New cards

What will be stored in the variable modulo after running the following code snippet?

public static void main(String[] args)

{

int num = 2;

int modulo = 15;

modulo %= num;

}

1

18
New cards

In Java, the = sign in a statement means

an assignment of the right hand side value to the left hand side variable.

19
New cards

What will be stored in the variable age after this code snippet executes?

public static void main(String[] args)

{

int age = 16;

age++;

age--;

age++;

}

17

20
New cards

Which code snippet below will end with num holding the value 15?

All of the above

21
New cards

What is the correct syntax for creating a Scanner object?

Scanner objectName = new Scanner(System.in);

22
New cards

What is the correct line of code needed to request a whole number from a user?

Assume a Scanner object called input has already been created.

int num = input.nextInt();

23
New cards

What will this java expression evaluate to?

(int) 9.9

9

24
New cards

What is casting in Java?

Casting is turning a value of one type into another type.

25
New cards

What will be the output of the following code snippet?

public class Casting

{

public static void main(String[] args)

{

int total = 100;

int numPeople = 40;

double average;

average = total / (double) numPeople;

System.out.println("Average: " + average);

}

}

Average: 2.5

26
New cards

Which of the following is not a primitive type?

String

27
New cards

Which of the following statements is true about variables?

The memory associated with a variable of a primitive type holds an actual primitive value.

28
New cards

What is the value of x after this code runs?

int x = 5;

x = 10;

x = 4;

4

29
New cards

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

double temperature = 70.4;

30
New cards

What is the result of this expression?

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

6

31
New cards

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

x % 10

32
New cards

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

int myInteger = (int) 5.6;

5

33
New cards

What output will be produced by

System.out.println("Hello");

System.out.println("Karel");

Hello

Karel

34
New cards

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?

double myDouble = (double) 1/4;

35
New cards

What is the result of this expression?

11

36
New cards

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?

int hours;

double salary;

37
New cards

What will the following code print?

int n = 5;

n ++;

n ++;

n += n;

System.out.println(n);

14

38
New cards

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 and II Only

39
New cards

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

double y = 1.0 * a / b;

40
New cards

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

y /= y;

41
New cards

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 becausex / y calculates to 7.66 then the cast to an int results in the value getting truncated to 7

42
New cards

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

43
New cards

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

44
New cards

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

45
New cards

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

46
New cards

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

II only

47
New cards

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

48
New cards

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

x % 6

5

49
New cards

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

50
New cards

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

true