Chapter 5. Data Types, Variables, and Arithmetic
Math.random() returns a double value 0 ≤ x < 1. Which of the following expressions assigns to the variable hour a random integer from 1 to 12? (I only gave one possible answer.)
int hour = (int)(Math.random() * 11.0 + 1.0)
Fill in the blanks in the following program: public class Cost { public static void main(String[] args) { ___; ___; ___; System.out.println(qty + " widgets @ " + dollar + price + " = " + dollar + qty * price; } } It should display 6 widgets @ $2.12 = $12.72
int qty = 6; String dollar = "$"; double price = 2.12;
Find and fix the two syntax errors in the following class: public class RightTriangle { private double aSide; bSide; < Constructors not shown > public double getAltitude() { private double hypotenuse; hypotenuse = Math.sqrt(aSide * aSide + bSide * bSide); return aSide * bSide / hypotenuse; } } In my answer, I rewrote the lines with syntax errors.
private double aSide, bSide; double hypotenuse;
Write a toString method for the class Movie (in the question) so that System.out.println(new Movie("The Terminator", 108)); displays The Terminator, 108 mins (Update 10:01pm: it is correct)
public String toString() { return title + ", " + mins + " mins"; }
Look at the program in #24. What is its actual output? Find and fix the bug.
Actual output: 0.0 Bug is on line 7: should say: side = a; not double side = a;
What is the result when the following statement is compiled and executed? System.out.println("1" + 2 + 3);
"123" (a String)
The following statement is an attempt to calculate and print how many bits will be transmitted over a one-gigabit/second transmission channel in a year: System.out.println(365 * 24 * 3600 * 1024 * 1024 * 1024); What is the result?
0
Given int sum = 3; what is the value of sum after the following statements are executed? sum *= 2; sum /= 5; sum++;
2
Given int n = 2, b = 3; double x = 2.5, y; what is the result from the following statement? y = (x * n + b / n) / 2;
3.0
Given int n = 12, m = 4; what is displayed when the following statement is executed? System.out.println(m % (n + 1) + n % (m + 1));
6
How many primitive data types are there in Java?
8
What style of naming fields is more common in Java?
Camel casing with a lowercase first letter
(T/F) 'String' is a valid String constant.
F
(T/F) '\"' is a valid String constant.
F
(T/F) 5,000.00 is a valid numeric constant.
F
(T/F) A local variable may be initialized in one method or constructor and used in another method.
F
(T/F) A numeric literal constant is always treated as a value of the double type.
F
(T/F) Declaration of fields must be placed at the top of the class, before all the constructors and methods.
F
(T/F) In Java expressions, all operations with int operands are performed before operations with double operands.
F
(T/F) In expressions without parentheses, multiplication is performed before division.
F
(T/F) Object is a primitive data type.
F
(T/F) String is a primitive data type.
F
(T/F) System is a primitive data type.
F
(T/F) When an (int) cast is applied to a double value, it rounds the value to the nearest integer.
F
(T/F) When an integer value is assigned to a double variable, the variable's type changes to int.
F
(T/F) double and float are two different names for the same Java data type.
F
(T/F) null is a primitive data type.
F
What is a valid reason for using a symbolic constant final double CM_PER_INCH = 2.54 for the conversion factor rather than the literal constant 2.54?
It makes code cleaner and more readable.
What is the scope of a variable?
Its accessibility to other parts of the program
What determines the data type of the results of an arithmetic operator +, -, *, or /?
Most inclusive operand type (int + double = double)
(T/F) "" is a valid String constant.
T
(T/F) "$" is a valid String constant.
T
(T/F) "\\" is a valid String constant.
T
(T/F) "\n" is a valid String constant.
T
(T/F) -.5 is a valid numeric constant.
T
(T/F) .50 is a valid numeric constant.
T
(T/F) 0.5 is a valid numeric constant.
T
(T/F) 5.0 is a valid numeric constant.
T
(T/F) A boolean variable can only have values true or false.
T
(T/F) A cast operator, when applied to an arithmetic expression in parentheses, converts every constant and variable in the expression into the specified type.
T
(T/F) A compound assignment operator combines an arithmetic operator with assignment.
T
(T/F) A field in a class may be public or private, but usually all fields are declared private.
T
(T/F) A local variable is neither public nor private.
T
(T/F) All fields, when not explicitly initialized, get default values.
T
(T/F) All local variables, when not explicitly initialized, get default values.
T
(T/F) Declarations of local variables may be placed anywhere in a constructor's or method's code, not necessarily at the top, but before their first use.
T
(T/F) If a field is an object, its default value is null.
T
(T/F) Literal strings can include \n and \t "escape" characters.
T
(T/F) Several variables of the same type may be declared in the same statement.
T
(T/F) The % operator has the same rank as the / operator.
T
(T/F) The value of a final field, once assigned, cannot change.
T
(T/F) Variables that represent objects may be initialized using the new operator.
T
(T/F) boolean is a primitive data type.
T
(T/F) char variables in Java hold characters in Unicode encoding, with two bytes per character.
T
(T/F) int is a primitive data type.
T
Fill in the blanks in the class Metrics below. The instructions are on the test because I'm too lazy to type them all out. public class Metrics { ___ LB_TO_GRAMS = ___; public static int weightToMetrics(int lbs, int ounces) { return ___; } } (There are multiple ways of doing this solution. I presented one way in my answer.)
public static final double LB_TO_GRAMS = 453.592; return (int)Math.round((double)lbs + (double)ounces / 16.0) * LB_TO_GRAMS);