Quiz 2 Data Structure, Quiz 1 Data Structure

Ace your homework & exams now with Quizwiz!

array list vs linked list worst O(runtimes)

!!!

this() vs super()

!!!

The combination of data together with its methods is called a(n) ________ Data type.

Abstract

Which of the following statements is correct? (a) Abstract classes can define constants. (b) Interfaces can extend another class. (c) Instances of abstract classes can be created.

Abstract classes can define constants

Method descriptions in an interface are called method ____________

Declaration

(True/False) An interface is a class

False

(True/False) The Collection interface is the root of the collection hierarchy.

False

(True/False) An array can store many different types of values.

False. An array can store only values of the same type.

__________ types allow us to define a collection such as an ArrayList of a specific type.

Generic: E

The nextInt() method will throw a(n) ______ if an integer choice is not entered.

InputMismatchException

A method of the same name and argument list as a method in a parent class, so it replaces the parent method.

Method overriding

The The nextInt() method will throw a(n) _________ is thrown when a program attempts to convert a non-numeric string to a numeric value.

NumberFormatException

The superclass of all Java classes is _________

Object

_____ errors occur during program execution .

Runtime

ArrayIndexOutOfBoundsException

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

(True/False) Each class that implements an interface must provide the complete definition of all methods declared in the interface.

True

(True/False) Array reallocation is effectively an O(1) operation.

True!

The has-a relationship

between classes means that one class has the second class as an attribute

A Java interface is a(n) _____________ between the interface designer and the programmer who codes a class that implements the interface. (a) precondition (b) postcondition (c) message (d) contract

contract

Which of the following would cause an ArrayIndexOutOfBounds error? (a) public boolean setElementOfX(int index, int val) { if (index >= 0 && index < x.length) { x[index] = val; return true; } else { return false; } } (b) public static void main(String[] args) { String inputFileName; if (args.length > 0) inputFileName = args[0]; else inputFileName = "Phone.dat"; } (c) for (int i = 0; i < x.length; i++) x[i] = i + i; (d) for (int i = 0; i <= x.length; i++) x[i] = i * i;

d

If a class declares constructors, the compiler will not create a(n) ________.

default constructor

One way to make code reusable is to ___________ or combine data elements together with methods that operate on that data in a separate program module.

encapsulate

It's possible to have several methods with the same name that each operate on different types or numbers of arguments. This feature is called method ______________________.

method overloading

Keyword ________ requests memory from the system to store an object, then calls the corresponding class's constructor to initialize the object.

new

If no constructors are defined for a class, the _____ constructor for that class will be provided by default. (a) private (b) super (c) public (d) no-parameter

no-parameter/default constructor

what is the original statements result? what is the result of new Building("x "); new House();

original: b h hn x Building("x "): b bn x newHouse(): b h

Which of the following is considered a visibility modifier in Java? (a) private (b) Inherit (c) superclass (d) String

private

_______________ visibility is for members of a class that should not be accessible to anyone but the class, not even classes that extend it.

private

Classes that are not part of a package may access only _______ members of classes in the package

public

The public methods of a class are also known as the class's __________

public interface

If a method contains a local variable with the same name as one of its class's fields, the local variable _________ the field in that method's scope.

shadows

The ___________ call in a class constructor invokes the superclass's constructor that has the corresponding argument list

super()

The call to invokes the constructor for the current class whose parameter list matches the argument list. (a) super (b) super() (c) this() (d) abstract

this()

A(n) _________ class is used to store a primitive-type value in an object type.

wrapper

Which of the following can be done with an array object? (a) Traverse the list structure without having to manage a subscript. (b) Increase or decrease its length, which is fixed. (c) Add an element at a specified position without shifting the other elements to make room. (d) Remove an element at a specified position without shifting the other elements to fill in the resulting gap.

(a) Traverse the list structure without having to manage a subscript.

Are Strings Iterable? (a) Yes (b) No

(b) No

A(n) _______ is a data structure that contains a data item and one or more links. (a) collection (b) node (c) iterator (d) interface

(b) node

The simplest class that implements the List interface is the class. (a) ListIterator (b) Collection (c) ArrayList (d) AbstractList

(c) ArrayList

A(n) ___________ is an indexed data structure, which means you can select its elements in arbitrary order as determined by the subscript value. (a) String (b) stack (c) array (d) list

(c) array

In the Java API documentation, inner classes are called ________ classes. (a) collection (b) interface (c) nested (d) iterator

(c) nested

The ___________ method of the ArrayList class creates a new array that is twice the size of the current array and then copies the contents of the current array into the new one. (a) remove (b) add (c) reallocate (d) vector

(c) reallocate

In the java.util.ArrayList class, which of the following returns a reference to the element at position index? (a) add(E anEntry) (b) indexOf(E target) (c) E remove(int index) (d) E get(int index)

(d) E get(int index)

