Sample Questions for 2610 Exam 1
(All 20 Questions are Here!)
Section 1.3
Section 1.4
All of Chapter 2
Section 3.1
Section 3.2
Section 3.3
Section 3.5
Section 4.1
Section 4.2
Section 4.3 pages 196-198
Section 9.1
Section 9.2
Section 9.3
Section 10.3
Section 11.1

Study Problems:
1.  What will be the value of x after each of these lines of code are executed:  (Assume x has been typed as an integer.)

• x = 1 - 2 - 3 - 4
• x = 1 + 2 * 3 - 4 * 5
• x = 10 / 3
• x = 3; x += 2 *= 3 -= 4
2.  What will be the output of the following fragment of code:  (explain your answer)

int x;
x = 4;
if(x % 2 == 1)
x *= 5;
x += 7;
cout << x;

3.  What will be the output of the following fragment of code:  (explain your answer)

int x, y, temp;
x = 3;
y = 7;
if(x > y)
temp = x;
x = y;
y = temp;
cout << x << " " << y;

4.  What will be the output of the following fragment of code:  (explain your answer)

int i, s;
i = 10;
s = 0;
while(i > 0){
s += i;
i--;
}
cout << s << " " << i;

5.  What will be the output of the following fragment of code:  (explain your answer)

int i, p;
i = 7;
p = 1;
do{
i -= 2;
p *= i;
}while(i > 0);
cout << p << " " << i;

6.  What will be the output of the following fragment of code:  (explain your answer)

int i;
for(i = 1; i < 9; i++){
cout << i << endl;
}

7.  Complete the following fragment of code.  Use a switch statement so that if the user inputs a number from 1 to 3, the program will output the spelling of that number, but if the user inputs anything else, he will get a polite error message.  (Don't worry about looping for another user input.)

int userinput;
cout << "Please enter a number from 1 to 3:  ";
cin >> userinput;

switch(...

A sample user interaction might look like this:
Please enter a number from 1 to 3:  3
three

8.  What is a function header?  What is a function prototype?

9.  Identify each part of the function prototype below:
bool guzinta(int divisor, int dividend);

10.  Write a prototype of a function called "istriangle" which takes three parameters of type double, and returns true if the three parameters form the sides of a triangle, and false if they don't.

11.  Give two good reasons to use functions in your C++ programs

12.  What's the difference between a pre-defined function and a user-defined function?

13.  Give an example of a pre-defined function that we've seen in this class.

14.  Here is a program with a function that passes its parameters by value:

#include<iostream>
#include<string>

void main(){
int

numos = numberofos(name);
cout << "For your name, the answer is " << numos << endl;
}

int numos(string name){
int i, numfound;

numfound = 0;

for(i = 0; i < name.length(); i++){
if(name[i] == 'o' || name[i] == 'O')
numfound++;
}

return numfound;
}

a.  What does the program do?
b.  Are the braces in the for loop required?
c.  Rewrite the program so that the function passes its parameter by reference.
d.  Rewrite the program so that numos has a void return value, but "returns" its answer by adding a new parameter which is passed by pointer.

15.  What do the following lines do?

string names[35];
double heights[35];
book occupied[8][8];

16.  What does the following code fragment do?

s = 0;
for(i = 0; i < 20; i++)
s += area[i];
cout << s;

17.  Suppose int s[40] is an array with 40 elements:

• How would you refer to the first element in this array?
• How would you refer to the last element in this array?
• Write a fragment of code that assigns to a variable "sum" the sum of the elements of this array.
18.  Suppose int s[40] is an array with 40 elements that have been sorted from smallest to largest.  Write a fragment of code that will search this array to see if one of the elements is equal to 57.

19.  Suppose I tell you I'm thinking of an integer from 1 to 1000, you have 10 tries to guess it, and after each guess I tell you "bigger" or "smaller."  Do you think you can get the number every time?  Suppose I'm thinking of a number from 1 to M instead of 1 to 1000.  Find a formula, in terms of M, for the number of guesses needed to guarantee that you can get my number within that many guesses.

20.  Okay, you won't need to be able to solve a question like #19 on the test, but you will need to be able to write code to perform a "binary search" on a sorted array to find whether or not some value is present in the array.  For example, suppose I tell you I have an array with M entries, and those entries are sorted from smallest to largest.  I am also thinking of a number and I want you to find out whether or not that value is in the array.  What is the fastest way to search this array to see if the value is present?  Write code to perform this search.  Do you see how this relates to question #19?