Quiz 3

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is an enhanced loop? What is it aka?

The enhanced for loop is a for loop that iterates through each element in an array or Collection. An enhanced for loop is also known as a for each loop. The enhanced for loop declares a new variable that will be assigned with each successive element of a container object, such as an array or ArrayList. The enhanced for loop only allows elements to be accessed forward from the first element to the last element.

An interface contains method declarations, as opposed to method definitions. True False

True An interface provides method declarations, which only consist of the method's return type, name, and parameters.

A dictionary has been stored in a file, one word per line. A perfect sized array can be used if the contents of this dictionary cannot be changed by the program (e.g. by adding or deleting words).

True Since the size of the array can be determined by the number of words in the file, a perfect sized array is a good approach.

The method signature void shuffleCards(int[] cards) indicates that cards is a perfect size array. True False

True: Since this method has no parameter for the size of the array, the array must be perfect size.

String[] bookTitles = new String[200]; int bookTitlesSize = 0; All correct. 200 should be a constant. The identifier bookTitlesSize is too long. bookTitleSize should be initialized to 200.

Using literals, like 200, for an oversize array size is bad practice, as isolated numeric literals are hard to understand. Using a constant, like final BOOK_TITLES_CAPACITY = 200; gives the value a meaningful name that yields more understandable code. 200 should be a constant

How can A method signature can tell a programmer whether a perfect size or oversize array is expected as an argument?

A method that expects a perfect size array as an argument will have one parameter for the array reference, but no parameter for the array size. Ex: int findIndexMax(String[] arrayReference) expects a perfect size array. A method that expects an oversize array as an argument will have two parameters to pass the array: one for the array reference and one for the array size. Ex: int binarySearch(String[] arrayReference, int arraySize, String searchKey) expects an oversize array.

What is a perfect size array?

A perfect size array is an array where the number of elements is exactly equal to the memory allocated. Ex: The maximum temperature for each day in the week could be stored in an array with 7 integer values.

What is an ArrayList?

An ArrayList is an ordered list of reference type items that comes with Java. Each item in an ArrayList is known as an element. The statement import java.util.ArrayList; enables use of an ArrayLis

What index must ArrayLists have?

An ArrayList's index must be an integer type. The index cannot be a floating-point type, even if the value is 0.0, 1.0, etc.

What is an array and an element?

An array is an ordered list of items of a given data type. Each item in an array is called an element. An array is an indexed reference data type with a size that is fixed at its creation. an array is an OBJECT

Explain what happens when an array passes through a method.

An array is passed to a method by passing a reference to the array. The array reference is copied to the method's parameter, so a method can modify the elements of an array argument.

What does an array reference variable do and what does the new operator do?

An array reference variable can refer to arrays of various sizes. The new operator creates space in memory to store the array with the specific number of elements.

What is an oversized array?

An oversize array is an array where the number of elements used is less than or equal to the memory allocated. Since the number of elements used in an oversize array is usually less than the array's length, a separate integer variable is used to keep track of how many array elements are currently used.

What is a common error when it comes to ArrayList Declaration?

ArrayList does not support primitive types like int, but rather reference types like Integer. A common error among beginners is to declare an ArrayList of a primitive type like int, as in ArrayList<int> myVals, yielding a compilation error: "unexpected type, found : int, required: reference."

Why do arrays behave differently in methods?

Arrays behave differently in methods because a copy of the array reference is stored in the method stack frame, and the array elements are stored on the heap. The array references in the stack frames of calling and called methods refer to the same array and have access to the array elements. Both methods can use and modify array elements.

final int CAPACITY = 20; int size = 0; int[] messageCount = new int[CAPACITY]; CAPACITY should not be final. Variable CAPACITY and size should use names linked to the array name. messageCount should be constructed with size elements instead of CAPACITY elements. CAPACITY should be initialized to 40 not 20.

Better names like MESSAGE_COUNT_CAPACITY and messageCountSize would indicate the relationship between the variables.

Which method signature is best for creating an oversize copy of a perfect size array. int[] copyArray(int[] sourceArray) void copyArray(int[] sourceArray, int[] copyArray) int copyArray(int[] sourceArray, int[] copyArray) void copyArray(int[] sourceArray, int[] copyArray, int copySize)

