CMSC 202 Midterm Exam 1 Fall 2015

Similar documents
a data type is Types

Name. CPTR246 Spring '17 (100 total points) Exam 2

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Pointers, Dynamic Data, and Reference Types

Programming Language. Functions. Eng. Anis Nazer First Semester

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

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.

Module 7 b. -Namespaces -Exceptions handling

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

CSCE Practice Midterm. Data Types

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

Lab Instructor : Jean Lai

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

PIC 10A. Review for Midterm I

Linked List using a Sentinel

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

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

C++_ MARKS 40 MIN

LECTURE 02 INTRODUCTION TO C++

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

CMPS 221 Sample Final

Arrays. Week 4. Assylbek Jumagaliyev

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

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

W3101: Programming Languages C++ Ramana Isukapalli

CS2141 Software Development using C/C++ C++ Basics

Understanding main() function Input/Output Streams

Exercise 1.1 Hello world

Do not turn to the next page until the start of the exam.

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 3

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSCE 110 PROGRAMMING FUNDAMENTALS

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Lab # 02. Basic Elements of C++ _ Part1

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Review Questions for Final Exam

Integer Data Types. Data Type. Data Types. int, short int, long int

Why Is Repetition Needed?

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Implementing an ADT with a Class

Inheritance, and Polymorphism.

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

pointers + memory double x; string a; int x; main overhead int y; main overhead

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Structured Data. CIS 15 : Spring 2007

CSCE Practice Midterm. Data Types

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

BITG 1233: Introduction to C++

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

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

Programming in C++: Assignment Week 8

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

C++ basics Getting started with, and Data Types.

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Name Section: M/W or T/TH. True or False (14 Points)

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Midterm Practice TA: Brian Choi Section Webpage:

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Tokens, Expressions and Control Structures

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Lecture 7. Log into Linux New documents posted to course webpage

VARIABLES & ASSIGNMENTS

Fast Introduction to Object Oriented Programming and C++

CS 141, Introduction to Computer Science Fall Midterm Exam

CS 115 Exam 3, Spring 2010

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Your first C++ program

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

A SHORT COURSE ON C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

Exam 3 Chapters 7 & 9

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Ch 6. Functions. Example: function calls function

2. Functions I: Passing by Value

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

6.096 Introduction to C++ January (IAP) 2009

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

CPSC 427: Object-Oriented Programming

CSCI 1370 APRIL 26, 2017

CPSC 427: Object-Oriented Programming

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Transcription:

1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below. 1 #include "iostream" 2 using namespace std; 3 4 void main() { 5 6 cout << "This program computes Fn, the nth fibonacci number." 7 << endl; 8 9 do { 10 cout << "Enter a positive integer: "; 11 cin >> n; 12 } while (n < 1); 13 14 if (n = 1 n == 2) 15 cout << "F" << n << " = 1" << endl; 16 else { 17 int a = 1, b = 1; 18 for (int i = 3; i <= n; i++) 19 int c = b; 20 b + a; 21 a = c; 22 cout << "F" << n << " = " << b << endl; 23 } 24 25 return 0; 26 } Line Number Correction 1 Change "iostream" to <iostream> 4 Change void main() to int main() 11 n not declared; add declaration, e.g. on line 5 14 Change n=1 to n ==1 18 / 21 Need { } around body of for loop 20 Change b+a to b = b + a or b+= a 1 of 8

