Some Practice Problems for the Mid-term Exam

1. Consider the following C++ program. Find all the errors (syntax or logical), if any, it contains. Also indicate how to fix each error.

#include <iostream>
using namespace std;

int main()
{

int a;

a = 34.0;
boolean b;

cout<< Cliff Hanger is a great movie!;

cout<<a;

if(a = 21) cout<<"a = "<< 21<<endl;

return 0;
}

2. a) The truth value of the expression !(2>3)&&(3<4)&&(-1) is ...

b) The relational operator "not equal to" in C++ is ...

3. Consider the following piece of program. Suppose val is an integer variable and it has some value stored in it.

if ( (val<-1)&&(val>-5)&&(val<-3))

cout<< val;

When is the body of the if statement executed by the program?

a) always

b) only when val = -2

c) only when val = -4

d) never

e) None of the above

4. Assuming a = 8, b = 4, c = 3, d = 10, x = 2.5 and y = 3.0 (a,b,c are integer variables and x, y are floats), evaluate the following expressions and give the results.

a) b*(a%b) + (int)pow(a,2.0)

b) y + x / ((d*c)%1) + 164/(2*82) -100

c) 10/4+x;

5. Write a complete C++ program to calculate discount prices at a jewelry store. The discount price, which is to be given at the register, is computed as follows: If the purchase amount is less than \$99.99 then there will be 10% discount. If the purchase amount is between \$100 and \$999.99 then the discount rate will be 20% and if the purchase amount is \$1000 or above then the discount rate will be 40%.The program should take the purchase amount from the cashier and print out the discounted price.

6. Explain what the following program fragment does. What is the value of a after the while loop?

int a = -300, k = 30;

k--;

while(k>=21)

{

a++;

cout<<k<<endl;

k = k-1;

}

cout<<"The value of a after the loop is: "<<a<<endl;

if (k< 21)

cout<< "I cannot buy beer! \n";

else

cout<<" You should study C++ harder! \n";

7. a) How many times is the following loop is executed?

for(int i = 0;i <= 5;i++)

{

j = i*i;

}

b) Fill in the blank appropriately so that the following code prints out an array named boom of 10 elements in the reverse order.

for( ... ; ... ; ... )

cout<<boom[...];

c)Fill in the blanks so that the loop produces the following output

*

**

***

****

for(int i = 0; ..... ; i++)

{

for(int k = 0; .........; k++)

{

cout<< .......

}

.......

}

8. Let a be an integer array (which is initialized) of a fixed size N. A partial sum S_n (1<= n <=N) for this array is

S_n = a+a+...+a[n]

Write part of a C++ program (not the entire program) that computes all the partial sums S_n, 0<= n <=N-1, of this array and stores them in a different array.

9. Let

void mult(int n);

be the prototype of a function which prints out the 2's multiplication table on the screen which should look like as follows:

2*1=2

2*2=4

2*3=6

until 2*n= (the value of 2 times n)

Assuming n>=1, give the definitionof this function in 2 ways:

i) using a for loop

ii) using a do-while loop.

10. In the following 2-dimensional array (called x) we have some elements. How do we access them? Fill in the blanks with the right numbers. It will definitely help if you label the rows and columns before proceeding.

i) A is located at x[ ][ ]

ii) B is located at x[ ][ ]

iii) M is located at x[ ][ ]

iv) D is located at x[ ][ ]

v) C is located at x[ ][ ]

 A B M D C

11. Suppose you have class of 25 students and you give 9 quizzes throughout the semester. You want to store the quiz scores (which are integers) of all studenst in a 2D array.

i) Declare an array that can hold this data. Then fill it in by taking input from the user.

ii) Find the total quiz score of each student.

iii) Find the class average for each quiz.

12. Which of the following statment(s) is/are incorrect?

A) Functions need to be defined before the main function

B) All elements of an array must be of the same data type

C) All variables must be declared and initialized by taking input from the user before using them.

D) Functions always return a value to the calling statement.

E) A function definition can appear within another function's definition.

13. Determine the output of the following program.

#include <iostream>

using namespace std;

void func(int& x, int& y)

{

int t = x;

x = y;

y = t;

}

int main( )

{

int u = 3, v = 4;

func(u,v);

cout<<"u is "<< u<<" v is "<<v<<endl;

}

14. Given

string phrase,word1("Holy "), word2(" cow!");

provide the code to concatenate word1 and word2 and assign them to phrase.

15. Declare an array of 20 String objects named list. Write a code fragment to prompt for and accept 20 names, one per line, then to print the same names to the screen.

16. a) Write a definition for the function whose prototype is given below. This function determines whether a given positive integer is "perfect" or not. It returns true if it is, false otherwise.

bool IsPerfect(int n);

A positive integer is called perfect if the sum of all its divisors, other than itself, is equal to the number. For example, 6 is a perfect number because the divisors of 6 (other than itself) are 1,2, and 3 and 6 = 1+2+3.

b) Use the above function to find first 10 perfect numbers, and store them in an array.

17. What is the content of the array entry after executing the following for loop?

int entry = {2,5,3,1,1,3};

for(int i = 0; i<6; i++)

entry[entry[i]]=entry[i];

18. Write the definition of the following function.

void computeCoin(int coinValue, int& number, int& amountLeft);

//Precondition: 0 < coinValue < 100; 0 <= amountLeft <100

//PostCondition: number has been set equal to the maximum number of coins of denomination coinValue cents that can be obtained from amountLeftcents

//amountLeft has been decreased by the value of the coins, that is decreased by number*coinValue.

For example, suppose the value of the variable amountLeft is 86. Then after the call

computeCoins(25,number,amountLeft);

the value of number will be 3, and the value of amountLeft will be 11.

19. Write function which takes a single argument n of type int (positive integer) and it computes and returns the value of

1^2-2^2+3^2-4^2+....+/- n^2.

20. a) A pair of positive integers is called amicable if each is the sum of the proper divisors of the other. The smallest example is the pair 220 and 284, because the proper divisors of 220 are: 1,2,4,5,10,11,20,22,44,55 and 110, and their sum is 284. Similarly, the sum of proper divisors of 284 is 1+2+4+ 71+142 = 220. Write a function that takes two arguments of int and decides whether the pair is amicable. The functions should return True or False.

b) Write a code segment to find all pairs (a,b) of amicable numbers where a and b are between 1000 and 2000. How many pairs did you find? What are they?

21) Write the definition of a function that reverses the elements of an array. Write two overloaded versions of the functions to handle integer arrays and string arrays. Test your functions in a program.