CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada

Similar documents
CSCI 102 Fall 2010 Exam #1

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

STL. Prof Tejada Week 14

Problem 1 Short Answers Why use the reserved words new or delete? Why use dynamic memory allocation?

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

LAB 4.1 Relational Operators and the if Statement

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CS 115 Exam 3, Spring 2011

CS 103 Unit 12 Slides

Score score < score < score < 65 Score < 50

Module 7 b. -Namespaces -Exceptions handling

2015 Academic Challenge

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

Arrays. Arizona State University 1

CS 115 Exam 3, Spring 2010

FORM 2 (Please put your name and form # on the scantron!!!!)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

CSCE Practice Midterm. Data Types

Abstraction in Software Development

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Arrays in C++ Instructor: Andy Abreu

Test 2: CPS Owen Astrachan. November 17, Name: Honor code acknowledgement (signature)

CSci 1113 Midterm 1. Name: Student ID:

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Chapter 20 - C++ Virtual Functions and Polymorphism

Where do we stand on inheritance?

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CSCE Practice Midterm. Data Types

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Exam 3 Chapters 7 & 9

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Namespaces and Class Hierarchies

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

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

CSCE 2004 Final Exam Spring Version A

Use the template below and fill in the areas in Red to complete it.

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Why Is Repetition Needed?

Name Section: M/W T/TH Number Definition Matching (8 Points)

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

CMPS 221 Sample Final

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Programming in C++: Assignment Week 3

CS 216 Exam 1 Fall SOLUTION

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

CMSC 202 Midterm Exam 1 Fall 2015

Philadelphia University Faculty of Engineering Department of Computer Engineering Programming Language (630263)

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

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

a data type is Types

Write a program that displays all the even integers between 1 and 100, inclusive

Programming in C++: Programming Test-2

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chap 0: Overview. Overview of basic C++ syntax Refresh programming basics C++ Vs. Java differences Coding conventions used. EECS 268 Programming II 1

C++ Final Exam 2017/2018

CSCI 104 Polymorphism. Mark Redekopp David Kempe

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Chapter 5: Control Structures II (Repetition)

CS2255 HOMEWORK #1 Fall 2012

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Largest Online Community of VU Students

Object oriented programming

CSci 1113 Final. Name: Student ID:

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)

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Lecture #1. Introduction to Classes and Objects

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

More Tutorial on C++:

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to Computer Science Midterm 3 Fall, Points

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Abstract Data Types (ADT) and C++ Classes

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Topics. Passing objects to functions. Sections 7.9, 7.10, Objects and functions

Instructions. Quiz #3 Solutions. Name: Student Number: Signature:

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

Programming, numerics and optimization

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

1. Write step by step code to delete element from existing Doubly Linked List. Suppose that all declarations are done

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Page 1 Name: CUNYfirstID: CS111 Summer Term 1- Final 6/27/18

Name Section: M/W T/TH Number Definition Matching (6 Points)

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Object Oriented Programming CS250

Chapter 5 Lab Methods

Transcription:

CSCI 101L - Data Structures Practice problems for Final Exam Instructor: Prof Tejada 1

