JAVA Chapter 16 & 18 Programming 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Cannot have a static inner class in a generic class as shown.

Consider the following code snippet: public class LinkedList<E> { private E defaultValue; public void add(E value, int n) { . . . } private static class Node { public E data; public Node next; } . . . } What is wrong with this code? -Cannot have a static method in a generic class as shown. -Cannot have a static inner class in a generic class as shown. -Cannot declare the variable defaultValue as a generic type. -Cannot pass a generic type as an argument to a method.

The return type is a class that implements Comparable.

Consider the following code snippet: public static <E extends Comparable<E>> E min(ArrayList<E> objects) What can we conclude about the return type of this method? -The return type is a class that implements Comparable. -The return type is a class that extends Comparable. -The return type is a subclass of the ArrayList class. -The return type is an array list of generic objects.

public static <D> void reverse(List<D> list) { . . . }

Consider the following code snippet: public static void reverse(List<?> list) { . . . } This method declaration is equivalent to which of the following declarations? -public static <D> void reverse(List<D> list) { . . . } -public static <List> void reverse(List list) { . . . } -public static void reverse(List<? super E> list) { . . . } -public static void reverse(List<? extends E> list) { . . . }

This is an example of an unbounded wildcard.

Consider the following code snippet: public static void reverse(List<?> list) { . . . } Which of the following statements about this code is correct? -This is an example of a wildcard with a lower bound. -This is an example of a wildcard with an upper bound. -This is an example of an unbounded wildcard. -This code is illegal.

myArray.getClass().getComponentType()

Given an array myArray, which of the following represents an expression to determine the type of the elements of the array? -myArray[0].getClass() -myArray.getClass().getElementType() -myArray.getClass().getComponentType() -myArray.getComponentType().getClass()

-III only

Given the following generic method, which of the following CANNOT be the return type? public static <E extends Comparable<E>> E max(E[] a) { . . .} I String II Integer III double -II only -I and III only -III only -I only

-I and III only

Given the following generic method, which of the following is a possible return type? public static <E extends Comparable<E>> E max(E[] a) { . . . } I String II Object III Double -I only -I and III only -I and II only -II only

-key and value

The type variables in HashMap<K, V> in the standard library mnemonically represent ____. -kernel and vector -kind and variable -String and Vector -key and value

To prevent storage of an outer-class reference that is not needed.

Using the textbook's implementation of a linked list, what is the purpose of declaring the Node class to be a static inner class? -To create an outer-class reference that is needed. -To create an outer-class reference. -To create a this reference to itself. -To prevent storage of an outer-class reference that is not needed.

correct, but unnecessary cast

Consider the following code snippet: public class Box<E> { private E data; public Box(){ . . . } public void insert(E value) { . . . } public E getData() { . . . } } What will result from executing the following code? Box<String> box = new Box<>(); . . . box.insert("blue Box"); String b = (String) box.getData(); -correct, with necessary cast -run-time error -correct, but unnecessary cast -compiler error

ClassCastException

Consider the following code snippet: public class Box<E> { private E data; public Box(){ . . . } public void insert(E value) { . . . } public E getData(){ . . . } } What specific exception will be thrown when the following code executes? Box<String> box = new Box<>(); . . . box.insert("blue Box"); Double myDouble = (Double) box.getData(); -Exception -ClassCastException -NoSuchElementException -IndexOutOfBoundsException

The node that will be updated is the most recently visited node.

Using the textbook's implementation of a linked list, which of the following statements about changing the data stored in a previously visited element is correct? -The set method can be called again immediately after calling the add method. -The position.next reference must be updated when the data is updated. -The previous reference must be updated when the node is updated. -The node that will be updated is the most recently visited node.

The position reference must be updated when a new node is added.

Using the textbook's implementation of a linked list, which of the following statements about managing nodes within a linked list using an iterator is correct? -The set method can be called immediately after removing an existing node using the remove method. -The position reference must be updated when a new node is added. -The set method can be called immediately after adding a new node using the add method. -The node that will be removed is the node pointed to by the position.next reference.

Any subclass of D may be used.

