class testQuestions

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

In a recursive sequential search of an unsorted array we search elements from

0to n-1

Given the following array, how many comparisons to an array entry are performed to search for the number 2 if you use the binary search algorithm? 2 3 5 7 11 13 17 19 23 29 31 37

5

Buble Sort

O(n^2) in all cases

The nth partial sum of the infinite series is described by the following formula, n ∑ k = n(n+1)/ 2. Which of the following is the correct big-O k = 1 expression for 1+2+3+...+n?

O(n²)

Which of the following formulas in big-O notation best represent the expression n²+ n²+5n+6000?

O(n²)

Which of the following formulas in big-O notation best represent the expression n³ + n²+35n+6

O(n³)

The operation for removing an entry from a stack is traditionally called__________

POP

One difference between a queue and a stack is:

Queues use two ends of the structure; stacks use only one.

time efficiency of selection sort?

Worst case : Quadratic O(n^2) in all cases.

Can two different classes contain methods with the same name?

Yes even though the exam says no

________________ is the acronym for last-in, first-out

LIFO

The Big-Oh (O) notation measure the performance of an algorithm at the best case.

false

In the following code that uses recursion to find the greatest common divisor of a number, what is the base case? public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); }

if (x % y == 0) return y;

In the following code that uses recursion to find the factorial of a number, what is the base case? private static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }

if(n== 0)

To return an array of long values from a method, which return type should be used for the method?

long[]

When an array of objects is declared but not initialized, the array values are set to___________

null

Any problem that can be solved recursively can also be solved iteratively, with a loop.

true

Objects in an array are accessed with subscripts, just like any other data type in an array.

true

You can remove from a bag an object having either a given value or one that is unspecified. You also can remove all objects from a bag.

true

What mathematical technique is often used to prove the correctness of a recursive method?

(Mathematical induction)

Write a recursive method that counts the number of nodes in a chain of linked nodes.

/** Recursively counts the nodes in a chain. @param start The first node. @returns The number of nodes in the linked chain. */ public int countNodes(Node start) { //complete the code here } public int countNodes(Node start) {if(start == null){return null;} else {countNodes(Node start);start.next;}}

By default, Java initializes array elements to _________

0

Suppose you have int b = new int[112]. What are the highest and lowest legal array indexes for b?

0 and 111

Which of the following applications may use a stack? A)Keeping track of local variables at run time. B)adding a book on to a pile of books A) parentheses balancing program. D)All of the above

All of the above

You use fewer comparisons in a recursive search than a sequential search on an unsorted array.

False

Given the following dataset, which sorting method would be recommended? 3 7 9 4 2 5

Iterative sequential search on an unsorted array

What kind of list is best to answer questions such as "What is the item at position n?"

Lists implemented with an array.

In the linked list implementation of the queue class, where does the insert method place the new entry on the linked list?

At the tail

A search on a data set that divides the data roughly in half every iteration is called a(n) _____ search

binary

A(n) __________ is used as an index to pinpoint a specific element within an array

subscript

A(n) __________ is used as an index to pinpoint a specific element within an array.

subscript

in java.util package, public class HashMap<K,V> extends __________ <K,V> implements _________<K,V>, Cloneable, SerializableA

(AbstractMap, Map)

_________________is an exception thrown when an index of some sort (such as to an array, to a string, or to a vector) is out of range

(IndexOutOfBoundsException)

In a selection sort structure, there is/are?

Two for loops, one nested in the other.

What is a primary difference between an array and a Vector from Java's Class Libraries:

Vector grow automatically as needed.

in java.util package, public class Stack<E> extends ____________________

Vector<E>

Assume you have an array of unsorted items that allows duplicate entries and that the item you are searching for is present. Using an iterative sequential search on an unsorted array, the loop exits whe

it locates the first entry in the array that matches the item being searched for

What is the primary purpose of a constructor?

To initialize each object as it is declared

A bag is a finite collection whose entries are in no particular order.

True

A sequential search on a very large array is very inefficient.

True

All objects have the method equals.

True

Recursion can be a powerful tool for solving repetitive problems and is an important topic in upper-level computer science courses

True

Recursion can be a powerful tool for solving repetitive problems and is an important topic in upper-level computer science courses.

True

The worst-case time efficiency of a sequential search on an array is

O(N)

Quick Sort

O(n*log(n)) average css and best case, O(n^2) worst case. space constant and stable.

The operation for adding an entry to a stack is traditionally called____________

Push

_____________ is an error which Java doesn't catch wen the stack runs out of space.

StackOverflowError

Assume that you have an array of decimals. The value in index 66 has a memory address of 1234, what is the memory address of an element that is stored in the index number 76 ?

1314

Consider this code using the ArrayBag of Section 5.2 and the Location class from Chapter 2. What is the output? Location i = new Location(0, 3); b.add(i); b.add(i); System.out.println(b.countOccurrences(i));

