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

Similar documents
CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

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

CS102 C++ Exception Handling & Namespaces

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

CSCI 102 Fall 2010 Exam #1

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

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

EL2310 Scientific Programming

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

CS24 Week 3 Lecture 1

Introduction to C++ Systems Programming

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

CSE 303: Concepts and Tools for Software Development

G52CPP C++ Programming Lecture 17

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CSc 328, Spring 2004 Final Examination May 12, 2004

Absolute C++ Walter Savitch

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)

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Part X. Advanced C ++

A Tour of the C++ Programming Language

Midterm Exam 5 April 20, 2015

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

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

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

05-01 Discussion Notes

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

LECTURE 03 LINKED LIST

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

Fast Introduction to Object Oriented Programming and C++

Chapter 15 - C++ As A "Better C"

CS250 Final Review Questions

Midterm Review. PIC 10B Spring 2018

Lecture 14: more class, C++ streams

A Tour of the C++ Programming Language

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

VIRTUAL FUNCTIONS Chapter 10

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

ECE Fall 20l2, Second Exam

CS102 Software Engineering Principles

EL2310 Scientific Programming

Polymorphism CSCI 201 Principles of Software Development

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

COMP 401 Spring 2014 Midterm 1

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

ECE 462 Fall 2011, Third Exam

EL2310 Scientific Programming

Introduction to Programming

CPSC 427: Object-Oriented Programming

Inheritance, and Polymorphism.

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

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:

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

a. a * c - 10 = b. a % b + (a * d) + 7 =

IS 0020 Program Design and Software Tools

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Chapter 2 - Control Structures

Introduction to C++ (Extensions to C)

CS 11 C++ track: lecture 1

CS201 Some Important Definitions

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

Computer Science II Lecture 1 Introduction and Background

Fundamentals of Programming CS-110. Lecture 2

Lecture #1. Introduction to Classes and Objects

Tokens, Expressions and Control Structures

ECE 449 OOP and Computer Simulation Lecture 14 Final Exam Review

Namespaces and Class Hierarchies

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Short Notes of CS201

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CS250 Final Review Questions

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

CS201 - Introduction to Programming Glossary By

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance

Intermediate Programming, Spring 2017*

Problem Solving with C++

! 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)

Object Oriented Programming 2015/16. Final Exam June 28, 2016

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

Engineering Tools III: OOP in C++

CPSC 427: Object-Oriented Programming

CSci 1113 Midterm 1. Name: Student ID:

Lecture 10: Introduction to Inheritance

CS

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Software Engineering /48

CPSC 427: Object-Oriented Programming

More on Templates. Shahram Rahatlou. Corso di Programmazione++

Transcription:

Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24 points) Problem #2 (25 points) Problem #3 (20 points) Problem #4 (16 points) Problem #5 (15 points) Total (100 points) ( This exam is closed book, closed notes, closed everything. No cheat sheet allowed. No calculators, cell phones, or any electronic gadgets. Will adhere to Basic Rules on the Reviews lecture slides. To be eligible for regrades, you must write your entire exam in ink. )

