COP 2258 Exam 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?

( (x < 100) && (x > 1) ) || (x < 0)

Suppose that x is an int variable. Which of the following expressions always evaluates to false?

(x > 0) && ( x <= 0)

Character class methods

...

After the execution of the following code, what will be the value of num if the input values are 0? (Assume that console is a Scanner object initialized to the standard input device.) int num = console.nextInt(); if (num > 0) num = num + 13; else if (num >= 3) num = num + 15;

0

What is the output of the following Java code? int num = 15; while (num > 0) num = num - 3; System.out.println(num);

0

What is the output of the following code? int count; int num = 2; for (count = 1; count < 2; count++) { num = num + 3; System.out.print(num + " "); } System.out.println();

0

public int mystery(int x, int y) { if (x >= y) return x - y; else return x + y; } Based on the code above, what would be the output of the following statement? System.out.println(mystery(8,7));

1

The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }

1 3 5 7 9

What is the output? System.out.println( ("I Love Java").length() );

11

If the String variable str contains the string "Now is the time", what is the result of the folllowing epression? String str = "Now is the time"; System.out.println(str.length() + " " + str.substring(1,3));

15 ow

Suppose the following strings are declared as follows: String city = "Tallahassee"; String state = "Florida"; What is the results of the following expressions? city.concat(state).lastIndexOf('a')

17

Suppose that you have the following code: int sum = 0; int num = 8; if (num < 0) sum = sum + num; else if (num > 5) sum = num + 15; After this code executes, what is the value of sum?

23

Suppose that s1, s2, and s3 are three strings, given as follows: String s1 ="Tallahassee, FL"; String s2 = "Atlanta, GA"; String s3 = "Birmingham, AL"; What is the results of the following expressions? s2.substring(3).lastIndexOf('a')

3

What is the output of the following Java code? int x = 57; int y = 3; switch (x % 9) { case 0: case 1: y++; case 2: y = y - 2; break; case 3: y = y + 2; case 4: break; case 5: case 6: y = y + 3; } System.out.println(y);

5

What is the output? System.out.print( ("ABCDABC").lastIndexOf("BC") );

5

