Computer science C++
SUNY Polytechnic Institute
Computer Science 240 Fall, 2020 Dr. R. Sarner
Exam #1 Monday Version
Part I. (10 points each, 50 points total)
1. Describe the waterfall model of software development.
2. What is meant by the term static allocation? Write a line of C++ code that creates a variable that is statically allocated.
3. What is meant by an anonymous variable? Write a line of C++ code that creates an anonymous variable.
4. What is meant by the term the stack? Explain what it is and how it is used.
5. What (according to Nyhoff) is an abstract data type?
CS 240 1 Fall 2020 Exam #1 (Monday)
Part II (50 points)
The questions that follow are based on the following C++ program: /*Program to demonstrate memory allocation Written by Ron September 2019 Language: C++ (Cygwin 64-bit g++ compiler)
Identifier names taken from characters on Happy Days (1974-1984) */
#include <iostream> Fonzie
using namespace std;
int main(void) { int richie_cunningham, // primary character joanie_cunningham, // Richie's sister fonzie; // lived with the Cunninghams and
// stole the show int *howard_cunningham, // the father
*marion_cunningham, // the mother Ralph Malph *potsie_weber, // a friend of Richie *ralph_malph; // also a friend of Richie
richie_cunningham=61; joanie_cunningham=8; fonzie=richie_cunningham%joanie_cunningham*4-11; howard_cunningham=&richie_cunningham; marion_cunningham=&joanie_cunningham; potsie_weber=&fonzie; ralph_malph=new int; *ralph_malph=fonzie+richie_cunningham-joanie_cunningham; cout<<"richie_cunningham is stored in location "<< &richie_cunningham<<" and its value is "<< Richie Cunningham richie_cunningham<<endl; (left) and Potsie cout<<"joanie_cunningham is stored in "<< Weber (right) &joanie_cunningham<<endl; cout<<"howard cunningham is stored in "<< &howard_cunningham<<" and its value is "<< howard_cunningham<<endl; cout<<"ralph_malph is stored in "<< &ralph_malph<<" and its value is "<< ralph_malph<<endl; cout<<"the value pointed to by ralph_malph is "<< *ralph_malph<<endl; return 0; }
CS 240 2 Fall 2020 Exam #1 (Monday)
When this code is compiled and executed the following output is obtained:
richie_cunningham is stored in location cbf8 and its value is 61 joanie_cunningham is stored in cbf4 howard cunningham is stored in cbe8 and its value is cbf8 ralph_malph is stored in cbd0 and its value is 10460 the value pointed to by ralph_malph is 62
a. A partially filled in memory map for this program is shown below. Fill in the missing elements (4 points each, 40 points total)
Address Identifier Stored Contents
cbf8 61
cbf4 joanie_cunningham
cbf0
cbe8 howard_cunningham cbf8
cbe0
cbd8
cbd0 ralph_malph
10460 none
b. Explain why seven of the data elements are all in consecutive memory spaces in the cb memory range, while one data element is in a completely different memory space (10460). 10 points.
CS 240 3 Fall 2020 Exam #1 (Monday)
Part III. Trace the code that follows showing all output in the order that it appears on the screen (50 points). /*Program to demonstrate array operations Written by Ron September 2020 Language: C++ (Cygwin 64-bit g++ compiler) */
#include <iostream> using namespace std;
#define SIZE 5
void print_the_thing(const int herry[SIZE]);
int main(void) { int oscar[SIZE]={16,2,5,8,3}; //creates and fills array int i, cookie; int *ptr1, *ptr2, *ptr3; //creates three pointers
cout<<"Program begins"<<endl; print_the_thing(oscar); cookie=0; for (i=0;i<SIZE;i++) {cookie=cookie+oscar[i]/3; cout<<"i is now "<<i<<" cookie is now "<<cookie<<endl; oscar[i]=cookie; } cout<<"after loop 1 cookie is "<<cookie<<endl; print_the_thing(oscar); ptr3=&oscar[2]; ptr1=oscar; cookie=0; //cookie reset to zero for (ptr2=ptr1;ptr2<=ptr3;ptr2++) {cout<<"the array element pointed to by ptr2 is "<< *ptr2<<endl; cookie=cookie+ *ptr2; cout<<"cookie is is now "<<cookie<<endl; } return 0; }
void print_the_thing (const int herry[SIZE]) /*Function to print out an array - sort of Array is passed by constant reference to ensure that no values are overwritten but const can be ignored for this trace Written by Ron Sept 2020 Language: C++ (Cygwin 64-bit g++ compiler */ { int kount=4; cout<<"Entering print_the_thing"<<endl; while (kount>=0) {cout<<herry[kount]<<" "; kount--; } cout<<endl; return; }
CS 240 4 Fall 2020 Exam #1 (Monday)
Part IV. Trace the code that follows showing all output in the order that it appears on the screen (50 points). Note, there are two files shown here: main.cpp and exam1.h. The squirrel class is fully defined within the exam1.h file. For the sake of simplicity the code for all the functions that are in the class (not just the headers) are in the exam1.h file. ===============================main.cpp follows===================== /*Object Oriented Trace Written By Ron September 2020 Language: C++ (Cygwin 64-bit g++ compiler)
*/
#include <iostream>
using namespace std; #include "exam1.h"
int main(void) { squirrel rocky; squirrel ricky(16,3); int sum; sum=0; cout<<"In rocky, bw is "<<rocky.get_bw()<<endl; cout<<"In rocky, boris is "<<rocky.get_boris()<<endl; cout<<"In rocky, peachfuzz is "<<rocky.get_peachfuzz()<<endl; cout<<"In rocky values of cloyd are: "; for (int i=0;i<5;i++) {cout<<rocky.get_cloyd(i)<<" "; sum=sum+rocky.get_cloyd(i); } cout<<endl; cout<<"sum is now "<<sum<<endl; cout<<"In ricky, rocky is "<<ricky.get_rocky()<<endl; cout<<"In ricky, boris is "<<ricky.get_boris()<<endl; cout<<"In ricky, peachfuzz is "<<ricky.get_peachfuzz()<<endl; for (int i=0; i<5;i++) {cout<<ricky.get_cloyd(i)<<" "; sum=sum+ricky.get_cloyd(i); } cout<<"At end sum is "<<sum<<endl; return 0; } //exam1.h file is shown on the next page
CS 240 5 Fall 2020 Exam #1 (Monday)
=========================exam1.h follows (2 pages)================ /*Header file exam1.h Written by Ron September 2020 Language: C++ (Cygwin 64-bit g++ compiler */
#include <iostream>
using namespace std;
class squirrel { public:
squirrel() /*Default constructor for squirrel class*/ {int i; cout<<"Entering default squirrel constructor"<<endl; rocky=12; bw=5; boris=rocky*(bw-bw); peachfuzz=rocky+boris-bw; for (i=0;i<5;i++) cloyd[i]=i+boris+peachfuzz; }
squirrel(int peabody, int sherman) {int i; cout<<"Entering explicit value constructor"<<endl; rocky=peabody; bw=sherman; boris=rocky+bw; peachfuzz=boris*rocky; for (i=0;i<5;i++) cloyd[i]=i+peachfuzz; }
//continued on next page
CS 240 6 Fall 2020 Exam #1 (Monday)
//Accessor functions int get_rocky(void) /*Accessor function to return value of rocky data member*/ {return rocky; }
int get_bw(void) /*Accessor function to return value of bw data member */ {return bw; }
int get_boris(void) /*Accessor function to return value of boris data member */ {return boris; }
int get_peachfuzz(void) /*Accessor function to return value of peachfuzz data member*/ {return peachfuzz; }
int get_cloyd(int i) /*Accessor function to return value of one element of cloyd data member*/ {return cloyd[i]; }
private: int rocky, bw, boris, peachfuzz, cloyd[5]; }; //end of class definition
Boris Badenov
Capt. Peter Peachfuzz Bullwinke, Rocky,Cloyd and Gidney
CS 240 7 Fall 2020 Exam #1 (Monday)