Programming Exam #2
Select all that apply. Any method that calls a method with a throws clause in its header must ________.
handle the potential exception and have the same throws clause
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?
import java.io.*;
Subscripting always starts with ________.
0
A special variable that holds a value being passed into a method is called a(n) ________.
parameter
Before entering a loop to compute a running total, the program should first ________.
set the accumulator variable to an initial value, often zero
A(n) ________ is used as an index to pinpoint a specific element within an array.
subscript
The process of breaking a problem down into smaller pieces is sometimes called ________.
the divide and conquer method
When an individual element of an array is passed to a method ________.
the method does not have access to the original array
When you pass an argument to a method you should be sure that the argument's type is compatible with ________.
the parameters variables data type
Java does not limit the number of dimensions an array may have.
True
No statement outside the method in which a parameter variable is declared can access the parameter by its name.
True
To determine if two arrays are equal you must compare each of the elements of the two arrays.
True
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
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 file must always be opened before using it and closed when the program is finished using it. Correct!
True
Any method that calls a method with a throws clause in its header must either handle the potential exception or have the same throws clause.
True
Java provides a set of simple unary operators designed just for incrementing and decrementing variables.
True
Methods are commonly used to break a problem into small manageable pieces.
True
Once an array is created, its size cannot be changed.
True
When the continue 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.
True
You must have a return statement in a value-returning method.
True
The ________ indicates the number of elements the array can hold.
arrays data type
To create a method, you must write its ________.
Definition
A sorting algorithm is used to locate a specific item in a larger collection of data.
False
If a[] and b[] are two integer arrays, the expression a == b compares the array contents
False
In a for loop, the control variable cannot be initialized to a constant value and tested against a constant value.
False
In a for loop, the control variable is always incremented.
False
In the method header the static method modifier means the method is available to code outside the class.
False
In the method header, the method modifier public means that the method belongs to the class, not a specific object.
False
Java limits the number of dimensions that an array can have to 15
False
Only constants and variables may be passed as arguments to methods.
False
The while loop is always the best choice in situations where the exact number of iterations is known.
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.
False
Select all that apply. Which of the following statements is(are) true about this code?final int ARRAY_SIZE = 10;long[] array1 = new long[ARRAY_SIZE];
It creates an instance of an array of ten long values.
It is common practice to use a ________ variable as a size declarator.
final
The ________ loop is ideal in situations where the exact number of iterations is known.
for
If a loop does not contain, within itself, a valid way to terminate, it is called a(n) ________ loop
infinite
Select all that apply. Which of the following types of values can be passed to a method that has an int parameter variable?
int
A random number, created as an object of the Random class, is always a(n) ________.
integer
Each repetition of a loop is known as a(n) ________.
iteration
A ________ is a part of a method that contains a collection of statements that are performed when the method is executed.
method body
A ________ loop will always be executed at least once.
posttest
Which of the following is not a part of a method call?
return type
The header of a value-returning method must specify ________.
the data type of the return value
A parameter variable's scope is ________.
the method in which the parameter is declared
An item that separates other items is known as a ________.
delimiter
A method ________.
may have zero or more parameters
A parameter variable's scope is the method in which the parameter is declared.
True
The sequential search algorithm ________.
uses a loop to sequentially step through an array, starting with the first element
You should always document a method by writing comments that appear ________.
just before the method's definition
Each array in Java has a public field named ________ that contains the number of elements in the array.
length
To return an array of long values from a method, which return type should be used for the method?
long[]
The do-while loop must be terminated with a semicolon.
True
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.
True
In all but very rare cases, loops must contain, within themselves ________.
a way to terminate
The variable used to keep a running total in a loop is called a(n) ________.
accumulator
Methods are commonly used to ________.
break a program down into small manageable pieces
If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]?
0 through 14
The ________ loop is ideal in situations where you always want the loop to iterate at least once.
do-while
In Java, you do not use the new operator when you use a(n) ________.
initialization list
A partially filled array is normally used ________.
with an accompanying integer value that holds the number of items stored in the array
An array can hold multiple values of several different types of data simultaneously.
False
Which of the following statements opens a file named MyFile.txt and allows you to read data from it?
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents?
FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);
In memory, an array of String objects ________.
consists of an array of references to String objects
Local variables can be initialized with ________.
constants, parameter values, the result of an arithmetic operation.
Which of the following statements will create an object from the Random class?
Random myNumber = new Random();
If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?
control is returned to method C
A loop that repeats a specific number of times is known as a(n) ________ loop
count-controlled
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?
int number = inputFile.nextInt();
To document the return value of a method you can use a ________.
@return tag
Values that are sent into a method are called ________.
arguments
In a @return tag statement the description
describes the return value
Which of the following expressions will generate a random number in the range of 1 through 10?
myNumber = randomNumbers.nextInt(10) + 1;
Select all that apply. Which method of the Random class will return a random number within the range of 0.0 and 1.0?
nextDouble(), nextFloat()
If numbers is a two-dimensional array, which of the following would give the number of columns in row r?
numbers[r].length
A loop that executes as long as a particular condition exists is called a(n) ________ loop.
conditional
When writing documentation comments for a method, you can provide a description of each parameter by using a ________.
@param tag
What does the following statement do?double[] array1 = new double[10];
It declares array1 to be a reference to an array of double values., It will allow valid subscripts in the range of 0 through 9., It creates an instance of an array of ten double values.
A(n) ________ is a special value that cannot be mistaken as a member of a list of data items and signals that there are no more data items to be processed.
Sentinel
A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.
True
A value-returning method can return a reference to a non-primitive type.
True
Constants, variables, and the values of expressions may be passed as arguments to a method.
True
Declaring an array reference variable does not create an array.
True
The do-while loop is ideal in situations where you always want the loop to iterate at least once.
True
The expression in a return statement can be any expression that has a value.
True
The while loop has two important parts: (1) a boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.
True
Two general categories of methods are void methods and value-returning methods.
True
When an array of objects is declared but not initialized, the array values are set to null.
True
When a method tests an argument and returns a true or false value, it should return ________.
a boolean value
In a general sense, a method is ________.
a collection of statements that perform a specific task
If you attempt to use a local variable before it has been given a value, ________.
a compiler error will occur
n the following code, System.out.println(num) is an example of ________. double num = 5.4; System.out.println(num); num = 0.0;
a void method
When an object, such as a String, is passed as an argument it is ________.
actually a reference to the object that is passed
A value-returning method must specify ________ as its return type in the method header.
any valid data type.
All @param tags in a method's documentation must
appear after the general description of the method
Values stored in local variables ________.
are lost between calls to the method in which they are declared
Java performs ________, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for the array.
array bounds checking
When an argument value is passed to a method, the receiving parameter variable is ________.
declared in the method header inside the parentheses
Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt?PrintWriter diskOut = new PrintWriter("DiskFile.txt");
diskOut.println("Calvin");
A search algorithm ________.
is used to locate a specific item in a collection of data
When an array is passed to a method ________.
it is passed just as any other object would be passed, the method has direct access to the original array, a reference to the array is passed
When an argument is passed to a method ________.
its value is copied into the method's parameter variable
Which is a control structure that causes a statement or group of statements to repeat?
loop
The variable that controls the number of times a loop iterates is known as a ________.
loop control variable
Select all that apply. Local variables
lose the values stored in them between calls to the method in which the variable is declared
The lifetime of a method's local variable is ________.
only while the method is executing
In the header, the method name is always followed by ________.
parentheses
Select all that apply. Which of the following are benefits of using methods in programming?
programs are simplified
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
public static void passArray(int [][])
Which of the following is not part of a method header?v
semicolon
A ________ is a value that signals when the end of a list of values has been reached.
sentinel
Given that String[] str has been initialized, to get a copy of str[0] with all the characters converted to uppercase, you would use the ________ statement.
str[0].toUpperCase();
Which of the following is the method you can use to determine whether a file exists?
the File class's exists method
Select all that apply. Which of the following steps is normally performed by a for loop?
update the control variable during each iteration, test the control variable by comparing it to a maximum or minimum value, terminate when the control variable reaches its maximum or minimum value, initialize the control variable to a starting value.
The ________ loop allows the user to decide on the number of iterations.
user controlled loop
Which type of method performs a task and sends a value back to the code that called it?
value-returning
A ________ type of method performs a task and then terminates.
void
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops is the correct way to read data from the file until the end of the file is reached?
while (inputFile.hasNext()) { ... }
Which of the following are pre-test loops?
while, for