public int mystery(int x, int y) { if (x >= y) return x - y; else return x + y; } Based on the code above, what would be the output of the following statement? System.out.println(mystery(8, mystery(2, 1));

7

How many times will the following code print "Welcome to Java"? int count = 0; while (count < 8) { System.out.println("Welcome to Java"); count++; }//end of while loop

8

int x, y; if (x < 4) y = 2; else if (x > 4) { if (x > 7) y = 4; else y = 6; } else y = 8; Based on the code above, what is the value of y if x = 4?

8

if (quotaAmt > 100 || sales > 100 && productCode == "C") bonusAmt = 50; When the above code is executed, which operator is evaluated first?

==

System.out.println("Your name is " + yourName); The above statement is an example of ____, which is used to join Strings.

concatenation

methods for working with string indexes

indexOf(ch): Returns the index of the first occurrence of ch in the string. Returns -1 if not matched. indexOf(ch, fromIndex): Returns the index of the first occurrence of ch after fromIndex in the string. Returns -1 if not matched. indexOf(s): Returns the index of the first occurrence of string s in this string. Returns -1 if not matched. indexOf(s, fromIndex): Returns the index of the first occurrence of string s in this string after fromIndex. Returns -1 if not matched. lastIndexOf(ch): Returns the index of the last occurrence of ch in the string. Returns -1 if not matched. lastIndexOf(ch, fromIndex): Returns the index of the last occurrence of ch before fromIndex in this string. Returns -1 if not matched. lastIndexOf(s): Returns the index of the last occurrence of string s. Returns -1 if not matched. lastIndexOf(s, fromIndex): Returns the index of the last occurrence of string s before fromIndex. Returns -1 if not matched.

A(n) ____ causes a value to be sent from a called method back to the calling method.

return statement

public static double secret(int first, double second) { double temp; if (second > first) temp = first * second; else temp = first - second; return temp; } What is the name of the above method?

secret

public static double secret(int first, double second) { double temp; if (second > first) temp = first * second; else temp = first - second; return temp; } Which of the following is a valid call statement to the method given above?

secret(5, 4.8);

Using break

A break statement can be used to end a loop immediately. The break statement ends only the innermost loop or switch statement that contains the break statement. break statements make loops more difficult to understand. Use break statements sparingly (if ever).

Understand the purpose of the break statement

A break statement separates the cases within a switch statement

Using continue

A continue statement Ends current loop iteration Begins the next one Text recommends avoiding use Introduce unneeded complications

Which of the following loops prints "Welcome to Java" 10 times? A: for (int count = 1; count <= 10; count++) { System.out.println("Welcome to Java"); } B: for (int count = 0; count < 10; count++) { System.out.println("Welcome to Java"); } C: for (int count = 1; count < 10; count++) { System.out.println("Welcome to Java"); } D: for (int count = 0; count <= 10; count++) { System.out.println("Welcome to Java"); }

AB

isLowerCase(ch)

Returns true if the specified character is a lowercase letter.

A computer program will recognize both = and == as the equality operator.

False

In Java, || has a higher precedence than &&.

False

Suppose that the input is 6 and console is a Scanner object initialized to the standard input device. Consider the following code. int alpha = 7; int beta = console.nextInt(); switch (beta) { case 5: alpha = alpha + 1; case 6: alpha = alpha + 2; case 7: alpha = alpha + 3; default: alpha = alpha + 5; } System.out.print(alpha); The output of this code is 9.

False

The following for loop executes 21 times. (Assume all variables are properly declared.) for (i = 1; i <= 20; i = i + 1) System.out.println(i);

False

The output of the Java code, assuming that all variables are properly declared, is 32. num = 10; while (num <= 32) num = num + 5; System.out.println(num);

False

The output of the Java code, assuming that all variables are properly declared, is: 2 3 4 5 6. n = 2; while (n >= 6) { System.out.print(n + " "); n++; } System.out.println();

False

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; }

False

What is the output? System.out.println("Happy".substring(0,3));

Hap

methods to compare strings

How to use the equals method to test for equality: (CORRECT) String productCode = "java"; if (productCode.equals("java")) { System.out.println("This tests for equality."); }

isUpperCase(ch)

Returns true if the specified character is an uppercase letter.

Escape sequence: \\

Name: Backslash Unicode code: \u005C Decimal value: 92

Escape sequence: \r

Name: Carriage Return Unicode code: u000D Decimal value: 13

Escape sequence: "\

Name: Double Quote Unicode code: u0022 Decimal value: 32

Escape sequence: \f

Name: Formfeed Unicode code: \u000c Decimal value: 12

Escape sequence: \n

Name: Linefeed Unicode code: \u000A Decimal value: 10

Logical Operator: &&

Name: and Java Expression: p && q Logical Expression: p AND q Meaning: p && q is true if both p and q are true; false otherwise

Escape sequence: \b

Name: backspace Unicode Code: \u0008 Decimal Value: 8

Logical Operator: ^

Name: exclusive or Java expression: p ^ q Logical Expression: p EXCLUSIVE OR q Meaning: p^q is true when exactly one is true; false otherwise

Logical Operator: !

Name: not Java Expression: !p Logical Expression: NOT p Meaning: !p is false if p is true !p is true if p is false

Logical Operator: ||

Name: or Java Expression: p || q Logical Expression: p OR q Meaning: p||q is true if either p or q or both are true; false otherwise

Escape sequence: \t

Name: tab Unicode Code: \u0009 Decimal Value: 9

toLowerCase(ch)

Returns the lowercase of the specified character.

toUpperCase(ch)

Returns the uppercase of the specified character

isDigit(ch)

Returns true if the specified character is a digit

isLetter(ch)

Returns true if the specified character is a letter

isLetterOfDigit(ch)

Returns true if the specified character is a letter or digit

How to use the + operator to concatenate strings

String name = "Bob"; // name is "Bob" String message = "Hi, " + name; // Output will display "Hi, Bob"

