Example Final Questions Instructions

Similar documents
Example Final Questions Instructions

This examination has 11 pages. Check that you have a complete paper.

CSCE 110 PROGRAMMING FUNDAMENTALS

Part I: Short Answer (12 questions, 65 points total)

CSE030 Fall 2012 Final Exam Friday, December 14, PM

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Chapter 20: Binary Trees

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

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

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Linked List using a Sentinel

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Inheritance, and Polymorphism.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

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)

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

COMP 250 Midterm #2 March 11 th 2013

CS 216 Exam 1 Fall SOLUTION

CSCE 110 PROGRAMMING FUNDAMENTALS

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Tutorial AVL TREES. arra[5] = {1,2,3,4,5} arrb[8] = {20,30,80,40,10,60,50,70} FIGURE 1 Equivalent Binary Search and AVL Trees. arra = {1, 2, 3, 4, 5}

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Data Structures and Algorithms

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Topic Binary Trees (Non-Linear Data Structures)

Introduction to the C programming language

Introduction to the C programming language

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

AN OVERVIEW OF C++ 1

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1

Review Questions for Final Exam

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Lab 2: ADT Design & Implementation

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

! A data type for which: ! An ADT may be implemented using various. ! Examples:

3137 Data Structures and Algorithms in C++

Midterm Exam 5 April 20, 2015

CMSC 341 Lecture 10 Binary Search Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

1 Short Answer (10 Points Each)

Bruce Merry. IOI Training Dec 2013

C++_ MARKS 40 MIN

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Chapter 1: Object-Oriented Programming Using C++

CS 162, Lecture 25: Exam II Review. 30 May 2018

An Introduction to C++

Questions: ECE551 PRACTICE Final

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

Trees, Heaps, and Priority Queues

Dynamic Data Structures

We have the pointers reference the next node in an inorder traversal; called threads

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

Midterm Review. PIC 10B Spring 2018

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

COMP 2355 Introduction to Systems Programming

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Function Overloading

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Module 6: Binary Trees

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:30-7:00

Programming in C++: Assignment Week 8

Object Oriented Programming COP3330 / CGS5409

CS302 - Data Structures using C++

CpSc212 Goddard Notes Chapter 10. Linked Lists

Exam Informatik D-MATH/D-PHYS :00 11:00

CS24 Week 3 Lecture 1

Short Notes of CS201

Data Structure - Binary Tree 1 -

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

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

CS201 - Introduction to Programming Glossary By

CMSC 202 Midterm Exam 1 Fall 2015

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

CMSC 341. Binary Search Trees CMSC 341 BST

CE204 Data Structures and Algorithms Part 2

Question Paper Code : 97044

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CS24 Week 4 Lecture 2

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

CGS 2405 Advanced Programming with C++ Course Justification

Programming Abstractions

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

CS 376b Computer Vision

Data Structures And Algorithms

1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <cstring> // for string tokenizer and c-style string processing 5 #include

CSE 214 Computer Science II Heaps and Priority Queues

CMSC 341 Lecture 7 Lists

Transcription:

Example Final Questions Instructions This exam paper contains a set of sample final exam questions. It is for practice purposes only. You ll most likely need longer than three hours to answer all the questions. 1

1. State whether each of the following statements is true or false, and briefly explain your reason. (a) If a function in a class is declared to be a virtual function, then it must NOT have definition (implementation) in that class. (b) You can not create objects from an abstract class, therefore an abstract class is useless. (c) If class A is derived from class B (i.e., class A inherits class B), then class A can overwrite (override) a private function defined in class B and class B s constructor. (d) The only way to implement a binary tree ADT is to use a pointer based data structure, because you have to use pointers to point to child nodes. (e) When you need to raise an exception, you have to put the throwstatement directly in a try-block. (f) If the condition in an assert statement is evaluated to be false, the function that contains that assert statement will be terminated, but the program can still continue. (g) The following two functions are allowed to be in the same class Data because their return types are different and they have different parameter names. void Data::updateStatus(int initialstatus); bool Data::updateStatus(int newstatus); 2. What is the precise output from the following complete C++ program? // Program begins here #include <iostream> using namespace std; class Data { public: Data(); ~Data(); void set(double x1, double x2); void display(); 2

static int count; private: int id; double *x; int Data::count = 0; Data::Data() { id = ++count; cout << "Initialize object " << id << ".\n"; x = new double[2]; x[0] = x[1] = 0.0; } Data::~Data() { delete [] x; count--; cout << "destroy object " << id << ".\n"; } void Data::set(double x1, double x2) { x[0] = x1; x[1] = x2; } void Data::display() { cout << "Object " << id << " has:\n"; cout << x[0] << " " << x[1] << endl; } int main() { Data a[2]; a[0].set(1.0, 2.0); a[1].set(3.0, 4.0); Data & b = a[1]; Data * c = new Data(); Data * d = &a[0]; c->set(5.0, 6.0); cout << "Total objects: " << Data::count << endl; b.display(); c->display(); 3

d->display(); delete c; cout << "Total objects: " << Data::count << endl; return 0; } // Program ends here 3. First, explain what is the ADT Dictionary. Then, describe the basic operations of a Dictionary. For each operation, describe its interface and what it should do. (Note: You do not need to implement this ADT.) 4. Given the following interface of class Window that describes the standard window objects: class Window { public: Window(); ~Window(); void setposition(int X, int Y); void setsize(int width, int height); void getposition(int & X, int & Y); void getsize(int & width, int & height); void display(); private: int positionx; int positiony; int width; int height; Design the interface of a message-window class, called MsgWindow. A message window is a standard window that is used to display some message. The methods used to handle the position and size of a message window should be the same as those in the standard window. However, the method used to display a message window is different from the one used to display a standard window. A message window also should store the message to be displayed and provide methods to update the 4

