need answer key for old exams computer prgramming
CSCI 111, Spring 2013, Final Exam
1. (1 point) Write your name on the backside of the last page. 2. (1 point) Every C++ program begins at the function ____________. 3. (4 point) What is the difference between these 2 include statements. #include <iostream> #include “iostream”
4. (2 point) What is the expected output of the following piece of code? int x = 5.7; int y = 3.2; cout << x-y; 5. (3 point) Assuming we have three C++ file named main.cpp, test.h, and test.cpp, what command could we use to compile our code into an executable? 6. (4 point) Name the 4 steps that occur when a program is compiled 1.
2.
3.
4.
7. (10 point) Write the implementation file (person.cpp) for the following person.h file. The file must be complete and should compile. #include <string> using namespace std; class Person { public: Person(string, int); //should set private data members string getName(); int getAge(); private: string name; int age; };
8. (5 point) What is the output of the following program?
#include <iostream>
using namespace std; void figureMeOut(int& x, int y, int& z);
int main( ) {
int a, b, c; a = 10; b = 20; c = 30; figureMeOut(a, b, c); cout << a << " " << b << " " << c << endl; return 0; }
void figureMeOut(int& x, int y, int& z) { cout << x << " " << y << " " << z << endl; x = 1; y = 2; z = 3; cout << x << " " << y << " " << z << endl; }
9. (4 point) What is the purpose of a constructor? When does it run?
10. (3 point) Why do you use #ifndef, #define, and #endif in a header file?
11. (2 point) Describe the action of the new operator
12. (3 point) Suppose you are writing a program the uses a stream called fin, which will be connected to an input file called “stuff1.txt” and a stream called fout, which will be connected to an output file called “stuff2.txt”. How do you declare fin and fout? What include directive, if any, do you need to place in the program file?
13. (2 point) What is wrong with the following piece of code?
int sampleArray[10]; for (int index = 1; index <= 10; index++) sampleArray[index] = 3*index;