FOUNDATIONS OF C++ - MCD4720

Sample Exam

Diploma of Information Technology


Examination time:    2 hours 10 minutes


Structure of Examination

  Section
Number of Questions
to be answered
Total Possible
Marks
Actual
Marks
  1: General Knowledge of C++ and
  Programming Principles
ALL
35

  2: Debugging and Basic C++ Coding
ALL
35

  3: Object Design and Implementation
ALL
30

  Total marks

100



Section 1: General Knowledge of C++ and Programming Principles 35%

Answer ALL questions in this section

1. If I want changes to be persistent with basic data type variables (like ints or floats), why would I pass references to those variables rather than pass them by value?

    Discuss using an example. [4 marks]



2. A function in C++ needs to be forward declared before we can use it, often in header files. What do we mean by this? [4 marks]



3. How does a vector from the Standard Template Library compare with a standard one-dimensional array? What similarities are there? What differences? [4 marks]



4. What do we mean by “dereferencing” a pointer? Provide an example of when we may use a pointer normally and dereferenced. [4 marks]



5. What are we referring to when we talk about the stack and the heap? Provide an example of declaring an int on the heap, and also an example of declaring one on the stack. [4 marks]



6. What happens if we try to read or write past the end of an array with our C++ code? Is the behaviour consistent? Explain why or why not, describing how arrays are represented in memory. [4 marks]



7. Explain any issues with the following code: [4 marks]

int foo() {

int const number1 = 5;

int* ptr = new int(-174);

number1++;

return number1;

}



8. Why in C++ do we need destructors? Provide a practical example of why having a destructor coded would be important. [4 marks]



9. What does the C++ virtual keyword do? [3 marks]



Section 2: Debugging and Basic C++ Coding 35%

Answer ALL questions in this section

10. A class called “Animal” exists that has three data members: a string called name, a char to represent its type (‘i’ for invertebrate, ‘f’ for fish, ‘a’ for amphibian, ‘r’ for reptile, ‘m’ for mammal, or ‘b’ for bird), and an int to represent a unique ID. The constructor of the class is passed a string and a char and uses them to set the two relating data members. The ID is set automatically using another static data member (not listed above). The class also has one member function: getDetails() which returns an appropriately formatted string containing the values of all data members. Write the code to declare the class called Animal as described above. Also write the constructor and getDetails() functions. [10 marks]


11. The following function is attempt to extract the digits from a string (the string is a collection of letters, digits, and special characters) and return them but it doesn’t quite work as expected. [10 marks]

      For example, if this string “12Sax2” past to the function it should return “122”

int digits(string str) {

int returnStr = 0;

int j=0;

for (int i = 0; i < str.length; 1++) {

if (str[i] >= '0' && str[i] <= '9') {

returnStr = str;

}

else

j++;

}

return str;

}


a. What are the mistakes in the function (highlight them)? Try to fix it



b. Is there any part of the code is not required? 13



Select one of the following scenarios and write an appropriate function to produce the required result. [10 marks]

You must also provide an example of the function call to the function you write, demonstrating the correct syntax used.

a. Write a function that will accept a sentence (as any length string) and a letter and the function returned will be the number of times the sent letter appears in the sent sentence.


b. OR Write a function that will receive a minimum and maximum value and returns a random number between them (including the min/max values).

(Please indicate which scenario you are responding to)



13. What is the output of the flowing code: [5 marks]

int main() {

vector<int>vectorOfInt{ 3,5,7,9,11 };

int *p = &vectorOfInt[0];

int number = 20;

1:      cout << *p << endl;

2:      cout << *(p++) << endl;

3:      cout << *(++p) << endl;

p = &number;

number++;

4:      cout << *p << endl;

p = new int(90);

5:      cout << *(p++) << endl;

system("pause");

return 0;

}


Answer:

1:

2:

3:

4:

5:



Section 3: Object Design and Implementation 30%

Answer ALL questions in this section

You are to design a basic dungeon exploration puzzle game like Zelda, Diablo, or any game where the player explores rooms, fights monsters, and finds keys & treasure. The game consists of a room that is populated by:

• The player (a moveable character, with a name, skill level, health, points tally, and inventory of found items)

• 5 Monsters (moveable characters, with a name, skill level, and health)

• 3 treasure chests (non-moveable, with a name and points value)

• 1 key to the exit door (non-moveable)

• 2 doors (one that has been entered through, the other locked which the player can exit)

The basic game mechanics are:

• The player will enter the room through one door. They must move about the room until they find the key. At this time, they can then go to the exit and leave the room.

• The room is dark so they cannot see where things are in the room.

• If they move onto a square that holds a monster they will fight. If they lose, the game is over. If they win, they earn 1 skill point.

• If they move on to a square that holds a piece of treasure, that gets added to their inventory.

• If they move on to the square with the key, it gets added to their inventory for use later.

The dungeon is represented by a 2-dimensional array, 20 x 20 in size. Each spot in the array can hold either a piece of the wall, a door, the player, a monster, treasure, a key, or nothing.

In the following questions describe your approach to designing a solution to this game problem. Clearly state any assumptions you make. You ARE NOT expected to code the game.

State any and all assumptions:

13.Given the scenario above, provide an overview of how you will structure an object-oriented design. Write a brief overview in plain English of your approach to designing a solution along with a diagram if appropriate. Describe how you might use inheritance and polymorphism. [10 marks]



14.Describe the classes and/or structs your C++ game will have. For each class:

a. Describe the purpose of the class in one or two lines. Also indicate if it is a base class or inherits from another class.

b. Create a UML diagram of the data members and functions each class may have. Indicate the data type you will use to represent each data member and the visibility of each.

c. Describe the member functions that each class would have. Indicate the visibility of each function and write one line that indicates what the job of the function is. Also indicate if the function has any special characteristics (for example, is static, virtual, a friend, etc.). [15 marks]



15.Describe the functionality your main function will have. Also list and describe any other functions your game will use that are not part of a class detailed above.