What does it mean when the syntax ? extends D is used? -This indicates a wildcard with an upper bound. -Any subclass of D may be used. -Any subclass or superclass of D may be used. -Any superclass of D may be used.

The class represented by E implements Comparable and Measurable.

What does the following code snippet mean: <E extends Comparable<E> & Measurable> -The class represented by E implements Comparable and extends Measurable. -The class represented by E extends Comparable and Measurable. -The class represented by E extends Comparable and implements Measurable. -The class represented by E implements Comparable and Measurable.

-I and III

What is included in a linked list node? I a reference to the next node II an array reference III a data element -II and III -I -I and III -II

O(1)+

When considering the reallocation operation for a list whose buffer is full, on average it will take ____ time. -O(n) -O(n^2) -O(1)+ -O(1)

For better efficiency, nodes should be added at the back and removed at the front.

When implementing a queue as a singly-linked list, which of these statements is correct? -There is no difference in efficiency whether nodes are added at the front and removed at the back, or added at the back and removed at the front. -You cannot effectively implement a queue as a singly-linked list. -For better efficiency, nodes should be added at the back and removed at the front. -For better efficiency, nodes should be added at the front and removed at the back.

-III only

Which Java generic programming technique(s) requires the programmer to use casting to access variables stored as Object types? I type variables II primitive types III inheritance -II only -I and III only -III only -I only

-java.util

Which Java package contains the LinkedList class? -java.io -java.util -java.lang -java.collections

-I and III only

Which Java technique(s) allows generic programming? I type variables II primitive types III inheritance -III only -II only -I and III only -I only

-III only

Which argument type cannot be passed to generic methods? I Object II GUI components III primitive -III only -I only -I and III only -II only

I and III

Which of the following actions must be taken to add a node X at the end of a doubly-linked list? I Update the next reference in the node before the position where X will be placed II Update the previous reference in the node before the position where X will be placed III Update the list's last reference -I -I and III -I and II -II

-I and II

Which of the following actions must be taken to remove a node X from the middle of a doubly-linked list? I Update the next reference in the node before X II Update the previous reference in the node after X III Update the list's first reference -I -I and II -II -II and III

-I and III only

Which of the following headers for a generic method myMethod allows the actionPerformed method from the ActionListener class to be called? I public static <E extends ActionListener> E myMethod(E e) II public static <E implements ActionListener> E myMethod(E e) III public <E extends ActionListener> E myMethod(E e) -I only -III only -I and III only -II only

? sub B

Which of the following is not a wildcard type? -? sub B -? super B -? -? extends B

-When the insertion happens after the last element, it is O(1)

Which of the following is true about the efficiency of inserting an element into an array list? -When the insertion happens on the last element, it is O(n) -It always has O(n) efficiency -When the insertion happens before the last element, it is O(1) -When the insertion happens after the last element, it is O(1)

-I and III

Which of the following operations from the array list data structure could be used in the implementation of the push and pop operations of a stack data structure? I addLast II addFirst III removeLast -I -II -I and III -II and III

-JButton

Which of the following satisfies the wildcard ? extends Component? -Scanner -String -Color -JButton

I, II and III

Which of the following satisfies the wildcard ? extends Object? I String II JComponent III Scanner -I, II and III -I and II only -II only -I only

None satisfy this wildcard

Which of the following satisfies the wildcard ? super Object? -Scanner -None satisfy this wildcard. -JComponent -String

The iterator is at the end of the list if the position reference is null.

Which of the following statements about a linked list iterator is NOT correct? -The iterator is at the end of the list if the position reference is null. -The list is empty if the linked list's first node reference is null. -The iterator is at the end of the list if the position.next reference is null. -The iterator is at the end of the list if the linked list's first node reference is null.

-You can replace type parameters with primitive types.

Which of the following statements about generic methods is NOT correct? -Supply the type parameters of a generic method between the modifiers and the method return type. -You need not instantiate the type parameter when calling a generic method. -You can replace type parameters with primitive types. -Generic methods can be static methods.

A generic method must have a generic type parameter.

