CS Studying - Copied

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

Core methods

Methods that should be implemented and immediately tested before moving on in the project.

What is the time complexity for adding an entry to a fixed-size array-based bag ADT?

O(1)

What is the time complexity for adding an entry to a linked-based bag ADT?

O(1)

What is the worst-case performance of the add method in a full binary search tree with linked nodes?

O(log n)

What is the efficiency of a Binary Search?

O(log2(n))

A complete traversal of an n-node binary tree is a(n) _____ operation if visiting a node is O(1) for the recursive implementation.

O(n)

For an array implementation of a list, the efficiency of remove(index) is:

O(n)

What is the getEntry(int pos) method's efficiency when used on a linked ADT?

O(n)

What is the worst-case time complexity for searching a linked-based bag ADT for a particular entry?

O(n)

What is an iterator's efficiency when traversing a linked chain compared to when a sort method does it?

O(n) compared to the sort method's O(n^2). This is because iterators have direct access to the underlying structure

Efficiency of a selection sort (no matter what the initial order is)

O(n^2)

What are examples of primitive data types?

byte, short, int, long, float, double, boolean, char

Removing an entry from an Array Bag

The object being removed is replaces by the last object in the array and then that last object is nulled out.

Abstraction

The process that asks what instead of how. The point at the beginning of designing a class where the programmer must focus on what methods to make rather than how to implement methods.

After the following statements execute, what item is at the front of the queue? QueueInterface zooDelivery = new LinkedQueue(); zooDelivery.enqueue("lion"); zooDelivery.enqueue("tiger"); zooDelivery.enqueue("cheetah"); String next = zooDelivery.dequeue(); next = zooDelivery.dequeue(); zooDelivery.enqueue("jaguar"); a) "cheetah" b) "jaguar" c) "tiger" d) "lion"

a) "cheetah"

Assume a Queue class has been implemented as a circular array with one unused location. Next assume an instance of the Queue has been constructed with a default capacity of six, storing String references. Then the following six strings are inserted, (i.e., enqueued), into the Queue: "!", "@", "#", "$", "%", "?". Now the front of the queue is deleted, (i.e., dequeued) and another string, "&", is inserted (i.e., enqueued) Again the front of the queue is deleted and another string is immediately inserted. Assume this pattern, (dequeue, enqueue ...) is repeated an arbitrary number of times. Which of the following expressions best describes the resulting relationship between the frontIndex(first element in the queue) and backIndex(last element in the queue) indices of the queue after the final enqueue? a) ((backIndex + 2) % 7) == frontIndex b) ((backIndex + 1) % 7) == frontIndex c) ((backIndex + 2) % 6) == frontIndex d) ((backIndex + 1) % 6) == frontIndex e) frontIndex == backIndex

a) ((backIndex + 2) % 7) == frontIndex

Which bag behavior(s) would need to be modified when implementing a set? a) add() b) clear() c) contains() d) toArray()

a) ?

What is the output of the following program when the method is called with 4? void unknown(int n) { if (n > 0) unknown(n-1); System.out.print("?"); } a) ????? b) ???? c) ??? d) none of the above

a) ?????

What is the output of the following program when the method is called with 4? void unknown(int n) { System.out.print("?"); if (n > 0) unknown(n-1); } a) ????? b) ???? c) ??? d) none of the above

a) ?????

To implement a Deque(double ended queue) with a firstNode and a lastNode reference, which of the following underlying representations would result in a more efficient implementation? a) Double Linked chain b) Single linked chain c) Neither, the efficiency would be the same

a) Double linked chain

Imagine you have an empty queue of strings named queue. What is the contents of the queue from front to rear, (listed left to right below), after the following operations have executed. queue.enqueue("K"); queue.enqueue("P"); queue.dequeue(); queue.enqueue("G"); String str = queue.getFront(); queue.enqueue("R"); queue.dequeue(); queue.enqueue("V"); queue.enqueue(str); a) GRVP b) GVGK c) KPGR d) KGRVP

a) GRVP

The following Checked Exceptions should be handled gracefully, they cannot always be prevented a) IOException b) ArrayIndexOutofBoundsException c) ClassNotFoundException d) FileNotFoundException e) NullPointerException

a) IOException c) ClassNotFoundException d) FileNotFoundException

If an algorithm requires 7 basic operations for an algorithm with a problem size of n, the algorithmic complexity is a) O(1) b) O(7) c) O(n) d) O(7n)

a) O(1)

If an algorithm requires 7 basic operations for an algorithm with a problem size of n, the algorithmic complexity is: a) O(1) b) O(7) c) O(n) d) O(7n)

