Chapter 5(Java)

Ace your homework & exams now with Quizwiz!

What is the output of the following? for (double sample = 2; sample >0; sample = sample -0.5) System.out.print(sample + " ");

Sample 2.0 Sample 1.5 Sample 1.0 Sample 0.5

What is the output produced by the following code? char letter = 'B'; switch (letter) { case 'A': case 'a': System.out.println("Some kind of A."); case 'B': case 'b': System.out.println("Some kind of B."); break; default: System.out.println("Something else."); break; }

Some kind of B

true

True/False: An expression containing the || operator is true if either or both of its operands is true

true

True/False: Class Math is defined in java.lang and does not require an import statement to be used by a program.

true

True/False: Listing cases consecutively with no statements between them enables the cases to perform the same set of statements.

false

True/False: The break statement is required in the last case of a switch selection statement.

true

True/False: The comma (,) formatting flag in a format specifier (e.g., %, 20.2f) indicates that a value should be output with a thousands separator

false

True/False: The default case is required in the switch selection statement.

false

True/False: The expression ((x > y) && (a < b)) is true if either (x > y) is true OR (a < b) is true.

double result = Math.pow(2. 5, 3 );

Write a statement: Calculate the value of 2.5 raised to the power of 3, using the 'pow' method

for (i=1;i<= 20; i++ ) { System.out.print( i ); if (i% 5 == 0 ) System.out.println(); else System.out.print( '\ t' ); }

Write a statement: Print the integers from 1 to 20, using a for loop and the counter variable i. Assume that i has already been declared, but not initialized. Print only 5 integers per line.

i=1; while (i<= 20 ) { System.out.print( i ); if (i% 5== 0 ) System.out.println(); else System.out.print( '\ t' ); ++i; }

Write a statement: Print the integers from 1 to 20, using a while loop and the counter variable i. Assume that i has already been declared, but not initialized. Print only 5 integers per line.

false

True/False: To test for a range of values in a switch statement, use a hyphen (-) between the start and the end values of the range in a case label.

true

True/False: in a do....while statement the brackets ({ }) are used after the 'do' keyword and before the 'while' keyword, then the conditional statement is added after the 'while' keyword, followed by a (;).

for

Typically, ___________statements are used for counter-controlled repetition and while statements for sentinel-controlled repetition

while

Typically, for statements are used for counter-controlled repetition and __________ statements for sentinel-controlled repetition

sum =0; for ( count =1; count <=99; count +=2 ) sum += count;

Write a statement: Sum the odd integers between 1 and 99, using a for statement. Assume that the integer variables sum and count have been declared.

scope

where in a program a variable can be utelized

What is the output produced by the following? int n = -42; do { System.out.println(n); n=n-3; } while (n>0);

-42 A do while always executes once regardless of whether the conditions are met.

If...else construct Vs Switch...case construct

Any kind of relation can be checked in any condition of the If...else construct. The condition can include any relational operator(<,>, <=,>=,==,!=). The Switch...case construct checks only for equality.

If...else construct Vs Switch...case construct

Float values/variables can be used in the boolean expressions of If construct. Float variable cannot be used as in control expression of switch construct. The controlling expression for a switch statement must be one of the types - ​char​,​int​,​short​,​byte​,or​String​.

Suppose variable1 and variable2 are two variables that have been give values. How do you test whether they are equal when the variables are of type int? How do you test whether they are equal when the variables are of type String?

For int: variable1 == variable 2; For string: variable1.equals(variable2);

false

If the loop-continuation condition in a for header is initially __________ the program does not execute the for statement's body.

What is the output produced by the following code? int key = 1; switch (key + 1) { case 1: System.out.println("Apples"); break; case 2: System.out.println("Oranges"); break; case 3: System.out.println("Peaches"); case 4: System.out.println("Plumbs"); break; default: System.out.println("Fruitless"); }

Oranges

static

Methods that perform common tasks and do not require objects are called _________ methods.

Does the following sequence produce a division by zero? int j = -1; if ((j>0) && (1/(j+1) > 10)) System.out.println(i);

No, it calculated (j>0) and because that is false it short circuits

Can the values true and false be numbers (0 or 1) in Java ?

No. In Java, the values true and false are not numbers, nor can they be type cast to any numeric type. Similarly, values of type int cannot be type cast to boolean values.

switch

The _________ statement selects among multiple actions based on the possible values of an integer variable or expression.

&&

The __________ operator can be used to ensure that two conditions are both true before choosing a certain path of execution.

continue

The __________ statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

Break statement in switch...case construct

The break keyword causes the entire switch statement to exit, and the control is passed to statement following the switch...case construct. Without break, the execution just continues to the next case. case 'A': case 'a': System.out.println("Excellent. You need not take the final."); break; Because the first case has no break statement, the effect is the same as having two labels for one case. ​The break statement is optional in the switch...case construct.

after

The do... while statement tests the loop-continuation condition __________ executing the loop's body; therefore, the body always executes at least once.

structured programming

The use of only Sequence, Selection and/or Repetition statements in programming.

default case in switch...case construct

This keyword gives the switch...case construct a way to take an action if the value of the switch variable does not match with any of the case constants.​ The default case is optional in the switch...case construct. The default case need not be the last case in a​switch statement, but making it the last case, makes the code clearer.

switch statement (multiple-selection)

a selection statement that is controlled with an integer based expression and uses case declarations to execute programming statements.

increment statement

a statement that increments or decrements a control variable in a loop

constant variable

a variable containing a value which does not change for the entire program.

For each of the following situation, tell which type of loop (while, do-while, and for) would work best: a. Summing a series, such as 1/2 + 1/3 + 1/4 + 1/5 +...+1/10. b. Reading in the list of exam scores for one student. c. Reading in the number of days of sick leave taken by employees in a department.

a. loop b. while c. while

hasNext()

boolean value method used with Scanner to determine if all data has been input

off-by-one error

common programming error where a control statement uses the '<' or '>' operator when it needed the '<=' or '>=' operator.

statement header (for)

first line of a for statement. It includes initialized control variable, loop-continuation condition, and increment of control variable..

%b

formatter used to display a boolean expressions value in a formatted output statement.

Write a multiway if-else statement that classifies the value of an int variable n into one of the following categories and writes out an appropriate message: n<0 or 0≤n<100 or n≥100 Hint: Remember that Boolean expressions are checked in order

if (n<0) System.out.println("n<0"); else if(n<100) System.out.println("0≤n<100"); else System.out.println("n≥100");

end-of-file indicator

system-dependent keystroke combination which the user enter's to indicate that there is no more data to input


Related study sets

Test 3 ECON LAST TEST Chapters 10, 17, 13 14

View Set

Texas SAE: Real Estate investment

View Set

stats final comprehensive questions

View Set

Why Study Reading? Quiz 1: Using Prefixes & Roots

View Set

HCD Mobility and Tissue Integrity

View Set

Med/Surg II - Test #2 - Endocrine and Cardiac

View Set

Clinical Integration Exam 8 Combined

View Set

NU112 Patient safety and quality

View Set