Programming Final Exam

Ace your homework & exams now with Quizwiz!

Evaluate the following postfix expression: 7,2,*,9,3,/,4,-,*

-14

16. Assume a Java array implementation of a Stack of ints is used. Write a Java method called collapse that will add all integers currently in the Stack, then empty the Stack, then push the resulting sum onto the Stack.

// Assume the method is part of the Stack class void collapse() { int sum = 0; while (!isEmpty()) { sum += pop(); } push(sum); }

20. Assume a linked list implementation of a Queue is used. Write a Java method called moveUp(x) that will look for a node in the Queue with a value of x and move it up to the front of the Queue.

// Assume the queue holds ints and x is the value of node void moveUp(x) { LinkedNode prev = null; LinkedNode cur = front; while (cur!=null) { if (cur.x==x) break; prev=cur; cur=cur.next; } if (cur==null) return; // x was not found in list if (prev==null) return; // x is already at the front prev.next = cur.next; // delete node with x from list cur.next = front; // connect the node with x to front of list front = cur; // node with x becomes the front }

What is the output of the following code ? class A { public static int changeArr(int arr) { arr = arr + 1; return arr + 2; } public static void main(String args[]) { int[] arr = {0, 1, 2}; changeArr(arr[0]); System.out.println(arr[0]); } }

0

What are going to be the values of the left (boundary) index in each iteration of a binary search, when searching for 60 in the following array: {1, 7, 15, 17, 22, 31, 60}.

0,4,6

8. Assume a binary search is performed on the following array of integers: {3, 17, 18, 24, 32, 55, 73, 90}. For each iteration of the algorithm, write the number that will be the middle element when searching for the number 3.

24,17,3

What is the output of the following code? public class Test { public static void main(String[] args) { System.out.println(foo(10)); } public static int foo(int i) { if (i<=0) return 0; if (i==1) return 1; return foo(i-1)+bar(i); } public static int bar(int i) { if (i<=0) return 0; if (i==1) return 1; return foo(i-2); } }

55

Given the following array declaration: int a[7]; What is the index of the last element in the array?

6

Given the following array declaration: float[] arr = new float[9]; How many elements are in the array?

9

17. Implement the method in question 5, but assuming that a Linked List implementation was used instead

Assuming that isEmpty(), pop(), and push() methods have been implemented correctly, using a Linked List implementation does not change the code used for question 5.

13. What is the worst case running time of the Stack push operation when using Linked Lists?

Constant time - O(1)

Use mergesort to sort the following array: {44, 12, 78, 33, 17, 19}. Show how the arrays will look in each step during decomposition and merging.

Decomposition: {44, 12, 78, 33, 17, 19} -> {44, 12, 78}, {33, 17,19} {44, 12, 78} -> {44}, {12, 78} {33, 17, 19} -> {33}, {17, 19} {12, 78} -> {12}, {78} {17, 19} -> {17}, {19} Merging: {44} -> {44} {12},{78} -> {12, 78} {33} -> {33} {17}, {19} -> {17, 19} {44}, {12, 78} -> {12, 44, 78} {33}, {17, 19} -> {17, 19, 33} {12, 44, 78}, {17, 19, 33} -> {12, 17, 19, 33, 44, 78}

19. Assume a repeating key cipher with key 1, 2, 3 is used to encode the message, "attack", which corresponds to the following numbers: 0, 19, 19, 0, 2, 10. If a Queue is used to perform the encoding, show how the Queue will look like at each step of the algorithm.

Depends on the key. Assume the following key: 1,2,3. Then the queue will initially look as follows: [1,2,3] Then after every iteration: [2,3,1] - 1 used to encode "a" [3,1,2] - 2 used to encode "t" [1,2,3] - 3 used to encode "t" [2,3,1] - 1 used to encode "a" [3,1,2] - 2 used to encode "c" [1,2,3] - 3 used to encode "k

What is the invariant being maintained in each iteration of the insertion sort?

Every element up to the i'th position makes up a sorted sublist.

If a method may throw a checked exception, the code that calls this method MUST be enclosed in a try.. catch block.

F

Recursive methods are more efficient than iterative methods.

F

Statements in the finally clause of a try statement are executed only if an exception is caught.

F

T/F: An enqueue operation is used to remove an element from a queue data structure.

F

T/F: If T1(N) = O(N log N) and T2(N) = O(log N), then T1(N) + T2(N) = O(log N).

F

T/F: If a list is already sorted, the selection sort algorithm will execute faster than insertion sort.

F

T/F: Using mergesort is always faster than insertion sort.

F

The size of an array can be changed after it is created.

F

Assume a selection sort is used to sort the following array of integers: {7, 19, 1, 24, 18, 77, 4, 21}. Show how the array will look after each iteration of the outer loop.

INITIAL ARRAY: {7, 19, 1, 24, 18, 77, 4, 21} {1, 19, 7, 24, 18, 77, 4, 21} {1, 4, 7, 24, 18, 77, 19, 21} {1, 4, 7, 24, 18, 77, 19, 21} {1, 4, 7, 18, 24, 77, 19, 21} {1, 4, 7, 18, 19, 77, 24, 21} {1, 4, 7, 18, 19, 21, 24, 77} {1, 4, 7, 18, 19, 21, 24, 77}

14. Convert the following infix expression to a postfix expression: (3 + 5) - ( 7 / 4) * 3.

In postfix form: 3,5,+,7,4,/,3,*,-

Assume you have a linked list with a reference named front that points to the first node in the list. What type of operation is being performed by the following Java method? void op(int x) { LinkedNode newNode = new LinkedNode(x); if (front==null) { front = newNode; return; } LinkedNode cur = front; while(cur.next!=null) cur=cur.next; cur.next = newNode; }