a) O(1)

In a circular array-based implementation of a queue, what is the performance when the dequeue operation ? a) O(1) b) O(logn) c) O(n) d) O(n^2)

a) O(1)

In a linked chain implementation of a queue, the performance of the enqueue operation is: a) O(1) b) O(logn) c) O(n) d) O(n^2)

a) O(1)

In the Ch 6 linked-chain implementation of a stack ADT, the performance of popping an entry from the stack is: a) O(1) b) O(2) c) O(n) d) O(n^2)

a) O(1)

What is the time complexity for adding an entry to a LINKED-based bag ADT? a) O(1) b) O(n) c) O(n^2) d) negligible

a) O(1)

What is the time complexity for adding an entry to a fixed-size ARRAY-based bag ADT? a) O(1) b) O(n) c) O(n^2) d) negligible

a) O(1)

The selection sort requires ________ comparisons for an array of n items. a) O(n^2) b) O(2^n) c) O(n) d) O(n log n)

a) O(n^2)

What item is at the front of the list after these statements are executed? DequeInterface waitingLine = new LinkedDeque(); waitingLine.addToFront("Jack"); waitingLine.addToBack("Rudy"); waitingLine.addToBack("Larry"); waitingLine.addToFront("Sam"); String name = waitingLine.getFront(); name = waitingLine.getBack(); a) Sam b) Adam c) Rudy d) Jack

a) Sam

When you declare a method to be protected and final, who can access it? a) a subclass b) a superclass c) a client d) all of the above

a) a subclass

In the Ch 6 array-based implementation of a Stack ADT, what value for the topindex of the stack indicates that it is empty? a) 0 b) 1 c) -1 d) null

c) -1

Q 10: Given the linked chain above, which is the resulting linked chain after https://canvas.vt.edu/courses/31277/quizzes/37119?module_item_id=132218

b)

For large values of n which statement is true? a) (n^2 + n)/ 2 behaves like n b) (n^2 + n)/ 2 behaves like n^2 c) b) (n^2 + n)/ 2 behaves like n/2 d) all of the above

b) (n^2 + n)/ 2 behaves like n^2

What is the output of the following program when the method is called with 4? void unknown(int n) { if (n > 0) { System.out.print("?"); unknown(n-1); } } a) ????? b) ???? c) ??? d) none of the above

b) ????

The efficiency for recursively traversing a chain of linked nodes is: a) O(1) b) O(n) c) O(n^2) d) it cannot be proven

b) O(n)

What is the worst-case time complexity for searching a LINKED-based bag ADT for a particular entry? a) O(1) b) O(n) c) O(n^2) d) negligible

b) O(n)

What item is at the front of the list after these statements are executed? DequeInterface waitingLine = new LinkedDeque(); waitingLine.addToFront("Jack"); waitingLine.addToFront("Rudy"); waitingLine.addToBack("Larry"); waitingLine.addToBack("Sam"); String name = waitingLine.getFront(); a) Jack b) Rudy c) Larry d) Sam

b) Rudy

Given the following infix expression, which one of the following is the corresponding postfix expression? (a + b) * (c -d)/ (e + f) a) abcdef+*-/+ b) ab+ cd-*ef+/ c) a b + c d - e f + * / d) none of the above

b) ab+ cd-*ef+/

Which of the following List member methods should not be implemented for a SortedList? a) add (T newEntry) b) add (int newPosition, T newEntry) c) remove(int givenPosition) d) getEntry(int givenPosition) e) a and b f) a, b and c

b) add(int newPosition, T newEntry);

Which of the following is an advantage of using an array to implement the ADT bag? a) removing a particular entry requires time to locate the entry b) adding the entry to a bag is fast c) increasing the size of the array takes time to copy the entries d) the client has control over the size of the bag

b) adding is fast

In a binary search tree, the getInorderIterator inherited from the class BinaryTree sorts data a) descending order b) ascending order c) striped order d) none of the above

b) ascending order

A superclass is also called a(n) a) subclass b) base class c) composed class d) derived class

b) base class

When adding an entry to an array-based implementation of the ADT list at a specified location before the end of the list: a) a new entry must be allocated and linked to attach the new item b) entries must be shifted to vacate a position for the new item c) no shift is necessary d) the entry at the specified location will be overwritten by the new item

b) entries must be shifted to vacate a position for the new item

What type of behavior defines a queue? a) first in last out b) first in first out c) last in first out d) none of the above

b) first in first out

At the time a method is called during program execution, the activation record is pushed onto the: a) frame b) program stack c) execution stack d) none of the above

