CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Similar documents
CMP-338 Solutions Midterm Spring Version 1

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Midterm Spring 2015 Solutions Version 1

CMP-338 Solutions Midterm Spring Version 1

CMP Points Total Final Fall (10 Points) Multiple Choice:

CS24 Week 4 Lecture 2

Inheritance. Transitivity

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

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

== isn t always equal?

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CSE 143 SAMPLE MIDTERM

ITI Introduction to Computing II

csci 210: Data Structures Stacks and Queues

Stacks and Queues. David Greenstein Monta Vista

Abstract Data Types. Abstract Data Types

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

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

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Programming II (CS300)

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

Name CPTR246 Spring '17 (100 total points) Exam 3

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

CS 61B Spring 2017 Guerrilla Section 3 Solutions

8. Fundamental Data Structures

Data Abstraction and Specification of ADTs

Has-a Relationships. A pen has a or contains a ball

Linked Structures. See Section 3.2 of the text.

JAVA MOCK TEST JAVA MOCK TEST II

CS171 Midterm Exam. October 29, Name:

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


CS350: Data Structures Stacks

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

CMSC 132: Object-Oriented Programming II

Basic Data Structures

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

Interfaces & Generics

Basic Data Structures 1 / 24

CS61B Spring 2016 Guerrilla Section 2 Worksheet

Programming II (CS300)

CT 229 Object-Oriented Programming Continued

+ Abstract Data Types

Queues Fall 2018 Margaret Reid-Miller

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

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

Implementing Dynamic Data Structures

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

Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

INHERITANCE. Spring 2019

Queue Linked List Implementation

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Stacks and Queues

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

Introduction to Computing II (ITI 1121) Final Examination

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Stacks Fall 2018 Margaret Reid-Miller

CSE 143 SAMPLE MIDTERM SOLUTION

CE204 Data Structures and Algorithms Part 2

Chapter 18: Stacks And Queues

Introduction to Computing II (ITI 1121) Final Examination

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Queues. Stacks and Queues

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Stack and Queue. Stack:

Subclassing for ADTs Implementation

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Data Structures And Algorithms

Stack as an ADT. COMP 182: Data Structures and Program Design Lecture Slides 8. Stack as an Idea

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Midterm Exam (REGULAR SECTION)

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Linked Lists. Chapter 12.3 in Savitch

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS61BL Summer 2013 Midterm 2

DNHI Homework 3 Solutions List, Stacs and Queues

Queues. CITS2200 Data Structures and Algorithms. Topic 5

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

ECE Fall st Midterm Examination (120 Minutes, closed book)

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Midterm Exam 2 CS 455, Spring 2011

CSC 1052 Algorithms & Data Structures II: Stacks

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

Inheritance (Part 5) Odds and ends

Announcements Queues. Queues

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

CS/ENGRD 2110 SPRING 2018

Transcription:

Version 1 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use only one side of the yellow paper. 1. (16 Points) Multiple Choice: A. (2 Points) In the ADT list, when an item is deleted from position i of the list,. a. the position of all items is decreased by 1 b. the position of each item that was at a position smaller than i is decreased by 1 c. the position of each item that was at a position greater than i is decreased by 1 d. the position of each item that was at a position smaller than i is increased by 1 while the position of each item that was at a position greater than i is decreased by 1 B. (2 Points) A(n) is a Java construct that enables a programmer to define a new data type. a. class b. method c. data field d. object C. (2 Points) If you attempt to use a reference variable before it is instantiated, a(n) will be thrown. a. IndexOutOfBoundsException b. InstantiationException c. IllegalAccessException d. NullPointerException D. (2 Points) A linked list contains components, called, which are linked to one another. a. nodes b. arrays c. vectors d. references E. (2 Points) Which of the following statements deletes the node that curr references? a. prev.setnext(curr); b. curr.setnext(prev); c. curr.setnext(curr.getnext()); d. prev.setnext(curr.getnext()); F. (2 Points) If the array: {6, 2, 7, 13, 5, 4 is added to a stack, in the order given, which number will be the first number to be removed from the stack? a. 6 b. 2 c. 5 d. 4 G. (2 Points) The method of the ADT stack retrieves and then removes the top of the stack. a. createstack b. push c. pop d. peek H. (2 Points) A superclass method can be accessed by a subclass, even though it has been overridden by the subclass, by using the reference. a. super b. final c. static d. new

Version 1 2. (20 Points) Given the following generic MyArray class that contains 10 syntax and logical errors: public class MyArray { private Object array = new Object[100]; private int currentlocation = 0; public int size() { return currentlocation; public boolean isempty() { return (currentlocation <= 0); public void addelement(i element) { array[currentlocation] = element; public void getelement(int index) { I element = null; if ((index < currentlocation) (index >= 0)) { element = (I) array[index]; return element; public void replaceelement(i newelement, int index) { if ((index >= currentlocation) && (index < 0)) { System.out.println("Error"); array[index] = newelement; public void removeelement(int index) { if ((index <= currentlocation) (index < 0)) { System.out.println("Error"); for (int i = index - 1; i < currentlocation; i++) { array[i - 1] = array[i]; array[currentlocation] = null; public void clear() { for (int i = 0; i < array.length; i++) { array[i] = null; currentlocation = 0; Re-write the MyArray class and fix the 10 syntax and logical errors.

Version 1 3. (40 Points) Given the following definition for QueueInterface<I> import java.util.vector; public interface QueueInterface<I> { // returns true if Queue is empty // returns false otherwise public boolean isempty(); // returns the size of the Queue public int size(); // adds the specified element // to the Queue // trying to enqueue into a full array should return without queueing anything public void enqueue(i element); // removes and returns the front // of the Queue public I dequeue(); // returns a Vector containing all the // elements in the Queue public Vector<I> peekall(); // tests if this Queue is equal to the // Queue specified by obj // Two Queues are equal if they have // the same size and all their elements // are equal public boolean equals(object obj); Write the complete Java class for the ArrayBasedQueue that implements the given QueueInterface<I>.

Version 1 4. (30 Points) Given the following list: Head A B C D E F Tail 6 listsize And the following method: public void dostuff1() { Node[] nodes = new Node[listSize]; Node node = head; int i = 0; while (node!= null) { nodes[i++] = node; node = node.getnext(); for ( i = 0 ; i < listsize ; i += 2 ) { nodes[i+1].setnext(nodes[i]); for ( i = (listsize - 1) ; i > 1 ; i -= 2 ) { nodes[i-3].setnext(nodes[i]); head = nodes[1]; tail = nodes[listsize - 2]; nodes[listsize-2].setnext(null); Draw the list after dostuff1() has finished executing.

Version 2 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use only one side of the yellow paper. 1. (16 Points) Multiple Choice: A. (2 Points) In the ADT list, when an item is inserted into position i of the list,. a. the position of all items is increased by 1 b. the position of each item that was at a position smaller than i is increased by 1 c. the position of each item that was at a position greater than i is increased by 1 d. the position of each item that was at a position smaller than i is decreased by 1 while the position of each item that was at a position greater than i is increased by 1 B. (2 Points) A(n) is an instance of a class. a. method b. data field c. interface d. object C. (2 Points) When you declare a variable that refers to an object of a given class, you are creating a(n) to the object. a. interface b. reference c. method d. ADT D. (2 Points) The last node of a linear linked list. A. has the value null B. has a next reference whose value is null C. has a next reference which references the first node of the list D. cannot store any data E. (2 Points) I Which of the following statements deletes the first node of a linear linked list that has 10 nodes? a. head.setnext(curr.getnext()); b. prev.setnext(curr.getnext()); c. head = head.getnext(); d. head = null; F. (2 Points) If the array: {6, 21, 35, 3, 6, 2, 13 is added to a stack, in the order given, which of the following is the top of the stack? a. 2 b. 6 c. 3 d. 13 e. 35 G. (2 Points) The method of the ADT stack retrieves the top of the stack, but does not change the stack. a. createstack b. push c. pop d. peek H. (2 Points) The constructor of a subclass can call the constructor of the superclass by using the reference. a. extends b. new c. super d. import

Version 2 2. (20 Points) Given the following generic MyArray class that contains syntax and logical errors: public class MyArray { private I[] array = new Object[100]; private int currentlocation = 0; public int size() { return currentlocation; public boolean isempty() { return (currentlocation = 0); public void addelement(i element) { array[currentlocation--] = element; public void getelement(int index) { I element = null; if ((index >= currentlocation) && (index >= 0)) { element = (I) array[index]; public void replaceelement(i newelement, int index) { if ((index >= currentlocation) && (index < 0)) { System.out.println("Error"); array[index] = newelement; public void removeelement(int index) { if ((index < currentlocation) (index < 0)) { System.out.println("Error"); for (int i = index + 1; i < currentlocation; i++) { array[i + 1] = array[i]; array[--currentlocation] = null; public void clear() { for (int i = 0; i < array.length; i++) { array[i] = null; currentlocation = 0; Re-write the MyArray class and fix the 10 syntax and logical errors.

Version 2 3. (40 Points) Given the following generic definition for StackInterface<I> import java.util.vector; public interface StackInterface<I> { // returns true if Stack is empty // returns false otherwise public boolean isempty(); // returns the size of the Stack public int size(); // pushes the specified element // onto the stack // trying to push onto a full array should return without stacking anything public void push(i element); // pops and returns the element // at the top of the stack public I pop(); // returns a Vector containing all the // elements in the Stack public Vector<I> peekall(); // tests if Stack is equal to the // Stack specified by obj // Two Stacks are equal if they have // the same size and all their elements // are equal public boolean equals(object obj); Write the complete Java class for the ArrayBasedStack that implements the given StackInterface<I>.

4. (30 Points) Given the following list: Version 2 Head G H I J K L Tail 6 listsize And the following method: public void dostuff2() { Node[] nodes = new Node[listSize]; Node node = head; int i = 0; while (node!= null) { nodes[i++] = node; node = node.getnext(); for ( i = 1 ; i < listsize-1 ; i += 2 ) { nodes[i+1].setnext(nodes[i]); for ( i = (listsize - 2) ; i > 2 ; i -= 2 ) { nodes[i-3].setnext(nodes[i]); nodes[listsize-1].setnext(nodes[2]); nodes[listsize-3].setnext(nodes[0]); head = nodes[listsize-1]; tail = nodes[0]; tail.setnext(null); Draw the list after dostuff2() has finished executing.