C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Similar documents
C Sc 227 Practice Test 2 Section Leader Name 134pts

C Sc 227 Practice Test 2 Section Leader Name 134pts ANSWERS

C Sc 127B Practice Test 2 Section Leader Your Name 100pts

C Sc 227 Practice Final

1. Is it currently raining in Tucson (4pts) a) Yes b) No? c) Don't know d) Couldn't know (not in Tucson)

C Sc 227 Practice Final Summer 12 Name 200pt

C Sc 227 Practice Final Summer 13 Name 200 pts

This midterm has about 4 questions more than the actual midterm of 200 pts will have

1. Write the output generated by the following code. (8pts) 2. Write the output generated by the following code (8pts)

Stacks and Queues. Chapter Stacks

Implement factorial to return n! that is defined as n * n-1 * n-2 * n-3 * 2 * 1. 1! And 0! Are both defined as 1. Use recursion, no loop.

CSE 143 SAMPLE MIDTERM

CSE 143 Sample Midterm Exam #7 (11wi)

Building Java Programs

Recursion. Chapter Simple Recursion. Goals Trace recursive algorithms Implement recursive algorithms

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

A Linked Structure. Chapter 17

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Csci 102: Sample Exam

CMPSCI 187: Programming With Data Structures. Review for First Midterm 9 October 2011

CSE 143 SAMPLE MIDTERM SOLUTION

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

An Introduction to Data Structures

CIS Fall Data Structures Midterm exam 10/16/2012

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

CS171 Midterm Exam. October 29, Name:

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

CSE 143 Sample Midterm Exam #1

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

Basic Data Structures

Basic Data Structures 1 / 24

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

1. Stack Implementation Using 1D Array

Data Structures And Algorithms

Programming Project: ArrayFun

Java Review: Objects

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 314 Exam 2 Spring 2016