b) program stack

An incomplete definition of a method is called a _____. a) core method b) stub c) fail safe method d) security problem

b) stub

In the Ch 6 array-based implementation of a Stack ADT, the entry peek returns may be found at: a) the first location in the array b) the last occupied location in the array c) the last location in the array d) none of the above

b) the last occupied location in the array

If iteration has ended, the next method a) throws an EndOfIterationException b) throws a NoSuchElementException c) throws an IllegalStateException d) none of the above

b) throws a NoSuchElementException

Advantage(s) of an inner iterator class over a separate iterator class:

It has direct access to the ADT's data so it can execute more efficiently (Brady, it has nothing to do with the remove methods)

The most efficient approach to dealing with a gap left in an array after removing an entry from a bag is to

replace the entry being removed with the last entry in the array and replace the last entry with null

Runtime Exceptions

results from a logical error in a program.

Scope error? public static void main(String[] args) { int target = 4; target = mystery(target + 1); System.out.println(sum); } public static int mystery(int target) { int sum = 0; for (int i = 0; i < target; target++) { int target = i + 1; sum = sum + target; } return sum; }

sum is not declared

In the interface SearchTreeInterface, what does the method add return if the object being added already exists in the tree?

the existing entry that matched the parameter

For an array implementation of a stack, the efficient choice for the top is:

the last element in the array

Scope error? public static void main(String[] args) { int target = 4; target = mystery(target + 1); System.out.println(sum); } public static int mystery(int target) { int sum = 0; for (int i = 0; i < target; target++) { int target = i + 1; sum = sum + target; } return sum; }

there are two local variables named target that overlap

What does a primitive value represent?

A simple indecomposable value

What does it mean for a class to be final?

It cannot have subclasses

Write a recursive method for a finding the nth number in a fibonacci sequence:

/** * This method finds the nth number of a fibonacci sequence. * * @param n The number of terms in the fibonacci sequence. * @return The nth term of the fibonacci sequence */ // precondition: n is non-negative public static int fib(int n) { if (n == 0) { return 0; } else if (n == 1 || n == 2) { return 1; } return fib(n - 1) + fib(n - 2); }

Write a recursive method for isPalindrome:

/** * This method is awesome. * @param str Is the string * @return The Boolean */ public static boolean isPalindrome(String str) { if (str == null) { return false; } String newStr = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); if (newStr.equals("")) { return true; } else if (newStr.length() == 1) { return true; } else if (newStr.length() == 2) { return newStr.charAt(1) == newStr.charAt(0); } return newStr.charAt(0) == newStr.charAt(newStr.length() - 1) && isPalindrome(newStr.substring(1, newStr.length() - 1)); }

What are two cons of using an array to implement bags?

1. Removing a specific entry is slow 2. Expanding the array takes time

Generic types why?

1. So the client can choose what type of the objects in the collection. 2. You can restrict the types of entries in your collections to only objects related by inheritance. ***

What is Alex's Tinder radius?

40 miles

What was Alex's Maximum Tinder Radius ever?

65 miles

When to use <T>, T

<T> after identifier name in class header, not in the constructor. T can be a data type of all fields.

Difference between == and .equals

== refers to the same spot in memory (integers are different) .equals refers to the same value

Constructor for a Array Bag

@SuprpressWarnings("unchecked") bag = (T[]) new Object[capacity]; bag = tempbag; numberOfEntries = 0;

What is a set

A bag that can have no repeat items.

How is a binary tree different than a generic tree?

A binary tree has a max of 2 children per parent.

What is a completely balanced tree?

A full tree

Core group

A group of core methods.

What does a reference type represent?

A memory address instead of the actual item stored in that address.

What is an interior node?

A node that is not a leaf. A parent node is an interior node

ADT

A specification of a data set and the operations on that set.

What is a balanced tree?

A tree in which the left and right branches from the root are within 1 unit of the same height.

implementation

All the data fields and details of method definitions.

What is a checked exception?

An exception thrown when something goes wrong that if out of the programmers hands. They must be handled by the code.

Examples of runtime errors

ArithmeticException ArrayIndexOutOfBoundsException ClassCastException EmptyStackException IllegalArgumentException IllegalStateException IndexOutOfBoundsException NoSuchElementException NullPointerException UnsupportedOperationsException

Reference type

Array, Object, Class, Strings Something that can hold a primitive type

When are object types determined?

At runtime

What is levelorder traversal?

Begin at root and visit nodes one level at a time