Correct int copyArray(int[] sourceArray, int[] copyArray) This method signature allows the data from sourceArray to be copied into copyArray. Since copyArray is oversize, the size of the array must be returned. copyArray must be constructed before the method is called.

To maintain a list in descending sorted order, a given new item should be inserted at the position of the first element that is equal to the item. True False

FALSE Should be less than the item, not equal. For example, given 88 61 42, new item 70 would go just before the first smaller item 61, yielding 88 70 61 42

A class may not simultaneously "extend" a class and "implement" an interface. True False

False A class can both extend a class and implement an interface.

An array parameter is a copy of the array passed to the method. True False

False The reference to the array is copied to the parameter, so the argument and parameter both refer to the same array. A method can thus modify the elements of an array argument.

The method signature void shuffleCards(int[] cards, int size) indicates that cards is a perfect size array. True False

False: The method has a parameter for the size of the array, which indicates that the array is not perfect size.

What is the difference between perfect and oversized arrays?

Full vs partially full Perfect size = array where the number of elements is exactly equal to the memory allocated an oversize array is an array where the number of elements used is less than or equal to the memory allocated - typically use two data items when used for methods

How can the return type can also help a programmer determine whether a method uses a perfect size or a oversize array.

If a method returns an array reference, the method constructs a perfect size array. A oversize array cannot be constructed inside a method because the method would need to return both an array reference and array size, which is not permitted in Java. If a method returns an int, the method may use an oversize array, where the return value indicates the new array size. Ex: int removeAll(String[] arrayReference, int arraySize, String target) removes all of the occurrences of target from an oversize array. Because the method has an arraySize parameter and returns an int, a programmer can determine that the method uses an oversize array.

How can the return type also help a programmer determine whether a method uses a perfect size or a oversize array?

If a method returns an array reference, the method constructs a perfect size array. A oversize array cannot be constructed inside a method because the method would need to return both an array reference and array size, which is not permitted in Java. If a method returns an int, the method may use an oversize array, where the return value indicates the new array size. Ex: int removeAll(String[] arrayReference, int arraySize, String target) removes all of the occurrences of target from an oversize array. Because the method has an arraySize parameter and returns an int, a programmer can determine that the method uses an oversize array.

Can objects be copied through assignment?

Objects (including arrays) can not be copied through assignment

Why are oversize arrays useful?

Oversize arrays are useful when the number of elements stored in the array is not known in advance, or when the number of elements stored in an array varies over time. Ex: An oversize array would be used for a shopping list since the number of elements in the list increases when items are added to list and decreases when items are removed or purchased.

int removeAll(int[] arrayReference, int startIndex, int stopIndex, int target) Only a perfect size array Only an oversize array Both perfect size and oversize arrays could be used Correct

Oversize: This method is designed to process a range within an array. By setting startIndex to 0 and stopIndex to the array's length or the array's size, the method can be used with oversize arrays. Because the method returns an int, a programmer can determine that the method could change the array size, so cannot be used with a perfect size array.

can perfect size arrays be returned from methods? If so, give an example

Perfect size arrays can be returned from methods. Ex: double[] createInitializedArray(int numberElements, double initializeValue) declares a method with two parameters. The first parameter, numberElements, contains the number of elements that will be put in the array constructed inside the method. The second parameter, initializeValue, contains the value assigned to each array element. The method returns a new perfect size array constructed inside the method.

To maintain a list in ascending sorted order, a given new item should be inserted at the position of the first element that is greater than the item. True False

TRUE For example, given list 1 4 8 9, new item 6 would go just before the first greater item 8, yielding 1 4 6 8 9.

A method cannot be used to swap two references. True False

TRUE References are stored in the stack frame. The references can be swapped within a stack frame. However, stack frames are deleted when a method returns, leaving the original references unchanged.

How are arrays declared?

The array declaration uses [ ] symbols after the data type to indicate that the variable is an array reference.

What is encapsulation?

The process of enclosing programming elements inside larger, more abstract entities. AKA information hiding or separation of concerns

