OBJECT-ORIENTED DESIGN 273-422 Midterm Exam 2 hours
There are 4 questions. The percentage of marks is indicated for each question.
NAME: ______________________________ STUDENT #: __________________
 1. Write a C++ function 'print_pattern() which uses nested 'for' loops
to display patterns like the following:
1 2 3
1 2 3 4
1 2 3 4 5
The function should take one integer parameter which is the number
of lines to be printed (e.g. 5 to produce the above pattern).
Note: no main() function is required for this question.
 2. What would be printed by the following C++ program ?
int calc( int n )
static int sum = 0;
sum += n;
if ((n++ % 2) == 0)
return (sum + 1);
int main( void )
cout << calc( calc(5) ) ;
 3. Write a C++ function which, given an array of integers (range: 0-100) representing the marks of a class of programming students, calculates the average mark and two other pieces of information: the number of students who failed (mark less than 50), and the number of students who got A (mark 80 or more).
This function should be called 'marks', should be of return type 'void' and have 5 parameters:
The 2 input parameters to this function should be the array of marks and the number of elements in that array.
The 3 pointer output parameters to this function should be the average mark, the number who failed, and the number who got A.
(Do not use global or file-scope variables. Do not use reference parameters. This function should not print anything.)
Note: the average of a set of values is the sum of those values divided by the number of values.
Note: you are just asked to write this function, not a whole program, so no main() function is required.
 4. The following C++ program has at least 9 errors (syntactic, semantic, etc).
Indicate the errors by circling them (where appropriate) and giving a short explanation of what is wrong.
int main( void )
char buffer = "dictionary",
ptr; /* an abbreviation for pointer */
ptr = buffer;
while ( *ptr != '0' ) ;
num2 = strlen( ptr );
cout << divide( num1, num2 ) ;
return ( num2 );
static double divide( double a, b )
if ( b != 0.0 )
return ( a / b );