Given an array of ints named values, what does the following code do: double total = 0; for(double element : values) { total = total + element; } double average = 0; if (values.length > 0) { average = total/values.length; }

Calculates the sum and average of the values

What does this(); do?

Calls the constructor for the current class that takes no arguments

What does super(); do?

Calls the constructor of the parent class that takes no arguments.

Examples of checked exceptions

ClassNotFound FileNotFound IO NoSuchMethod WriteAborted

Encapsulation

Encloses methods and data within a class while hiding the implementation details that are not necessary for using a class.

Given an array of ints named values, what does the following code do: for (int i = 0; i < values.length; i++) { values[i] = i * i; }

Fills the array with squares (0, 1, 4, 9, 16, ...)

Given an array of ints named values, what does this code do? double largest = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] > largest) { largest = values[i]; } }

Find the max value

What is a "has a" relationship?

For example, when a class called "Name" has a string in the field the two things have that relationship.

Stack: stack.push("K"); stack.push("P"); stack.pop(); stack.push("G"); String str = stack.peek(); stack.push("R"); stack.pop(); stack.push("V"); stack.push(str);

G V G K

queue: queue.enqueue("K"); queue.enqueue("P"); queue.dequeue(); queue.enqueue("G"); String str = queue.getFront(); queue.enqueue("R"); queue.dequeue(); queue.enqueue("V"); queue.enqueue(str);

GRVP

What is the length of an array?

How many little squares are in our array thing

What is the size of an array?

How many objects are present inside of the array