Which of the following statements about generic methods is correct? -The generic type parameter of a generic method designates the method's return type. -A generic method must reside in a generic class. -When calling a generic method, you need to instantiate the type parameters. -A generic method must have a generic type parameter.

Type parameters can be instantiated with primitive data types.

Which of the following statements about generic programming is NOT correct? -Type parameters can be instantiated with interface types. -Type parameters can be instantiated with wrapper class types. -Type parameters can be instantiated with class types. -Type parameters can be instantiated with primitive data types.

Box<Double> box = new Box<>();

Consider the following code snippet: public class Box<E> { private E data; public Box(){ . . . } } Which of the following is a valid Box<E> object instantiation? -Box<Double> box = new Box<>(); -Box<int> box = new Box<>; -Box box = new Box<>(); -Box<E> box = new Box();

If implementing the stack as a linked list, it is more expensive to add and remove elements at the end than at the beginning.

A stack can be implemented as a sequence of nodes in a linked list or an array list. Which of the following statements about this is correct? -If implementing the stack as a linked list, there is no cost difference whether adding and removing elements at the beginning or the end. -If implementing the stack as an array list, it is more expensive to add and remove elements at the end than at the beginning. -If implementing the stack as a linked list, it is more expensive to add and remove elements at the end than at the beginning. -If implementing the stack as an array list, there is no cost difference whether adding and removing elements at the beginning or the end.

-O(n)

Adding or removing an arbitrary element in the middle of an array list takes ____ time. -O(n) -O(1) -O(n^2) -O(log(n))

none are necessary

An inner helper class, such as a TreeNode inside the generic BinaryTree class, must meet which of the following criteria? I be public II be private III be declared as generic -none are necessary -I, II and III -I and III only -II and III only

The most expensive operation of a doubly-linked is to add an element in the middle.

An iterator is currently pointing to the correct location for insertion or removal of a doubly-linked list. Which of the following statements about doubly-linked list operations is correct? -The most expensive operation of a doubly-linked is to add an element in the middle. -The most expensive operation of a doubly-linked is to add an element at the front. -The most expensive operation of a doubly-linked is to add an element at the end. -The most expensive operation of a doubly-linked is to remove an element at the end.

Adding an element to the end of the linked list is O(1).

Assume that the linked list implementation includes a reference to the last node as well as to the first node. Which of the following statements about the efficiency of the linked list is correct? -Adding an element to the end of the linked list is O(1). -Accessing an element in the linked list using an iterator is O(n). -Adding an element to the middle of the linked list at the current position of the iterator is O(n). -Removing an element other than the last element from the linked list at the current position of the iterator is O(n).

There is no relationship between ArrayList<BankAccount> and ArrayList<SavingsAccount>

Consider the following class declaration: public class SavingsAccount extends BankAccount { . . . } Which of the following statements about these classes is correct? -There is no relationship between ArrayList<BankAccount> and ArrayList<SavingsAccount> - ArrayList<SavingsAccount> is a subclass of ArrayList<BankAccount>. -ArrayList<BankAccount> is a subclass of ArrayList<SavingsAccount>. -ArrayList<SavingsAccount> extends ArrayList<BankAccount>.

For the element type of other, you must supply a type that is a subtype of E.

Consider the following code snippet in the LinkedList<E> class: public void addAll(LinkedList<? extends E> other) { ListIterator<E> iter = other.listIterator(); while (iter.hasNext()) { add(iter.next()); } } Which of the following statements about this code is correct? -You must supply a specific type for the element type of other. -For the element type of other, you must supply a type that is a supertype of E. -For the element type of other, you must supply a type that is either a subtype or a supertype of E. -For the element type of other, you must supply a type that is a subtype of E.

II and III only

Consider the following code snippet that declares the GraduateStudent class: public GraduateStudent extends Student { . . .} Which of these statements are false? I GraduateStudent is a subclass of Student II Stack<GraduateStudent> is a subclass of Stack<Student> III Stack<Student> is a subclass of Stack<GraduateStudent> -II only -II and III only -III only -I only

Yes, a compile-time error will occur because you cannot assign a Double value to a String variable.