message and retrieve the message. Your interface design should show the public methods, the private data fields and the relationship between the class Window and the class MsgWindow. (Note: You do not need to implement the methods.) 5. Write a C++ function that takes two strings as parameters and finds and returns a string that contains the longest common prefix of these two strings. For example, the two strings abcxyz and abxyz have the longest common prefix of ab ; and the two strings abc and xyz have the longest common prefix of (empty string). 6. Given the following interface of a Queue class: class Queue { public: Queue(); ~Queue(); void enqueue(int data); int dequeue(); bool isempty(); private: int values[100]; // a circular array int front; int back; A circular array is used to implement this Queue class. Implement the method enqueue and isempty. 7. Given the following interface of a LinkedList class: class LinkedList { public: // create an empty linked list LinkedList(); // 1 // destroy each node in the linked list ~LinkedList(); // 2 // insert the data to the front of the linked list void insertfront(int data); // 3 5

// remove and return the data that s in the front // of the linked list, throw an exception // if the list is empty int removefront() throw (string); // 4 // return true if the list is empty bool isempty(); // 5 // return the data resides in the position indicated // by the index; if the index is less than 0 or // greater than or equal to the number of elements // in the list, throw an exception int & operator [] (int index) throw (string); // 6 private: struct Node { int data; Node * next; Node * front; // pointing to the front of the list int size; // the number of elements in the list Implement all six functions of this class. (You can decide whether or not you want to create a dummy node for the linked list, as long as the implementation of all the functions is consistent.) 8. Given the following struct definition: struct Data { string name; int number; Develop a C++ function, called loaddata, that opens a file and reads data from the file into an array. This function takes the following three parameters: (a) a string, called filename, which stores the name of the file to be opened; (b) a pointer pointing to an array of Data, called arr, which will be set to point to an array that stores the data; 6

(c) an integer, called arrsize, which will be set to store the size of the array created. If the file can be opened successfully, then you can safely assume that the first line in the file is an integer number indicating how many data items are stored in the file, and each data item occupies two lines, the first line contains a character string as name (may have white spaces) and the second line contains an integer as number. The function should open the file, read in the number of elements first, set the arrsize to be that number, dynamically create a Data array big enough to store all the data items and make arr pointing to this array, and then read the data items from file to the array, close the file and return true. If the file can not be opened successfully, then the function should return false. The prototype of the function is shown below: bool loaddata(string filename, Data *& arr, int & arrsize); 9. Write a C++ function, findmax, that takes an array of strings, called values, and the array s size, called size, as its parameters and returns a double value. Each string stored in array values should be able to be converted to a positive double number. The function findmax converts each string stored in the array values to the double number it represents and then finds and returns the maximum of these numbers. There are the following exceptions that need to be handled by try-catch-throw method in this function: (a) If the size of the array is zero, the function should throw an exception of type EmptyArray, catch it and simply return 0.0. (b) If any of the strings can not be converted successfully to a positive double number, the function should throw an exception of type WrongFormat out. (Note: There is no need for the function to handle this exception itself.) You can safely assume that the two classes EmptyArray and WrongFormat are already defined and can be used by function findmax. The constructors of both these two classes require no parameters. The function interface should include a throw list. 7

To convert a string to a double number, you can use the library function double atof(char*);. 10. Given the following binary tree: 6 4 8 1 10 3 7 12 (a) Show the in-order, pre-order and post-order traversals of the above shown binary tree. (b) If the number in each node represents the data stored in that node, is this binary tree a binary search tree? Why or why not? (c) Breadth-first traversal means to visit the nodes in a tree by layers, i.e., visit all the nodes with depth 1 first, then visit all the nodes with depth 2, and so on. Show the breadth-first traversal of the above shown binary tree. 11. The following picture shows a thread tree (with dashed lines representing thread pointers). 8

The struct of a thread tree node is defined below: struct TreeNode { string data; TreeNode *left; bool leftthread; TreeNode *right; bool rightthread; Write a C++ function, called threadtraversal, that takes a pointer to a thread tree s root as its parameter, and performs in-order traversal of the tree. The function should not use any extra ADTs and should not be a recursive function. The prototype of the function is shown below: void threadtraversal(treenode * root); You can safely assume that the function, visit, that performs actions on the data stored in the tree nodes has been developed (you do not need to implement this function). The prototype of function visit is shown below: void visit(string & data); 12. A set is a collection of distinct objects. You are asked to develop a class Set. To do that, a dynamically allocated array is used to store the objects and template is used so that different Set objects can be created to store different types of objects. The Set class should have the following methods: a default constructor, the default size of the array is 128. a constructor that takes an integer as its parameter and use that integer as the size of the array. a destructor that releases the array space. insert an object into the set. This function should return true if the insertion is successful, and return false if the insertion fails (i.e., the object is already in the set). 9

remove an object from the set. This function should return true if the object was in the set and the remove is successful, and return false if the object was not in the set at all. Your tasks include: (a) Design the interface of the templated class Set, including its public methods mentioned above and its private data fields. (b) Implement the two constructors and the destructor of class Set. (c) Write a block of main function code that declares two Set objects. One Set object is used to store integers and the size of the set is left as the default one. The other Set object is used to store pointers pointing to integers and the size of the set should be 200. ====== END OF EXAM QUESTIONS ====== 10