Given the following definition for BagInterface and skeleton for a Linked Bag class, complete the contains method. Write the method header and body for your response. Do not use break. /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1 */ /** An interface that describes the operations of a bag of objects. */ public interface BagInterface<T> { /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty. @return True if the bag is empty, or false if not. */ public boolean isEmpty(); /** Adds a new entry to this bag. @param newEntry The object to be added as a new entry. @return True if the addition is successful, or false if not. */ public boolean add(T newEntry); /** Removes one unspecified entry from this bag, if possible. @return Either the removed entry, if the removal was successful, or null. */ public T remove(); /** Removes one occurrence of a given entry from this bag. @param anEntry The entry to be removed. @return True if the removal was successful, or false if not. */ public boolean remove(T anEntry); /** Removes all entries from this bag. */ public void clear(); /** Counts the number of times a given entry appears in this bag. @param anEntry The entry to be counted. @return The number of times anEntry appears in the bag. */ public int getFrequencyOf(T anEntry); /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if the bag contains anEntry, or false if not. */ public boolean contains(T anEntry); /** Retrieves all entries that are in this bag. @return A newly allocated array of all the entries in the bag. Note: If the bag is empty, the returned array is empty. */ public T[] toArray(); //public <T> T[] toArray(); // Alternate //public Object[] toArray(); // Alternate } // end BagInterface /** A class of bags whose entries are stored in a chain of linked nodes. **/ public final class LinkedBag<T> implements BagInterface<T> { private Node firstNode; // Reference to first node private int numberOfEntries; public LinkedBag() { firstNode = null; numberOfEntries = 0; } // end default constructor ... /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if the bag contains anEntry, or false otherwise. */ public boolean contains(T anEntry) { WRITE CODE FOR THIS METHOD } // end contains private class Node { private T data; // Entry in bag private Node next; // Link to next node private Node(T dataPortion) { this(dataPortion, null); } // end constructor private Node(T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } // end constructor } // end Node } // end LinkedBag1 Your Answer: /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if the bag contains anEntry, or false otherwise. */ public boolean contains(T anEntry) { for (i = 0; i <= numberOfEntries; i++) { if (data.equals(anEntry)) { return true; } } return false; } // end contains

IDK YET

Given the following definition for BagInterface and skeleton for an ArrayBag class, complete the add method and helper isArrayFull method. When the array is full, the add method will return false, otherwise the addition should be successful. Write the header and body for both methods for your response. /** An interface that describes the operations of a bag of objects. */ public interface BagInterface<T> { /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty. @return True if the bag is empty, or false if not. */ public boolean isEmpty(); /** Adds a new entry to this bag. @param newEntry The object to be added as a new entry. @return True if the addition is successful, or false if not. */ public boolean add(T newEntry); /** Removes one unspecified entry from this bag, if possible. @return Either the removed entry, if the removal was successful, or null. */ public T remove(); /** Removes one occurrence of a given entry from this bag. @param anEntry The entry to be removed. @return True if the removal was successful, or false if not. */ public boolean remove(T anEntry); /** Removes all entries from this bag. */ public void clear(); /** Counts the number of times a given entry appears in this bag. @param anEntry The entry to be counted. @return The number of times anEntry appears in the bag. */ public int getFrequencyOf(T anEntry); /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if the bag contains anEntry, or false if not. */ public boolean contains(T anEntry); /** Retrieves all entries that are in this bag. @return A newly allocated array of all the entries in the bag. Note: If the bag is empty, the returned array is empty. */ public T[] toArray(); //public <T> T[] toArray(); // Alternate //public Object[] toArray(); // Alternate } // end BagInterface /** A class of bags whose entries are stored in an array. */ public final class ArrayBag<T> implements BagInterface<T> { private final T[] bag; private int numberOfEntries; private static final int DEFAULT_CAPACITY = 25; /** Creates an empty bag whose capacity is 25. */ public ArrayBag() { this(DEFAULT_CAPACITY); } // end default constructor /** Creates an empty bag having a given capacity. @param desiredCapacity The integer capacity desired. */ public ArrayBag(int desiredCapacity) { // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempBag = (T[])new Object[desiredCapacity]; // Unchecked cast bag = tempBag; numberOfEntries = 0; } // end constructor /** Adds a new entry to this bag. @param newEntry The object to be added as a new entry. @return True if the addition is successful, or false if not. */ public boolean add(T newEntry) { WRITE CODE FOR THIS METHOD } // end add // Returns true if the array bag is full, or false if not. private boolean isArrayFull() { WRITE CODE FOR THIS METHOD } // end isArrayFull ... } // end ArrayBag1 Your Answer: private boolean isArrayFull() { if (bag.size() < desiredCapacity) { return false; } return true; } public boolean add() { if (isArrayFull) { return false; } else { bag.add(newEntry); return true; } }

IDK YET

What is an error?

Indicates the occurrence of an abnormal situation such as running out of memory

What is prefix notation? How would you write x + y in prefix notation?

Infix notation is when operators (*+/- etc.) are written before operands. x + y would be +xy in infix notation **Also known as Polish notation

What is infix notation? How would you write x + y in infix notation?

Infix notation is when operators (*+/- etc.) are written in between operands. x + y would be x + y in infix notation

Of the two sorting algorithms we studied in lecture, which would do less work, in terms of comparisons and swaps, if executed upon an already sorted array?

Insertion Sort

What is an advantage of using a chain for a Bag ADT?

It avoids moving data when adding or removing bag entries.

What does it mean for a method to be final?

It cannot be overriden

Perform an analysis on the algorithm to determine its complexity class and choose its Big-O order below. Consider the following LinkedList member code which implements the algorithm: public int[] occursLater() { int[] occurs = new int[numberOfEntries]; Node first = firstNode; int position = 0; while (first != null) { occurs[position] = 1; T target = first.getData(); Node curr = first.getNextNode(); while (curr != null) { if (target.equals(curr.getData())) { occurs[position]++; } curr = curr.getNextNode(); } first = first.getNextNode(); position++; } return occurs; } This method returns an int array that contains counts of the number of times the element in the corresponding position in the list occurs in the list starting at its position through the end of the list. Thus every count will be at least one for the occurrence of the element itself plus the number of times it occurs in the following elements of the list. For example, given the following list on integer objects in memory: firstNode → 23 → 44 → 33 → 32 → 23 → 33 → 23 → 44 → 23 → 44 the invoked method, occursLater(), would return the array: index: 0 1 2 3 4 5 6 7 8 9 value: 4 3 2 1 3 1 2 2 1 1

O(n^2)

Checked Exceptions

Out of our control. Use a try catch method to recover gracefully

You should express the complexity of an algorithm in terms of its a) space requirements b) execution time c) problem size d) overall complexity

PROBLEM SIZEE

What is postfix notation? How would you write x + y in postfix notation?

Postfix notation is when operators (*+/- etc.) are written after their operands. x + y would be xy+ in postfix notation **Also known as Reverse Polish notation

What is this: ? extends Gizmo

Represents any subclass of Gizmo, so Gizmo is upper bound

What is this: ? super Gizmo

Represents any superclass of Gizmo, so Gizmo is lower bound

Examples of a virtual machine error

StackOverflow OutOfMemory

What are examples of reference types?

String, Scanner, Random, Die, int[], String[], and any other instantiable class

How do you determine the length of a path of a tree?

The amount of connectors between the top node and bottom node.

What does it mean for a reference type to be final?

The data within the variable can be changed but the type that it references cannot be changed

BST search, insert, and delete operations typically run in time O(d). What is d?

The depth of the relevant node in the tree