The LinkedList class implements the List interface using a(n) . (a) iterator (b) interface (c) collection (d) double-linked list

(d) double-linked list

What is the difference between a local variable and a field?

A local variable is declared in the body of a method and can be used only from the point at which it's declared through the end of the method declaration. A field is declared in a class, but not in the body of any of the class's methods. Also, fields are accessible to all methods of the class (with one exception: static fields are not accessible to non-static methods).

A(n) _____________ is a model of a physical entity or activity. (a) abstraction (b) interface (c) contract (d) use case

Abstraction

ClassCastException (maybe not needed)?

An exception thrown when an object variable is cast with an incompatible class.2.6: when code tries to downcast a tree to a Redwood an exception error is thrown at runtime

Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next;

Answer : Deletes from the list the node immediately following x.

Suppose that x is a linked list Node. What does the following code fragment do? t.next = x.next; x.next = t;

Answer : Inserts node t immediately after node x.

Why does the following code fragment not do the same thing as in the previous question? x.next = t; t.next = x.next;

Answer : When it comes time to update t.next, x.next is no longer the original node following x, but is instead t itself!

The _______________ interface is used to pass collections of data as a method parameter in the most general way.

Collection

(True/False) Because an ArrayList<E> is an indexed collection, you can access its elements using a subscript .

FALSE! Even though an ArrayList is an indexed collection, you can't access its elements using a subscript. Instead, you use the "get()" method to access its elements. For example, the statement String dwarf = myList.get(2) stores a reference to the string object "Jumpy" in variable dwarf, without changing myList (PG. 65)

(True/False) The has-a relationship between classes means that one class is a subclass of the other.

False

(True/False) The is-a relationship between classes means that one class has the second class as an attribute.

False

(True/False) The ArrayList<E> has the limitation that the add and remove methods operate in O(n^2 ) time.

False! Add and remove methods operates in O(n) not O(n^2) (PG. 75)

(a) linear (N + N/2 + N/4 + . . .) O(n) (b) linear (1 + 2 + 4 + 8 + . . .) O(n) (c) linearithmic (the outer loop loops log N times). O(nlogn)

Give the order of growth (as a function of N) of the running times of each of the following code fragments:

