MidtermSample

**Sample Midterm Exam **
 * CS215 Spring 2012 **

int numarray[ ] = { 1, 2, 3, 4, 5 }; cout << numarray[1]; cout << numarray[4]; What is the output of the code snippet? A) 15 B) 14 C) 25 D) 04
 * 1) Consider the following code snippet:

int ctr = 0; int myarray[3]; for (int i = 0; i < 3; i++) { myarray[i] = ctr; ctr = ctr + i; } cout << myarray[2]; What is the output of the code snippet? A) 0 B) 1 C) 2 D) 3
 * 1) Consider the following code snippet:

A) vector num1; vector odnum;  int data;  for (int i = 0; i < 10; i++)  {  cin >> data;  num1.push_back(data);  if (num1[i] % 2 == 0)  {  odnum.push_back(num1[i]);  }  } B) vector num1; vector odnum; int data; for (int i = 0; i < 10; i++) { cin >> data; num1.push_back(data); if (num1[i] % 2 != 0) { odnum.push_back(num1[i]); } } <span style="font-family: 'times new roman','serif'; font-size: 15px;">C) vector num1;  vector odnum;  int data;  for int i = 0; i < 10; i++) { cin >> data; num1.push_back(data); if (num1[i] % 2 == 0) { odnum[i] = num1[i]);  }  } <span style="font-family: 'times new roman','serif'; font-size: 15px;">D) vector num1; vector odnum; int data; for (int i = 0; i < 10; i++) { cin >> data; num1[i] = data; if (num1[i] % 2 == 0) { odnum[i] = num1[i]);  }  }
 * 1) <span style="font-family: 'Times New Roman','serif'; font-size: 15px;">Which one of the following code snippets accepts the integer input in a vector named num1 <span style="font-family: 'Times New Roman','serif'; font-size: 15px;"> and stores the odd integers of num1 <span style="font-family: 'Times New Roman','serif'; font-size: 15px;"> in another vector named odnum <span style="font-family: 'Times New Roman','serif'; font-size: 15px;">?


 * 1) <span style="font-family: 'Times New Roman','serif';">A pointer describes which of the following?

<span style="font-family: 'times new roman','serif';">A) What a certain value in memory is <span style="font-family: 'times new roman','serif';">B) Where a certain value in memory is <span style="font-family: 'times new roman','serif';">C) When a certain value is in memory <span style="font-family: 'times new roman','serif';">D) The size of a certain value in memory


 * 1) <span style="font-family: 'Times New Roman','serif';">Which of the following statements is true about pointers?

<span style="font-family: 'times new roman','serif';">A) Pointers contain values as well as addresses of variables. <span style="font-family: 'times new roman','serif';">B) Pointers contain address locations of variables. <span style="font-family: 'times new roman','serif';">C) Pointers do not have any address location of their own. <span style="font-family: 'times new roman','serif';">D) Pointers occupy permanent storage locations inside the hard disk.

int num = 0; int* ptr = &num; num = 5; cout << num << " " << *ptr << endl;
 * 1) <span style="font-family: 'Times New Roman','serif';">What is the output of the following code snippet?
 * ptr = *ptr + 5;

<span style="font-family: 'times new roman','serif';">A) 5 5 <span style="font-family: 'times new roman','serif';">B) 5 10 <span style="font-family: 'times new roman','serif';">C) 10 5 <span style="font-family: 'times new roman','serif';">D) 10 10

int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; <span style="font-family: 'Times New Roman','serif';">Which of the following is the value of *(arr + 5)<span style="font-family: 'Times New Roman','serif';">?
 * 1) <span style="font-family: 'Times New Roman','serif';">Consider the code snippet below.

<span style="font-family: 'times new roman','serif';">A) 4 <span style="font-family: 'times new roman','serif';">B) 5 <span style="font-family: 'times new roman','serif';">C) 6 <span style="font-family: 'times new roman','serif';">D) 8

void copy_file(... in_file, ... out_file) { char ch; while (in_file.get(ch)) { out_file << ch; } }
 * 1) <span style="font-family: 'Times New Roman','serif';">Which of the following statements is true about the parameters in the given code snippet?

<span style="font-family: 'times new roman','serif';">A) The parameters to the function must be references: void copy_file(ifstream& in_file, ofstream& out_file) <span style="font-family: 'times new roman','serif';">B) The parameters to the function must be pointers: void copy_file(ifstream* in_file, ofstream* out_file) <span style="font-family: 'times new roman','serif';">C) The parameters to the function must be of the type fstream:void copy_file(fstream in_file, fstream out_file) <span style="font-family: 'times new roman','serif';">D) The parameters to the function must be of type istream<span style="font-family: 'Times New Roman','serif';"> and ostream<span style="font-family: 'Times New Roman','serif';">: void copy_file(istream in_file, ostream out_file)