Consider the following code snippet: ArrayList<Double> arr = new ArrayList<>(); String element = arr.get(0); Is there an error in this code? -Yes, a compile-time error will occur because you cannot assign a Double value to a String variable. -Yes, a compile-time error will occur because you cannot create an ArrayList of type Double. -No run-time error or compile-time errors will occur. -Yes, a run-time error will occur because a Double value cannot be cast to a String value.

xyzdefjkl

Consider the following code snippet: LinkedList<String> words = new LinkedList<String>(); words.addFirst("xyz"); words.addLast("jkl"); words.addLast("def"); System.out.print(words.removeFirst()); System.out.print(words.removeLast()); System.out.print(words.removeLast()); What does this code print? -defjklxyz -xyzdefjkl -xyzjkldef -defxyzjkl

-no error

Consider the following code snippet: public class Box<E> { private E data; public Box() { . . . } public void insert(E value) { . . . } public E getData(){ . . . } } What will result from executing the following code? Box<String> box = new Box<>(); . . . box.insert("blue Box"); String b = box.getData(); -run-time error -compiler warning -no error -compiler error

-compile-time error

Consider the following code snippet: public class Box<E> { private E data; public Box() { . . . } public void insert(E value) { . . . } } What will result from executing the following code? Box<Boolean> box = new Box<>(); box.insert("blue Box"); -compile-time error -run-time error -correct generic assignment -compile-time warning

i < currentSize

Given the partial ArrayList class declaration below, select an expression to complete the contains method, which is designed to return true if the element is contained within the list. public class ArrayList { private Object[] elements; private int currentSize; public ArrayList() { final int INITIAL_SIZE = 10; elements = new Object[INITIAL_SIZE]; currentSize = 0; } public boolean contains(Object item) { for (int i = 0; ________________ ; i++) { if (elements[i].equals(item)) { return true; } } return false; } ... } -i < currentSize -i <= currentSize -i <= elements.length -i < elements.length

currentSize == 0

Given the partial ArrayList class declaration below, select an expression to complete the empty method, which is designed to return true if the list contains no elements. public class ArrayList { private Object[] elements; private int currentSize; public ArrayList() { final int INITIAL_SIZE = 10; elements = new Object[INITIAL_SIZE]; currentSize = 0; } public boolean empty() { return ________________________ ; } } -elements[0] == null -elements.currentSize == 0 -elements.length == 0 -currentSize == 0

-it.next()

Given the partial LinkedList and LinkedListIterator class declarations below, select an expression to complete the get(index) method of the LinkedList class, which returns the element at the position indicated by index. You can assume that the method is only called when the index is less than the size of the linked list. public class LinkedList { . . . public ListIterator listIterator() { return new LinkedListIterator(); } class LinkedListIterator implements ListIterator { private Node position; private Node previous; private boolean isAfterNext; public LinkedListIterator() { . . . } public Object next() { . . . } public boolean hasNext() { . . . } } public Object get(int index) { ListIterator it = listIterator(); for (int i = 0; i < index; ++i) { it.next(); } return ________________________ ; } } -it.next() -it.position -it.next().data -it.previous

first == null

Given the partial LinkedList class declaration below, select an expression to complete the empty method, which is designed to return true if the list contains no elements. public class LinkedList { class Node { public Object data; public Node next; } private Node first; . . . public boolean empty() { return ________________________ ; } } -first.data == null -first != null -first == null -first.next == null

O()+

How do you symbolize amortized big-Oh time? O()" O()* O()+ O()

-I and III only

If a class requires two generic type variables, how many can you legally provide in Java? I 0 II 1 III 2 -I only -II only -I and III only -I and II only

-O(1)

If the current size of an array list is less than the length of the buffer, adding an element at the end of an array list takes ____ time. -O(log(n)) -O(1) -O(n) -O(n2)

2

If we want a create a doubly-linked list data structure so that we can move from a node to the next node as well as to a previous node, we need to add a previous reference to the Node class. Each such DNode (doubly-linked Node) will have a data portion and two DNode references, next and previous. How many references need to be updated when we remove a node from the middle of such a list? Consider the neighboring nodes. -4 -3 -1 -2

