Computer Science Unit 1 Test
Consider the following code segment. double d = 0.25; int i = 3; double diff = d - i; System.out.print((int)diff - 0.5); What is printed as a result of executing the code segment?
-2.5
int x = 9; x++
10
to find the ones digit of a number, you mod by
10
to tell if a number is even or odd, you mod by
2
Consider the code segment below. int x = 10; int y = 20; System.out.print(y + x / y); What is printed as a result of executing the code segment?
20
int x = 3; x *= 7
21
int x = 9; int y = 2; System.out.print (x/y) = ?
4
int x = 9; int y = 2; double z = 1.0 * x / y; System.out.println(z) = ?
4.5
int x = 4; x += 2
6
int x = 9; x++; //x is now 10// x--
9
____________ conversion is moving a data type to a smaller box
Narrowing
_________ conversion is moving a data type to a larger box
Widening
If we want to round a positive double to the nearest integer we can _______ before casting to an int.
add 0.5
The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to π (pi) times the square of the radius. The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized. /* missing code */ System.out.print(volume); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? I. double baseArea = pi * r * r; double volume = baseArea * h; II. double volume = pi * r * r; volume = volume * h; III. double volume = pi * r * r * h;
all
Consider the following code segment, which is intended to display 6.0. double fact1 = 1 / 2; double fact2 = 3 * 4; double product = fact1 * fact2; System.out.println(product); Which of the following best describes the error, if any, in the code segment? A) there are no errors and the code works as intended. B) either the numerator or the denominator of the fraction 1 / 2 should be cast as double. C) the expression fact1 * fact 2 should be cast as double. D) the expressions 1 / 2 and 3 * 4 should both be cast as double. E) the variables fact1 and fact2 should both be declared as int.
b
Data types in order from smallest to largest
boolean, int, double, string
Consider the following code segment. int x = 4; int y = 6; x -= y; y += x; Which of the following best describes the behavior of the code segment? (Have x and y increased or decreased)?
both decreased
Compiling is when the computer
checks your program for errors before running it
Primitive data types are stored
directly in a variable
What data type to use when computing averages
double
When naming variable, we can start with a number (T/F)?
f
When naming variable, we can use names that already have a meaning in java (like "string") (T/F)?
f
When naming variables, we can use spaces (T/F)?
f
String only interpret what is in
quotations
Modular division is also known as ______ division
remainder
When taking in input, you need to include
scan
When Java converts a double to an int it
truncates
You cannot print a string without setting its
value
Strings can contain
words, symbols, letters, and digits
Modular division still causes an error if we try to divide by
0
When using division calculations, you need to avoid dividing by
0
int 2/5 =?
0.4
What is domain?
Domain is what possible values can be stored
If we don't explicitly cast when moving from a larger data type to a smaller one, we'll get an error telling us there's a
Possible Lossy Conversion
Reference data types store
a memory reference to where the data is located
A 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? A) The code segment works as intended for all nonnegative values of len1, len2, and len3. B) 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. C) 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. D) 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. E) 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.
b
What four things tell a computer what to do
code commands instructions and programs
What happens if you input a number larger than 2147483647?
it rolls over and becomes a massive negative number
When we declare a variable, we tell the computer to allocate
some space in memory for something
final values make the number
stay the same
Java recognizes anything within quotation marks as a
string
If we wanted to round a negative number we would have to _________ rather than adding it.
subtract 0.5
Primitive data types only hold one piece of data at a time (T/F)?
t
We lose numeric casts in Java since there is a chance that you lose data when you convert from a larger data type to a smaller data type (T/F?)
t
When naming variables, we can use letter/number characters (T/F)?
t
When naming variables, we can use the special characters $ and _ (T/F)?
t
Overflow is when
there arent any bits to store the number
int x = 9; int y = 2; double z = x /y ; System.out.println(z) = ?
4
Variables are
containers of memory
Which of the following arithmetic expressions evaluates to 1 ? I. 2 / 5 % 3 II. 2 / (5 % 3) III. c) 2 / 5 + 1
2 and 3
Consider the following code segment. 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;
2 only
What is the largest possible integer value Java can display
2147483647
Consider the following code segment. int a = 1; int b = 2; int c = 3; int d = 4; double x = a + b * c % d; What is the value of x when the code segment has been executed?
3
Consider the following code segment. int x = 0; x++; x += 1; x = x + 1; x -= -1; System.out.println(x); What is printed when the code segment has been executed?
4
Consider the following code segment, which is intended to display 0.5. int num1 = 5; int num2 = 10; double ans = num1 / num2; System.out.print(ans); Which of the following best describes the error, if any, in the code segment? a) There is no error and the code works as intended. b) The code should have cast the expression num1 / num2 to double. c) The code should have cast either num1 or num2 in the expression num1 / num2 to double. d) The code should have declared ans as an int. e) The code should have initialized num1 to 5.0 and num2 to 10.0.
c