Insertion at the rear

3. Analyze the running time of the algorithm used for question 11 in terms of the best and worst case time complexity. Use the Big-Oh notation.

No best or worst case because we need to examine all elements regardless of the input. Time complexity is O(N) - linear.

What is the running time of the following method in terms of its input? void foo(int N) { int k = 0; int j = 10; int sum=0; while (k<N && k<j) { sum += 5; k++;

O(1)

What is the running time of the following method in terms of its input? void bar(int K) { int Z=K*K; int count=1; for (int i=0; i<Z; i++) { count++; } }

O(K2)

What is the order of the following growth function expressed using Big-Oh notation: T(N)=7*N3 + N/2 + 2 * log N + 38 ?

O(N3)

What is the running time of the following method in terms of its input? int foo(int N) { if (M<=1) return 0; else return foo(N/2) + 1; }

O(log N)

The mergesort algorithm always divides the elements in half with each recursive call. The resulting time complexity is O(n log n). What would be the time complexity if the division would occur after the first element, so that the left portion would contain 1 element and the right portion would contain the rest?

O(n2)

You are writing a program that processes postfix expressions. What would be the best data structure to use for this purpose?

Stack

6. O(N log N) has lower time complexity than O(N2 ). (True/False)

T

An initializer list cannot be used to assign values in an array after the array was already declared and initialized.

T

Command-line arguments are separated using spaces.

T

Multidimensional arrays can contain arrays of various lengths.

T

T/F: Elements in a stack are processed in a last in, first out manner.

T

T/F: If an array is sorted, the time complexity of binary search will be O(log N).

T

Which of the following growth functions has the smallest asymptotic order?

T(N) = 2k * 6564596546794, where k is independent of N

4. Analyze the running time of the following Java method: int foo(int N) { int result = 0; for (int i=0; i<N; i++) for (int j=0; j<1000000; j++) for (int k=0; k<N; k++) result++; return result; }

The number of time steps is N*1000000*N*O(1) = O(N2)

What happens if a base case is not handled in a recursive method and the method is executed?

The program will eventually crash.

18. In a Queue using a linked list implementation, is it more efficient to dequeue at the front or the rear? Why?

To do this, we would have to start from the front and advance the reference until the next becomes the rear. That would make dequeue a linear time operation,instead of constant time. If a doubly linked list is used, dequeue from either end can be done in constant time since the reference to the previous node is part of every node.

15. Assume a Stack is used to evaluate the postfix expression you found in (3), show how the stack will look after each step in the evaluation algorithm.

[3] [3,5] [3,5,+] [8] [8,7] [8,7,4] [8,7,4,/] [8,1] [8,1,3] [8,1.75,3,*] [8,5.25] [8,5.25,-] [2.75]

7. What is the output of the following program? public class Test { public static void main(String[] args) { String s = "abcde"; System.out.println(foo(s)); } public static String foo(String s) { if (s==null || s.length()==1) return s; else { return s.charAt(s.length()-1) + foo(s.substring(0,s.length()-1)); } } }

edcba

What is the output of the following code? char[][] p = { {'a','b','c'}, {'e','f'}, {'g'} }; char[] t = p[2]; p[2] = p[0]; p[0] = p[1]; p[1] = t; for (int i=0; i<p.length; i++) { for (int j=0; j<p[i].length; j++) { System.out.print(p[i][j]); } }

efgabc

1. Provide an example of an indirect recursive method call.

foo() { bar(); } bar() { foo(); }

12. Assume Insertion Sort is used to sort the following array of integers: [6,5,4,3]. How will the array look like at each iteration?

initial - [6,5,4,3] 1st- [5,6,4,3] 2nd- [4,5,6,3] 3rd- [3,4,5,6]

2. Write a recursive method that finds the maximum of N integers in an array.

int max(int a[], int l, int r) { if (l==r) return a[l]; else { int maxOther = max(a,l+1,r); if (a[l]>=maxOther) return a[l]; else return maxOther; } }

What is the output of the following code? public class Test { public static void main(String[] args) { try { int[] arr = null; int b = arr.length; } catch (ArrayIndexOutOfBoundsException e) { System.out.print("oops..."); } catch (NullPointerException e) { System.out.print("ok..."); } finally { System.out.println("done"); } } }

ok...done

What is the best time complexity that can be achieved for comparison-based sorting algorithms?

Ω(n log n)

What is the time complexity of the following Java method in terms of the input N? int foo(int N) { int result = 0; int C=10+N; for (int i=0; i<C; i++) for (int j=0; j<1000000; j++) for (int k=0; k<C; k++) result++; }

θ(N2)

5. Give the order of the following growth functions in terms of the Big-Oh notation: 𝑇1(𝑁) = 𝑁99 + 2𝑁 + 2𝑁 + 128 𝑇2(𝑁) = 𝑁2log10(𝑁2) 𝑇3(𝑁) = 2𝑁+10 + 10 𝑇4(𝑁) = 𝑇2(𝑁) + 𝑇3(𝑁)

𝑇1(𝑁) = 𝑁99 + 2𝑁 + 2𝑁 + 128 𝑇2(𝑁) = 𝑁2log10(𝑁2) 𝑇3(𝑁) = 2𝑁+10 + 10 𝑇4(𝑁) = 𝑇2(𝑁) + 𝑇3(𝑁)


Related study sets

Module 2: Reading Assessment Quiz

View Set

World History - The Age of Napoleon (Ch. 3 Lesson 8)

View Set

Intro to Theatre Exam #2 Study Guide

View Set

Mr. Brewer's Java: Ch. 10 Review Questions

View Set

Management of Patients with Hematologic Neoplasms

View Set

Chapter 35 Assessment Of Immune Function

View Set