Comp test 3

Ace your homework & exams now with Quizwiz!

21) If x is currently equal to 5, what will the value of x be after the switch statement executes?

D) 11

24) Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates? for (int i=0;i<5;i++)

E) 10

34) String s1 is said to overlap String s2 if all of the characters in s1 also appear in s2. Write a set of code that will set the boolean variable overlap to true if s1 overlaps s2 and false otherwise. Assume both s1 and s2 have already been input.

) boolean overlap = true; // assume the two Strings will overlap int j = 0; while (overlap && j < s1.length( )) { boolean found = false; // search in s2 for character at position j in s1 int k = 0; while (!found && k < s2.length( )) // if found, set found to true if (s2.charAt(k) == s1.charAt(j)) found = true; else k++; // otherwise go on to next character in s2 if (!found) overlap = false; // if a full pass is made through s2 and a character not j++; // found then set overlaps to false, otherwise continue }

38) How many times will the System.out.println(*); statement execute inside of the following nested for-loops? for (j=0; j<10; j++) for (k=10;k>j;k--) System.out.println("*");

55

44) The following for-loop is odd in that the loop increment value changes during iterations of the loop. Determine the number of times the loop iterates and the final value of j after the loop terminates. int k = 0; for (j = 0; j < 20; j += k) k++;

6 times with j = 21. Explanation: The first iteration has j = 0, k is then incremented in the loop body to 1 and j is incremented after the loop body executes to be j + k = 0 + 1 = 1. For the second iteration, k is incremented to 2 and j is incremented to j + k = 1 + 2 = 3. For the third iteration, k is incremented to 3 and j is incremented to 3 + 3 = 6. For the fourth iteration, k is incremented to 4 and j is incremented to 6 + 4 = 10. For the fifth iteration, k is incremented to 5 and j is incremented to 10 + 5 = 15. For the sixth iteration, k is incremented to 6 and j is incremented to 15 + 6 = 21. Now, since (j < 20) is no longer true, the loop terminates.

22) If x is currently equal to 3, what will the value of x be after the switch statement executes?

A) 12

23) The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as

A) y = (x < 0) ? x : 0

29) What will the following code do? Assume s is a String, x is an int initialized to 10, page is a Graphics object, and this is part of a paint method for an applet. boolean isVowel = false; String vowels = "aeiou"; for (int j = 0; j < s.length( ); j++) { for (int k = 0; k < 5; k++) if (s.charAt(j) == vowels.charAt(k)) isVowel = true; if (isVowel) page.drawString(""+s.charAt(j), 10, 15*x++); else page.drawString(""+s.charAt(j), 110, 15*x++); isVowel = false; }

C) The String s is printed down the applet in two columns with vowels appearing in the left column and all other characters in the right column

17) How many times will the following loop iterate? int x = 10; while (x > 0) { System.out.println(x); x--; }

C) 10 times

30) Which of the following statements are true about Java loops?

C) all three loop statements are functionally equivalent

25) The do loop differs from the while loop in that

C) the do loop will always execute the body of the loop at least once

14) Every Interator

D) has a hasNext( ) method

28) The following nested loop structure will execute the inner most statement (x++) how many times? for (int j = 0; j < 100; j++) for (int k = 100; k > 0; k--) x++;

E) 10,000

32) Explain what is meant by short circuiting and provide an example of short circuiting a condition with && and provide an example of short circuiting a condition with | |.

Short circuiting is when a boolean expression does not have to be completely evaluated because the final answer is determined with just a partial evaluation. This can occur in an expression with && if one part of the expression evaluates to false, and in an expression with | | if one part of the expression evaluates to true. Examples (x != 0) && (sum / x > 100) can be short circuited if x != 0 is false, preventing a division by zero error. (a.equals("yes") | | a.equals("no")) can be short circuited if a is equal to "yes" so that the computer does not waste time in testing a.equals("no").