Recall that the array declaration was int[] yearsArr = new int[4];. Is currYear = yearsArr[4] a valid assignment?

The valid indices start at 0, so the four elements are 0, 1, 2, and 3. Accessing yearsArr[4] will cause an error.

A method cannot modify an argument of primitive type. True False

True The parameter is a local copy. Any modification of that local copy has no impact on the argument.

What is a list?

an abstract concept that refers to a sequence of items that can contain duplicates - one item (the list) that refers to a whole set of items ( the full collection)

After itemList.clear(), itemList.get(0) is an invalid access. True False

clear() removes all elements. So there is no element 0. TRUE

What is java collections?

collection objects in java hold a group of objects (reference data types) - will let us put any reference type in a collection - generally collects one type of data

How do you copy an array?

create an array of same size and type. then one by one, copy the new values into the arrays

The following statement sorts an ArrayList called prevEmployees. Assume prevEmployees is an appropriately initialized ArrayList of EmployeeData elements.sort(prevEmployees); True False

false sort() is a static method. Therefore, both the Collections class name and a dot should be appended before the method call, as in Collections.sort(prevEmployees).

What is a linkedlist?

implementation of list - concept in which each element(node) maintains a reference (pointer) to the next and sometimes, in the case of the doubly-linked list, the previous element

Which method signature is best for creating a perfect size copy of an oversize array? int[] copyArray(int[] sourceArray) void copyArray(int[] sourceArray) int[] copyArray(int[] sourceArray, int size) int[] copyArray(int[] sourceArray, int fromIndex, int toIndex)

int[] copyArray(int[] sourceArray, int fromIndex, int toIndex) The parameters fromIndex and toIndex allow any range of elements to be copied from the oversize array. The method returns the perfect size with the copied elements.

How can an array can be declared with two dimensions.

int[][] myArray = new int[R][C] represents a table of int variables with R rows and C columns, so R*C elements total. For exam

An interface cannot be instantiated. True False

true An interface only declares methods, and does not provide complete method definitions. Thus, it cannot be instantiated.

How does an array list and likendlist differ in terms of content storing?

Array contents stored in an array, consecutive memory location Linked Contents stored in individual nodes, anywhere in memory

What are the advantages and disadvantages to using a perfect size array vs an oversize array

Perfect size arrays have two advantages over oversize arrays. First, methods that use perfect size arrays have fewer parameters than methods that use oversize arrays. Second, a perfect size array contains no excess elements. A disadvantage of using perfect size arrays is that if the number of array elements needs to change, a new array will have to be constructed. Ex: Suppose an array contained three days of sales from an internet store: double[] dailySales = {34.25, 98.57, 140.83};. If a fourth day of sales is added, an array with four elements must be constructed. Then, the data from the three element array must be copied to the new four element array. Creating a new array and copying elements can be time consuming, especially for very large arrays.

what is a for-each loop?

accesses each element one at a time without the need of indexing each one. for(String s: words (<---- array)){ print s; }

What is the equals, fill and sort methods in arrays?

equals returns true if two specified arrays of ints are equal to one another fill assigns a specified double value to each element of the specified array of doubles sort sorts the specified array into ascending numerical order

Write a statement that assigns 99 into the fifth row, third column of array numVals. Note: the first row/column is at index 0, not 1.

numVals[4][2] = 99; New programmers commonly forget that the first row, first column is at [0][0], not [1][1].

What three variables do oversize arrays typically use?

oversize arrays typically use three variables including: the array reference, the current size, and a constant for the array capacity. These three variables can be given similar identifiers to indicate that they belong together. The array capacity is stored as a constant to clarify the meaning of the number.


Conjuntos de estudio relacionados

Medical Terminology: Chapter 2 (Body Structure)

View Set

6ème - Lesson 1 - Classroom English

View Set

Forearm and Hand - Study Guide (Priscilla)

View Set

MGMT 321 Chapters 1-4 Written answers good

View Set

Ch. 13 Learning curve, Biology of Organisms Ch.13 Leaning Curve, Bio 1010 Chapters 14-17, Bio Chapter 15, Biology of Organisms Ch.14 Leaning Curve, Biology Ch. 14, Biology launchpad chapter 15 questions USM

View Set