Java Chapter 3 Test Questions – Flashcards
104 test answers
Unlock all answers in this set
Unlock answers 104question
equal to (as relational operator)
answer
==
Unlock the answer
question
not equal to (as relational operator)
answer
!=
Unlock the answer
question
The _ testing operator is two equal signs (==), not a single equal sign (=). The latter symbol is for _.
answer
equality, assignment
Unlock the answer
question
double radius = 1; System.out.println(radius > 0); this displays:
answer
true
Unlock the answer
question
A variable that holds a Boolean value is known as a
answer
Boolean variable
Unlock the answer
question
A variable that holds a Boolean value is known as a Boolean variable. The boolean _ is used to declare Boolean variables.
answer
data type
Unlock the answer
question
assign true to lightsOn
answer
boolean lightsOn = true;
Unlock the answer
question
True and false are _, just like a number such as 10. They are treated as reserved words and cannot be used as identifiers in the program.
answer
literals
Unlock the answer
question
An _ is a construct that enables a program to specify alternative paths of execution.
answer
if statement
Unlock the answer
question
Java has several types of selection statements: _ if statements, _ if-else statements, _ statements, _ if-else statements, _ statements, and _ expressions.
answer
one-way, two-way, nested if, multi-way, switch, conditional expressions
Unlock the answer
question
A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is:
answer
if (boolean-expression) { statement; }
Unlock the answer
question
The block braces can be omitted if they enclose a _. For example, the following statements are equivalent. if (i > 0) System.out.println("i is positive");
answer
single statement
Unlock the answer
question
(1) If the number is a multiple of 5, the program displays HiFive. If the number is divisible by 2, it displays HiEven. (Just write if statement)
answer
if (number % 5 == 0)
Unlock the answer
question
An _ statement decides the execution path based on whether the condition is true or false.
answer
if-else
Unlock the answer
question
The inner if statement is said to be _ inside the outer if statement.
answer
nested
Unlock the answer
question
style, called _, avoids deep indentation and makes the program easy to read
answer
multi-way if-else statements
Unlock the answer
question
The following errors are common among new programmers. Common Error 1: forgetting _
answer
Forgetting Braces
Unlock the answer
question
Common Error 2: Wrong Semicolon at the
answer
if Line
Unlock the answer
question
if (radius >= 0); same as if (radius >= 0) { } What type of error is this?
answer
empty block
Unlock the answer
question
if (radius >= 0); same as if (radius >= 0) { }. This mistake is hard to find, because it is neither a compile error nor a runtime error; it is a _.
answer
logic error
Unlock the answer
question
Common Error 3: Redundant Testing of
answer
Boolean Values
Unlock the answer
question
if (even == true) is redundant. Better:
answer
if (even)
Unlock the answer
question
if (even = true) is wrong but even so, won't show a _ error. Should be:
answer
compile, ==
Unlock the answer
question
Common Error 4: __ Ambiguity
answer
Dangling else
Unlock the answer
question
Common Error 5: _of Two Floating-Point Values
answer
Equality Test
Unlock the answer
question
double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1; System.out.println(x == 0.5); Here, x is not exactly 0.5, but is 0.5000000000000001. You cannot reliably test equality of two _ values.
answer
floating-point
Unlock the answer
question
you can compare whether they are close enough by testing whether the difference of the two numbers is less than some threshold. That is, two numbers x and y are very close if |x?y| ; e for a very small value, e. e, a Greek letter pronounced _, is commonly used to denote a very small value
answer
epsilon
Unlock the answer
question
Normally, you set e to _for comparing two values of the double type and to _ for float
answer
1E-14, 1E-7
Unlock the answer
question
setup the EPSILON variable for a floating variable
answer
final double EPSILON = 1E-14;
Unlock the answer
question
if statement to check whether 0.5000000000000001 (x) is approximately 0.5
answer
if (Math.abs(x - 0.5) ; EPSILON)
Unlock the answer
question
delete
answer
d
Unlock the answer
question
The _ method can be used to return the absolute value of a
answer
Math.abs(a)
Unlock the answer
question
improve: Often, new programmers write the code that assigns a test condition to a boolean variable like the code in (a): if (number % 2 == 0) even = true; else even = false;
answer
boolean even = number % 2 == 0;
Unlock the answer
question
You can use _ to obtain a random double value between 0.0 and 1.0, excluding 1.0.
answer
Math.random()
Unlock the answer
question
The previous programs generate random numbers using System.currentTimeMillis(). A better approach is to use the random() _ in the Math _.
answer
method, class
Unlock the answer
question
A better approach is to use the random() method in the Math class. Invoking this method returns a random value of the type _.
answer
double
Unlock the answer
question
_ returns a random single-digit integer (i.e., a number between 0 and 9).
answer
(int)(Math.random() * 10)
Unlock the answer
question
// 1. Generate a random single-digit integer, number1 between 1-10
answer
int number1 = (int)(Math.random() * 10);
Unlock the answer
question
// If number1 ; number2, swap number1 with number2. First line in program (ignoring setup parameters)
answer
if (number1 ; number2) {
Unlock the answer
question
// If number1 ; number2, swap number1 with number2 line 2
answer
int temp = number1;
Unlock the answer
question
// If number1 ; number2, swap number1 with number2 line 3
answer
number1 = number2;
Unlock the answer
question
// If number1 ; number2, swap number1 with number2 line 4 (Swap)
answer
number2 = temp;
Unlock the answer
question
// If number1 ; number2, swap number1 with number2 line 5: the closing line
answer
}
Unlock the answer
question
_ (line 53) is defined in the System class. Invoking this method terminates the program. The status 0 indicates that the program is terminated normally. A nonzero status code indicates abnormal termination.
answer
System.exit(status)
Unlock the answer
question
System.exit(status) (line 53) is defined in the System class. Invoking this method terminates the program. The status 0 indicates that the program is terminated _.
answer
normally
Unlock the answer
question
System.exit(status) (line 53) is defined in the System class. Invoking this method terminates the program. A _ status code indicates abnormal termination.
answer
nonzero
Unlock the answer
question
An initial value of 0 is assigned to tax (line 20). A _ error would occur if it had no initial value, because all of the other statements that assign values to tax are within the if statement. The compiler thinks that these statements may not be executed and therefore reports a compile error.
answer
compile
Unlock the answer
question
For all programs, you should write a small amount of code and test it before moving on to add more code. This is called _ development and testing
answer
incremental
Unlock the answer
question
_ operators, also known as Boolean operators, operate on Boolean values to create a new Boolean value.
answer
Logical
Unlock the answer
question
the __ operator, which negates true to false and false to true.
answer
not (!)
Unlock the answer
question
The __ of two Boolean operands is true if and only if both operands are true
answer
and (;;)
Unlock the answer
question
The __ of two Boolean operands is true if at least one of the operands is true
answer
or (||)
Unlock the answer
question
The __ of two Boolean operands is true if and only if the two operands have different Boolean values
answer
exclusive or (^)
Unlock the answer
question
Note that p1 ^ p2 is the same as _
answer
p1 != p2
Unlock the answer
question
!
answer
logical negation
Unlock the answer
question
;;
answer
logical conjunction
Unlock the answer
question
||
answer
logical disjunction
Unlock the answer
question
^
answer
logical exclusion
Unlock the answer
question
Example (assume age = 24, weight = 140) is false, because (age ; 18) is _.
answer
!(age ; 18), true
Unlock the answer
question
_ is true, because (weight == 150) is false.
answer
!(weight == 150)
Unlock the answer
question
(age ; 18) _ (weight ;= 140) is true, because (age ; 18) and (weight ;= 140) are both true
answer
;;
Unlock the answer
question
(age ; 34) _ (weight ;= 150) is false, because (age ; 34) and (weight ;= 150) are both false
answer
||
Unlock the answer
question
(age ; 18) _ (weight 18) is true.
answer
|| (or operator)
Unlock the answer
question
(age ; 34) ^ (weight ; 140) is _, because (age ; 34) and (weight ; 140) are both false.
answer
false
Unlock the answer
question
(age ; 34) ^ (weight ;= 140) is _, because (age ; 34) is false but (weight ;= 140) is true
answer
true
Unlock the answer
question
if (number % 2 == 0 || number % 3 == 0) System.out.println(number + "__"); ;;;
answer
is divisible by 2 or 3
Unlock the answer
question
if (number % 2 == 0 ^ number % 3 == 0) System.out.println(number + "is __");
answer
divisible by 2 or 3 but not both
Unlock the answer
question
1 ;= no ;= 31 should be
answer
(1 ;= no) ;; (no ;= 31)
Unlock the answer
question
In mathematics, the expression 1 ;= numberOfDaysInAMonth ;= 31 is correct. However, it is incorrect in Java, because 1 ;= numberOfDaysInAMonth is evaluated to a __, which cannot be compared with 31.
answer
boolean value
Unlock the answer
question
_'s law, named after Indian-born British mathematician and logician can be used to simplify Boolean expressions. The law states: !(condition1 && condition2) is the same as !condition1 || !condition2 !(condition1 || condition2) is the same as !condition1 && !condition2
answer
De Morgan
Unlock the answer
question
!(number == 2 || number == 3) is better written as
answer
number != 2 && number != 3
Unlock the answer
question
In programming language terminology, && and || are known as the _ or _ operators
answer
short-circuit, lazy
Unlock the answer
question
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. Collect the input for year:
answer
int year = input.nextInt();
Unlock the answer
question
Generate a lottery number (0-100) & call the variable lottery
answer
int lottery = (int)(Math.random() * 100);
Unlock the answer
question
enter a guess of lottery number
answer
int guess = input.nextInt();
Unlock the answer
question
A _ statement executes statements based on the value of a variable or an expression.
answer
switch
Unlock the answer
question
Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; ___; ...
answer
break
Unlock the answer
question
Here is the full syntax for the switch statement: switch (switch-expression) { ___ statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
answer
case value1:
Unlock the answer
question
Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; __ }
answer
default: statement(s)-for-default;
Unlock the answer
question
Here is the full syntax for the switch statement: ____ case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
answer
switch (switch-expression) {
Unlock the answer
question
The value1, . . ., and valueN must have the same __ as the value of the switchexpression. Value1, . . ., & valueN are ___ expressions, meaning that they cannot contain variables, such as 1 + x.
answer
data type, constant
Unlock the answer
question
When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a _ _ or the end of the switch statement is reached
answer
break statement
Unlock the answer
question
The _ which is optional, can be used to perform actions when none of the specified cases matches the _.
answer
default case, switch-expression
Unlock the answer
question
The _ case, which is _, can be used to perform actions when none of the specified cases matches the switch-expression
answer
default, optional
Unlock the answer
question
The keyword break is _. The break statement immediately ends the switch statement.
answer
optional
Unlock the answer
question
Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a _ statement or the __ statement is reached
answer
break, end of the switch
Unlock the answer
question
Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as _.
answer
fall-through behavior
Unlock the answer
question
To avoid programming errors and improve code maintainability, it is a good idea to put a _ in a case clause if break is purposely omitted.
answer
comment
Unlock the answer
question
A __ evaluates an expression based on a condition
answer
conditional expression
Unlock the answer
question
if (x > 0) y = 1; else y = -1; Alternatively, as in the following example, you can use a conditional expression to achieve the same result.
answer
y = (x > 0) ? 1 : -1;
Unlock the answer
question
Conditional expressions are in a completely different style, with no explicit if in the statement. The syntax is:
answer
boolean-expression ? expression1 : expression2;
Unlock the answer
question
Suppose you want to assign the larger number of variable num1 and num2 to max. You can simply write a statement using the conditional expression:
answer
max = (num1 > num2) ? num1 : num2;
Unlock the answer
question
The symbols ? and : appear together in a conditional expression. They form a _ and also called a _ operator because it uses three operands.
answer
conditional operator, ternary (It is the only ternary operator in Java.)
Unlock the answer
question
Operator _ and _ determine the order in which operators are evaluated.
answer
precedence, associativity
Unlock the answer
question
If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are __.
answer
left associative
Unlock the answer
question
Assignment operators are right associative. Therefore, the expression a = b += c = 5 is equivalent to
answer
a = (b += (c = 5))
Unlock the answer
question
Finding logic errors, on the other hand, can be very challenging. Logic errors are called _. The process of finding and correcting errors is called debugging
answer
bugs
Unlock the answer
question
A common approach to debugging is to use a combination of methods to help pinpoint the part of the program where the bug is located. You can _ the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program.
answer
hand-trace
Unlock the answer
question
JDK includes a command-line debugger, _, which is invoked with a class name. _ is itself a Java program, running its own copy of Java .
answer
jdb, interpreter
Unlock the answer
question
__: The debugger lets you trace all of the method calls. This feature is helpful when you need to see a large picture of the program-execution flow.
answer
Displaying call stacks
Unlock the answer