Relationship between Interface and implementation

The interface is for the client and it defines the public methods and variables. The implementation implements all of the methods in the interface but all the methods in the implementation do not have to be present in the interface.

Given the following, is the toString method from computer called or from notebook? Computer theComputer; theComputer = new Notebook("Bravo","Intel", 4, 240, 2/4, 15.07.5); System.out.println(theComputer.toString());

The notebook toString

private int sum = 0; private int product = 0; public static final int AMOUNT = 5; public void output(){ System.out.println("The sum of the numbers from 1 to " + AMOUNT + " is " + sum); System.out.println("The product of the numbers from 1 to " + AMOUNT + " is " + product); } public void calculate(){ int sum = 0; int product = 1; for (int i = 1; i <= AMOUNT; i++){ sum = sum + i; product = product * i; } output(); }

The sum of the numbers from 1 to 5 is 0 The product of the numbers from 1 to 5 is 0

What is the capacity of an array?

The total maximum highest amount of items that an array can hold.

What does it mean for a primitive type to be final?

The value cannot change

What Is taken into account when determining efficiency?

Time Memory (more important)

An algorithm has what requirements?

Time and space requirements

Why do we use inheritance?

To have a general class with common properties and behaviors shared by multiple more specialized classes.

True or false: A heap is an example of a complete binary tree?

True

True or false: When you print out the nodes of binary tree, the leaf nodes appear in the same relative order for the preorder, inorder, and postorder traversals.

True!

The operator == tests to see if:

Two variables reference the same place in the computer's memory

Advantages of Linked Bags

Uses memory only as needed and returns it when an entry is deleted.

Scope error

Using a local variable name when there is a field variable wit that name. And when you try to call a variable from another method where its local.

What is a runtime error?

Usually the result of a logic error. Usually code is not written to handle these but the original code is fixed in order to get rid of them completely.

What is postorder traversal?

Visit root of a binary tree after visiting nodes in root's subtrees

What is inorder traversal?

Visit root of a binary tree between visiting nodes in root's subtrees.

What is a preorder traversal?

Visit the root before visiting the root's subtrees

client interface

What the client code needs to know to use a class.

What is an enumeration of a tree's nodes?

When a traversal visits every node in a tree exactly once

Resizing

When an array bag becomes full a new array is created tat is twice the size of the old array and the old contents are referenced by spots in the new array. Then the old array is discarded.

What is overloaded?

When one method has the same name but a different set of parameters than a second method

Implementing two interfaces

When two interfaces are implemented by a class that class must define methods for all of those in each interface.

How does referencing a parent/child class work?

You can do Car = new Subaru; but not Subaru = new Car;

What is a bubble sort?

You compare two items until the biggest value is all the way to the right

A key benefit of inheritance is a) an object can have several types, it is compatible with any of its superclasses b) It allows the invocation of this() c) private, public and protected visibility modifiers can be used on all members d) only some of the parent's methods are applicable in the child class

a) an object can have several types, it is compatible with any of its superclasses

An expression that has correctly paired delimiters is called a(n): a) balanced expression b) Reverse Polish expression c) infix expression d) algebraic expression

a) balanced expression

Recursive methods need a(n) a) base case b) for loop c) trace d) all of the above

a) base case

Java sorting implementations sort objects that implement the _________ interface: a) comparable b) sortable c) hierarchial d) All of the above

a) comparable

In the LList implementation of a list, given a node called currentNode, which statement moves the node's reference to the next node? a) currentNode = currentNode.getNextNode(); b) currentNode.get(); c) currentNode.forward(); d) currentNode.traverse();

a) currentNode = currentNode.getNextNode();

How many recursive calls will be made if the following method is called with 6 from main? void greeting(int n) { System.out.println("Hello!"); greeting(n-1); } a) infinite b) 7 c) 6 d) 5

a) infinite Why? no base case - needs a if( n > 0) or something

Placing the Node class inside the LinkedBag class makes it a(n) a) inner class b) outer class c) dependent class d) inherited class

a) inner class

If you plan for a future subclass to be allowed to manipulate a class's data fields, provide _____ methods to enable the subclass to do this safely and efficiently. a) protected b) private c) public d) limited

a) protected

An iterative version of a level order traversal for a binary tree uses a(n): a) queue b) priority queue c) stack d) bag

a) queue

In the Carrano ListInterface, the method with the signature: public void remove(int givenPosition); does which of the following: a) removes the entry at a given position from the list b) increases the size by 1 c) does not affect any other entries on the list d) all of the above

a) removes the entry at a given position from the list

