need answer key for old exams computer prgramming
CSCI 111, Fall 2013, Final Exam
You do NOT have to write complete programs to answer the questions below,
unless the question asks explicitly that you do so.
1. (2 points) Write your name on the backside of the last page.
2. (8 points) Given the function:
void printMessage(string message);
Answer the following questions
What is the function's return type?
List the formal parameter(s) of the function. Include the data type and name
of each formal parameter.
Write a call/invocation of the function printMessage with the string "This is
so easy!" as the argument.
Is the function declaration or function definition shown above?
3. (5 points) Given the statement:
float* fp=new float[3];
To release this memory:
a) delete fp;
b) delete [] fp;
c) for(int i=0; i<3; ++i) delete fp[i];
d) delete fp[3]
e) delete float[3];
4.(5 points) Given:
a=3;
b=4;
expression 1: a -= b;
expression 2: a =- b;
What's the difference between the first and second expressions
a) They are equivalent
b) First is positive and second is negative
c) Second is negative and first is positive
d) The second one doesn't compile
e) None of the above
5.(10 points) Consider the program below in the test.cpp file
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. do {
6. cout << “give me an int: “;
7. int i;
8. cin >> i;
9. } while (i < 0 || i > 10);
10. cout << “Thanks, i is “ << i << endl;
11. return 0;
12. }
This program has a bug. It will not compile and gives the compiler error of
test.cpp:10:28 error: ’i’ was not declared in this scope
Identify what line number the bug is on and propose how to eliminate it.
You’re welcome to write on or alongside the code itself.
6.(10 points) Given the statement
string* airplane[25];
Describe what is created when this statement is executed. Be explicit in your
answer.
7. (10 points) Given an array of integers:
int myArray[20];
open a text file for writing called “arraycontents.txt” and then write a loop
that will print all of the contents of the array to the file.
8. (10 points) Carefully distinguish between the meaning and use of the dot
operator “.” and the scope resolution operator “::”
9. (15 points) Suppose your program contains the following class, contained in
automobile.h,
class Automobile
{
public:
Automobile(double prc, double prft); //sets private data members
void setPrice(double prc);
void setProfit(double prft);
double getPrice( );
private:
double price;
double profit;
double getProfit( );
};
and suppose the main function of your program contains the following
declarations
Automobile hyundai (3999.99, 299.50);
Automobile jaguar (38999.99, 3200.00);
Which of the following statements will compile in the main function of your
program? Write YES or NO before each statement.
hyundai.price = 4999.99;
jaguar.setPrice(30000.97);
double aPrice, aProfit;
aPrice = jaguar.getPrice( );
aProfit = jaguar.getProfit( );
aProfit = hyundai.getProfit( );
hyundai = jaguar;
10. (25 points) Write the implementation file (automobile.cpp) for the class
contained in automobile.h, which is described in the previous question.