Chapter 2: Variables and Assignments
Which expression fails to compute the area of a triangle having base b and height h (area is one-half base time height)? (1.0 / 2.0 ) * b * h (1 / 2) * b * h (b * h) / 2.0 0.5 * b * h
(1 / 2) * b * h
Which expression evaluates to 3.5? int x = 7; int y = 1; x * (1 / 2) (x / 2.0) x.0 / 2 (x / 2) * 1.0
(x / 2.0)
What is the ending value of y? int x; int y; x = 6; y = (1 / 2) * (x + 5); 0 3 5 6
0
What possible values can x % 10 evaluate to? (x is an integer). 0..9 1..10 0..10 1..11
0..9
What is the ending value of y? int x; int y; x = 2; y = ((x + 1) / 5) + (x / 2) ; 1 1.6 4.5 Error: Division not possible
1
Which is not a valid identifier? firstName first_name 1stName name1
1stName
What is the ending value of x? x = 160 / 20 / 4; 2 16 32 No value; runtime error
2
What is the ending value of z? int x = 5; int y = 12; double z; z = (double)(y / x); 0.0 2.0 2.4 3.0
2.0
What is the value of x? int y = 9; int z = 3; double x; x = (y / (z + 1.0)); 2 2.25 3 Error: The operands are different types and cannot be divided
2.25
Which expression is evaluated first? w = y + 2 + 3 * x + z; y + 2 2 + 3 3 * x x + z
3 * x
What is the ending value of cost int numApples = 3; double cost; cost = 1.2 * numApples; 3 3.2 3.6 4
3.6
What is the ending value of w? int w; int y; y = 34; w = y % 5; 1 4 6 7
4
What is the ending value of x? int y = 6; x = (1 / 2) * y + 8; 7 8 11 14
8
What is the ending value of z? x = 0; y = 3; z = Math.pow(x + 2, y); 0.0 4.0 8.0 Error: Cannot have an expression within a method call
8.0
What is the ending value of y? Math.sqrt(u) returns the square root of u. Math.pow(u, v) returns u raised to the power of v. x = 9.0; y = 4.0; y = Math.pow(Math.sqrt(x), Math.sqrt(y)); 8.0 9.0 6561.0 Error: The sqrt methods cannot appear in the call to the pow method
9.0
int x is 1000000 and int y is 2000000. Outputting x * y will result in overflow. What is likely to appear at the output? -The error message "overflow" -The integer value -1 -A floating-point value -A seemingly arbitrary integer
A seemingly arbitrary integer
What is myChar assigned with?char myChar;myChar = '\''; A backslash: \ A double-quote: " A single-quote: ' A backslash and double-quote: \"
A single-quote: '
What is the ending value of y? Math.pow(u, v) returns u raised to the power of v. x = 2.0; y = 3.0; y = Math.pow(Math.pow(x, y)) + 1.0; 8.0 9.0 65.0 Error: The compiler complains about calling pow without the correct number of arguments
Error: The compiler complains about calling pow without the correct number of arguments
For which quantity is a double the best type? People in a household Boxes on a conveyor belt Planes above a city Height of a building
Height of a building
What is read into string myString for input "One-hit wonder".myString = scnr.next(); One One- One-hit One-hit wonder
One-hit
Which is best declared as a constant? The number of people in a car The number of feet in a yard The number of leaves on a tree The number of insects in a room
The number of feet in a yard
Given only int variables a and b, which is a valid expression? 4a + 3 a + ab a + -2 2 x 3
a + -2
A character variable's value is stored in memory as _____ . a graphical symbol a string a floating-point value an integer value
an integer value
Which declaration assigns a variable with 5 such that the value can not be later changed? final int = 5; int numItems = 5; int NUM_ITEMS = 5; final int NUM_ITEMS = 5;
final int NUM_ITEMS = 5;
Identify the correct syntax for importing a user-defined class Guitar of the package Strings. import Strings:Guitar; import Strings.Guitar; import Guitar.Strings; import Guitar:Strings;
import Strings.Guitar;
The world population is over 7 billion. Which declaration uses the fewest bits while guaranteeing that worldPopulation can be assigned the value 7 billion without error? -byte worldPopulation; -short worldPopulation; -int worldPopulation; -long worldPopulation;
long worldPopulation;
myChar is a character variable and is the only declared variable. Which assignment is valid? myChar = 't'; myChar = "t"; myChar = t; myChar = 'tx';
myChar = 't';
myString is declared as a string and is the only variable declared. Which is a valid assignment? myString = 'Hello'; myString = Hey; myString = "H"; myString = [Hoy];
myString = "H";
Which reads an entire line of text into string myString? myString = scnr.next(); scnr.next(myString); myString = scnr.nextLine(); scnr.nextLine(myString);
myString = scnr.nextLine();
Given Scanner's method summary below, which XXX completes the code to check for and read in doubles from the file, "myScores.txt"? public class TestScores { public static void main(String[] args) { Scanner scnr = null; scnr = new Scanner(new File("myScores.txt")); while (XXX) { double inDouble = scnr.nextDouble(); ... } } } hasNextDouble() scnr.hasNextDouble() scnr.hasNext() hasNextLine()
scnr.hasNextDouble()
An identifier can _____ . be a reserved word start with an underscore contain a period contain spaces
start with an underscore
Which expression doubles x? x += 2; x *= 2; x *= x; x *= x * 2;
x *= 2;
For an integer constant x, which statement will yield a compiler error? w = x + 1; x = y + 1; y = x; z = x + x;
x = y + 1;
Given char x and char z, which assigns x with the character value held in z? x = 'z'; x = "z"; x = z; 'x' = 'z';
x = z;