QUIZ 1 - 14

Ace your homework & exams now with Quizwiz!

Match each of the 4 pillars of Object oriented programming with its description

- Abstraction: Allows you to focus on only the things that you need to be aware of without worrying - Encapsulation: Hides certain data and implementation details by access modifiers which allows the information... - Inheritance: process of making sub-types of other types - Polymorphism: technique of overriding methods as parent type in a child type so that the child can behave differently

Which of the following are reasons to mark data members as private. (Select all that apply)

- It allows the class to have control over modifications done to that variable (so it can do things like prevent setting a number to a negative value for example) - It prevents external classes from depending on internal details that may change in a future implementation

Which of the following is a reason to mark data members as private (Select all that apply)

- It allows the developer to change internal implementation without affecting other developers that use the software - It helps a programmer ensure that a value is set to valid values

Which of the following are true properties of a List? (Select all that apply)

- Lists allow duplicates - Lists elements are accessible by their indexes

Match each Big O with its description:

- O(1): constant - O(n): linear - O(log(n)): logarithmic - O(n^5): polynomial - O(n!): factorial - O(5^n): exponential

Match each Big O with its description

- O(1): constant time - O(n): linear time - O(log(n)): logarithmic time - O(n^2): quadratic time - O(n!): factorial time - O(2^n): exponential time

Which of the following inheritance relationships show a proper example of using an "Is-A" relationship (Select all that apply)

- Parallelogram extends Quadralateral - Oven extends Appliance

Which of the following inheritance relationships shows a proper example of using an "Is-A" relationship. (Select all that apply)

- Pringles extends Chips - Dog extends Mammal

Match each access level with its description

- public: all classes can access this member - private: only the class containing the member can access the member - protected: only classes with the same package or children of the class can see the member - packaged: only classes with the same package can access the member

Match the following term with its definition

- root node: the top level node in a tree (i.i., has no parent) - leaf node: a node that has no children - inner node: a node that has at least one child

Which of the following is a purpose of a constructor (Select all that apply)

- to perform any operations that are required when an object is created - to create a new object -to set initial values for data members of an object

Given the following tree: 8 / \ 7 10 / 9 What is the balance factor of the root node?

-1

Given an empty stack, what are the contents of the stack after performing the following operations: push(5) push(3) pop() push(7) peek() push(9) pop() push(0) (Note in the answers, the leftmost number is the "top" of the stack)

0, 7, 5

Given the following AVL tree: 10 / \ 6 14 \ 16 What will the tree look like after inserting 18?

10 / \ 6 16 / \ 14 18

Given the following tree: 10 / \ 7 14 \ /\ 8 11 15 \ 13

11 / \ 7 14 \ / \ 8 13 15

The following lines show the state of an array after each swap in a bubble sort. 5, 4, 2, 8, 3 4, 5, 2, 8, 3 4, 2, 5, 8, 3 4, 2, 5, 3, 8 Which of the following shows the state of the array after the next swap

2, 4, 5, 3, 8

Given the following directed non-weighted graph, what is the length of the shortest path from A to G A -> {B, C} B -> {E} C -> {B} D-> {C, F} E-> {C, D, F} F-> { E, G} G -> {A, B, C}

4

Given the following weighted adjacency matrix (rows represent the sources of edges and columns represent the destinations). | 1 | 2 | 3 | 4 | 1 | | 1 | 5 | | 2 | | 3 | 2 | 7 | 3 | | | | 1 | 4 | 2 | | | |

4

Given the following AVL tree: 4 / \ 2 6 \ / \ 3 5 7 What would the result be after adding 9?

4 / \ 2 6 \ / \ 3 5 7 \ 9

What is the output of the following program: public class Foobar { public static int foo(int x) { x += 1; System.out.print(x); return x; } public static void bar() { System.out.print("3" + foo(foo(4))); } public static void main(String args[]) } System.out.print("4"); bar(); } }

45636

Given the following array of numbers 56, 23, 46, 34, 76, 19, 20, 43, 6, 34, 9, 10, 54 Which two numbers will be swapped in the first swap of a selection sort?

56 and 6

Given the following AVL tree: 4 / \ 2 6 \ / \ 3 5 7 \ 8

6 / \ 4 7 / \ \ 3 5 8

Given the following tree: 8 / 6 \ 7

8 / 7

Given the following AVL tree: 10 / \ 6 12 / \ 4 8 What tree would result from adding 9?

8 / \ 6 10 / / \ 4 9 12

What is the result of adding 80 to the given max-heap: 86 / \ 73 42 / \ / \ 13 9 14 36

86 / \ 80 42 / \ / \ 73 9 14 36 / 13