45) Write a set of code that outputs all of the int values between 1 and 100 with 5 values per line, and each of those 5 values spaced out equally. Use a single for-loop to solve this problem.

for (int j = 1; j <= 100; j++) { System.out.print(j + "\t"); if (j % 5 == 0) System.out.println( ); }

46) The following code has a syntax error immediately before the word else. What is the error and why does it arise? Fix the code so that this statement is a legal if-else statement. if (x < 0) ; x++; else x--;

if (x < 0) x++; else x--;

35) Rewrite the following nested if-else statement as an equivalent switch statement. if (letter == 'A' | | letter == 'a') System.out.println("Excellent"); else if (letter == 'B' | | letter == 'b') System.out.println("You can do better"); else if (letter == 'C' | | letter == 'c') System.out.println("Try harder"); else if (letter == 'D' | | letter == 'd') System.out.println("Try much harder"); else System.out.println("Try another major! ");

switch (letter) { case 'A', 'a' : System.out.println("Excellent"); break; case 'B', 'b' : System.out.println("You can do better"); break; case 'C', 'c' : System.out.println("Try harder"); break; case 'D', 'd' : System.out.println("Try much harder"); break; default : System.out.println("Try another major! "); }

43) Rewrite the following if-else statement using a conditional operator. if (x > y) z = x; else z = y;

z = (x > y) ? x : y;

1) The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as

B) flow of control

39) Write a "query-controlled" loop that will continue to input int values from the user, adding each to the int value sum, and then ask if the user has another value to input, until the user says that there are no more values. Assume that cs1.Keyboard has been imported.

) int x, sum = 0; String query; do { System.out.println("Enter an int value to be summed"); x = Keyboard.readInt( ); sum += x; System.out.println("Do you have any more ints to input? (Yes or No) "); query = Keyboard.readString( ); } while (query.equalsIgnoreCase("Yes"))

41) A data verification loop is a loop that asks the user to input a value within a restricted range repeatedly until the user has input a value within the requested range. Write a data verification loop that that inputs an int value x within the range 0 and 100. Assume cs1.Keyboard has been imported.

) int x; do { System.out.println("Enter an int value between 0 and 100"); x = Keyboard.readInt( ); } while (x >= 0 && x <= 100);

37) Show the output that would occur from the following code, including proper spacing. for (j = 0; j < 5; j++) { for (k = 0; k < 5; k++) if (j!=k) System.out.print(' '); else System.out.print('*'); System.out.println( );

) * * * * * The outer loop iterates from 0 to 4, and for each iteration, the inner loop also iterates from 0 to 4. For iteration of the inner loop, if j and k are not the same, a blank is output, otherwise an *. So, if j = 0 and k = 2, then two blanks are output before an *, followed by two more blanks. At the end of each iteration of the inner loop, the cursor is returned to start a new line

7) Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score

B) This code will work correctly only if grade < 70

9) What is wrong, logically, with the following code? if (x > 10) System.out.println("Large"); else if (x > 6 && x <= 10) System.out.println("Medium"); else if (x > 3 && x <= 6) System.out.println("Small"); else System.out.println("Very small");

A) There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditiona

26) How many times will the following loop iterate? int x = 10; do { System.out.println(x); x--; } while (x > 0);

A) 11 times

15) If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) x *= 2;

A) 128

20) A continue statement switch (x) { case 3 : x += 1; case 4 : x += 2; case 5 : x += 3; case 6 : x++; case 7 : x += 2; case 8 : x--; case 9 : x++ }

A) may be used within any Java loop statement

10) Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is true regarding this structure? if (condition1) if (condition2) statement1; else statement2

A) statement2 will only execute if condition1 is false and condition2 is false

18) You might choose to use a switch statement instead of nested if-else statements if

A) the variable being tested might equal one of only a few int values

13) If a break occurs within the innermost loop of a nested loop that is three levels deep

A) when the break is encountered just the innermost loop is "broken"

3) Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?

