COMP 1210 Exam 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

When reading from a file, the required import statements are.... A) import java.io.File; B) import java.io.FileNotFoundException; C) import java.util.Scanner D) None of the above E) All of the above

E) All of the above

Boolean Example (Where email references string object)

Example: (where email references a String object) boolean validEmail = email.contains("@");

Example of while statement: Example: print all numbers from 1 to 10

Example: int count = 1; while (count <= 10) { System.out.println(count); count++; }

while statement (or while loop)

will continually execute a statement or block of statements as long as its condition (boolean expression) is true; i.e., it repeats the statement (or block) until the condition is false

The continue statement.....

will skip the rest of the code in that iteration and attempt to do the next iteration of the loop

The && and || operators are....

"short circuited"

Suppose that strIn is a String. Which of the two if clauses will cause a run-time error if strIn is equal to null? A) if (strIn != null && strIn.length() > 0) or B) if (strIn.length() > 0 && strIn != null)

A) if (strIn != null && strIn.length() > 0)

TriangleList A

Add Triangle Object

What is the value of total after the following code is executed? int total = 0; int numberIn = 7; switch (numberIn) { case 3: total += 1; case 7: total += 2; case 9: total += 4; case 11: total += 5; break; default: total += 10; } System.out.println(total);

Answer: 11 Be sure to note that total is being incremented (not numberIn) and that the only break comes at the end of case 11.

What is the value of resultValue after the following code is executed? int resultValue = 8; do { resultValue++; } while (resultValue < 6);

Answer: 9

Suppose that myObj is an instance of the class that contains the methods turnRight and turnLeft. Which method will be invoked on myObj if inValue is equal to 'U'? boolean result = (inValue == 'R') ? myObj.turnRight() : myObj.turnLeft(); What is the return type for methods turnRight and turnLeft?

Answer: myObj.turnLeft() Answer: Boolean

An if statement uses a boolean expression as its....

Condition

The order in which a program's instructions execute can be determined by which of the following categories of control constructs: a) sequence b) selection c) iteration d) all of the above e) none of the above

D) All of the above

Which term is NOT a complex boolean expression? A) Equality B) Logical Operators C) Relational D) System

D) System

TriangleList D

Delete Triangle Object

TriangleListMenuApp

Displays a menu of options then uses a do-while loop with a switch statement to take action based on the user's selection

If a FileNotFoundException does occur the program will start immediately True or false?

False. If a FileNotFoundException does occur the program will end immediately

Our coding standard (supported by Checkstyle) does not require blocks in if statements. True or False?

False. Our coding standard (supported by Checkstyle) REQUIRES blocks in if statements

True or False? A break statement immediately enters a loop.

False. A break statement immediately EXITS a loop.

True or False? An if statement cannot have any number of else if blocks (else block containing an if statement)

False. An if statement CAN have any number of else if blocks (else block containing an if statement)

True or False? You can't (and shouldn't) use a generic type to specify the type of objects the list can hold

False. You CAN (and SHOULD) use a generic type to specify the type of objects the list can hold

any upper case character will have a higher numeric value than any lower case character. True or false?

False. any upper case character will have a LOWER numeric value than any lower case character

if, else if, else Statement (i.e., if with optional else if and else)

If the temperature > 80 and humidity >= 60, tell user it's hot. if § Otherwise, if the temperature < 40, tell the user it's cold. else if § For any other condition, tell the user that the weather is good. else

Operator: &&

Meaning: Logical AND (applied before ||)

Operator: !

Meaning: Logical NOT (applied before &&, ||)

Operator: ||

Meaning: Logical OR

When comparing two items consider.....

Numeric types (including their corresponding wrapper classes) and type char can be compared using the equality and relational operators (==, !=, <, >, <=, >=) § Numeric types double and float should use == or != with care due to possible rounding; best to check that the absolute value of the difference between two items is less a specified tolerance § Object types (other than the numeric wrapper classes) can be compared using the equality operators (==, !=), but in most cases the object's equals or compareTo method should be used

TriangleList L

Print Largest Perimeter

TriangleList S

Print Smallest Perimeter

Triangle List T

Print Total Perimeter

TriangleList P

Print Triangle List

TriangleList Q

Quit

Example: ReverseLinesReadFromFile.java What will happen?

Read in a lines of text from a file; print lines in reverse order

TriangleListMenu App R

Read in file and create TriangleList

A character is represented by....

a 16-bit numerical value

A break statement in a loop will skip the rest of the code in that iteration and exit the loop True or false?

True

An else clause is matched to the last unmatched if (no matter what the indentation implies, unless braces are used)

True

True or False? indentation in Java is for the human reader and is ignored by the computer

True

True or False? Any variables in the boolean expression need to initialized before the loop

True

True or False? Code in the loop body should alter values so that the condition is eventually false and the loop terminates