public static<e> List<E> removeoccurrences(list<e> origlist, E remove) {

C Sc 127A Practice Test 2 SL Name 150pts

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

General computer science term that is described as a set of data and a set of operations(methods)to manipulate the data.

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Test 1: CPS 100. Owen Astrachan. October 1, 2004

Stacks Fall 2018 Margaret Reid-Miller

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CS350: Data Structures Stacks

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSE 143 Lecture 10. Recursion

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Section 05: Solutions

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CMSC 132: Object-Oriented Programming II. Stack and Queue

Largest Online Community of VU Students

Array Based Lists. Collections

ITI Introduction to Computing II

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

CSE 143. Lecture 4: Stacks and Queues

Introduction to Computing II (ITI 1121) Final Examination

DNHI Homework 3 Solutions List, Stacs and Queues

CSE413 Midterm. Question Max Points Total 100

CSE 143, Winter 2009 Sample Midterm Exam #2

Stacks, Queues (cont d)

0. What is the capital of Canada? (4pts) a. Toronto b. Quebec c. Ottawa d. Vancouver e. Calgary f. Not sure

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

ITI Introduction to Computing II

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

CSE 143 Sample Midterm Exam #4

Section 05: Midterm Review

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Introduction to Computer Science II (ITI 1121) Midterm Examination

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

University of KwaZulu-Natal Pietermaritzburg Campus Examinations

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

Advanced Java Concepts Unit 3: Stacks and Queues

COS 126 General Computer Science Fall Written Exam 1

Stacks. Revised based on textbook author s notes.

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

CSIS 10B Lab 2 Bags and Stacks

Question 1. [5 points] What output is printed by the following program (which begins on the left and continues on the right)?

CSE 143 Sample Midterm Exam #8 (12wi)

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Queues. Data Structures and Design with Java and JUnit Rick Mercer 18-1

Control flow in Eiffel

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

Computer Science 1 Ah

Introduction to Computer Science II (ITI 1121) Midterm Examination

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Midterm Exam 2 CS 455, Spring 2011

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CS 367: Introduction to Data Structures Midterm Sample Questions

CSE 143. Lecture 9: introduction to recursion reading: 12.1

Data Abstraction and Specification of ADTs

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

Transcription:

C Sc 227 Practice Test 2 Section Leader Your Name 100pts 1. Approximately how many lectures remain in C Sc 227 (give or take 2)? (2pts) 2. Determine the tightest upper bound runtimes of the following loops. Express your answer in the Big-O notation we have been using in class (assume the initialization int sum = 0;). 6pts (1pt ea, 2pts for overall style) a. for (int j = n; j >= 0; j--) b. for (int j = 1; j <= 50 * n; j++) c. for(int k = 1; k <= n; k = 2 * k) d. for( int j = 1; j <= n; j++ ) for( int k = 1; k <= n; k++ ) for(int m = 1; m <= n; m++ ) e. int j = 1; 3. Using the definitions from Rick's book, for each item, write DS if the name refers to a data structure, COLL if a collection class or ADT if an abstract data type. (3pts) a. 1D array b. PriorityList<E> c. ArrayPriorityList<E> 4. Use the Node class shown below with data and next instance variables and this view of a linked structure to answer the questions a) through d) (7pts) first "one" "two" "three" a) What is the value of first.data? b) What is the value of first.next.next.data? c) What reference value equals first.next.next.next? d) Write the code that prints all values. Make the code general enough to work on any sized linked structure where the last node references the first (this is a circular linked list) 5. Write Java code that will generate the following linked structure assuming you have access to the Node class shown below. (3pts) back "A" "B" 1

6. To class LinkedList, write method reverse() so all elements are stored in reverse order. Also write method duplicateall(e) that adds another element that equals the argument next to every occurrence of that argument. The code in the box must compile with passing assertions. This version of LinkedList only has the constructor plus the three methods shown as stubs addlast(e), tostring(), and size().you cannot pretend there are other LinkedList methods. If you need another method, write it. public class LinkedList<E> { private class Node { private E data; private Node next; public Node(E objectreference) { data = objectreference; public Node(E objectreference, Node nextreference) { data = objectreference; next = nextreference; private Node first; private int size; public LinkedList() { first = null; size = 0; public int size() { return n; public String tostring() { //... public void addlast () { //... public void reverse() { // 10pts @Test public void testreverse() { LinkedList<String> list = new LinkedList<String>(); list.addlast("a"); list.addlast("b"); list.addlast("c"); assertequals("[a, B, C]", list.tostring()); list.reverse(); assertequals("[c, B, A]", list.tostring()); @Test public void testduplicateall() { LinkedListOld<Integer> list = new LinkedListOld<Integer>(); list.addlast(1); list.addlast(2); list.addlast(3); list.addlast(2); assertequals(4, list.size()); assertequals("[1, 2, 3, 2]", list.tostring()); list.duplicateall(2); assertequals("[1, 2, 2, 3, 2, 2]", list.tostring()); assertequals(6, list.size()); Write duplicateall on the next page 2

public void duplicateall(e value) { // 12pts Use the following interface for the two Stack questions that follow: public interface OurStack<E> { public boolean isempty(); public void push(e element); public E peek(); public E pop(); 7. Write the output generated by the following code: (4pts) OurStack<Character> s = new LinkedStack<Character>(); s.push('a'); s.push('b'); s.push('c'); System.out.println(s.peek()); System.out.println(s.pop()); System.out.println(s.isEmpty()); System.out.println(s.peek()); 8. Complete method popuntil to modify the stack argument until value is found or the stack is empty. Do not allow popuntil to throw any exceptions if value is not in the stack argument. (6pts) OurStack<String> s = new LinkedStack<String>(); s.push("1"); s.push("+"); s.push("("); s.push("2"); popuntil("(", s); assertequals("+", s.pop()); assertequals("1", s.pop()); public void popuntil(string value, OurStack<String> s) { 3

9. Write the return values from each call to the method named mystery. (4pts) mystery(25) mystery(19) mystery(9) public int mystery(int n) { if (n > 20) return 1; else if (n > 10) return 5 + mystery(n + 5); else return 3 + mystery(n + 8); 10. Write the output generated by the method call mystery2("x", 6); (3pts) public void mystery2(string s, int digit) { if(digit <= 1) System.out.println(digit); else { s = s + "<"; mystery2(s, digit - 2); System.out.println(s + digit); 11. Implement recursive method trib. Use recursion, do not use a loop. (4pts) 12. Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. The following assertions must pass. (12pts) assertequals(1, count8(8)); assertequals(2, count8(818)); assertequals(3, count8(88)); assertequals(3, count8(881)); assertequals(3, count8(188)); assertequals(9, count8(88888)); public int count8(int n) { 4

13. Use recursion to complete method odddownevenup so it first prints all odd integers from the largest odd number <= argument down to 1. The same method call must then print all the even integers in the range of 2 through the largest even integers <= argument. The following method calls shown to the left must generate the output shown in comments to the right. Do not use any loop anywhere, use recursion. (12pts) odddownevenup(1); // 1 odddownevenup(2); // 1 2 odddownevenup(3); // 3 1 2 odddownevenup(4); // 3 1 2 4 odddownevenup(5); // 5 3 1 2 4 odddownevenup(9); // 9 7 5 3 1 2 4 6 8 public void odddownevenup(int n) 14. Complete method occurrencesof that returns how often an int element occurs in an array on integer. First complete this test method (fill in the four blanks). (12pts) int[] a = { 1, 2, 1, 2, 4 ; assertequals(, occurrencesof(1, a); assertequals(, occurrencesof(2, a); assertequals(, occurrencesof(4, a); assertequals(, occurrencesof(999, a); public int occurrencesof(int value, int[] x) { 5