If a call to remove (java.util.Iterator interface) is not preceded by a call to next, remove will throw a(n) ___________ (Don't know of needed)

IllegalStateException (PG. 91)

Which of the following statements is correct? (a) is-a and has-a relationships cannot be combined. (b) An abstract class can be instantiated. (c) An abstract class cannot declare abstract methods. (d) In Java, a variable of a superclass type can reference an object of a subclass type.

In Java, a variable of a superclass type can reference an object of a subclass type.

A Java ___________ specifies the names, parameters, and return values of the ADT methods without specifying how the methods perform their operations or how the data is internally represented.

Interface

If you want to remove two consecutive elements in a list, a separate call to __________ must occur before each call to remove.

Next

Are arrays Iterable? (a) Yes (b) No

No. You can use the foreach syntax with them. However, you can not pass an array to a method that expects an Iterable or return an array from a method which returns an Iterable. This would be convenient, but it doesn't work that way.

Iterator objects throw a(n) ________ if they are asked to retrieve the next element after all elements have been processed.

NoSuchElementException (PG. 90)

What does the following code fragment print? Give a high-level description of what it does when presented with a positive integer n. int n = 50; Stack s = new Stack(); while (n > 0) { s.push(n % 2); n = n / 2; } while (!s.isEmpty()) System.out.print(s.pop());

Prints the binary representation of n (e.g., 110010 when n is 50).

(True/False) The Node class for a double-linked list has references to the data and to the next and previous nodes.

TRUE!

(True/False) A major reason for the popularity of object-oriented programming is that it enables programmers to reuse previously written code saved as classes.

True

(True/False) The actual object type stored in an object of type CollectionClassName is specified when the object is created.

True! (PG. 66)

(e) This code runs just fine. It prints [Puddles, Quack].

What happens when you run the following code? Choose all that apply. (a) The code does not compile. (b) The error is on line 11. (c) The errors are on lines 8 and 9. (d) The errors are on lines 4 and 17. (e) This code runs just fine. It prints [Puddles, Quack]. (f) This code runs just fine. It prints [Quack, Puddles]. (g) The error is on line 7.

(a) Without making the Duck object Comparable, the Collections.sort() method on line 12 has no idea how to compare the Duck objects. The method compareTo() is just another method - it does not help in sorting if Duck does not implement the Comparable interface.

What happens when you run the following code? Choose all that apply. (a) The code does not compile. (b) The error is on line 11. (c) The errors are on lines 8 and 9. (d) The errors are on lines 4 and 17. (e) This code runs just fine. It prints [Puddles, Quack]. (f) This code runs just fine. It prints [Quack, Puddles]. (g) The error is on line 7.

(b) [one, two, 7]

What is the result of the following statements? (a) onetwo (b) [one, two, 7] (c) onetwo7 (d) [one, two] followed by an exception (e) Compiler error on line 6 (f) Compiler error on line 7

(e). The code does not compile. It attempts to mix generics and legacy code. Lines 3 through 7 create an ArrayList without generics. This means that we can put any objects in it. Line 7 should be looping through a list of Objects rather than Strings since we didn't use generics. generic(String, Integer)

What is the result of the following statements? (a) onetwo (b) onetwo7 (c) onetwo followed by an exception (d) Compiler error on line 6 (e) Compiler error on line 7

(e) Since we call push() rather than offer(), we are treating the ArrayDeque as a LIFO (last-in, first-out) stack. On line 7, we remove the last element added, which is ola. On line 8, we look at the new last element (hi), but don't remove it. Lines 9 and 10, we remove each element in turn until none are left. Note that we don't use an Iterator to loop through the ArrayDeque. The order in which the elements are stored internally is not part of the API contract.

What is the result of the following statements? (Look at the Java documentation of ArrayDeque, predict what this will output, then compile and run it to see the answer.) (a) hello (b) hellohi (c) hellohiola (d) hi (e) hihello (f) The code does not compile. (g) An exception is thrown.

(a), (b). C is both a class and a type parameter. This means that within the class D, when we refer to C, it uses the type parameter. All of the choices that mention class C are incorrect because it no longer means the class C.

Which of the following lines can be inserted to make the code compile? (Choose all that apply.) (a) A a1 = new A(); (b) A a2 = new B(); (c) A a3 = new C(); (d) C c1 = new A(); (e) C c2 = new B(); (f) C c1 = new C();

(a), (d). A LinkedList implements both List and Queue. The List interface has a method to remove by index. Since this method exists, Java does not autobox to call the other method. Queue has only the remove by object method, so Java does autobox there. Since the number 1 is not in the list, Java does not remove anything for the Queue.

Which options are true of the following code? (Choose all that apply.) (a) If we fill in the blank with List, the output is [10]. (b) If we fill in the blank with List, the output is [10, 12]. (c) If we fill in the blank with Queue, the output is [10]. (d) If we fill in the blank with Queue, the output is [10, 12]. (e) The code does not compile in either scenario. (f) A runtime exception is thrown.

Difference between wrapper class and primitive-types

Wrapper: Integer, Float, Double !!! String is not a wrapper class primitive-type: float, double int

Iterable Interface

base interface for all interfaces of the Java collection framework (excluding Map). Iterable defines exactly one method, namely: Iterator iterator(); This allows you to get an Iterator for iterating over the elements of a collection. Since all other collection interfaces extend Iterable (either directly or indirectly), they all inherit the above method; that is, they all provide an iterator for iterating over their elements. For example, the Collection interface directly extends Iterable, and hence, if you have a Collection, you can get an iterator to iterate over its elements: Collection<Book> coll = getOverdueBooks(); Iterator<Book> iter = coll.iterator(); while (iter.hasNext()) { Book book = iter.next(); } Because the need to iterate over all items in a collection is a common practice, the method iterator() is placed in the base interface Iterable, and then through inheritance, it's made available to all collections that extend Collection (because Collection extends Iterable). The extension of Iterable by Collection (and then List, Set, and Queue) is known as interface inheritance.

The is-a relationship

between classes means that one class is a subclass of the other

Which of the following methods of java.lang.Object compares an object to its argument? (a) boolean equals(Object obj) (b) int hashCode() (c) Object clone() (d) String toString()

boolean equals(Object obj)

A Node is generally defined inside another class, making it a(n) ________________________ class.

inner class A class that is defined within another class is called a nested class.

The ArrayList<E> class is part of the package called ______________

java.util

To obtain a ListIterator, you call the _____________ method of the LinkedList class.

listIterator

boolean matches(char c) { if (bracketType == '[' && c == ']') return true; if (bracketType == '{' && c == '}') return true; if (bracketType == '(' && c == ')') return true; return false; }

methad that takes an argument char and returns a boolean method call: Object.matches(char c); if this method was static: you could just call: matches(char c);

Having multiple methods with the same name but different signatures in a class is called _______

method overloading

A data field (or method) with ____ visibility can be accessed in either the class defining it, in any subclass of that class, or any class in the same package. (a) private (b) protected (c) public (d) instanceof

protected

what members do classes that are not part of a package have access to?

public members

main

public static void main(String[] args) {}

A(n) _______ variable represents classwide information that's shared by all the objects of the class.

static

You should always override the _______ method if you want to represent an object's state.

toString

Java automatically applies the _________ method to an object referenced in a String expression

toString()

Give the output printed by java Stack for the input it was - the best - of times - - - it was - the - -

was best times of the was the it (1 left on stack)


Related study sets

French: Chapter 1 (Une Amie et Un Ami)

View Set