inheritance

In Java, generic programming can be achieved with ____. -inheritance -polymorphism -arrays -encapsulation

I and II

In a linked list data structure, when does the reference to the first node need to be updated? I inserting into an empty list II deleting from a list with one node III deleting an inner node -I and II -II -I -III

The methods of the Node class can be directly accessed by other classes.

In the textbook implementation, the Node class is a private inner class of the LinkedList class. Which of the following statements regarding this implementation is NOT correct? -The methods of the Node class can be directly accessed by other classes. -The methods of the LinkedList class can access the public features of the Node class. -The Node class's instance variables that represent the node element and its next node reference are declared as public. -The methods of the Node class can access the public features of the LinkedList class.

first = first.next; return element;

Insert the missing code in the following code fragment. This fragment is intended to remove a node from the head of a linked list: public class LinkedList { . . . public Object removeFirst() { if (first == null) { throw new NoSuchElementException(); } Object element = first.data; ________________ ________________ } . . . } -first.next = first; return element; -first = first.next; return element; -first = element.next; return element; -first = element.next; return null;

The most expensive operation of a doubly-linked list is to retrieve an arbitrary element.

Linked list operations that were studied included adding/removing an element at the end or in the middle, and retrieving the kth element. If the iterator is currently pointing to the correct location for insertion or removal, which of the following statements about these doubly-linked list operations is correct? -The most expensive operation of a doubly-linked list is to add an element in the middle. -The most expensive operation of a doubly-linked list is to remove an element at the end. -The most expensive operation of a doubly-linked list is to retrieve an arbitrary element. -The most expensive operation of a doubly-linked list is to add an element at the end.

n / 2

On average, how many elements of an array list of size n need to be moved when an element is added? -n -n / 2 -n^2 -2^n

Can only execute Object class methods.

Suppose a generic method accepts a generic unconstrained argument p. What restrictions are placed on p by the compiler? -Can not execute any methods. -There are no restrictions. -Can only execute Object class methods. -Can only execute methods from its own class.

I, II, and III

Suppose a linked-list class called MyLinkedList with a generic E type has been instantiated with a java.awt.Component type variable. Consider its instance method locate with the following header: public void locate(MyLinkedList<? extends E>) { . . . } Which type cannot be passed to method locate? I MyLinkedList<JButton> II MyLinkedList<Component> III MyLinkedList<JTextField> -I only -I, II, and III -II and III only -I and II only

O(n^2)

Suppose we maintain a linked list of length n in random element order. What would be the big-Oh notation for printing out those elements which occur exactly once in the list (without sorting the list)? -O(n) -O(n^2) -O(n log n) -O(1)

-O(n)

Suppose we maintain a linked list of length n in sorted order. What would be the big-Oh notation for the add operation? -O(n) -O(n2) -O(n log n) -O(1)

A node's data is returned to the program when the node is removed from the linked list, and its memory space is reclaimed later by the garbage collector.

Which of the following statements about removing a node from a linked list is correct? -A node's data is discarded when it is removed from the linked list, and its memory space is reclaimed later by the garbage collector. -A node's data is returned to the program when the node is removed from the linked list, and its memory space is reclaimed later by the garbage collector. -A node's data is discarded when it is removed from the linked list, and its memory space is immediately reclaimed. -A node's data is returned to the program when the node is removed from the linked list, and its memory space is immediately reclaimed.

-O(1)

You have implemented a queue as a singly-linked list, adding elements at the end and removing elements at the front. What is the cost of the add operation? -O(1) -O(n^2) -O(log n) -O(n)


Kaugnay na mga set ng pag-aaral

Chapter 27 - Reproductive System 2

View Set

community health exam 2 (CH. 2, 3, 4 , 6, 18, 19, 20)

View Set

Smooth Muscle Cell Function and Microstructure (1/7/15)

View Set

BLS Lesson 4: Basic Life Support for Children and Infants

View Set

Marketing Final(Marketing Info Management)

View Set

Med/surg test 3 prepu chapter 15

View Set