2

What will be the value of x[8] after the following code is executed? final int SUB = 12; int[] x = new int[SUB]; int y = 20; for(int i = 0; i < SUB; i++) { x[i] = y; y += 5; }

60

Merge Sort

A list is split into individual lists, these are then combined (2 lists at a time). efficiency:O(n*Log n) in all the 3 cases (worst, average and best)

If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed?

DCBA

A binary search is usually much faster than a sequential search on unsorted data stored in a chain of linked nodes

False

A binary search is usually slower than a sequential search on sorted array of data.

False

A search can only be performed iteratively

False

_________is the acronym for last-in, first-out

LIFO

The best case running time of insertion sort is

Linear O(n)

In a real computer, what will happen if you make a recursive call without making the problem smaller?

The run-time stack overflows, halting the program

Question 51. what is the problem with the following code describe it. public class AddMe { static int num = 0; public static int calc(int x) { num = num + 1; System.out.println(num); // add code here return num + calc(num + 1); } public static void main(String[] args) { AddMe.calc(num); } }

You are using The AddMe class .calc to call a method in the same class there fore this call would not run at all. Further more if I fix it the method will run endlessly there should be a way to make sure that the run-time stack overflows is not triggered.public class AddMe { public static void main(String[] args) { calc(num); } static int num = 0; public static int calc(int x) { num = num + 1; int result = 0; if(num <= 1200){ result = num + calc(num + 1); } System.out.println(num); // add code here return result; } }public class AddMe { public static void main(String[] args) { calc(num); } static int num = 0; public static int calc(int x) { num = num + 1; int result = 0; if(num <= 1200){ result = num + calc(num + 1); } System.out.println(num); // add code here return result; } }public class AddMe { public static void main(String[] args) { calc(num); } static int num = 0; public static int calc(int x) { num = num + 1; int result = 0; if(num <= 1200){ result = num + calc(num + 1); } System.out.println(num); // add code here return result; } }

Using Big Oh notation, describe the time complexity of each queue operation in the class ArrayQueue. Briefly explain your answers.

enqueue complexity is O(1) because it has to add one item at the end of the queue.getFront is the O(n) because it will depend on the number of the the queue.isEmpty is O(1) it checks at least one.clear is O(1) because it has not go through the queue.

What will happen if a method is executed and the precondition for the method is not met? A)An exception will be thrown. B)The program will loop indefinitely. C)The system will crash. D)Any of the above results could happen

Any of the above results could happen.

What is the primary purpose of an Iterator object? A;)To add new objects to a collection. B)To display a graphical object. C)To step through the objects of a collection one at a time. D)To play an audio clip.

C)To step through the objects of a collection one at a time.

What is the difference between capacity and size in an array ?

Capacity is the total number of Elements in an array. Size is the number of Elements that have data in them.

in java.util package, public interface Queue<E> extends __________________

Collection<E>

If you have a list object to search that is an instance of ADT list, you search it by using the list operation

Contains

Heap Sort

Non-stable, in place sort which has an order of growth of NlogN. Requires only one spot of extra space. Works like an improved version of selection sort. It divides its input into a sorted and unsorted region, and iteratively shrinks the unsorted region by extracting the smallest element and moving it into the sorted region. It will make use of a heap structure instead of a linear time search to find the minimum.

In the linked-list version of the Stack class, which operations require linear time for their worst-case behavior? peek pop push None of these operations require linear time

None of these operations require linear time

The best-case time efficiency of a sequential search on an array is

O(1)

The average-case time efficiency of a sequential search on an array is

O(N)

When a method is called, who is responsible for ensuring that the precondition is valid? The person who is using the program. The programmer who called the method. The programmer who wrote the method. The programmer who implemented the Java Runtime System.

The programmer who called the method.

Entries in a stack are "ordered". What is the meaning of this statement?

There is a first entry, a second entry, and so on.

In a single method declaration, what is the maximum number of statements that may be recursive calls?

There is no fixed maximum

The __________ is at least one case in which a problem can be solved without recursion.

base case

An algorithm with worst case time behavior of 5n takes at least 50 operations for every input of size n=10.

false

An array can hold multiple values of several different types of data simultaneously.

false

For all possible inputs, a linear algorithm to solve a problem must perform faster than a quadratic algorithm to solve the same problem.

false

Without a base case, a recursive method will call itself only once and stop.

false

n the following code that uses recursion to find the greatest common divisor of a number, what is the base case? public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); }

if(x % y == 0)

In Java, you do not use the new operator when you use a(n) ____________.

initialization list


Kaugnay na mga set ng pag-aaral

словарь переводческих терминов

View Set

OB EXAM 2 (Prep U Chapters 14, 15, 19, 20)

View Set

Chapter 4: Sound Recording and Popular Music

View Set

Chapter 15: Anti-discrimination Laws and Other Marketing Regulations (Colibri Real Estate)

View Set