Java Ch 9 & 10
Which one of the following is the not equal operator? <> *& != NOT
!=
If str1 and str2 are both Strings, which of the following expressions will correctly determine whether they are equal? (1)(str1 == str2) (2) str1.equals(str2) (3)(str1.compareTo(str2) == 0) 1, 2, and 3 will all work 1 and 2 2 and 3 1 and 3
(2) str1.equals(str2) (3)(str1.compareTo(str2) == 0)
The increment operator is: -- -= *= Correct! ++
++
What would be the value of bonus after the following statements are executed? int bonus, sales = 85000; char dept = 'S'; if (sales > 100000) if (dept == 'R') bonus = 2000; else bonus = 1500; else if (sales > 75000) if (dept == 'R') bonus = 1250; else bonus = 1000; else bonus = 0; 1250 1500 1000 2000
1000
What does the following code display? double x = 12.3798146; System.out.printf("%.2f\n", x);
12.38
What will be the value of ans after the following code has been executed? int ans = 10; int x = 65; int y = 55; if (x >= y) ans = x + y; 10 100 120 No value, there is a syntax error.
120
What will be the value of x after the following code is executed? int x = 75; int y = 60; if (x > y) x = x - y; 15 135 60 75
15
What is the value of x after the following code has been executed? int x = 75; int y = 90; if ( x != y) x += y; 15 90 75 165
165
What will be the value of charges after the following code is executed? double charges, rate = 7.00; int time = 180; charges = time <= 119 ? rate * 2 : time / 60.0 * rate; 28.00 7.00 21.00 14.00
21.00
What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y; 25 30 Correct! 40 Invalid for statement
40
What will be printed when the following code is executed? double x = 45678.259; String output = String.format("%,.1f", x); System.out.println(output); 45,678.3 45,678.26 45,678.259 45678.259
45,678.3
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100); You Answered 1 3 4 Correct Answer 5
5
These operators use two operands: Tertiary Unary Binary None of the above
Binary
A block of code is enclosed in a set of: brackets [ ] braces { } double quotes " " parentheses ( )
Braces
If you are using a block of statements, don't forget to enclose all of the statements in a set of: Double quotes Semicolons Correct! Braces Parentheses
Braces
This is an item that separates other items. Doorway Partition Correct! Delimiter Controller
Delimiter
An important style rule you should follow when writing if statements is to line up the conditionally executed statement with the if statement. True False
False
In a for statement, the control variable can only be incremented. True Correct! False
False
Programs never need more than one path of execution. True False
False
When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration. You Answered True Correct Answer False
False
Which of the following will open a file named MyFile.txt and allow you to read data from it? Correct! File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); PrintWriter inputFile = new PrintWriter("MyFile.txt"); File file = new File("MyFile.txt"); Scanner inputFile = new Scanner("MyFile.txt");
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
This type of loop is ideal in situations where the exact number of iterations is known. Correct! for loop do-while loop if statement while loop
For loop
You can use this method to determine whether a file exists. The File class's canOpen method The Scanner class's exists method The PrintWriter class's fileExists method Correct! The File class's exists method
The File class's exists method
In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number "); int number = keyboard.nextInt(); while (number < 100 && number > 500) { System.out.print("Enter another number "); number = keyboard.nextInt(); } Numbers in the range 100 - 499 Numbers in the range 100 - 500 Correct! The boolean condition can never be true. Numbers less than 100 or greater than 500
The boolean condition can never be true
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; } 110 90 210 Correct! This is an infinite loop.
This is an infinite loop
What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x > 5); 10 200 The loop will not be executed, the initial value of x > 5. Correct! This is an infinite loop.
This is an infinite loop
A file must always be opened before using it and closed when the program is finished using it. Correct! True False
True
Unicode is an international encoding system that is extensive enough to represent ALL the characters of ALL the world's alphabets. True False
True
When you open a file with the PrintWriter class, the class can potentially throw an IOException. Correct! True False
True
What will be the values of ans, x, and y after the following statements are executed? int ans = 0, x = 15, y =25; if ( x >= y) { ans = x + 10; x -=y; } else { ans = y + 10; y += x; } ans = 35, x = 15, y = 40 ans = 25, x = 15, y = 40 ans = 0, x = 15, y = 25 ans = 25, x = -10, y = 25
ans = 35, x = 15, y = 40
Enclosing a group of statements inside a set of braces creates a: boolean expression loop block of statements nothing, it is just for readability
block of statements
A loop that executes as long as a particular condition exists is called a(n): sentinel loop infinite loop count-controlled loop Correct! conditional loop
conditional loop
This type of loop is ideal in situations where you always want the loop to iterate at least once. while loop if statement for loop Correct!
do while
Which of the following correctly tests the char variable chr to determine whether it is NOT equal to the character B? if (chr < 'B') if (chr != "B") if (chr > 'B') if (chr != 'B')
if (chr != 'B')
When using the PrintWriter class, which of the following import statements would you write near the top of your program? import java.file.*; Correct! import java.io.*; import PrintWriter; import javax.swing.*;
import java.io.*;
Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file? Correct Answer int number = inputFile.nextInt(); You Answered int number = inputFile.readInt(); int number = inputFile.next(); int number = inputFile.integer();
int number = inputFile.nextInt();
Which of the following are pre-test loops? while, for, do-while for, do-while while, do-while Correct! while, for
while, for
Which of the following expressions will determine whether x is less than or equal to y? x <= y x >= y x =< y x > y
x<=y
If str1 and str2 are both Strings, which of the following will correctly test to determine whether str1 is less than str2? (1) (str1 < str2) (2) (str1.equals(str2) < 0) (3) (str1.compareTo(str2) < 0) 1, 2, and 3 will all work 2 2 and 3 3
(str1.compareTo(str2) < 0)
What would be the value of discountRate after the following statements are executed? double discountRate = 0.0; int purchase = 1250; char cust = 'N'; if (purchase > 1000) if (cust == 'Y') discountRate = .05; else discountRate = .04; else if (purchase > 750) if (cust == 'Y') discountRate = .04; else discountRate = .03; else discountRate = 0; .03 .04 0 .05
.04
What would be the value of discountRate after the following statements are executed? double discountRate; char custType = 'B'; switch (custType) { case 'A': discountRate = .08; break; case 'B': discountRate = .06; case 'C': discountRate = .04; default: discountRate = 0.0; }
0.0
What would be the value of discountRate after the following statements are executed? double discountRate; char custType = 'B'; switch (custType) { case 'A': discountRate = .08; break; case 'B': discountRate = .06; case 'C': discountRate = .04; default: discountRate = 0.0; } .08 .06 0.0 .04
0.0
What would be the value of bonus after the following statements are executed? int bonus, sales = 1250; if (sales > 1000) bonus = 100; if (sales > 750) bonus = 50; if (sales > 500) bonus = 25; else bonus = 0; 0 500 25 100
100
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; y += 20; } 90 110 Correct! 210 130
210
What would be the value of x after the following statements were executed? int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; break; default: x *= 3; } 20 30 5 25
25
What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); Correct! 28 24 35 30
28
Question 14 5 / 5 pts A loop that executes as long as a particular condition exists is called a(n): sentinel loop infinite loop count-controlled loop Correct! conditional loop
Conditional loop
This is a sum of numbers that accumulates with each iteration of a loop. Final total Galloping total Grand finale Correct! Running total
Running total
This is a control structure that causes a statement or group of statements to repeat. Block Body Correct! Loop Prefix mode
Loop
This variable controls the number of times that the loop iterates. Decrement variable Counter variable Correct! Loop control variable Running total
Loop control variable
What is the value of ans after the following code has been executed? int x = 40; int y = 40; int ans = 0; if (x = y) ans = x + 10; 80 30 50 No value, this is a syntax error.
No value, this is a syntax error
In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number "); int number = keyboard.nextInt(); while (number < 100 || number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); } Correct! Numbers in the range 100 - 500 Numbers less than 100 Numbers in the range 100 - 499 Numbers greater than 500
Numbers in the range 100 - 500
This is a value that signals when the end of a list of values has been reached. Final value End value Correct! Sentinel Terminal value
Sentinel
A local variable's scope always ends at the closing brace of the block of code in which it is declared. True False
True
Because the && operator performs short-circuit evaluation, your boolean expression will usually execute faster if the subexpression that is most likely false is on the left of the && operator. True False
True
In a switch statement, each of the case values must be unique. True False
True
The System.out.printf method formats a string and displays it in the console window. True False
True
The do-while loop must be terminated with a semicolon. Correct! True False
True
When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created. Correct Answer True You Answered False
True
A Boolean expression is one that is either: true or false Positive or negative x or y None of the above
True or False
In Java, when a character is stored in memory, it is actually stored as a(n): EBCDIC character code ASCII character code Morse code Unicode number
Unicode number
This type of loop allows the user to decide the number of iterations. Correct Answer User controlled loop You Answered Counter-controlled loop Infinite loop Dynamically executed loop
User controlled loop
A sentinel value ________ and signals that there are no more values to be entered. Correct! is a special value that cannot be mistaken as a member of the list is a different data type than the values being processed indicates the start of a list guards the list
is a special value that cannot be mistaken as a member of the list
What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--; Correct! x = 17, y = 4 x = 12, y = 5 x = 16, y = 4 x = 17, y = 5
x = 17, y = 4
What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++; x = 25, y = 8 x = 33, y = 8 x = 34, y = 9 Correct! x = 33, y = 9
x=33, y=9