True

True or False? When a boolean expression is evaluated, the result can be assigned to a boolean variable

True

For the String class, objects are compared based on the characters that they contain. True or False?

True.

True or False? Usually the break and continue statements in loops are used in conjunction with an if statement inside a loop

True.

String s1 = new String("Red Sox"); String s2 = new String("Red Sox"); s1 == s2 false Is this true?

Yes, because the code would have to read: "s2=s1" in order for the statement to be labeled as "true".

Iterator

an object that allows you to process a collection of items one at a time (lets you step through each item in turn and process it as needed)

Given the following, what is the result of running the following program? class Bool { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 && b2 || b2 && b3 || b2 ) System.out.print("Auburn "); if ( b1 && b2 || b2 && b3 || b2 || b1 ) System.out.println("Tigers"); } } a) Auburn b) Tigers c) Auburn Tigers d) No output is produced e) Compilation error

b) Tigers

How many times is the body of the loop below executed? int counter; counter = 1; while (counter > 20) { // body of loop counter = counter + 1; } a) 19 b) 20 c) 21 d) 0 e) syntax error

d) 0

Which of the following segments will call the method readData()four times? // Loop 1 int i; i = 0; while (i != 5) { readData(); i = i + 1; } // Loop 2 int i; i = 0; while (i <= 4) { readData(); i = i + 1; } // Loop 3 int i; i = 0; while ( i < 4 ) { readData(); } // Loop 4 int i; i = 0; while ( i < 4 ) { readData(); i = i + 1; } a) Loop 1 b) Loop 2 c) Loop 3 d) Loop 4 e) none of the above

d) Loop 4 Explanation: In Loop 1, the loop body will execute for i equal to 0, 1, 2, 3, and 4, which is five times. In Loop 2, the loop body will execute for i equal to 0, 1, 2, 3, and 4, which is five times. In Loop 3, the loop body will execute for an "infinite" number of times, since i is not incremented. In Loop 4, the loop body will execute for i equal to 0, 1, 2, and 3, which is four times.

The ArrayList class....

defines an object that can hold a list of other objects called elements • Includes methods to add an element, remove an element, get an element, find the index of an element, determine if the list is empty, and determine the size (number of items in the list)

What is the value of isIt after the following code is executed? boolean isIt = 4 + 3 > 6 && 2 + 3 == 1 + 4 && !(1 < 2); a) bananas b) 12 c) 10 d) true e) false

e) false

==

equal to

>

greater than

>=

greater than or equal to

Logical operators

have boolean operands; evaluate to a boolean result (true or false); have lower precedence than the equality and relational operators

Logical Operator if statement example: "If the temperature (temp) is greater than 80 and humidity is greater than or equal to 60, then tell the user to stay indoors" What will this look like?

if (temp > 80 && humidity >= 60) { System.out.println("Hot: Stay indoors."); }

if, else if, else Statement (using logical operator) example

if (temp > 80 && humidity >= 60) { System.out.println("Hot: Stay indoors."); } else if (temp < 40) { System.out.println("Cold: Stay indoors."); } else { System.out.println("Weather is good."); }

If-else statement if with optional else (what would this look like?): Suppose you wanted to add "... otherwise, tell the user that the weather is good."

if (temp > 80 && humidity >= 60) { System.out.println("Hot: Stay indoors."); } else { System.out.println("Weather is good."); }

The break and continue statements for loops are generally used...

in conjunction with an if statement inside a loop

A boolean expression is....

in programming, an expression that evaluates to True or False. Example: (where num1 and num2 are int values) num1 > num2 + 5

Some classes provide a compareTo method, which returns _______ instead of boolean

int

If the first operand of the || is true, the other operand.....

is not evaluated

Several classes in the Java standard class library are______

iterators

<

less than

<=

less than or equal to

When an if or if-else contains other if or if-else statements, we have_______

nested if statements

!=

not equal to

If the first operand of the && is false, then the other operand is.....

not evaluated

Letters A through Z represent....

numeric values 65 to 90

Letters a through z represent....

numeric values 97 to 122

Less than 0 indicates.....

obj1 < obj2

Greater than 0 indicates

obj1 > obj2

Equal to 0 indicates

obj1 is equal to obj2

The next method_________

returns the next item

The continue statement will...

skip the rest of the code in that iteration and attempt the next iteration of the loop


Ensembles d'études connexes

Transportation Operations LINCS Exam

View Set

Midterm Review AP World Units 1-2 Multiple choice review

View Set

Regents Practice Problems: Biomolecules

View Set

Chapter 38: Care of Patients with Acute Coronary Syndromes

View Set

Strategic Management LearnSmart Questions

View Set

Pre-Assessment: Introduction to Human Resource Management

View Set

Module 46. Humanistic Theories and Trait Theories

View Set