CS 1054 Exam 2
TRUE OR FALSE: Finding a minimum value requires that every element in the array be considered.
TRUE
TRUE OR FALSE: If you try to access an element outside of the valid index range for an array, an exception will be thrown.
TRUE
TRUE OR FALSE: Objects made from wrapper classes are immutable.
TRUE
TRUE OR FALSE: Swapping two elements in an array requires three assignment statements.
TRUE
TRUE OR FALSE: Switch statements can only test equality; they cannot make relational comparisons like greater than (>).
TRUE
TRUE OR FALSE: The print and println methods can be used to write data to a PrintWriter object.
TRUE
TRUE OR FALSE: Traversing an array or collection means accessing each element one at a time.
TRUE
TRUE OR FALSE: in the palindromes program, the inner loop evaluates one string to see if its a palindrome
TRUE, the outer loop allows multiple strings to be evaluated
Unicode
The character set Java uses to represent characters.
What will happen if you try to add a String object to an ArrayList created to hold Person objects?
The compiler will issue an error message.
root node
The node that contains all other nodes in a scene.
What happens when an element is removed from an ArrayList?
The remaining elements are shifted to close the gap.
lexicographic ordering
The technique of ordering characters based on a character set.
element type
The type of data that an array is declared to hold.
What does the following statement print? System.out.println((count < 0) ? "Invalid" : count);
The value of count, or "Invalid" if count is negative.
character encoding
The way in which those characters are stored in memory
If the value of weight is 7, what does the following statement print? System.out.println("The weight is " + ((weight % 2 == 0) ? "even" : "odd"));
The weight is odd
What output is produced by the following code? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) System.out.print("*"); System.out.println(); }
***** ***** ***** ***** *****
What are the contents of the items array after the following code is executed? int[] items = {10, 20, 30, 40, 50}; for (int item : items) { item = item * 2; }
10, 20, 30, 40, 50
How many values can the heights array hold?
100
What value is printed by the following code? int count = 0; for (int i = 0; i < 5; i++) count += 3; System.out.println(count);
15
How many comparisons are necessary to find the value 43 in the following list using a binary search? 13 25 28 30 39 43 44 58 66 70 78 81 86 88 95
3
If the value of num is 7 and the value of max is 10, what is the value of num after the following statement is executed? num = (max < num) ? max : max - num;
3
What is the output of the following code? int[] list = {2, 4, 6, 8, 10}; int num = 0; for (int val : list) { num = num + val; } System.out.println(num);
30
How many comparisons are necessary to find the value 86 in the following list using a binary search? 13 25 28 30 39 43 44 58 66 70 78 81 86 88 95
4
What number is printed by the following code? int num = 20; switch (num) { case 1: num = num + 1; break; case 12: num = num + 1; num = num * 2; case 20: num = num * 2; case 25: num = num + 1; break; case 999: num = 20; break; default: num = 999; } System.out.println(num);
41
layout pane
A container that manages the visual presentation of the nodes it contains.
character set
A list of characters used by a programming language.
What would happen if there were more than 100 values in the input file?
A run-time error would occur.
ASCII
A small character set containing primarily English characters and basic symbols.
array initialization list
A technique for creating an array and assigning it some initial values.
Unicode escape sequence
A way to represent non-ASCII Unicode characters in a program.
index
An integer value that is used to access a particular element in an array.
array
An object that holds a set of values that can be accessed using a numeric index.
What is the proper type designation for an ArrayList of Person objects?
ArrayList<Person>
Why is the input data for this program stored in an array after being read?
Because 2 passes through the data are needed to compute the result.
Why was the heights array declared to hold double values?
Because the input file contains floating-point values.
bounds error
Caused when you try to access an array element that doesn't exist.
non-printable characters
Characters (like newline) that have no visual symbol.
supplementary characters
Characters that don't map to the fixed-width, 16-bit Unicode encoding.
TRUE OR FALSE: A Scanner object can be used to both read and write text files in Java.
FALSE
TRUE OR FALSE: A binary search only works if the number of elements to search is odd.
FALSE
TRUE OR FALSE: A binary search works best on an unsorted list of values.
FALSE
TRUE OR FALSE: A for-each loop can be used to fill the values in an array.
FALSE
TRUE OR FALSE: A linear search examines every element in the array.
FALSE
TRUE OR FALSE: All Java wrapper classes are part of the java.util package.
FALSE
TRUE OR FALSE: An array of objects cannot be created with an initialization list.
FALSE
TRUE OR FALSE: Arrays can hold primitive types, but not objects.
FALSE
TRUE OR FALSE: Each value of a case can be a constant, variable, or expression.
FALSE
TRUE OR FALSE: If an array can hold N values, the valid indexes for that array range from 0 to N.
FALSE
TRUE OR FALSE: It's important to understand array processing to understand how to use an ArrayList.
FALSE
TRUE OR FALSE: The body of a for loop is always executed at least once.
FALSE
TRUE OR FALSE: The control variable must be declared in the for loop header.
FALSE
TRUE OR FALSE: The expression in a switch statement can be an int or double value.
FALSE
TRUE OR FALSE: The increment section of the for loop header always adds 1 to the loop control variable.
FALSE
TRUE OR FALSE: To change the algorithm for finding a minimum value into one that finds a maximum value, the condition of the for loop would change.
FALSE
What output is printed by the following code?What output is printed by the following code? String str = "Refactor Java"; int index = 0; int num = 0; while (index < str.length()) { if (str.charAt(index) == 'a') num++; index++; } System.out.println("Num: " + num);
Num: 3
Which one of the following constants and methods is NOT part of the Integer wrapper class?
POSITIVE_INFINITY
What is the element type of the athletes array in the ArrayOfPersonDemo program?
Person
What is the type of the variable athletes in the ArrayOfPersonDemo program?
Person[]
Which of the following declares a valid PrintWriter object that represents an output file named myOutput.txt?
PrintWriter out = new PrintWriter("myOutput.txt");
What does the following for loop do? for (int num = 3; num <= 30; num += 3) System.out.println(num);
Prints the multiples of 3 between 3 and 30 (3, 6, 9, etc.)
Which of the following declares a valid Scanner object to read an input file named myData.txt?
Scanner in = new Scanner(new File("myData.txt"));
What is the element type of the adjectives array in the ArrayOfStringDemo program?
String
What is the type of the variable adjectives in the ArrayOfStringDemo program?
String[]
TRUE OR FALSE: An array that holds objects is, itself, an object.
TRU
TRUE OR FALSE: A break statement is used at the end of each case of a switch statement to keep processing from continuing into the next case.
TRUE
TRUE OR FALSE: A for loop could always be written as an equivalent while loop.
TRUE
TRUE OR FALSE: A for-each loop can be used to compute the sum of all values in an array.
TRUE
TRUE OR FALSE: A for-each loop cannot be used to print the elements in an array backwards.
TRUE
TRUE OR FALSE: A for-each loop cannot be used to set the values in an array.
TRUE
TRUE OR FALSE: A generic class operates on a generic type, which is specified when an object is created.
TRUE
TRUE OR FALSE: A switch statement can always be rewritten as a series of nested if statements.
TRUE
TRUE OR FALSE: A wrapper class "wraps" a primitive value as an object.
TRUE
TRUE OR FALSE: An ArrayList cannot store primitive types such as int or double.
TRUE
TRUE OR FALSE: An ArrayList is a collection that can be processed using a for-each loop.
TRUE
TRUE OR FALSE: An array of objects is really an array of object references.
TRUE
TRUE OR FALSE: An output file should always be closed when you're done using it or data may be lost.
TRUE
TRUE OR FALSE: File I/O operations may throw checked exceptions.
TRUE
what kind of statement is the if statement
a conditional statement
what causes an infinite loop
a loop condition that is never false
What are the two terms that mean to automatically convert from a primitive value to a corresponding wrapper object and vice versa.
autoboxing and unboxing
TRUE OR FALSE: the string I prefer pi would be considered a palindrome by the original version of the program
false, even though it's in all lower case letters, the spaces would throw it off
TRUE OR FALSE: the number of times the while loop will be executed can be calculated before the loop begins
false, it depends on how long it takes the user to guess the target value
TRUE OR FALSE: it's impossible for user to guess the target value in one guess
false, it's possible but not likely
TRUE OR FALSE: the nested if statement in this program had to be written to check for the correct guess last
false, the logic of the if statement could be written to check any of the possibilities in any order
true or false: there is no way to stop an infinite loop
false, the program must be interrupted by pressing ctrl-c or ctrl break or by closing the run window
Which of the following correctly shows the general structure of a for-each loop header?
for (type variable-name : array-or-collection)
Going from left to right, what are the three sections of a for loop header?
initialization, condition, increment
When performing a binary search, how many comparisons are required to find a value in a list of N values, in the worst case?
log2N + 1
What string is printed by the following code if it is executed when the value of finger is 3? switch (finger) { case 1: System.out.println("Thumb"); break; case 2: System.out.println("Index finger"); break; case 3: System.out.println("Middle finger"); break; case 4: System.out.println("Ring finger"); break; case 5: System.out.println("Pinky"); break; default: System.out.println("Not a finger!"); }
middle finger
Which assignment statement is functionally equivalent to the following if statement? if (num1 > total) num1 = total; else num1 = 0;
num1 = (num1 > total) ? total : 0;
Which method call will replace the element at index 3 of an ArrayList of String objects called stuff?
stuff.set(3, "new stuff")
TRUE or FALSE: The body of a while loop may never execute.
true
TRUE or FALSE: in some cases, a sentinel value can be used to signal the end of input
true
TRUE or FALSE: using a while loop to check each input value of correctness is a form of input validation
true
TRUE OR FALSE: This program would still work if the initial value of guess were set to -1 instead of -999.
true The value only needs to be outside of the target range.
TRUE OR FALSE: the inner loop will stop immediately if the string is determined not to be a palindrome
true, the boolean variable palindrome keeps track of it
TRUE OR FALSE: a potential palindrome is evaluated by comparing characters at both ends and working inwards
true, the indexes left and right determine which characters are compared
repetition statements
while for for-each do-while