Practice Problems

advertisement
CS215 – Introduction to C+ +
Section 805, Winter 01
Instructor: O. Hamadache
Final Review Exam
What is the value of n:
int a = 3, int n = 5;
n += a ;
………………………………………
n-- ;
………………………………………
n *= a + 2 ; ……………………………………
n %= 4 ;
………………………………………
n /=(a-1) ; ………………………………………
n
n
n
n
n
=
=
=
=
=
8
7
35
3
1
The following code has a logic error. What would be the output if it is not corrected.
int x=2;
if(x=2)
cout << “x is not” << 2;
else
cout << “x is ” << 2;
Answer:
What is the output produced by the following , when embedded in a correct program.
int x = 10;
do
{
cout << x << endl;
x = x – 3 ;
}while (x > 0);
Answer:
In the above code change the operator > to < in the while control test and give the output.
Answer:
Convert the last version of the above do-while loop into a while loop and print the output.
Answer:
Write a function called swap that takes to parameters of type int and interchanges the values of
the two arguments.
Answer:
Write a function that takes an integer argument and returns true if the integer is even and false if
odd.
bool even( int x)
{
if(x%2==0)
return true;
else
return false;
}
Write a function that takes two integers as arguments and returns their sum.
Answer:
Change the function above to return the sum through the first parameter.
Answer:
Write a function that takes a reference of an integer, and two integer value parameters. This
function computes the second parameter raised to the power of the third parameter. Do not call
the pow function. Use a loop to compute this value. Return the value in the first parameter.
Answer:
Write a piece of code that prompts a user for numbers. The user will enter -1 when finished. Use
a loop and compute the sum of the numbers entered.
Answer:
What is the output.
double compute (double a, double b )
{
double c;
if (a > b)
a = a – b;
else
{
a = a + b
b = 2 * b
}
c = 2 * a;
return c;
cout << a << “ ” << b << “ ” << c << endl;
}
int main()
{
double x = 9.2, y = 4.2;
z = compute (x, y);
cout << x << “ ” << y << “ ” << z << endl;
double f (double x)
{
return x / 2.5;
}
int g (int m)
{
return m % 5;
}
int main ( )
{
int n , n1 , n2 ;
double y, r1, r2 , d1 , d2 ;
n = 5 ;
y = n ;
r1 = f(n);
r2 = f(y);
cout << n << “” << y << “” << d1 << “” << d2 << endl;
cout << r1 << “” << r2 << “ \n ”;
cout << g(5);
Write a function which reverses a string.
#include <string.h>
void reverse( char s[ ] )
{
int i, j;
For ( i = 0, j = strlen(s)-1; i < j; i++, j--)
{
char temp = s[ i ];
s[ i ] = s[ j ];
s[ j ] = temp;
}
}
Write a function search that has three arguments. An array of integers, an integer (size of the
array), an integer (search value). This function must return a 1 if the search value was found in
the array and a 0 if the search value was not found in the array. This is the search routine we did
in class – we do not need to know where the value was found, just if the value was found or not.
Given the following function:
void loadArray(int a[], int size, int& n)
int next, i=0;
cout <<”Enter up to” << size << “nonnegative integers and –1 when finished.\n”
cin >> next;
while((next>=0) && (i < size))
{
a[i] = next ;
i++ ;
cin >> next ;
}
Convert the while loop to a for loop to output elements of a backward. 1 2 3 4 will print 4
3 2 1
for(i=n-1; i>=0; i--)
{
cout << a[i];
}
Multiple choice:
Any stream that is of type istream is also of type ifstream, so a formal parameter of type ifstream
can be replaced by argument of type istream.
(a)
true
(b) false
A constructor is a member function of the class so it is called using the dot operator.
(a) true
(b) false
A constructor must have the same name as the class of which is a member.
(a) true
(b)false
An object can be passed as formal parameters in a function but can not be returned by a function
except as reference.
(a) true
(b) false
A friend function can access private member variables even though it is a member function of the
class.
(a) true
(b) false
Class Definition:
Write a class Emp (for an employee) which has 5 data members: id (int), name (char array of size
50 ), hwage (double), hours (double), and gross (double). Member functions include:
-default constructor
-constructor which receives 4 arguments which correspond to the first 4 data members above. This
function also sets gross to 0.
-get and set methods for the first 4 data members
-get method and a method to compute the gross pay of an employee. Make sure to pay 1 1/2 for –
-overtime (all hours over 40 hours). The result should be set to gross.
-print method which prints an Emp object the terminal as:
ID: 214
Name: John Doe
Hourly Wage: $24.50
Hours Worked: 41.25
Gross Pay: $1025.94
Write a piece of code which appear in main that performs the following.
Create 2 Emp objects: "John Doe" from the example above using the constructor with arguments, and
another one using the default constructor. Use set methods to assign values in the second object to:
"Fred Who" whose id is 156, hourly wage is $25.15 and hours worked is 40.
Compute the gross pay for both objects. This will set the data member gross in each object.
Determine who has bigger gross pay, and report the result.
Print both objects.
class Emp
{
int id;
char name[50];
double hwage;
double hours;
double gross;
public:
Emp( );
// default constructor
Emp( int i, char na[ ], double hw, double hr ); // constructor w/ parameters
double getHWage( );
void setHWage( double hw );
int getId( );
void setId ( int i );
double getHour( );
void setHour( double hr );
//
void getName( char s[ ] );
void setName( char na[ ] );
double getGross( );
void setGross( ); // no parameter
void Print( );
};
Download