This test includes 7 pages and 6 sections of questions for a total of 40 marks. You are
responsible for ensuring that your copy of the paper is complete.
... [Show More] Bring any discrepancy to
the attention of your invigilator. Include your information on the top of ALL pages.
Special instructions: You are allowed to bring one double-sided HANDWRITTEN reference
sheet on 8-1/2 x 11 inch paper, an English language dictionary, a calculator and a rubber duck.
No electronic references, electronic devices, textbooks or other assistance is allowed. Answer
sections 1 and 2 on these papers and sections 3 – 6 in the exam booklet. Hand in this paper along
with your reference sheet and any additional exam booklets at the end of the allocated time. Label
each page and booklet with your name and student number (including the reference sheet). If you
write the test in pencil, the test will not be remarked after it has been returned and the grade given
will be final. The value of each section is given at the beginning of the corresponding section.
OOP244 SUMMER 2016 Page 2 of 10
Name:_________________
Student Number:_________________
Section #1 – General C++ and Object Orientated Programming [4 Marks]
(a) [1 mark] Explain the purpose of the following preprocessor directives that are found in header files.
#ifndef HEADER_FILE_NAME_H
#define HEADER_FILE_NAME_H
// code
#endif
The purpose of the preprocessor directive is to prevent the header file from being included twice, thus
breaking the multiple definition rule.
(b) [1 mark] What are the 3 stages of the compilation process?
Preprocessing, Compiling, Linking
(c) [1 mark] What are the 3 core concepts in Object Orientated Programming?
Encapsulation, Inheritance, Polymorphism
(d) [1 marks] Explain why the output of the program is the following.
Program Output
#include
using namespace std;
int main() {
int x = 10 ;
for(int i = 0; i < 3; i++ ){
int x = i + 1;
cout << x << endl;
}
cout << x << endl;
} | 123
10The reason the output of the program is what it is, is because the x defined in the for loop shadows the x defined
outside of the for loop. This means that the program will use the most local copy when there are multiple
variables that share the same name.
OOP244 SUMMER 2016 Page 3 of 10
Name:_________________
Student Number:_________________
Section #2 – C++ Input/Output [6 marks]
(a) [3 marks] Fill in the code to get the required output.
Output
abcdefg
Z------
#include
#include < iostream >
using namespace std;
int main(){
cout << “abcdefg“ <<____endl______________;
cout <<___left______<<____setfill(‘-‘)____<<_____setw(7)___<< “Z”;
}(b) [3 marks] Add the code required to retrieve input data from the user and store them in the local
variables declared. Assume that the user will input this information on one line with a space between
the requested values. Also assume that the user will enter valid input.
#include
int main(){
char name[30];
int num;
std::cout << “Enter a student name and a student number\n”;
// add your code here
std::cin >> name >> num;
std::cout << “Student Name: ” << name << “ Student Number: “ << num;
} [Show Less]