At what line will there be a compilation error? public class Generics { public static void main(String[] args) { B<String> b = new B<>(); /* LINE A */ B<A> b2 = new B<>(); /* LINE B */ B<C> b3 = new B<C>(); /* LINE C */ C c2 = b4.process(new C()); /* LINE D */ } } interface A { int count(); void show(); } class B<T extends A> { T process(T t) { t.count(); t.show(); return t; } } class C implements A { public int count() { return 25; } public void show() { System.out.print("Class C"); } }

A

Which of the following statements about Hash Tables is FALSE?

A Hash Table is divided into buckets that always hold a maximum of one value

Which of the following is a useful benefit to using a LinkedList over an ArrayList in Java?

A LinkedList has constant time insertion at the front of the list (index 0)

Which of the following statements is not true concerning linear and binary search functions.

A binary search is always faster than a linear search

Which of the following is a difference between a queue and a deque

A deque allows adding and removing from both the beginning and the end

Which of the following is a benefit of a heap over a BST if all you need to ever do is add items and retrieve the minimum value

A heap can be implemented in a single array which yields memory savings

Which of the following correctly describes an abstract method

A method signature that has no associated method implementation in a class and therefore must be implemented in child classes

Given the following bi-directional (or un-directed) graph, which of the following is a breadth-first traversal starting with 'A'? A---B ___ / \ / \ C D --E--F \______/ (If there are multiple choices about which node to visit next, choose the one that comes first alphabetically)

ABCDFE

Given the following bi-directional (or un-directed) graph, which of the following is a depth-first traversal starting with 'A'? A---B ___ / \ / \ C D --E--F \______/ (If there are multiple choices about which node to visit next, choose the one that comes first alphabetically). Note that in class we discussed that there are a few algorithms that can be used for depth first searches. Only one of the answers below is a valid depth first search though, so don't think too much about the exact algorithm, but think more about the concept of what a depth first search is).

ABDEFC

What are the two common approaches to representing a graph data structure that we have covered so far?

Adjacency list and adjacency matrix

Which of the following is an advantage of an adjacency list implementation over an adjacency matrix

Adjacency lists use much less memory in sparse graphs

Which of the following is a benefit of an ArrayList over a LinkedList

Any item in an ArrayList can be accessed by its index in constant time.

For each data structure, fill in the appropriate average Big O for each operation ArrayList

ArrayList - Insert beginning: O(n) - Insert end: O(1) - Insert (middle): O(n) - Access by index: O(1) Singly Linked list without tail reference - Insert beginning: O(1) - Insert end: O(n) - Insert (middle): O(n) - Access by index: O(n) Doubly Linked list with tail reference - Insert beginning: O(n) - Insert end: O(N) - Insert (middle): O(n) - Access by index: O(n)

Why is the following tree not considered a min-heap? 32 / \ 45 67 / \ / \ 12 49 78 89

Because the 12 is a child of the 45

Which of the following is the advantage of Bellman-Ford's algorithm over Dijkstra's?

Bellman-Ford's algorithm supports negative edges

Which of the following is the difference between a binary search tree and regular binary tree

Binary Search Trees have an order property that allows for quick searching

Which of the following best describes the difference between checked and unchecked exceptions in Java

Checked exceptions may only be thrown inside a try block or in a method that declares that it throws the exception

Which of the following is a useful benefit to using an ArrayList over a LinkedList in Java?

Constant time access to elements by index

Which of the following is an advantage of a matrix implementation over the adjacency list approach

Determining if two nodes are adjacent can be done in O(1) time

Which of the following is the advantage of Dijkstra's algorithm over Bellman-Ford's?

Dijkstra's algorithm is usually faster than Bellman-Fords

What is the difference between a doubly-linked list and a singly-linked list

Each node in a doubly linked list has a reference to the next node and the previous node whereas nodes in a singly linked list only have references to the next node

What is the output of the following code: public class MyClass { public static void main(String args[]) { System.out.println("Here's a number: " + 3 + 9); } }

Here's a number: 39

Which of the following is a requirement for a validhash function (note that a valid hash function is not the same thing as a good hash function).

If two items are considered equal based on the .equals method, they must have the same hash code

Which of the following is the primary disadvantage to always selecting either the first or last item in an array for a quick sort pivot

In a sorted list, this will cause the sort to run in O(n^2) time

Which of the following is generally considered to be a slow sorting algorithm, but actually results in linear runtime when the list is already sorted or nearly sorted.

Insertion Sort

