Exam 1 Answers
Given double variables n1, n2, and n3 that have been declared and initialized, write a Java expression whose value is the average of the three numbers
(n1 + n2 + n3) / 3
What is the value returned by Math.ceil(-7.11)?
-7
Which file extension name is used for Java bytecode? a. .class b. .java c. .txt d. .source
a. .class
Given the declarations with initial values: int m = 9; int n = 5; String name = "Jack"; What is the value of each of the following expressions? a. 23 / m b. 23 % m c. m + 2.5 * n d. "You" + "know" + name e. ((n * 2) < m) || (m > n)
a. 2 b. 5 c. 21.5 d. "You know Jack" e. true
What will be the value of z after the following statements have been executed? int x = 4, y = 33; double z; z = (double) (y / x); a. 8.0 b. 9.0 c. 0 d. 8.25
a. 8.0
The following data 72 'A' "Hello CPS 180" 3.1416 are all examples of a. literals b. variables c. strings d. none of the above
a. literals
Define a constant called PI that holds the value of 3.1415926
final double PI = 3.1415926
What is the value range for a Java int data type? a. from -2^16 to 2^16 b. from -2^16 - 1 to 2^16 c. from -2^16 to 2^16 - 1 d. from -2^17 to 2^16 e. from -2^31 to 2^31 f. from -2^31 - 1 to 2^31 g. from -2^31 to 2^31 - 1 h. from -2^32 to 2^32
g. from -2^31 to 2^31 - 1
A person's Body Mass Index (BMI) is calculated as: BMI = (703 * weight) / (height^2) Write a complete Java program that asks the user to input name, weight, and height, calculates and displays BMI. Keep two decimal places
import java.util.Scanner; public class BodyMassIndex { public static void main(String[] args) { final int FACTOR = 703; Scanner keyboard = new Scanner(System.in); System.out.print("What is your name? "); String name = keyboard.nextLine(); System.out.print("Enter your weight (pounds) and height (inches): "); double weight = keyboard.nextDouble(); double height = keyboard.nextDouble(); keyboard.close(); double BMI = (FACTOR * weight) / (height * height); System.out.printf(name + ", your body mass index is %.2f\n", BMI); } }
Write a Java boolean expression to test whether a positive integer n is an odd or even number. The expression should evaluate to true if n is an even number, or false for an odd number
n % 2 == 0
Given the double variables price (for full admission price) and discount (a percentage) that have been declared and initialized, write a Java expression corresponding to the price of a discounted admission
price * (1 - discount)
Assume that an integer variable x has been declared and initialized, write a Java expression that evaluates to true when the value of x is in the range of 0 and 100 (both inclusive)
x >= 0 && x <= 100
What is the value of y after execution? int x = 7; int y = 3 * x--;
21
What is the value of y after execution? int x = 7; int y = 3 * ++x;
24
What is the value of x after execution? int x = 4; x *= 7;
28
Suppose an integer x has been declared and initialized. Write one Java statement using ternary operators to display a text of either "even" or "odd" depending on the value of x.
System.out.print(x % 2 == 0? "even": "odd");
Suppose the following statement has been written: double discount = 0.2, price = 12.99; Write one Java printf statement that will produce the following output. [30 characters] T-Shirt [20 characters] 20% off [30 characters] 10.39
System.out.printf("%30s%20.0f%% off%30.2f\n", "T-Shirt", discount * 100, price * (1 - discount));
Suppose an integer number n has been declared and initialized to hold a value between 0 (inclusive) and 999 (inclusive). Write Java statements to find the digits of such number. int d1 = a. int d2 = b. int d3 = c.
a. n / 100 b. (n / 10) % 10 c. n % 10
Which extension is used for Java source code files? a. .class b. .java c. .txt d. .source
b. .java
RAM is usually a. an input/output device b. a volatile type of memory, used only for temporary storage c. secondary storage d. a static type of memory, used for permanent storage
b. a volatile type of memory, used only for temporary storage
Internally, the central processing unit (CPU) consists of two parts: a. the input and output devices b. the control unit and the arithmetic and logic unit c. RAM and ROM d. the arithmetic and logic unit (ALU) and main memory
b. the control unit and the arithmetic and logic unit
List at least 10 Java keywords
byte, char, short, int, long, float, double, boolean, true, false, final, public, private, protected, package, import, abstract, static, new, null, if, switch, case, default, for, while, do, break, continue, return, void
Which of the following is used to indicate the end of a Java statement? a. . b. , c. ; d. : e. // f. nothing is used
c. ;
Which of the following is not a valid identifier in Java? a. CPS180 b. $CMU c. CMU&You d. _CMU
c. CMU&You
Which of the following expression evaluates the value for the assertion 'John is at least 3 years older than Mary'? a. MaryAge == JohnAge - 3 b. (JohnAge > MaryAge) && (MaryAge + 3 > JohnAge) c. JohnAge >= MaryAge + 3 d. MaryAge + 3 < JohnAge
c. JohnAge >= MaryAge + 3
Variables are a. symbolic names made up by the programmer and once created, their values cannot be changed b. operators that perform operations on one or more operands c. a named storage location in memory d. java keywords
c. a named storage location in memory
A high-level computer programming language a. is directly executed by the computer CPU b. must run on the Eclipse environment c. must be translated into a lower-level language before it can be executed d. describes the memory structure of the computer
c. must be translated into a lower-level language before it can be executed
Character literals are enclosed in __; string literals are enclosed in__ a. single quotes; single quotes b. double quotes; double quotes c. single quotes; double quotes d. double quotes; single quotes
c. single quotes; double quotes
Select all invalid comments in a Java program a. // comments b. /* comments */ c. /** comments */ d. # comments e. % comments f. {comments} g. ;comments
d. # comments e. % comments f. {comments} g. ;comments
What is the result of the following statement? 25/4 + 4 * 10 % 3 a. 10 b. 7.25 c. 10.25 d. 7
d. 7
What will be the value of z after the following statements have been executed? int x = 4, y = 33; double z; z = (double) y / x; a. 8.0 b. 9.0 c. 0 d. 8.25
d. 8.25