# BIT 2204 OBJECT ORIENTED PROGRAMMING KCA Past Paper UNIVERSITY EXAMINATIONS: 2013/2014
ORDINARY EXAMINATION FOR THE BACHELOR OF SCIENCE
IN INFORMATION TECHNOLOGY
BIT 2204 OBJECT ORIENTED PROGRAMMING
DATE: APRIL, 2014 TIME: 2 HOURS
INSTRUCTIONS: Answer Question ONE and any other TWO

QUESTION ONE
BIT 2204: Object Oriented Programming Question One
a.) Answer true or false for the following question
12 marks
b.) Describe the exact output that is produced by the following program segments. Use an
underscore (_) to indicate blank spaces.
double PI = 3.14159;
int NUM = 73;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout << “NUM=” << setw(6) << NUM<< “\nPI=” << PI << endl;
4 marks
c.) Terms and definitions
i) What are the three structures of structured programming? (6 marks)
ii) What is a post-test loop? (2 marks)
iii) What are the values a bool variable can have? (2 marks)
i. Using 10 separate integer variables to store 10 test scores in a program is just as easy
as
declaring an array of 10 integers.
ii. Assume we have an array of integers called theNumbers that we would like to pass to
a function called sumValues to calculate the sum of the values in the array. We also have a
global
constant called SIZE which is the size of the array. This function could be called from
within
main using:
sum = sumValues( theNumbers[ SIZE], SIZE);
iii. To pass a 2-dimensional array called matrixValues to a function, the sizes of both
dimensions
must be specified in the function declaration, such as:
void theFunction( int matrixValues[ 10][ 15])
iv. The following code is valid:
int theNumbers = {0,1,2,3};
for( int i=0; i<5; i++)
cout << theNumbers[ i];
v. The output from the following code is the numbers: 5 6 7 8
int x=5;
while( x<8); {
cout << x << ” “;
x++;
}
vi. Any of the three looping structures (do, while, for) can be implemented using one of
the other
looping structures, with the possible addition of a few lines of code.
iv) What is a variable? (2 marks)
v) True or false? Every C++ program must have a main() function (2 marks)
Question Two
a.) Assuming the value of variable count is 0 and the value of variable limit is 10,
state whether each the following expressions is true or false.
1. (count == 0) && (limit > 12)
2. (limit > 20) || (count < 5)
3. !(count == 12)
6 marks
b.) In algebra we see numeric intervals given as
2 < x < 3
This is not how we express this relationship in C++, however. Give the correct
C++ boolean expression that specifies that x lies between 2 and 3.
4 marks
c.) Write the output of each of the code segments below.
i) string s = “gandalf”;
s.insert(3,”SAM”);
cout << s << “\n”;
s.erase(2,4);
cout << s << “\n”;
s = s.substr(2,4);
cout << s;
ii) int a=4;
int j=1;
do {
cout << j << “+”;
j = j + 2;
if (j>a)
j–;
} while (j<8);
iii) cout << setprecision(2);
for (int i=3; i>0; i–) {
cout << i/3.0 << ” “;
}
iv) int a = {3, 4, -2, 6};
for (int i=3; i>0; i–)
a[i] += a[i-1];
for (int i=0; i<4; i++)
cout << a[i] << ” “;
10 marks
Question Three
a.) A palindrome is a word that is the same backwards and forwards. For example,
the strings below are all palindromes:
ODO dood racecar H*q*H 123321
Write a boolean function isPalindrome that returns true if a given string is a
palindrome and false if the string is not a palindrome.
10 marks
b.) Write the output of the program below. Track your variables in a walkthrough
table.
#include <iostream>
using namespace std;
int* fun(int* b, int* &c) {
b–;
*c = (*b)%2;
c = c + 2;
*c = *c + 2;
cout << “b=” << *b
<< ” c=” << *c << “\n”;
return b + (*b);
}
int main() {
int a = {3, 4, -1, 1, 3};
int* p = a;
int* q;
while (p != &a) {
q = fun(&a, p);
cout << “p=” << *p
<< ” q=” << *q << “\n”;
}
for (int i=0; i<5; i++)
cout << a[i] << ” “;
return 0;
}
10 marks
Question Four
a.) Suppose we have a pointer p. Briefly explain the difference between the two cout
statements below.
cout << p; cout << *p;
4 marks
b.) Write a single cout statement that prints the following text exactly as below,
including the quotation marks.
The file is in: “C:\My Documents”
4 marks
c.) A cool fact which did not come in lecture is that you can cast between chars and
ints. For example the char ‘A’ has int value 65, ‘B’ corresponds to 66, ‘C’
corresponds to 67, and so on in sequential order.
cout << (int) ‘A’; //Prints the number 65.
cout << (char) 68; //Prints the letter D.
Using this new information, write a simple for loop that prints the 26 letters A
through Z.
8 marks
d.) Correct the error(s) in each block of code below. You will get points off for
changing code that was not in error.
a.) cout << “Enter your first name: “;
string name;
cin << name;
if (name < “Frodo”)
cout << “Hello!”;
b.) int* p;
*p = 42;
int* q = p;
*q = 20;
4 marks
Question Five
The homework grades for students in BIT 2108 are stored in a
Homework class that keeps track of the student’s name and all their homework scores.
The class constructor takes the name of a student as input.
The add function adds a homework score to the student’s list of scores. You may
assume the homework scores are all integers.
The getName function should return the student’s name.
The getAverage function returns the student’s average. Because the professor is
not a total jerk, the lowest homework score is dropped when the homework
average is computed. You may assume the student has at least 2 homework scores
when the average function is called. (Dropping the lowest score when there is
only one score doesn’t really make sense.)
When two Homework objects are compared, they are compared based on their
homework averages.
Write the declaration and function definitions for the Homework class. Some sample
code using the Homework class is shown below. You will have to figure out what
functions to add based on their usage below.
20 marks
Question One
d.) Answer true or false for the following question
12 marks
e.) Describe the exact output that is produced by the following program segments. Use an
underscore (_) to indicate blank spaces.
double PI = 3.14159;
int NUM = 73;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout << “NUM=” << setw(6) << NUM<< “\nPI=” << PI << endl;
4 marks
f.) Terms and definitions
vi) What are the three structures of structured programming? (6 marks)
vii)What is a post-test loop? (2 marks)
viii) What are the values a bool variable can have? (2 marks)
i. Using 10 separate integer variables to store 10 test scores in a program is just as easy
as
declaring an array of 10 integers.
ii. Assume we have an array of integers called theNumbers that we would like to pass to
a function called sumValues to calculate the sum of the values in the array. We also have a
global
constant called SIZE which is the size of the array. This function could be called from
within
main using:
sum = sumValues( theNumbers[ SIZE], SIZE);
iii. To pass a 2-dimensional array called matrixValues to a function, the sizes of both
dimensions
must be specified in the function declaration, such as:
void theFunction( int matrixValues[ 10][ 15])
iv. The following code is valid:
int theNumbers = {0,1,2,3};
for( int i=0; i<5; i++)
cout << theNumbers[ i];
v. The output from the following code is the numbers: 5 6 7 8
int x=5;
while( x<8); {
cout << x << ” “;
x++;
}
vi. Any of the three looping structures (do, while, for) can be implemented using one of
the other
looping structures, with the possible addition of a few lines of code.
ix) What is a variable? (2 marks)
x) True or false? Every C++ program must have a main() function (2 marks)
Question Two
d.) Assuming the value of variable count is 0 and the value of variable limit is 10,
state whether each the following expressions is true or false.
1. (count == 0) && (limit > 12)
2. (limit > 20) || (count < 5)
3. !(count == 12)
6 marks
e.) In algebra we see numeric intervals given as
2 < x < 3
This is not how we express this relationship in C++, however. Give the correct
C++ boolean expression that specifies that x lies between 2 and 3.
4 marks
f.) Write the output of each of the code segments below.
v) string s = “gandalf”;
s.insert(3,”SAM”);
cout << s << “\n”;
s.erase(2,4);
cout << s << “\n”;
s = s.substr(2,4);
cout << s;
vi) int a=4;
int j=1;
do {
cout << j << “+”;
j = j + 2;
if (j>a)
j–;
} while (j<8);
vii) cout << setprecision(2);
for (int i=3; i>0; i–) {
cout << i/3.0 << ” “;
}
viii) int a = {3, 4, -2, 6};
for (int i=3; i>0; i–)
a[i] += a[i-1];
for (int i=0; i<4; i++)
cout << a[i] << ” “;
10 marks
Question Three
c.) A palindrome is a word that is the same backwards and forwards. For example,
the strings below are all palindromes:
ODO dood racecar H*q*H 123321
Write a boolean function isPalindrome that returns true if a given string is a
palindrome and false if the string is not a palindrome.
10 marks
d.) Write the output of the program below. Track your variables in a walkthrough
table.
#include <iostream>
using namespace std;
int* fun(int* b, int* &c) {
b–;
*c = (*b)%2;
c = c + 2;
*c = *c + 2;
cout << “b=” << *b
<< ” c=” << *c << “\n”;
return b + (*b);
}
int main() {
int a = {3, 4, -1, 1, 3};
int* p = a;
int* q;
while (p != &a) {
q = fun(&a, p);
cout << “p=” << *p
<< ” q=” << *q << “\n”;
}
for (int i=0; i<5; i++)
cout << a[i] << ” “;
return 0;
}
10 marks
Question Four
e.) Suppose we have a pointer p. Briefly explain the difference between the two cout
statements below.
cout << p; cout << *p;
4 marks
f.) Write a single cout statement that prints the following text exactly as below,
including the quotation marks.
The file is in: “C:\My Documents”
4 marks
g.) A cool fact which did not come in lecture is that you can cast between chars and
ints. For example the char ‘A’ has int value 65, ‘B’ corresponds to 66, ‘C’
corresponds to 67, and so on in sequential order.
cout << (int) ‘A’; //Prints the number 65.
cout << (char) 68; //Prints the letter D.
Using this new information, write a simple for loop that prints the 26 letters A
through Z.
8 marks
9
h.) Correct the error(s) in each block of code below. You will get points off for
changing code that was not in error.
a.) cout << “Enter your first name: “;
string name;
cin << name;
if (name < “Frodo”)
cout << “Hello!”;
b.) int* p;
*p = 42;
int* q = p;
*q = 20;
4 marks
Question Five
The homework grades for students in BIT 2108 are stored in a
Homework class that keeps track of the student’s name and all their homework scores.
The class constructor takes the name of a student as input.
The add function adds a homework score to the student’s list of scores. You may
assume the homework scores are all integers.
The getName function should return the student’s name.
The getAverage function returns the student’s average. Because the professor is
not a total jerk, the lowest homework score is dropped when the homework
average is computed. You may assume the student has at least 2 homework scores
when the average function is called. (Dropping the lowest score when there is
only one score doesn’t really make sense.)
When two Homework objects are compared, they are compared based on their
homework averages.
Write the declaration and function definitions for the Homework class. Some sample
code using the Homework class is shown below. You will have to figure out what
functions to add based on their usage below.
20 marks

(Visited 17 times, 1 visits today) 