Which sorting algorithm does the following describe: Consider the left side of an array a sorted part (initially with 1 value). 1. Select the first value to the right of the sorted portion of your array. 2. Repeatedly swap that value with the value to the left of it until it reaches a value that is less than or equal to it. 3. Now consider your sorted part to be one element longer 4. Repeat the process starting at step one until your sorted part includes the entire array.

Insertion sort

Which of the following best describes how Java's implementation of ArrayList handles adding a new element once its internal array is already full.

It creates a new array that is twice the size of the old array and then copies the contents into it

Which of the following is true about an AVL tree?

It is a binary search tree with ordering properties such that the runtime of a contains method is always O(log(n))

Which of the following best describes why the code below does not compile. public static <T1 extends Comparable<T1>, T2 extends Comparable<T2>> Object min(T1 val1, T2 val2) { return val1.compareTo(val2) < 0 ? val1 : val2; }

Just because T1 implements Comparable<T1> does not mean that you can call .compareTo(T2) on it.

If you are using a TreeMap<K,V> (java's binary search tree implementation of map which uses type K for the key and V for the value), which of the following must you ensure about the types K and V? (Note that we didn't really talk about this in class, so this is going to take some critical thinking, but think about how a map works and how a binary search tree works and you should be able to figure this out).

K should implement Comparable<K> and have a valid compareTo and equals method