Problem 1. Debug this code Given the following code to increase the value of a variable: void Increment(int x) { x ++ ; int main() { int num1 = 5; cout << Before = << num1 << endl; Increment(num1); cout << After = << num1 << endl; The Increment function needs to increase the value of num1 in main, but currently the code prints out the same value for num1 after the call to Increment(num1). (a) Show how you would modify the code using pass by reference so that the value of num1 is increased after the call to Increment(num1) (you don't have to rewrite all the code, just rewrite the lines that need to change or the lines you want to add). (b) Show how you would modify the code using pass by pointer so that the value of num1 is changed after the call to Increment(num1) (you don't have to rewrite all the code, just rewrite the lines that need to change or the lines you want to add). 2

Given this function for calculating the nth Fibonacci number using iteration. Rewrite this function using recursion: int Fibonacci(int nthfibonacci) { int previous1=1; int previous2=1; int current; if (nthfibonacci == 0 nthfibonacci == 1 ) current = nthfibonacci; else { for (int counter = 2; counter <= nthfibonacci; counter++) { current = previous2 + previous1; previous1 = previous2; previous2 = current; counter++; //end for //end else return current; Rewrite this function using recursion: int min_value(int arr[], int size) { int min = arr[0]; for(int i = 1; i < size; i++) { if (arr[i] < min) { min = arr[i]; return min; 3

Problem 2 Short Answers What does the keyword virtual mean in C++? Why use recursion? Why iteration? What is Big O? Why is Big O important? What is the difference between composition and inheritance? Why use the reserved words new or delete? Why use dynamic memory allocation? When would you use pointers over reference variables? Why? What is the difference between public, protected, and private? Define the Composition relationship between classes. Give an example of this type of relationship and then draw a diagram: Define the Inheritance relationship between classes. Give an example of this type of relationship and then draw a diagram: 4

Problem 3. What does this code do? 1) What does this code fragment print out? int x = 10, Sum = 0 ; while ( x!= 0 ){ Sum += x--; cout << --x << Sum; 2) What does this code fragment print out? vector<int> my_vec(5); for(int i=0; i < 5; i++) { my_vec[i] = i+5; my_vec.push_back(9); my_vec.push_back(8); my_vec[0] = 3; int i; for(i=0; i < my_vec.size(); i++){ cout << my_vec[i] << ; 5

3) For the code fragment below assume the user input is: 99 34 56 60 77 What is the contents of output.txt file? int x; double average=0; ofstream ofile; ofile.open( output.txt, ios::out); for (int i=0; i<5; i++) { cin >> x; average +=x; ofile << Student << i << : score << x << endl; cout << Average: << average/5 << endl; ofile.close(); What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; *p = 35; *q = 98; *p = *q; cout<<x<<" "<<y<<endl; cout<<*p<<" "<<*q<<endl; 6

What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; x = 35; y = 46; p = q; *p = 78; cout<<x<<" "<<y<<endl; cout<<*p<<" "<<*q<<endl; 7

Problem 4 Classes & Dynamic memory allocation Given this class definition: class Student { char name[26]; double gpa; int sid; char grade; ; Write a function called Enrollment that asks the user for the number of students enrolled in a course, then creates an array of Student using dynamic memory allocation. The function asks the user to enter the information for each Student object in the array. The function returns the array, as well as the average gpa of all the students and the maximum grade. 8

Given the following class definition: class circletype { public: void setradius(double r); //Function to set the radius. //Postcondition: if (r >= 0) radius = r; // otherwise radius = 0; double getradius(); //Function to return the radius. //Postcondition: The value of radius is returned. double area(); //Function to return the area of a circle. //Postcondition: Area is calculated and returned. double circumference(); //Function to return the circumference of a circle. //Postcondition: Circumference is calculated and returned. circletype(double r = 0); //Constructor with a default parameter. //Radius is set according to the parameter. //The default value of the radius is 0.0; //Postcondition: radius = r; private: double radius; ; Write all of the member functions for the circletype class: 9

What does this print out? circletype circle1(8); circletype circle2; cout << "radius: " << circle1.getradius() << ", area: " << circle1.area() << ", circumference: " << circle1.circumference() << endl; circle2.setradius(4); cout << "radius: " << circle2.getradius() << ", area: " << circle2.area() << ", circumference: " << circle2.circumference() << endl; 10

You are given the following class definitions: class Shape { protected: int x; int y; public: Shape(int a, int b) { x=a; y=b; int getx() const { return x; int gety() const { return y; void setx(int a) { x=a; void sety(int b){ y=b; void print() const { cout << I am an instance of Shape! << endl; ; class Square : public Shape { protected: int side; public: Square(int a, int b, int s); double getside() const { return side; void setside(int s) { side = s; void print(); ; Write the implementation of the provided constructor for the Square class. Write the implementation of the provided void print function for the Square class, so that the function will print out I am an instance of Square!. 11

What will the following code print? Shape s(100,100); s.print(); Square sq(20,40,5); sq.print(); Shape q = sq; q.print(); 12