2. (18 points) Complete the code: a. Complete the following interface for a Dinosaur class: class Dinosaur { public: void SetDescription(string description); void SetCarnivorous(bool carnivorous); string GetDescription() const; bool GetCarnivorous() const; private: string m_description; // description bool m_carnivorous; // carnivorous if true }; must have semicolon b. Complete the implementation of the GetDescription() method of the Dinosaur class. Assume the implementation is in a separate file from the interface. Dinosaur::GetDescription string () const { } return m_description; c. In my main function, I create a Dinosaur object named barney and use the "Set" methods to initialize barney's variables. Complete the following code to print barney's description: cout << barney.getdescription() << endl; 3. (2 points) Why are the two "Get" functions in problem (2) const? They are accessor methods that do not modify any of the class variables. Do not have to use term "accessor"; must say they do not modify class variables. 2 of 8

4. (10 points) Will the following program work as the programmer intended? Why or why not? Address both the method by which parameters are passed and the logic of the UpperCase() function. 1 #include <iostream> 2 using namespace std; 3 void UpperCase(char str[]); 4 int main() { 5 char msg[80] = "I'm having candy for dinner!"; 6 // Convert msg[] to upper case and print it to the screen 7 UpperCase(msg); 8 cout << msg << endl; 9 } 10 void UpperCase(char str[]){ 11 for (int i = 0; str[i]!= '\0'; i++) 12 if ( 'a' <= str[i] && 'z' >= str[i] ) 13 str[i] = str[i] - 'a' + 'A'; 14 } Yes, it will work as expected. C-string is passed as address (reference), so the changes made in UpperCase() will be visible in main() Have to say something like C-string / arrays are passed as pointers or passed by reference. The function UpperCase() uses character literals to determine if a letter is lower case and to covert it to upper case Reasonable description of the function logic and indication that they understand the use of char literals. 3 of 8

5. (10 points) What will the following program print to the screen? Complete the boxes below and explain your answers. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 double data[5] = {-1.0, 3.14, 7.59, -0.3, 1.1}; 5 double max = data[0]; 6 int maxi = 0; 7 // Find the maximum value and index of the maximum value 8 // in the data array 9 for (int i = 1; i < 5; i++) { 10 if (data[i] > max) { 11 int max = data[i]; 12 maxi = i; 13 } 14 } 15 cout << "max value is " << max << endl; 16 cout << "max occurs at index " << maxi << endl; 17 } Output: max value is -1.0 max occurs at index 4 Explanation: Because the variable max is re-declared within the body of the if statement, it's scope is the body of the if. The modification to max on line 11 is limited to the scope of the if statement and so when max is printed on line 15, the original value from line 5 is printed (data[0] which is -1.0). The variable maxi is not re-declared within the body of the if statement, so the modification on line 12 effects the variable in the main() scope. The value of maxi increases so long as data[i] > max, and max is always -1.0, so the end value of maxi is 4, the last index of the array. Must demonstrate understanding of scope 4 of 8

6. I want to write a function RemoveSpaces() to remove the spaces from a C-string. The C- string to be processed will be passed as a parameter, and the resulting string, with spaces removed, should overwrite the original string. The function has no return value. a. (8 points) Complete the function prototype: void char str[] RemoveSpaces( ); b. (12 points) Write the implementation of the RemoveSpaces() function: void RemoveSpaces( char str[] ) { int i, j; for (i = 0, j = 0; str[i]!= '\0'; i++) if (str[i]!= ' ') str[j++] = str[i]; } str[j] = '\0'; } return; 5 of 8

CMSC 202 Midterm Exam 1 Fall 2015 7. (6 points) Write a function header comment for your RemoveSpaces() function from (6.b), including a description of the function, its pre-conditions, and post-conditions. RemoveSpaces() - remove the spaces from a C-string Pre-conditions: str is a null-terminated C-string Post-conditions: spaces are removed from the input string; the transformed string overwrites the original string. The transformed string will be null-terminated. 8. (4 points) Encapsulation is an important concept in object-oriented programming. a. Define encapsulation: Encapsulation is the bundling of data and methods (variables and functions) along with methods of restricting access to data and methods. b. Give an example of how encapsulation is implemented in C++: C++ classes combine data and methods and provide mechanisms for restricting access to both. 6 of 8

9. (15 points) Consider the following program: 1 #include <iostream> 2 using namespace std; 3 4 int main() { 5 6 char word[] = "terryology"; 7 8 char *cptr = &word[9]; 9 10 while (cptr >= word) { 11 cout << *cptr; 12 cptr--; 13 } 14 cout << endl; 15 } a. What is the output of the program? ygoloyrret b. Describe the values that are compared in the conditional cptr >= word on line #10 in terms of the elements of the word[] array. cptr is the address of a letter in word[]; word is the address of the first letter in word[]. Therefore, the comparison tests whether cptr is pointing to a letter after the first letter. c. Explain the different meanings of the "*" character on line #8 and line #11. Line 8: * is part of the declaration of cptr; it says the cptr is a pointer variable. Line 11: * is a pointer dereference; *cptr is the char pointed to by cptr. 7 of 8

Points Earned 1 15 2 20 3 10 4 10 5 20 6 10 7 15 Total 100 8 of 8