B) if (x > 0) x++; else if (x < 0) x--;

if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; 5) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?

B) 2

if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; 4) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?

C) 3

2) Of the following if statements, which one correctly executes three instructions if the condition is true?

D) if (x < 0) { a = b * 2; y = x; z = a - y; }

12) The break statement does which of the following?

D) transfers control out of the current control structure such as a loop or switch statement

if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; 6) If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?

E) 5

11) Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the following conditions: Condition 1: (x < y && x > 0) Condition 2: (a != d || x != 5) Condition 3: !(true && false) Condition 4: (x > y || a == 'A' || d != 'A')

E) Conditions 2, 3 and 4 are all true, Condition 1 is not

8) Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following? if (count != 0 && total / count > max) max = total / count;

E) The condition short circuits and the assignment statement is not executed

27) Given that s is a String, what does the following loop do?

E) it prints s out backwards

19) If a switch statement is written that contains no break statements whatsoever

E) none of the above

16) If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x *= 2

E) none of the above, this is an infinite loop

42) Describe a situation where you should use a do loop and a situation where you should use a while loop

The do loop is used if you want to execute the loop body at least one time. This is useful for data verification (asking the user for a value, and only if the user has entered an improper value does your code repeat and try again). A while loop does not necessarily execute if the loop condition is not initially true. This might be useful if you want to control entrance to a loop such as only trying to sum up a list of values if the user actually has values to sum up.

33) The following code has a syntax error immediately before the word else. What is the error and why does it arise? Fix the code so that this statement is a legal if-else statement. if (x < 0) ; x++; else x--;

The error is "else without if" and it arises because of the ";" after the condition but before x++. The Java compiler determines that the if clause is in fact ; (no statement) and that x++; is a statement that follows the if statement. Therefore, since x++; is not part of the if statement, the "else" is felt to be an else without an if, and that is why the error has arise. The statement should be if (x < 0) x++; else x--;

36) Given the following tax table information, write Java code to assign the double taxRate appropriately given the double pay. Select the selection statement (if, if-else, switch) that makes the most sense. If pay is more than 100,000, tax rate is 40% If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30% If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20% If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10% If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5% If pay is less than or equal to 5,000, tax rate is 0%

Use a nested if-else structure as it requires the least amount of code. A switch statement is not possible because pay is a double and the switch statement can only be used for integral types. if (pay > 100000) taxRate = 0.40; else if (pay > 60000) taxRate = 0.30; else if (pay > 30000) taxRate = 0.20; else if (pay > 15000) taxRate = 0.10; else if (pay > 5000) taxRate = 0.05; else taxRate = 0.0;

31) Rewrite the following set of if statements using a nested if-else structure. if (score >= 90) grade = 'A'; if (score >= 80 && score < 90) grade = 'B'; if (score >= 70 && score < 80) grade = 'C'; if (score >= 60 && score < 70) grade = 'D'; if (score < 60) grade = 'F';

if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else grade = 'F';

40) Write some code that inputs a set of int values and computes its average. Ask the user first how many int values will be input. Make sure that your code cannot produce a division by zero error. Assume that cs1.Keyboard has been imported.

int numberOfValues, j, current, sum = 0; System.out.println("Enter the number of ints you will be inputting"); numberOfValues = Keyboard.readInt( ); for (j = 0; j < numberOfValues; j++) { System.out.println("Enter your next int value"); current = Keyboard.readInt( ); sum += current; } if (numberOfValues != 0) double average = sum / numberOfValues; else System.out.println("Cannot compute the average of 0 values");


Related study sets

Chapter 34: Assessment and Management of Patients With Hematologic Disorders NCLEX

View Set

Theme #3: Insurrection in America Vocab

View Set

NSCA-CPT: Goal Setting and Motivation

View Set

Mental Health Chapter 25 Evolve Questions (Suicide and Non-Suicidal Self-Injury)

View Set

Intro to Psychology Unit 2 (ch. 6-10)

View Set