Given the following code: public interface Authenticator { public boolean isUser(String userName); } public class FingerPrintAuthenticator implements Authenticator { public boolean isUser(String userName) { FingerPrint print = readFingerPrint(); return isMatch(print, userName); } public FingerPrint readFingerPrint() { // code here to get fingerprint } public boolean isMatch(FingerPrint print, String name) { // Code here to check if it's a match } } public class FingerPrint { /* blah blah blah */ } public static boolean passesAllAuthenticators(List<Authenticator> authenticators, String name) { //Line A for(Authenticator authenticator : authenticators) { //Line B if(!authenticator.isUser(name)) { //Line C System.err.println("Failed authentication: " + authenticator.toString()); //Line D System.err.println("FingerPrint info: " + authenticator.readFingerPrint()); //Line E return false; } } //Line F return true; } Which line would result in a compiler error?

Line D

Which of the following describes Java Byte Code

Low level code that is interpreted by a Java Virtual Machine

What does the protected keyword do?

Makes a class's member accessible within a class and its subtypes (and other types in the package), but not available to external classes

Which of the following is a disadvantage of merge sort in comparison to quick sort?

Merge sort usually requires more memory to perform

If you have a hash map that utilizes chaining and it has chains of an average of 4 elements in length, what is the big O of calling "contains" on the map?

O(1)

What is the Big O of the following growth function: f(n) = 4 + 9

O(1)

What is the runtime of finding the maximum item in a max heap (note, this is asking for just the runtime of finding the max item, not finding and removing).

O(1)

What is the Big O of the following function: f(n) = 2^n + 2*n^2 + n - 3^9

O(2^n)

If data that is already in sorted order is inserted into an AVL tree, what will the Big O of the contains method be?

O(log(n)

For some reason, I forgot that email exists and decide that our class needs a phone tree in order to disseminate information. A phone tree works as follows: When I have an announcement, I call 2 specific students and tell them the announcement. Those 2 students are each assigned to call 2 other students. Each of those 4 students are assigned to call 2 other students. The process repeats until all of the students are called (note that the assignment structure is created before hand and guarantees that no one is called twice). If our class size is represented by "N" then what is the Big O of sending out an announcement over the call tree?

O(log(n))

What is the Big O of the following code segment while(n > 1) { System.out.println(n); n /= 2; }

O(log(n))

What is the Big O of the following code segment for(int i = 0; i < n; i *= 2) { System.out.println(n); }

O(log(n))

What is the average run-time of the insert operation on a binary search tree

O(log(n))

What is the average run-time of the remove operation on a binary search tree

O(log(n))

what is the average run-time of the "contains" method on a binary search tree

O(log(n))

What is the Big O of the following code segment for(int i = n; i > 3; i--) { System.out.println(n); }

O(n)

What is the Big O of the following code segment for(int i = 0; i < n; i++) { System.out.println(n);}

O(n)

What is the Big O of the following growth function: f(n) = 4*n + log(n)

O(n)

What is the Big O of the following growth function: f(n) = 6*n + 32*log(n)

O(n)

What is the Big O of the following growth function: f(n) = n*3*log(n) + log(n^3) Note, you may want to brush up on your math and think about what log(n^3) really means (and what it can be simplified to).

O(n*log(n))

What is the runtime of a quick sort

O(n*log(n))

Imagine that we have created a list implementation called BadArrayList which is identical to Java's ArrayList class, except that whenever it needs to increase it's array size, it only increases it by one. What is the Big Oh of the following code: public List<Integer> getNumbers(int size) { BadArrayList<Integer> list = new BadArrayList<>(); for(int i = 0; i < size; i++) { list.add(42); } return list; }

O(n^2)

What is the Big O of the following code segment for(int i = 0; i < n; i++) { for(int j = 0; j < i; j ++) { System.out.println(n); } }

O(n^2)

What is the Big O of the following code segment for(int i = 0; i < n; i++) { for(int j = 0; j < n; j += 2) { System.out.println(n); }}

O(n^2)

What is the Big O of the following growth function: f(n) = 3^2 + 2*n^2 + n - 3

O(n^2)

What is the runtime of a Bubble sort

O(n^2)

Which of the following types cannot be passed in as parameters to the following method (hint you may want to check the Java documentation for each of these classes). public static <T extends Comparable<T>> min(T v1, T v2) { return v1.compareTo(v2) < 0 ? v1 : v2; }

Object

Why would you never want to perform a binary search on a sorted linked list?

On average, it would result in worse performance than a linear search

Which of the following data structures is considered a FIFO (sometimes also called LILO) data structure?

Queue

Which of the following is a disadvantage of quick sort in comparison to merge sort

Quick sort does not always split the array very evenly at each recursive step

Which of the following choices is NOT related to Encapsulation?

Static methods

Given the following class: public class Size{ private double width; private double height; private double length; public Size(double width, double height, double length) { width = width; height = height; length = length; } //... } which of the following is a problem with this class:

The constructor does not properly initialize the member variable

Which of the following best describes polymorphism?

The practice of overriding a supertype's methods in its subtype to modify its behaviour

Which of the following is the best description of inheritance in the context of object oriented programming.

The principle of extending a class in order to make a subtype with different or additional data members and or member methods

The following code has a compiler error: public class Pair<T> { private int first; private int second; public Pair(T first, T second) { this.first = first; this.second = second; } public T getFirst() { return first; }public T getSecond() { return second; } public static void main(String args[]) { Pair<Integer> myPair = new Pair<>(43, 23); System.out.println(myPair.getFirst() + ", " + myPair.getSecond()); } }

The type of first and second should be "T" instead of "int"

The minimum distance between two vertices in a graph does not exist if which of the following conditions is true

There is a cycle with a negative weight along a path from the starting node to the ending node

Java's List<E> interface uses a generic type parameter to represent the type of data stored in the list. Which of the following is not an advantage of using generics instead of just making the list's data type "Object".

Using a generic parameter allows us to add an item of one type at one index and another type at another index.

You are given an Object that implements the Collection<E>. You know that it correctly implements the interface as defined by the java Collection<E> interface documentation, but you know nothing else about the underlying data structure (i.e. it could be a stack, a queue, a list, etc.). Which one of the following assumptions are guaranteed to be correct. (Note, you may want to consult the Collection interface documentation online).

after calling add, if add returns true, then a subsequent call to contains() with the same object will return true

Match the following Java primitive types with their descriptions

bool: true or false value int: non-fractional value long: integer type that stores data in more bytes than an int short: integer type that stores data in fewer bytes than an int char: type used to store ascii values that should be interpreted as characters byte: stores 8 bits of data float: stores a number that can contain a fractional component double: stores a floating point number using twice as much space as a float

Given the following method signature: public <T extends Comparable<T>> void doSomething(T v1, T v2) Which of the following will not result in a compiler error

doSomething("Hello", "hello");

Which of the following is generally considered to be a slow sorting algorithm, but actually results in linear runtime when the list is already sorted or nearly sorted

insertion sort

Which of the following represents the most common method signature for a "getter" or "accessor" method for a double variable called "height". (Note a method "signature" is the part of the method that defines the name, return type and parameter types for a method).

public double getHeight()

Which of the following is the proper way to declare a method that takes an integer as input and returns nothing

public void foobar(int val) { //code here }

Given the following BST, where should the number "45" be inserted? 67 / \ 48 83 / \ 43 51

the right child of 43

Linear probing is a method for dealing with collisions and involves storing multiple items into the same bucket via a list.

true


Related study sets

part 1 Final exam practice one rough draft new

View Set

1 Measurements and uncertainties

View Set

Cultural Anthropology: How Race Becomes Biology: Embodiment of Social Inequality

View Set

ESL: What's on Your Bucket List?

View Set

AP Environmental Science (APES) Chapter 1-9 Review Questions

View Set

Ch. 9 HW Quiz: Lifespan Development

View Set

ionic bonding and ionic compounds

View Set