ITP 120 Chapter 7 & 8
By default, Java initializes array elements to ________.
0
Java automatically stores a ________ value in all uninitialized static member variables.
0
Subscripting always starts with ________.
0
Given the following declaration: enum Tree ( OAK, MAPLE, PINE ) What is the ordinal value of the MAPLE enum constant?
1
A ragged array is ________.
a two-dimensional array where the rows have different numbers of columns
If a[] and b[] are two integer arrays, the expression a == b compares the array contents.
False
The key word this is the name of a reference variable that an object can use to refer to itself.
True
If an immutable class has a mutable object as a field, it is permissible for a method in the immutable class to return a reference to the mutable field.
False
Java limits the number of dimensions that an array can have to 15.
False
The key word this is the name of a reference variable that is available to all static methods.
False
You can declare an enumerated data type inside a method.
False
If numbers are a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array?
for (int row = 0; row < numbers.length; row++){for (int col = 0; col < numbers[row].length; col++)total += numbers[row][col];}
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
The ArrayList class is in the ________ package.
java.util
What would be the result after the following code is executed?int[] x = {23, 55, 83, 19};int[] y = {36, 78, 12, 24};for(int a = 0; a < x.length; a++){x[a] = y[a];y[a] = x[a];}
x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}
What does specify in the following statement?ArrayList nameList = new ArrayList();
It creates an instance of an array of ten long values.
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
The names of the enum constants in an enumerated data type must be enclosed in quotation marks.
False
A class whose internal state cannot be changed is known as a(n) ________ class.
Immutable
Is the following class mutable or immutable? public final class Status { private final String code; public Status(String c) { code = c; } public String getCode() { return code; } }
Immutable
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();
An enumerated data type is actually a special type of class.
True
Once an array is created, its size cannot be changed.
True
The ________ method is used to insert an item into an ArrayList.
add
You cannot use the == operator to compare the contents of ________.
objects
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
All fields in an immutable class should be declared ________.
private and final
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
A declaration for an enumerated type begins with the ________ key word.
enum
The whole-part relationship created by object aggregation is more often called a(n) ________ relationship.
has a
An object's ________ is simply the data that is stored in the object's fields at any given moment.
state
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
If you attempt to perform an operation with a null reference variable ________.
the program will terminate
When you make a copy of the aggregate object and of the objects that it references, ________.
you are performing a deep copy
Java does not limit the number of dimensions an array may have.
True
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.
When an array is passed to a method ________.
All of these are true
In Java it is possible to write a method that will return ________.
Any of these
Which of the following is not true about static methods?
It is necessary for an instance of the class to be created to execute the method.
If you write a toString method to display the contents of an object, object1, for a class, Class1, then the following two statements are equivalent: System.out.println(object1); System.out.println(object1.toString());
True
Objects in an array are accessed with subscripts, just like any other data type in an array.
True
When an object is passed as an argument, it is actually a reference to the object that is passed.
True
When an object reference is passed to a method, the method may change the values in the object.
True
It is common practice to use a ________ variable as a size declarator.
final
A class that provides mutators to change the values of the class's fields is known as a(n) ________ class.
mutable
What does the following statement do?double[] array1 = new double[10];
It does all of these.
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
public static void passArray(int [][])
You can use the ________ method to replace an item at a specific location in an ArrayList.
set
enum constants have a toString method.
True
In Java, you do not use the new operator when you use a(n) ________.
initialization list
Each array in Java has a public field named ________ that contains the number of elements in the array.
length
If numbers are a two-dimensional array, which of the following would give the number of columns in row r?
numbers[r].length
The ________ method removes an item from an ArrayList at a specific index.
remove
When a reference variable is passed as an argument to a method ________.
the method has access to the object that the variable references
The JVM periodically performs the ________ process to remove unreferenced objects from memory.
garbage collection
Given the following declaration: enum Tree ( OAK, MAPLE, PINE ) What is the fully-qualified name of the PINE enum constant?
Tree.PINE
When an array of objects is declared but not initialized, the array values are set to null.
True
What will be the results after the following code is executed?int[] array1 = new int[25];... // Code that will put values in array1int value = array1[0];for (int a = 1; a < array1.length; a++){if (array1[a] < value)value = array1[a];}
value contains the lowest value in array1
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.
If the following is from the method section of a UML diagram, which of the statements below is true? + add(object2:Stock) : Stock
This is a public method named add that accepts and returns references to objects in the Stock class.
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
The only limitation that static methods have is ________.
They cannot refer to non-static members of the class
If the following is from the method section of a UML diagram, which of the statements below is true? + equals(object2:Stock) : boolean
This is a public method that accepts a Stock object as its argument and returns a boolean value.
To determine if two arrays are equal you must compare each of the elements of the two arrays.
True
Which of the following for loops is valid, given the following declaration?String[] names = {"abc", "def", "ghi", "jkl"};
for (int i = 0; i < names.length; i++)System.out.println(names[i].length());
If you have defined a class, SavingsAccount, with a public static data member named numberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will assign numberOfAccounts to numAccounts?
numAccounts = SavingsAccount.numberOfAccounts;
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();
A(n) ________ is used as an index to pinpoint a specific element within an array.
subscript
What will be the value of x[8] after the following code is executed?final int SUB = 12;int[] x = new int[SUB];int y = 20;for(int i = 0; i < SUB; i++){x[i] = y;y += 5;}
60
What will be the value of x[8] after the following code is executed?final int SUB = 12;int[] x = new int[SUB];int y = 100;for(int i = 0; i < SUB; i++){x[i] = y;y += 10;}
180
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
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.
Which of the following ArrayList class methods is used to insert an item at a specific location in an ArrayList?
add
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
What would be the result after the following code is executed?final int SIZE = 25;int[] array1 = new int[SIZE];... // Code that will put values in array1int value = 0;for (int a = 0; a <= array1.length; a++){value += array1[a];}
This code would cause the program to crash.
A class's static methods do not operate on the fields that belong to any instance of the class.
True
A single copy of a class's static field is shared by all instances of the class.
True
A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.
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 a 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
Both instance fields and instance methods are associated with a specific instance of a class, and they cannot be used until an instance of the class is created.
True
Declaring an array reference variable does not create an array.
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