What is the code that will display the middle initial 'L' (right justified) with a total of 10 spaces, given the following declaration? String middleName = "Lanette";

System.out.printf("Here is the first character of the middle name: %10c", middleName.charAt(0));

Which of the following is true about a while loop?

The body of the loop may not execute at all.

A method body provides information about how other methods can interact with it.

True

An application's main() method must have a void return type.

True

In the case of an infinite while loop, the while expression (that is, the loop condition) is always true.

True

The output of the following Java code is: Stoor. int count = 5; System.out.print("Sto"); do { System.out.print('o'); count--; } while (count >= 5); System.out.println('r');

True

When nesting loops, the variable in the outer loop changes more frequently.

True

When using the prewritten equals() method, a true or false comparison between two Strings is returned, but you do not know how the code looks behind the scenes.

True

Suppose that s1, s2, and s3 are three strings, given as follows: String s1 ="Welcome to Java"; String s2 = "Programming is fun"; String s3 = "Welcome to Java"; What is the results of the following expressions? s1.concat(s2)

Welcome to JavaProgramming is fun

What is the output of the following code segment? String homeTown = "Tallahassee Florida"; System.out.println( homeTown.charAt(6) );

a

Data items you use in a call to a method are called ____.

arguments

The method ____ is the first line of a method.

declaration

Java Operator: ==

equal to

Suppose that s1, s2, and s3 are three strings, given as follows: String s1 ="Welcome to Java"; String s2 = "Programming is fun"; String s3 = "Welcome to Java"; What is the results of the following expressions? s2.startsWith("Wel")

false

The following code segment correctly tests for equality: String month = "October"; if (month == "October") { System.out.println("The month is October."); }

false

What is the output for the following code segment? char middleInit = 'L'; System.out.println( Character.isLowerCase(middleInit) );

false

When a variable ceases to exist at the end of a method, programmers say the variable ____.

goes out of scope

Java Operator: >

greater than

Java Operator: >=

greater than or equal to

simple string methods

length() charAt(index) concat(s1) toUpperCase() toLowerCase() trim()

Java Operator: <

less than

Java Operator: <=

less than or equal to

A(n) ____ variable is known only within the boundaries of the method.

local

Which is an infinite loop?

loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); }

The ____ method executes first in an application, regardless of where you physically place it within its class.

main()

A(n) ____ is a program module that contains a series of statements that carry out a task.

method

Java Operator: !=

not equal to

methods to obtain substrings

substring(beginIndex): Returns this string's substring that begins with the character at the specified beginIndex and extends to the end of the string substring(beginIndex, endIndex): Returns this string's substring that begins at the specified beginIndex and extends to the character at index endIndex - 1. Note: that the character at endIndex is not part of the substring.

Differences between the three types of loops

while (pre-test): Also called a while loop A while statement repeats while a controlling boolean expression remains true The loop body typically contains an action that ultimately causes the controlling boolean expression to become false. do-while (post-test): First, the loop body is executed. Then the boolean expression is checked. As long as it is true, the loop is executed again. If it is false, the loop is exited. Equivalent while statement Statement(s)_S1 while (Boolean_Condition) Statement(s)_S1 for: A for statement executes the body of a loop a fixed number of times

Assume x=4 and y=5, which of the following is true?

x != 4 ^ y == 5

Assume x=4, which of the following is true?

x != 5

Assume x = 4 and y = 5, which of the following is true?

x < 5 || y < 5

What is the output of the following Java code? int x = 0; if (x > 0) System.out.println("positive "); System.out.print("zero "); System.out.println("negative");

zero negative

Formatting output using printf

• Use the printf statement. System.out.printf(format, items); • Where format is a string that may consist of substrings and format specifiers. - A format specifier specifies how an item should be displayed. - An item may be a numeric value, character, boolean value, or a string. • Each specifier begins with a percent sign (%).


Conjuntos de estudio relacionados

AP Chemistry Semester 1 Exam Review

View Set

Macroeconomics Exam 1 Worksheets February 28th

View Set