<span style="font-family: 'Times New Roman','serif';">"Who am I? 24601!" ifstream in_file; char ch; in_file.open("test.txt"); while (in_file.get(ch)) { if (isdigit(ch)) { cout << ch; } } <span style="font-family: 'times new roman','serif';">A) ?24601! <span style="font-family: 'times new roman','serif';">B) 24601 <span style="font-family: 'times new roman','serif';">C) Who am I? ! <span style="font-family: 'times new roman','serif';">D) WhoamI?24601!
 * 1) <span style="font-family: 'Times New Roman','serif';">What is the output from the code snippet below when the input file test.txt<span style="font-family: 'Times New Roman','serif';"> is the single line

class CashRegister { public: void set_item_count(int count); void view const;
 * 1) <span style="font-family: 'Times New Roman','serif';">What is the output of the following code snippet?

private: int item_count; }; void CashRegister::view const { cout << item_count << endl; } void CashRegister::set_item_count(int count) { item_count = count; } int main { CashRegister reg1, reg2; reg1.set_item_count(15); reg2.set_item_count(10); reg1.view; reg2.view; return 0; } <span style="font-family: 'times new roman','serif';">A) 15 <span style="font-family: 'Times New Roman','serif';">25 <span style="font-family: 'times new roman','serif';">B) 15 <span style="font-family: 'Times New Roman','serif';">10 <span style="font-family: 'times new roman','serif';">C) 10 <span style="font-family: 'Times New Roman','serif';">15 <span style="font-family: 'times new roman','serif';">D) 15 <span style="font-family: 'Times New Roman','serif';">15


 * 1) <span style="font-family: 'Times New Roman','serif';">In a car rental application, the following Car <span style="font-family: 'Times New Roman','serif';"> class interface is defined. One of the programmers discovers a bug in the void accelerate(double speed) <span style="font-family: 'Times New Roman','serif';"> function. She corrects the bug by modifying the code snippet in the implementation of the Car <span style="font-family: 'Times New Roman','serif';"> class accelerate <span style="font-family: 'Times New Roman','serif';"> function. What must be changed in the interface?

class Car { public: void start; void accelerate(double speed); void stop; double get_speed const;

private: double speed; };

<span style="font-family: 'times new roman','serif';">A) The accelerate(double speed) <span style="font-family: 'Times New Roman','serif';"> declaration must be updated. <span style="font-family: 'times new roman','serif';">B) A new private acceleration data member must be added. <span style="font-family: 'times new roman','serif';">C) The accelerate function must be declared as void accelerate(double acceleration) <span style="font-family: 'times new roman','serif';">D) No change is required in the interface because only the implementation was modified.


 * 1) <span style="font-family: 'Times New Roman','serif';">What is the output of the following program?

using namespace std; class Mountain { public: double get_height const;
 * 1) include

Mountain; Mountain(double new_height);

private: double height; }; Mountain::Mountain { height = 0; } Mountain::Mountain(double new_height) { height = new_height; } double Mountain::get_height const { return height; } int main { Mountain cth1; Mountain cth2(15000); double total_height = 0; total_height = cth1.get_height + cth2.get_height; cout << total_height << endl; return 0; }

<span style="font-family: 'times new roman','serif';">A) 0 <span style="font-family: 'times new roman','serif';">B) 15000 <span style="font-family: 'times new roman','serif';">C) Nothing is printed because the program doesn't compile. <span style="font-family: 'times new roman','serif';">D) The answer cannot be determined because the height of cth1 <span style="font-family: 'Times New Roman','serif';"> has not been initialized.


 * 1) <span style="font-family: 'Times New Roman','serif';">Declare an array of type "Mountain" with 20 slots.
 * 2) <span style="font-family: 'Times New Roman','serif';">Write a loop to print out every slot of the array you have declared (assume there is a "Write" method defined in the "Mountain" class)
 * 3) <span style="font-family: 'Times New Roman','serif';">Declare an array of type "int" with 20 slots and initialize slot zero with 19, the next slot with 18, all the way to the last slot, with the value of zero.
 * 4) <span style="font-family: 'Times New Roman','serif';">Assume the array of integers (containing values from 19 down to zero) is meant to be an array of values to index into the array of "Mountain" objects. Write a loop to print them out in the order defined by the array of integers. In other words, the first Mountain element to print would be "myMountains[myArray[0]]".
 * 5) <span style="font-family: 'Times New Roman','serif';">What will be the output from the previous problem? Explain.