Selecting the smallest element of an array and swapping it with the smallest entry is an operation in which sorting method? a) selection sort b) insertion sort c) shell sort d) all of the above

a) selection sort

An iterative version of a preorder traversal for a binary tree uses a(n): a) stack b) queue c) priority queue d) queue e) bag

a) stack

To efficiently remove a node at the end of a linked chain implementation of a queue requires a: a) tail reference b) traversal c) extra reference in the node pointing to the previous d) none of the above

a) tail reference

The node that is easiest to access in a linked-chain is: a) the head node b) the tail node c) access time is the same for all nodes d) it cannot be determined

a) the head node

The inorder predecessor of a node N is a) the largest entry in N's left subtree b) the largest entry in N's right subtree c) the smallest entry in N's left subtree d) the smallest entry in N's right subtree

a) the largest entry in N's left subtree

In the linked chain implementation of a queue, the chain's first node contains: a) the queue's front entry b) the queue's back entry c) both a & b d) none of the above

a) the queue's front entry

When you remove an item from a stack, you remove it from: a) the top b) the bottom c) the middle d) wherever the client specifies

a) the top

If the SortedList class inherited the remove by position method from the LList class: a) we would not have to implement it again b) we would have to implement it again c) we would not be able to write it the way we wanted to d) none of the above

a) we would not have to implement it again

How many recursive calls will be made if the following method is called with 6 from main? void greeting(int n) { if (n > 0) { System.out.println("Hello!"); greeting(n-1); } } a) infinite b) 7 c) 6 d) 5

c) 6

The use of a generic data type is preferred over using Object as a general class in a collection because a) It allows the data type to be determined later by the client of the class. b) It can contain objects of type OrderedPair c) It ensures that all the members of the collection are objects related by inheritance.

c) It ensures that all the members of the collection are objects related by inheritance.

Where does a queue add new items? a) randomly b) in the middle c) at the back d) at the front

c) at the back

An algorithm has: a) time requirements b) space requirements c) both a & b d) none of the above

c) both a & b

After a call to remove, the nextPosition data field should be a) set to 0 b) left unaltered c) decremented d) incremented

c) decremented

When a linked chain contains nodes that reference both the next node and the previous node, it is called a(n) a) multi- linked chain b) two-way linked chain c) doubly linked chain d) ordinary chain

c) doubly linked chain

To prevent a subclass from overriding protected methods, we declare them to be a) limited b) private c) final d) protected

c) final

In the Carrano LList implementation of a list, when a list is empty the firstNode is _____ and the numberOfEntries is _____. a) an empty node, 0 b) an empty node, 1 c) null, 0 d) null, 1

c) null, 0

In a circular array-based implementation of a queue, the initial size of the array should be: a) two more than the queue's initial capacity b) two less than the queue's initial capacity c) one more than the queue's initial capacity d) one less than the queue's initial capacity

c) one more than the queue's initial capacity

You should express the complexity of an algorithm in terms of its: a) space requirements b) execution time c) problem size d) overall complexity

c) problem size

The fixed array implementation of the method remove that has no parameter in the bag: a) removes the first entry in an array b) removes a random entry in the array c) removes the last entry in the array d) none of the above

c) removes the last entry

In your reading, the pop and peek methods throw a _______________ exception when the stack is empty. a) checked b) compile time c) runtime d) stackEmpty

c) runtime

When too many recursive calls are made creating more activation records than the allocated program memory can handle, what kind of error occurs? a) recursive overflow b) infinite recursion c) stack overflow d) activation record overflow

c) stack overflow

Problem size is defined as a) the amount of memory an algorithm uses b) the amount of execution time an algorithm takes c) the number of items an algorithm processes d) none of the above

c) the number of items an algorithm processes

Problem size is defined as: a) the amount of memory an algorithm uses b) the amount of execution time an algorithm takes c) the number of items an algorithm processes d) none of the above

c) the number of items an algorithm processes

The largest entry in a node N's right subtree is a) right child of N b) left child of N c) the subtree's rightmost node d) the left subtree's node

c) the subtree's rightmost node

Q 11: Given the linked chain above, which is the resulting linked chain after https://canvas.vt.edu/courses/31277/quizzes/37119?module_item_id=132218

d)

What question should you keep in mind when debugging a recursive method? a) Does each base case produce a result that is correct for that case? b) Are there enough base cases? c) Is at least one of the cases a base case that has no recursive call? d) All of the above

d) All of the above

In a linked-based implementation of the ADT list with a tail reference, what is the performance of adding an entry at the end of the list? a) O(n^2) b) O(n) c) O(logn) d) O(1)

