COSC240 Exam 2
In general, there are two types of files which are
Text and Binary
________ is the term for the relationship created by object aggregation.
"Has a"
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
1
What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 100; }
100
How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!");
12
What will be the value of x after the following code is executed? int x, y = 15; x = y--;
15
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; }
210
What will be the value of x after the following statements are executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y;
40
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100);
5
What is the value of scores[2][3] in the following array? int[][] scores = { {88, 80, 79, 92}. {75, 84, 93, 80}, {98, 95, 92, 94}, {91, 84, 88, 96} };
94
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
When you write an enumerated type declaration, you
All of these
When an array is passed to a method
All of these are true.
Which of the following statements is(are) true about this code? final int ARRAY_SIZE = 10; long[] array1 = new long[ARRAY_SIZE];
All of these are true.
What would be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5};
An array of 6 values, ranging from 0 through 5 and referenced by the variable x will be created.
CRC stands for
Class, Responsibilities, Collaborations.
A loop that executes as long as a particular condition exists is called a(n) ________ loop.
Conditional
A loop that repeats a specific number of times is known as a(n)
Count-Controlled
The ________ loop is ideal in situations where you always want the loop to iterate at least once.
Do-While
You can use the enum key word to
F Both of these
A sorting algorithm is used to locate a specific item in a larger collection of data
False
In a for loop, the control variable is always incremented.
False
The Java compiler will display an error message when it processes a statement that uses an invalid subscript.
False
The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.
False
The names of the enum constants in an enumerated data type must be enclosed in quotation marks.
False
The while loop is always the best choice in situations where the exact number of iterations is known.
False
When an array of objects is declared but not initialized, the array values are set to 0.
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
You can declare an enumerated data type inside a method.
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);
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
What does the following code do? Scanner keyboard = new Scanner(System.in); String filename; System.out.print("Enter the filename: "); filename = keyboard.readString(); PrintWriter outFile = new PrintWriter(filename);
It allows the user to enter the name of the file that data will be written to
What does the following statement do? double[] array1 = new double[10];
It does all of these
What does specify in the following statement? ArrayList nameList = new ArrayList();
It specifies that only String objects may be stored in the ArrayList object.
Assume the following declaration exists: enum Tree ( OAK, MAPLE, PINE ) What will the following code display? System.out.println(Tree.OAK);
OAK
An item that separates other items is known as a
Partition
A ________ loop will always be executed at least once.
Post Test
If you have defined a class, SavingsAccount, with a public static method, getNumberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will call the getNumberOfAccounts method?
SavingsAccount.getNumberOfAccounts();
A ________ is a value that signals when the end of a list of values has been reached.
Sentinel
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
You can use the ________ method to replace an item at a specific location in an ArrayList.
Set
Assume the class BankAccount has been created and the following statement correctly creates an instance of the class. BankAccount account = new BankAccount(5000.00); What is true about the following statement? System.out.println(account);
The account object's toString method will be implicitly called.
What will be the result after the following code is executed? final int ARRAY_SIZE = 5; float[] x = float[ARRAY_SIZE]; for (i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; }
The code contains a syntax error and will not compile.
Given the following two-dimensional array declaration, which statement is true? int[][] numbers = new int[6][9];
The numbers array has 6 rows and 9 columns.
What would be the result after the following code is executed? int[] numbers = {40, 3, 5, 7, 8, 12, 10}; int value = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < value) value = numbers[i]; }
The value variable will contain the lowest value in the numbers array.
What would be the result after the following code is executed? int[] numbers = {50, 10, 15, 20, 25, 100, 30}; int value = 0; for (int i = 1; i < numbers.length; i++) value += numbers[i];
The value variable will contain the sum of all the values in the numbers array.
If the following is from the method section of a UML diagram, which of the statements shown is true? + equals(object2:Stock) : boolean
This is a public method that accepts a Stock object as its argument and returns a boolean value.
Given the following declaration: enum Tree ( OAK, MAPLE, PINE ) What is the fully-qualified name of the PINE enum constant?
Tree.PINE
A class's static methods do not operate on the fields that belong to any instance of the class.
True
A file must always be opened before using it and closed when the program is finished using it.
True
A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.
True
An ArrayList object automatically expands in size to accommodate the items stored in it.
True
An enumerated data type is actually a special type of class.
True
An instance of a class does not have to exist in order for values to be stored in a class's static fields.
True
Any items typed on the command-line, separated by space, after the name of the class, are considered to be one or more arguments that are to be passed into the main method.
True
If a class has a method named finalize, it is called automatically just before an instance of the class is destroyed by the garbage collector.
True
If you write a toString method for a class, Java will automatically call the method any time you concatenate an object of the class with a string.
True
Java does not limit the number of dimensions an array may have.
True
Objects in an array are accessed with subscripts, just like any other data type in an array.
True
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.
True
The do-while loop is ideal in situations where you always want the loop to iterate at least once.
True
The key word this is the name of a reference variable that an object can use to refer to itself.
True
To determine if two arrays are equal you must compare each of the elements of the two arrays.
True
When an object is passed as an argument, it is actually a reference to the object that is passed.
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
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
You can use the PrintWriter class to open a file and write data to it.
True
enum constants have a toString method.
True
You cannot use the fully-qualified name of an enum constant for
a case expression.
When a method's type is an object, what is actually returned by the calling program?
a reference to an object of that class
For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"};
a reference to the String object containing "ghi"
A ragged array is
a two-dimensional array where the rows have different numbers of columns.
Which of the following ArrayList class methods is used to insert an item at a specific location in an ArrayList?
add
A static field is created by placing the key word static
after the access specifier and before the field's data type.
When a method in the ________ class returns a reference to a field object, it should return a reference to a copy of the field object to prevent security holes.
aggregate
Select all that apply. Which of the following steps is normally performed by a for loop?
all of the above
An array of String objects
consists of an array of references to String objects.
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 declaration for an enumerated type begins with the ________ key word.
enum
The JVM periodically performs the ________ process to remove unreferenced objects from memory
garbage collection
Overloading is
having two or more methods with the same name but different signatures
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?
import java.io.*;
Which of the following import statements is required in order to use the ArrayList class?
import java.util.ArrayList;
A class that is defined inside another class is called a(n)
inner class
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();
Which of the following is a valid declaration for a ragged array with five rows but no columns?
int[][] ragged = new int[5][];
A search algorithm
is used to locate a specific item in a collection of data.
When the this variable is used to call a constructor
it must be the first statement in the constructor making the call.
To return an array of long values from a method, which return type should be used for the method?
long[]
By default, a reference variable that is an instance field is initialized to the value
null
If numbers is a two-dimensional array, which of the following would give the number of columns in row r?
numbers[r].length
When a field is declared static there will be
only one copy of the field in memory.
Enumerated types have the ________ method which returns the position of an enum constant in the declaration list.
ordinal
Of the following, which would be considered the no-arg constructor for the Rectangle class?
public Rectangle()
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
public static void passMyArray(int[][] myArray)
The ________ method removes an item from an ArrayList at a specific index.
remove
Before entering a loop to compute a running total, the program should first
set the accumulator variable to an initial value, often zero.
Which method is used to determine the number of items stored in an ArrayList object?
size
A(n) ________ is used as an index to pinpoint a specific element within an array
subscript
Which of the following is the method you can use to determine whether a file exists?
the File class's exists method
Given the following method header, what will be returned from the method? public Rectangle getRectangle()
the address of an object of the Rectangle class
In order to do a binary search on an array
the array must first be sorted.
Which of the following can you use to compare two enum data values?
the equals and compareTo methods
A method's signature consists of
the method name and the parameter list.
The only limitation that static methods have is
they cannot refer to nonstatic members of the class.
Which of the following methods returns a string representing all of the items stored in an ArrayList object?
toString
To get the name of a calling enum constant
use the toString method
The sequential search algorithm
uses a loop to sequentially step through an array, starting with the first element.
Java provides a mechanism known as a ________ which makes it possible to write a method that takes a variable number of arguments.
variable-length argument list
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())
The "has a" relationship is sometimes called a(n) ________ because one object is part of a greater whole.
whole-part relationship
The binary search algorithm
will cut the portion of the array being searched in half each time it fails to locate the search value.
A partially filled array is normally used
with an accompanying integer value that holds the number of items stored in the array.
If object1 and object2 are objects of the same class, to make object2 a copy of object1
write a method for the class that will make a field by field copy of object1 data members into object2 data members.
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
When you make a copy of the aggregate object and of the objects that it references,
you are performing a deep copy