Some Practice Problems for the Mid-term Exam

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


#include <iostream.h>
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 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 the if-then statement is executed by the program,

a) val's value will always be printed out

b) val's value will be printed out when val = -2

c) val's value will not be printed out when val = -4

d) val's value is never printed out

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)

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;

}

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<4; 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[0]+a[1]+...+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. 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 statment.

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


12. 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;

}


13. Suppose the function strlen were not already implemented for you in the library. Write a function definition for strlen. Remember that strlen takes only one argument, const char source[ ], and returns an integer.

14. Complete the dialog below when this code is run. Assume that it has been embedded in complete and correct program.


char a[10];

cout<<"Enter a line of input."<<endl;

cin.getline(a,10);

cout<<a<<" END OF OUTPUT<<endl;


DIALOG:

Enter a line of input.

What is this program doing??? //(This is what user enters)


15. Given

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

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


16. What is the output of the following code segment (assuming it is embedded in a correct and complete program) when executed. Explain.

.....

char a[10] = "Hello";

char b[10] = "Hi there";

if ( strcmp(a,b) )

cout<<"The strings are the same";

else

cout<<"The strings are NOT the same";

17. 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.


18. Write a code fragment that will prompt for and accept input to fill the array called list (declared below) with 10 names typed in at the keyboard, assuming that the code you wrote is embedded in a correct and complete program.

char list[10][20];


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

bool IsPrime(int n);

Recall that a positive integer greater than 1 is prime if its only (positive) divisors are 1 and itself. 1 is not considered to be a prime.

b) Use the above function to print out first 100 prime numbers.


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

int entry[6] = {2,5,3,1,1,3};

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

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