WorksheetsPrimitive Types in Java Programming -Unit 1- AP CSA
Total questions: 64
Worksheet time: 1hrs 1mins
Vincent is organizing his desk and wants to label a box to store his pens. In programming, what is a variable similar to?
A variable is a named storage location that holds a value.
A variable is a function in programming.
A variable is a type of loop in programming.
A variable is a mathematical equation.
Jacob and Gabriel are learning Java. Which of the following options are primitive data types in Java?
integer, boolean, double, String
integer, boolean, double
integer, boolean, double, String, Scanner
integer, boolean, double, String, Object
Eric is learning Java and wants to declare a variable to store his age. What is the correct syntax for declaring a variable in Java?
var myVar;
myVariable = int;
int myVariable;
declare myVariable;
Jeremy is writing a program to calculate the average score of his class. He needs to store whole numbers for the number of students and decimal numbers for the average score. What data types should he use for these variables?
int is used for storing characters while double is used for storing strings.
int is used for whole numbers while double is used for decimal numbers.
int is used for decimal numbers while double is used for whole numbers.
int is used for positive numbers while double is used for negative numbers.
Eric is writing a program to manage his book collection. What is the purpose of a variable in his program?
To store and manipulate data in a program.
To execute functions in a program.
To display data in a program.
To control the flow of a program.
During a programming class, Brennan and Om were discussing a code segment.
System.out.println("W");
System.out.println("X");
System.out.print("Y");
System.out.print("Z");
What is printed as a result of executing the code segment?
WXYZ
W
XYZ
WX
YZ
W
X
YZ
W
X
Y
Z
Richard and Eric are working on a project that involves calculating ratios.
int x = 10;
int y = 20;
/* missing code */
System.out.print(top / bottom);
Which of the following replacements for /* missing code */ will cause an ArithmeticException to occur?
I. int top = x - y;int bottom = y - x;
II.int top = 2 * x;int bottom = y - top;
III. int top = x + y;int bottom = 2 * top;
I. only
II. only
III. only
I. and II.
II. and III.
Joshua is creating a personalized message for his computer science club.
String message = "AP";
message += "CS " + 1 + 2;
System.out.println(message);
What is printed as a result of executing the code segment?
CS AP12
AP CS3
CSAP 12
APCS 12
APCS 3
Jacob has 4 apples. If he gets one more apple, how many apples does he have now?
16
8
5
3
Vincent and Richard are sharing 25 candies equally. In integer division, what happens to the decimal, such as int candiesPerPerson = 25/10, where the answer would be 2.5?
the decimal is dropped
the number is rouneded
the program gives you an error
only the decimal is returned
During a Java programming class, Jacob was asked to identify a String literal. Which of the following is a String literal in Java?
"Hello World!"
'Hello World'
// Hello World
String str = "Hello World";
Jeremy is calculating the cost of filling up his car's tank. Identify the most appropriate data type category for the price of one gallon of gasoline.
double
int
String
boolean
Om is counting the number of students in his school. Identify the most appropriate data type category for this number.
double
int
String
boolean
Om and Jeremy are developing a mobile app. Identify the most appropriate data type category to record when a user is logged in.
double
int
String
boolean
Gabriel and Jacob are sharing 5 apples. Which of the following expressions will result in an integer number of apples each will get? (choose all that apply)
5 / 2
5.0 / 2
(int) 5 / 2.0
5 / 2.0
Jeremy and Joshua are dividing a cake. Which of the following expressions will result in a double data type when calculating the portions? (choose all that apply)
(double) 5 / 2
5.0 / 2
5 / 2.0
5 / 2
Areen has 7 apples.
Which of the following operations will leave Areen with 2 apples? (choose all that apply)
Areen divides the apples into 3 equal parts;
Areen keeps the remainder when dividing the apples by 5;
Areen adds 6 more apples and then divides the total by 5;
Areen adds 6 more apples and then divides 6 by 5 and adds to the total;
Eric has 20 apples.
He wants to distribute them equally among his friends, but he mistakenly tries to divide by zero.
Which of the following expressions will cause an ArithmeticException? (choose all that apply)
Eric's apples /= 0;
Eric's apples = 20 / 0;
Eric's apples -= 0;
Gabriel has 0 apples.
Which of the following actions will increase the number of apples Gabriel has by 1? (choose all that apply)
Gabriel picks an apple from the tree.
Gabriel receives an apple from Richard.
Gabriel gives an apple to Eric.
Gabriel finds an apple on the ground.
Jacob has $2.75 and wants to round it to the nearest dollar.
Which of the following expressions will round the amount to the nearest integer?
(int) x + 0.5
(int) (x + 0.5)
(int) x
x = (int) x - 1
Jeremy has a balance of -4.25 in his bank account.
Which of the following expressions will round the balance to the nearest integer?
(int) x - 0.5
(int) (x - 0.5)
(int) x
x = (int) x - 1
Victor and Brennan are discussing integer overflow in Java. Which of the following statements are false? (Select all that apply)
Integer.MAX_VALUE + 1
has the same value as
Integer.MIN_VALUE
Integer.MIN_VALUE + 1
has the same value as
Integer.MAX_VALUE
Integer.MIN_VALUE - 1
has the same value as
Integer.MAX_VALUE
Integer.MAX_VALUE + 1
is 0
Consider the following code, assuming grade is an int:
Which of the following will always produce the same result as the code above, regardless of the value of grade? (multiple correct answers)
Om and Eric are solving a math problem. If Om adds 5 to a number and then multiplies the result by 2, he gets 22. What was the original number?
11
31
15
error
Eric and Jacob are writing a program to simulate a random event. Consider the following incomplete code segment.
Consider the following potential replacements for /* missing code */ . Which of these replacements will cause
the code segment to always print false? Assume that Math is imported correctly into their program.
(int)(Math.random() * 10) + 20
(int)(Math.random() * 10) + 25
(int)(Math.random() * 10) + 30
(int)(Math.random() + 30)
Jeremy and Justin are comparing their favorite quotes. Which condition should be used to test whether the contents of two strings referenced by string1 and string2 have the same value?
(string1.equals(string2))
(String.equals(string1, string2))
(string1 == string2)
(Equals(string1, string2))
Jeremy and Joshua are comparing their project files. Which operator or method tests whether two object references are identical?
==
equals
=
identity
A weather app records the current temperature to the nearest degree Fahrenheit using an int variable temp.
The app also uses a String variable tempAdj to store an adjective based on the value of temp which will be used
in a short written summary of the current weather. The values of tempAdj are determined as follows:
Which of the following code segments will correctly set the value of tempAdj for any given value of temp? (Multiple correct answers)
Assume that Brennan and Gabriel are working on a project together and have been properly assigned tasks.
What will be the output if Brennan completes his task and Gabriel does not:
One
Two
Three
OneTwo
A
B
C
D
Nothing is printed
biggest always returns the value of a.
biggest may not work correctly when c has the greatest value.
biggest may not work correctly when a and b have equal values.
biggest may not work correctly when a and c have equal values.
biggest may not work correctly when b and c have equal values.
Eric has 12 apples and gives away 3. How many apples does he have left?
9
10
11
12
Eric and Vincent are working on a Java project. Why do they use methods in their code?
To make code easier to understand
To avoid repeated code
To simplify code
All of the above
Jeremy has 10 apples and Om has 20 apples.
They decide to put their apples together.
System.out.println("" + Jeremy + Om);
Guess the output.
1020
10 20
30
"10 20"
Vincent is writing a program to find the position of a specific word in a sentence. Which method should he use to return the index (the position) of the first occurrence of a specified text in a string (including whitespace)?
returns the index (the position) of the last occurrence of a specified text in a string
returns the character of the first occurrence of a specified character in a string
returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)
returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)
Jeremy is writing a program and he has the following code:
String txt = "Hello World";
System.out.println(txt.toUpperCase());
Choose the correct output.
"HELLO WORLD"
HELLO WORLD
Hello world
Hello World
Jacob is writing a program to count the number of characters in a string. Which data type gets returned from the length() method in the String class?
double
String
int
number
During a computer science class, Om asked if String is a primitive data type. What is the correct answer?
Yes
No
Both
Eric is measuring the length of a table and notes down the measurement as 1.0. Name the data type of this measurement.
double
int
char
boolean
Vincent is programming a simple game. He needs to use a data type that returns true or false to check if a player has won. Which data type should he use?
String
boolean
char
int
Gabriel is working on a Java project and needs to convert a smaller data type to a larger data type. What is this process called in Java?
Widening primitive
Narrowing primitive
Victor and Eric are dividing candies among themselves. Which operator should they use in Java to find out how many candies will be left after evenly distributing them?
/
%
+
*
In a Java class, Om is writing a program and uses the (=) sign. What does it mean?
equals and it is used for math problems in Java
assignment operator. It used to assign a value
Gabriel is writing a Java program. Which one is the correct method signature for the main method?
public static main(Strings[] args) { }
public private static main(Strings[] args) { }
public static void main(Strings[] args) { }
public void static main(Strings[] args) { }
Joshua has 5 apples,
Victor has 6 apples,
and Areen has 50 apples.
How many apples do they have in total?
61
5650
66
5 + 6 + 50
Gabriel is measuring the length of a table and records the measurement as a float value:
float tableLength = 5.99f;
System.out.println(tableLength);
What will be the output?
5.99
5.99f
"5.99"
"5.99"f
Richard wants to store his friend's name in a variable. What is the right way of defining a String variable?
String name = "John";
String name = "John"
String name = John;
String name "John";
During a Java programming class, Tiron asked, "Which of the following is NOT a data type in Java?"
int
String
number
double
During a group project, Eric and Victor were discussing tools for software development. Eric asked, 'What does IDE mean?'
Integration Development Environment
Integration Device Environment
If Gabriel has 10 apples, what does Vincent have if Vincent has 15/4 of Gabriel's apples?
1
5
0
3
Vincent and Richard are developing a game where they need to track whether or not the player's score is 0. What variable type can they use?
int
double
String
boolelan
Om and Tiron are sharing candies. If they divide the candies equally, what does % represent in this context?
percentage
negation
modulus, like division but the remainder is the answer
inverse
What is the code for this output?
What is the code for this output?
What will be the output of this code?
What is the code for this output?
What is the code for this output?
Vincent and Jacob are planning a camping trip and need to buy rope for their tents. The store sells rope only in whole-foot increments. Given three lengths of rope, in feet, the following code segment is intended to display the minimum length of rope, in feet, that must be purchased so that the rope is long enough to be cut into the three lengths. For example, for lengths of 2.8 feet, 3 feet, and 5 feet, the minimum length of rope that must be purchased is 11 feet. For these inputs, the code segment should display 11. As another example, for lengths of 1.1 feet, 3.2 feet, and 2 feet, the minimum length of rope that must be purchased is 7 feet. For these inputs, the code segment should display 7.
double len1;
double len2;
double len3;
double total = len1 + len2 + len3;
int minLength = (int) (total + 0.5);
System.out.print(minLength);
Which of the following best describes the behavior of the code segment?
The code segment works as intended for all nonnegative values of len1, len2, and len3.
The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5.
The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by casting len1, len2, and len3 to int before adding them together and assigning the sum to minLength.
The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by declaring minLength as a double and casting it to an int in the System.out.print statement.
The code segment does not work as intended for any values of len1, len2, and len3. It returns the sum of 0.5 and the three lengths for all values of len1, len2, and len3.
Joshua and Jeremy are discussing programming. Joshua asks, 'Can you name one difference between primitives and objects?'
Name one difference between primitives and objects.
What is the result of the following code snippet?
int a = 5;
int b = 10;
System.out.println(a + b);
15
510
5
10
Which of the following is a valid way to declare a constant in Java?
final int MAX = 100;
const int MAX = 100;
int const MAX = 100;
int MAX = 100;
What will be the output of the following code?
boolean flag = true;
if(flag) {
System.out.println("Flag is true");
} else {
System.out.println("Flag is false");
}
Flag is true
Flag is false
No output
Error
Match the following
5
5.2
'a'
"abc"
true
