Java Chapter 3.1-3.6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given the variable declarations: int width = 1024, height = 768; Does the following relational statement evaluate to true or false? width < height

False

Given the variable declarations: int width = 1024, height = 768; Does the following relational statement evaluate to true or false? width <= height

False

Given the variable declarations: int width = 1024, height = 768; Does the following relational statement evaluate to true or false? width == height

False

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

False

Will the following expression using a logical operator be true or false? false && false

False

Will the following expression using a logical operator be true or false? false && true

False

Will the following expression using a logical operator be true or false? false || false

False

Will the following expression using a logical operator be true or false? true && false

False

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

False

Complete Checkpoint 3.15 on page 133-134 by evaluating the code and completing the table below. Write your answers in the same tabular format. import javax.swing.JOptionPane; public class Checkpoint { public static void main(String[ ] args) { int books, coupons; String input; input = JOptionPane.showInput Dialog("How many books " + "are being purchased?"); books = Integer.parseInt(input); if (books < 1) coupons = 0; else if (books < 3) coupons = 1; else if (books < 5) coupons = 2; else coupons = 3; JOptionPane.showMessage Dialog(null, "The number of coupons to give is " + coupons); System.exit(0); } }

If the customer purchases this many coupons 1 2 3 4 5 10 this many coupons are given 1 1 2 2 3 3

What will be printed by the following code? String name1 = "DCC"; String name2 = "dcc"; if (name1.equalsIgnoreCase(name2)) { System.out.println("The strings are the same"); } else { System.out.println("The strings are the different"); }

The strings are the same

Will the following expression using a logical operator be true or false? false || true

True

Will the following expression using a logical operator be true or false? true || false

True

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

True

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

True

Briefly summarize how the "compareTo" method compares two strings.

When you use the compareTo method to compare two strings, the strings are compared character by character.

Write an if statement that displays the message "The number is not valid" if the variable speed is outside the range 0 through 200.

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

Consider the following code and select the value that "str3" will have once the code is run. String str1 = "Mary"; String str2 = "Mark; String str3; if (str1.equals(str2)) { str3 = "Option 1"; } else if (str1.equalsIgnoreCase(str2)) { str3 = "Option 2"; } else if (str1.equalsIgnoreCase("mAry"); str3 = "Option 3"; } else if (!str1.equals(str2)) { str3 = "Option 4"; } else { str3 = "Option 5"; }

"Option 3"

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

= is used to assign a value to a variable == is used to determine whether the two operands are equal or not

Explain what is meant by the phrase "conditionally executed"

A conditionally executed statement is performed only when a certain condition is true.

Will the following expression using a logical operator be true or false? !false

True

Write NESTED if statements that perform the following test: If amount1 is greater than 10 and amount2 is less than 100, display the greatest of these two values (amount1 or amount2).

if (amount1 > 10) { if (amount2 < 100) { if (amount1 > amount2) { System.out.println("amount 1 is greatest: " + amount1); } else { System.out.println("amount 2 is greatest: " + amount2); } } }

Write the format of the "if-else-if" statement.

if (expression1) { statements } else if (expression2) { statements } else { statements }

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 + " comes before " + name2 + " alphabetically."); } else { System.out.println(name2 + " comes before " + name1 + " alphabetically."); }

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? A) name1 == name2 B) name1.equals(name2)

B) name1.equals(name2) because it will only compare what is written to compare. In addition it also compares the content of both strings.

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

Indentation helps to convoy a better structure of a program to the readers.

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

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 width = 1024, height = 768; Does the following relational statement evaluate to true or false? width != height

True

Given the variable declarations: int width = 1024, height = 768; Does the following relational statement evaluate to true or false? width > height

True

Given the variable declarations: int width = 1024, height = 768; Does the following relational statement evaluate to true or false? width >= height

True

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

True

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

Given the variable declarations; int a = 2, b = 4, c = 6; Indicate if the following conditional statement is true or false a == 4 || b > 2

True

Given the variable declarations; int a = 2, b = 4, c = 6; Indicate if the following conditional statement is true or false a >= -1 || a <= b

True


Kaugnay na mga set ng pag-aaral

Chapter 8: Conducting Causal Research

View Set

Folders and Files - vocabulary & techniques

View Set

Muscle Excitation and Relaxation

View Set

Principles of Economics - Modules 4-7 Review

View Set

BASIC VEHICLE TECHNOLOGIES 2: ENGINES

View Set

Ch. 9 connect practice questions

View Set