Programming Fundamentals Exam 1 - Chapter 3
You can use this method to display formatted output in a console window.
System.out.printf
briefly describe how the && operator works
answers may vary. use your own words
briefly describe how the || operator works
answers may vary. use your own words
what happens when you compare two string objects with the == operator
an error will occur. you have to use .equals for strings
This type of expression has a value of either true or false.
boolean expression
to create a block of statements, you enclose statements in these
braces
explain what is meant by the phrase "conditionally executed"
conditionally executed means that the statements will not execute if the condition preceding it is false
This section of a switch statement is branched to if none of the case expressions match the switch expression.
default
true or false: a conditionally executed statement should be indented one level from the if clause
true
true or false: all lines in a conditionally executed block should be indented one level
true
true or false: when an if statement is nested in the else clause of another statement, the only time the inner if statement is executed when the boolean expression of the outer if statement is true
true
true or false. the = operator and the == operator perform the same operation
false
This is a boolean variable that signals when some condition exists in the program.
flags
what risk does a programmer take when not placing a trailing else at the end of an if-else-if statement
if the user enters something that does not apply to any of the if-else-if statements, the code will simply stop running and produce no output
find the error: if (average = 100) System.out.println("Perfect Average!")
it is missing an additional ==
why is it good advice to indent all of the statements inside a set of braces
it makes the code easier to read, and it helps the programmer know which statements go with the conditional statement
assume that partNumber references a string object. the following if statement should perform a case-insensitive comparison. what is wrong with it: if (partNumber.equals("BQ789W4")) available = true;
it needs to add IgnoreCase after .equals
the following statement assigns 0 to z if a is less than 10; otherwise it should assign 7 to z. what is wrong with it: z = (a < 10) : 0 ? 7
it should look like: if (a < 10) z = 0; else z = 7;
&&, ||, and ! are
logical operators
This is an if statement that appears inside another if statement.
nested if statement
This is an empty statement that does nothing.
null statement
true or false: when an if statement is nested in the if clause of another statement, the only time the inner if statement is executed is when the boolean expression of the outer if statement is true
true
true or false: the scope of a variable is limited to the block in which it is defined
false
when determining whether a number is inside a range, its best to use
&&
How does the character 'A' compare to the character 'B'?
A is less than B
explain why a misplaced semicolon can cause an if statement to operate incorrectly
semicolon are used when it is it the end of a statement and java will treat the if statement like a single statement and throw an error or skip over the statements that are meant to be executed within the if statement
how often must you place break; statements when using a switch statement
after every case and the default
The if statement is an example of a __________.
decision structure
>, <, and == are
relational operators
the following statement should determine whether x is not greater than 20. what is wrong with it: if (!x > 20)
the ! is supposed to be beside the >
the following statement should determine whether count is outside the range of 0to100. what is wrong with it: if(count < 0 && count > 100)
the && means and, and we are looking for the statement to check if the count < 0 OR count > 100
An else clause always goes with __________.
the closest previous if clause that doesn't already have its own else clause
what is wrong with the following code: double value = 12345.678; System.out.printf("%.2d", value)
the d in "%.2d" needs to be an f
This determines whether two different String objects contain the same string.
the equals method
explain the purpose of a flag variable
the purpose of a flag variable is to determine whether to boolean expression is true or false
find the error: if (x==1); y=2; else if (x ==2); y=3; else if (x==3); y=4;
the semicolon after each if/else if statement will cause the following statements to be ignored, and the code will not run
the following statement should determine whether count if within the range of 0to100. what is wrong with it: if (count >= 0 || count <= 100)
the || means or, and we are looking for the statement to check if the count is >= 0 AND count <= 100
find the error: if (num2 == 0) System.out.println("Division by zero is not possible. "); System.out.println("please run the program again"); System.out.println("and enter a number besides zero");
there are no braces
the conditional operator takes this many operands
three