Problem 1. Memory (12 points) Page 2 of 8 (6 points) The figure below shows the code and the corresponding call stack. void foo(int *y) { (*y) += 100; int main() { int x = 102; foo(&x); You are here foo() calls main() stack 0x28cc40 y 0x28cc64 x addresses Let s say that your program has just finished executing the (*y) += 100; statement in foo() and foo() has not returned yet. What would be the numeric values of variables x in main() and y in foo() at this instance of time? Please note that &x is 0x28cc64 and &y is 0x28cc40 (please do not ask what this means because you are suppose to know). You can express your answer in either decimal or hex, whichever you think is more convenient for you. x: y: (6 points) The figure below shows the code and the corresponding call stack. void bar(int b) { b += 10; int main() { int a = 102; bar; You are here bar() calls main() stack 0x28cc40 b 0x28cc60 a addresses Let s say that your program has just finished executing the b += 10; statement in bar() and bar() has not returned yet. What would be the numeric values of variables a in main() and b in bar() at this instance of time? Please note that &a is 0x28cc60 and &b is 0x28cc40 (please do not ask what this means because you are suppose to know). You can express your answer in either decimal or hex, whichever you think is more convenient for you. a: b:

Problem 2. I/O Streams (16 points) Page 3 of 8 (6 points) Let x be a string whose content is unknown and let n be declared as an int. Please write a code segment to use stringstream to extract an integer from x and store the value into n. Please do not perform any error checking in your code. You may assume that everything else (e.g., #include and using statements) have been setup properly. (10 points) Please complete the code segment below to accomplish the following. In every iteration of the for loop, prompt the user to enter a value for x (this is done for you already). Then read an integer from the commandline (i.e., cin). Check if the reading operation is successful (please do not use the exception mechanism). If it is successful, break out of the for loop. Otherwise, print an error message to cerr and ask the user to try again, clear the error in cin, discard up to 1,000 character unless a \n character is encountered in cin s buffer, and go back to the top of the for loop. When you get out of the for loop, print the value of x (this is done for you already). You may assume that everything outside of the for loop (e.g., #include and using statements) have been setup properly. int x = (-1); for (;;) { cout << "Enter x: "; cout << "You entered: " << x << endl;

Problem 3. Struct & Classes (12 points) Page 4 of 8 (4 points) The implementation of getarea() method of the Rectangle class looks like the following (you can assume that the code shown as... are all correct): double Rectangle::getArea() const {... What s the meaning of the keyword const in the above code? (4 points) If you tried to compile the following code, you would get a compiler error saying that Gismo::xRef and Gismo::yRef are uninitialized reference member. class Gismo { private: int& xref; int& yref; public: Gismo(int& x, int& y) { xref = x; yref = y; ; Error Why are you getting this error message? You may assume that everything else have been setup properly. (c) (4 points) Continuing from part, how would you fix that single line of code? Please only give a single line of code to get rid of the compiler error and have xref and yref initialized properly. Please note that you will receive at most 2 points if you just give a correct description without correct code.

Problem 4. Exception Handling & Namespaces (14 points) Page 5 of 8 (4 points) Can you name a couple of things that are bad/ugly about C++ exceptions? (10 points) Please complete the code segment below to accomplish the following. Before the for loop is entered, setup cin to throw the iostream::failure exception (this is done for you already). In every iteration of the for loop, prompt the user to enter a value for x (this is done for you already). Then read an integer from the commandline (i.e., cin). If this caused an iostream::failure exception to be thrown, you need to catch it, print an error message to cerr and ask the user to try again, clear the error in cin, discard up to 1,000 character unless a \n character is encountered in cin s buffer, and go back to the top of the for loop. If an integer is successfully read into x, you need to break out of the for loop. When you get out of the for loop, print the value of x (this is done for you already). You may assume that everything outside of the for loop (e.g., #include and using statements) have been setup properly. int x = (-1); cin.exceptions(iostream::failbit); for (;;) { cout << "Enter x: "; cout << "You entered: " << x << endl;

Problem 5. Operator Overloading (16 points) Page 6 of 8 (8 points) Let the Point class be defined as follows: class Point { public: int x; // no getter and setter since this is public int y; // no getter and setter since this is public Point() { x = 0; y = 0; Point(int newx, int newy) { x = newx; y = newy; ; Let s say that you would like the following code to print p1 at (10,20), p2 at (30,40) : Point p1(10,20), p2(30,40); cout << "p1 at " << p1 << ", p2 at " << p2 << endl; Please write a function to overload cout s << operator so that it would work with any valid Point object. (8 points) Continuing from part and assuming that your implementation was correct, if you would like the following code to print p3 at (98,76), p4 at (54,32) if you entered 98 76 54 32 at the commandline when prompted: Point p3, p4; cout << "Enter p3 and p4: "; cin >> p3 >> p4; cout << "p3 at " << p3 << ", p4 at " << p4 << endl; Please write a function to overload cin s >> operator so that it would work with any valid Point object. Please do not perform any error checking in your code.

Problem 6. Composition & Inheritance (14 points) Page 7 of 8 (4 points) What does the keyword protected mean in a class definition? (6 points) Let the Shape and Triangle classes be defined as follows: class Shape { public: Point center; // no getter and setter since this is public Shape() { center.x = center.y = 0; // default constructor Shape(int newx, int newy) { center.x = newx; center.y = newy; void print() { std::cout << "Shape at " << center << std::endl; ; class Triangle : public Shape { public: int base; // no getter and setter since this is public int height; // no getter and setter since this is public INCOMPLETE Triangle() { base = height = 0; // default constructor Triangle(int b, int h, Point& center)... void print() { std::cout << "Triangle at " << center << std::endl; ; The Point class was defined in an earlier exam question. You may assume that everything else have been setup properly. Please finish the implementation for the constructor of the Triangle class. In order to receive any credit, you must stick to the class definition above. Please make sure that every member in the Triangle class and its superclass are initialized to correct values. (c) (4 points) Continuing from part, why wouldn t the 2nd line of code below compile? Shape s(100,200); // okay Triangle t2 = s; // will not compile, what s wrong?

Problem 7. Dynamic Objects & Polymorphism (16 points) Page 8 of 8 (4 points) What s bad about the code below? Please be as specific as possible. int *bar() { int x = 5; return &x; (4 points) When you have a dynamically allocated array of objects (not array of pointers), why is it necessary to use delete[] instead of just delete to destroy the array? (c) (4 points) What is the Rule of Three? Please be as specific as possible. (d) (4 points) What makes a class an Abstract Base Class? Please be as specific as possible.