Exam1-Chapter (1-4)
When two Strings are compared using the compareTo method, the cases of the two strings are not considered.
false
Which of the following is valid?
float w; w = 1.0f;
This type of loop is ideal in situations where the exact number of iterations is known.
for loop
If chr is a character variable, which of the following if statements is written correctly?
if (chr == 'a')
If a loop does not contain within itself a way to terminate, it is called a(n):
infinite loop
A sentinel value ________ and signals that there are no more values to be entered.
is a special value that cannot be mistaken as a member of the list
Which Scanner class method reads a String?
nextLine()
In the following code, what values could be read into number to terminate the while loop? while ( number < 100 || number > 500) { System.out.println("Enter another number: "); number = keyboard.nextInt();
numbers in ranges of 100-500.
This type of loop will always be executed at least once.
post-test loop
A local variable's scope always ends at the closing brace of the block of code in which it is declared.
true
In a switch statement, each of the case values must be unique.
true
The expression tested by an if statement must evaluate to:
true or false
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?
while (inputFile.hasNext()) { }
If x has been declared an int, which of the following statements is invalid?
x = 1,000;
What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++;
x = 33, y = 9
To do a case insensitive compare which of the following could be used to test the equality of two strings, str1 and str2?
(str1.equalsIgnoreCase(str)) and (str1.compareToIgnoreCase(str2) == 0)
What would be the value of discountRate after the following statements are executed? double discountRate; char cusType = 'B'; switch (custType) { case 'A': discountRate = .08; break; case 'B' discountRate = 0.6; case 'C' discountRate = .04; default : discountRate = 0.0; }
0.0
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x>100);
1
What does the following code display? double x = 12.3798146; System.out.printf("%.2f\n", x);
12.38
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
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)
2 and 3
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;
21.00 180/60.0 * 7.00 = 21.00
What is the result of the following expression? 25/4+4*10%3
7
What does the following code display? int d = 9, e = 12; System.out.printf("%d %d\n", d, e);
9 12
In all but rare cases, loops must contain within themselves:
A way to terminate.
In Java, it is standard practice to capitalize the first letter of:
Class names
A loop that repeats a specific number of times is known as a(n):
Counter-controlled loop.
Which of the following statements correctly creates a Scanner object for keyboard input?
Scanner keyboard = new Scanner(System.in);
Which one of the following methods would you use to convert a string to a double?
Double.parseDouble()
Which of the following will open a file named MyFile.txt and allow you to read data from it?
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?
FileWriter fwriter = new FileWriter("MyFile.txt", true);PrintWriter outFile = new PrintWriter(fwriter);
Which of the following is not a rule that must be followed when naming identifiers?
Identifiers can contain spaces.
The switch statement is a:
Multiple alternative decision structure
What would be printed out as a result of the following code? System.out.println("The quick brown fox" + "jumped over the \n" "slowing moving hen.");
Nothing; This is errors
In the following Java statement what value is stored in the variable name? String name = "John Doe";
The memory address where "John Doe" is located
A file must always be opened before using it and closed when the program is finished using it.
True
The if/else statement will execute one group of statements if its boolean expression is true or another group if its boolean expression is 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.
True
A for loop normally performs which of these steps?
a) Initializes a control variable to a starting value b) Tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value c) Updates the control variable during each iteration
A block of code is enclosed in a set of:
braces { }