2020-Spring-CPS141-ForTest2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

A block of code is enclosed in a set of

braces, { }

true && false

False

Will the following expression using a logical operator be true or false? (See page 135) true && true

True

Assume that the variables name1 and name2 reference two different String objects containing different strings. Write code that displays the strings referenced by these variables in alphabetical order.

if (name1.compareTo(name2) < 0)System.out.println(name1 + " " + name2);elseSystem.out.println(name2 + " " + name1);

An expression tested by an if statement must evaluate to

true or false

Explain what is meant by the phrase "conditionally executed" (pg 112).

An action is conditionally executed because it is performed only when a certain condition exists.

What is the difference between the "==" operator and the "=" operator?

The "==" operator with two equal signs is the equality operator that compares to values to determine if they are the same. The "=" operator with one equals sign is the assignment operator that sets the value of the left operand to the value of the right operand.

Write an if statement that displays the message "The number is valid" if the variable speed is within the range 0 through 200. See page 140 on checking ranges numeric ranges with logical operators.

if (speed >= 0 && speed <=200){ System.out.println("The number is valid"); }

Which of the two statements below is the correct way to check if the contents of two Strings are exactly equal and why? See page 142) A) name1 == name2 B) name1.equals(name2)

A) is not correct since that would only compare the addresses of the two strings. B) is correct since it compares the contents of both strings.

Why is it good advice to indent all of the statements inside a set of braces?

By indenting the conditionally executed statement you are causing it to stand out visually.

The "if" statement is an example of a ________?

Decision structure

summarize how the "compareTo" method compares two strings.

The compareTo method compares each string character by charchater. As soon as it detects a different it will return -1 if the character in the first string is less than the character in the second string or +1 if the character in the first string is greater than the character in th second string. If all characters are the same the method returns 0.

Why should a programmer be careful about putting a semicolon immediately after the "if (expression) part of an if statement (pg 117).

The compiler will assume that a null statement is being used and will not conditionally execute anything. No error is given so this could be hard to debug.

Given the variable declarations; int a = 2, b = 4, c = 6; Indicate if the following conditional statement is true or false 1 != b && c != 3

True

The if-else statement will execute one group of statements if its boolean expression is true or another group if its boolean expression is false.

True

>, <, and == are _______________?

relational operators

An else clause always goes with ____________?

the closest previous if clause that doesn't already have its own else clause. Answers: the if clause that is randomly selected by the compiler?


Set pelajaran terkait

Rheumatic fever,/Strep throat /scarlet fever

View Set

(PHILOSOPHY) Chapter 7: The Hellenistic Era

View Set