d) O(1)

In a chain of linked nodes you can: a) add nodes from the beginning of a chain b) add nodes from the end of a chain c) add nodes that are between other nodes d) all of the above

d) all of the above

In the Carrano ListInterface, the method with the signature: public void add(int newPosition, T newEntry); does which of the following a) adds a new entry to the specified position in the list b) increases the list size by 1 c) moves entries originally at or above the specified position one position higher d) all of the above

d) all of the above

In the Ch 6 linked-chain implementation of a Stack ADT, when a node is popped from the stack: a) the original first node will no longer be referenced b) the original first node will be deallocated c) the new first node will reference what was the second node in the chain d) all of the above

d) all of the above

What happens when you use the new operator in the LinkedBag constructor? a) a new node is created b) the JRE allocates memory for a node object c) a new object is instantiated d) all of the above

d) all of the above

What issue should you weigh when considering an implementation for an ADT? a) execution time b) memory use c) extensibility d) all of the above

d) all of the above

Which method does the interface Iterator specify? a) hasNext b) next c) remove d) all of the above

d) all of the above

You can add a new entry in a list: a) at the beginning b) at the end c) in between items d) all of the above

d) all of the above

A variable declared as an interface type can reference a) abstract classes b) static methods c) an object of a class that has the same methods as the interface d) any object of a class that implements that interface

d) any object of a class that implements that interface

Where will you find the item added earliest to a queue? a) randomly b) in the middle c) at the back d) at the front

d) at the front

A subclass is also called a(n) a) super class b) base class c) composed class d) derived class

d) derived class

Given the following method: public int func() { if (y == 1) return x; else return x + func(x, y+1); } What will be returned when func(2,3) is executed?

d) infinite recursion

In an array- based implementation of the ADT list, the makeRoom method does the most work when: a) newPosition is n-1 b) newPosition is n/2 c) newPosition is n d) newPosition is 1

d) newPosition is 1

Because SortedList is derived from LList, you write _____.add(newPosition, newEntry) to invoke the add operation of the ADT list a) invoke b) sub c) this d) super

d) super

Given the Computer and Notebook class definitions from the previous question and: Notebook yourComputer = new Notebook("DellGate", "AMD", 4, 240, 1.8, 15.0, 7.5); Computer anotherComputer = new Computer("Acme", "Intel", 2, 160, 2.4); Computer anotherLaptop = new Notebook("Acme", "Intel", 2, 160, 2.4,7,8); Notebook moreLaptop = new Notebook("Acme", "Intel", 2, 160, 2.4,7,8); Which line of code doesn't compile? a) anotherLaptop = yourComputer; b) yourComputer = moreLaptop; c) yourComputer = (Notebook) anotherLaptop; d) yourComputer = anotherLaptop;

d) yourComputer; = anotherLaptop

A class uses composition when it:

declares an instance of another class as a data field

firstNode → "D" ⇄ "E" ⇄ "S" ⇄ "I" ⇄ "G" ⇄ "N" ← lastNode Node<String> p = firstNode; Node<String> r = lastNode; p = firstNode; r = p.next; lastNode.next = p; p.previous = lastNode; p.next = null; lastNode = p; firstNode = r; firstNode.prev = null;

firstNode → "E" ⇄ "S" ⇄ "I" ⇄ "G" ⇄ "N" ⇄ "D" ← lastNode

Which method returns a count of the current number of items in a bag?

getCurrentSize()

A reference to the first node in a linked list is called the _____ reference.

head

What is an "is a" relationship?

inheritance is an example of this kind of relationship

Primitive Type

int, float, double, boolean A simple indecomposable value. Does not have an API

The data in a BST's node's _____ subtree are less than the data in a node's _____ subtree.

left, right

To remove a specified entry of a bag that is implemented with a linked chain, first get a reference to the node to remove, then the most efficient approach is to:

move the data in the first node to the referenced node , then remove the first node

What is the number of nodes of a full binary tree as a function of height?

nodes = 2^(height) - 1

In the interface SearchTreeInterface, what does the method getEntry return if the object being sought doesn't exist?

null

Which of the following visibility modifiers should be used for a field in a class C if the programmer does not want the the subclass to have direct access to the field? a) public b) private c) protected d) package

private


Kaugnay na mga set ng pag-aaral

[BSC1005] Extra Credit 4 - Answers

View Set

Chapter 7 Photosynthesis: Using Light to Make Food

View Set

Chapter 17: Pregnancy at Risk: Pregnancy-Related Complications

View Set