Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

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

36

 

2: Debugging and Basic C++ Coding

ALL

39

 

3: Object Design and Implementation

ALL

25

 

Total marks

 

100

 

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

Answer ALL questions in this section

1.   A static function [1]

a)   Should be called when an object is destroyed

b)  Can be called using the class name and function

c)   Is closely connected with an individual object of a class

d)  Is used when a dummy object must be created

2.   How do we access the seventh element stored in an array? [1]

a)   array[6];

b)  array(7);

c)   array[7];

d)  D. array

3.   What will be the output of the following C++ code? [1]

#include <iostream>

#include <string>

using namespace std;

int main ()

{

string str ("sanfoundry.");

cout << str.substr(3).substr(4) << endl;

return 0;

}

a) foundry.

b) dry.

c) oundry.

d) found

4.   Which of the following functions is used to get the actual number of elements stored

in a vector named v? [1]

a)  v.size()

b)  v.capacity()

c)   v.max_size()

d)  v.no_of_elements()

5.   What operator does a pointer object of a class use to access its data members and

member functions? [1]

a)   ::

b)  .

c)   ->

d)  :

6.   In which type are enums stored by the compiler? [1]

a)   String

b)  Integer

c)   Float

d)  None of the above

7.   What will be the order of execution of base class constructors in the following

method of inheritance: class A: public B, public C {...}; [1]

a)   B(); C(); A();

b)  C(); B(); A();

c)   A(); B(); C();

d)  B(); A(); C();

8.   What is the output of the following C++ code? [1]

#include<iostream>

using namespace std;

int a = 55;

void fun()

{

int a = 20;

{

int a = 10;

cout << a << “  ”;

}

cout << a << endl;

}

int main()

{

fun();

return 0;

}

a)   10 20

b)   55 55

c)   10 10

d)  10 55

9.    Which of the following gives the [value] stored at the address pointed to by the

pointer named ptr? [1]

a)  Value(ptr)

b)  Ptr

c)  &ptr

d)  *ptr

10.  The feature that its specification is if same message is passed to objects of several     different classes and all of those can respond in a different way than it is called? [1]

a)   Inheritance

b)  Classes

c)   Polymorphism

d)  none of these

11. What should be output of below program if use enter a = 5? [1]

int main()

{

int a;

cin>>a; // user can enter any value

if (++a*5 <= 25)

cout<<"Hello";

else

cout<<"Bye";

}

a)   Hello

b)  Bye

c)   HelloBye

d)  Compilation Error

12.  In situations where we need to execute body of the loop before testing the

condition, which loop structure would be best [1]

a) For loop

b) while loop

c) do-while loop

d) nested for loop

13. Which of the following statements allows you to overload a function in C++? [1]

a)   Data type

b)  Number of arguments

c)   Data type and number of arguments

d)  Return type

14. What is the output of the following C++ code? [1]

#include <iostream>

using namespace std;

int main()

{

int  const  c = 9;

cout << ++c;

return 0;

}

a)   9

b)   10

c)   Error

d)  None of the above

15. Why are comments used? [1]

a)   Make the program run faster

b)  To help others read & understand the program

c)   Increase the size of the executable program

d)  Make a program compile easier

16.  When the inheritance is private, the private methods in base class are                       in

the derived class (in C++). [1]

[dropdown: inaccessible, accessible, protected, public]

17. Virtual functions are primarily used in                              . [1]

[dropdown: inheritance, operator overloading, encapsulation, data binding]

18. Wrapping data and its related functionality into a single entity is known as

 .   [1]

[dropdown: abstraction, encapsulation, polymorphism, modularity]

19. The                             has the same name as that of the class and is used to destroy the

objects of the class. [1]

[dropdown: constructor, destructor, member function, virtual function]

20. New operator allocates memory blocks from the                     . [1]

[dropdown: stack, array, vector, heap]

21. 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]

22.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]

23. How does a vector from the Standard Template Library compare with a standard

one-dimensional array? What similarities are there? What differences? [4 marks]

24. What do we mean by “dereferencing” a pointer? Provide an example of when we

may use apointer normally and dereferenced.                                                [4 marks]

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

Answer ALL questions in this section

25.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 constructorand getDetails() functions. [15 marks]

a.    The header file must include inclusion guards and the appropriate class syntax required by good programming practice.                                             [4 marks]

b.    The class file must include the correct class syntax and appropriate functionality as identified in the class description.                                                      [11 marks]

26. The following function attempts 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 that is not required?

27. Select one of the following scenarios and write an appropriate code to produce the

required result.                                          [9 marks]

(Please indicate which scenario you are responding to a or b)

a.  Write afunction 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.

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

b.   OR Write a program that will ask the user to enter two values,a minimum and a maximum value, and print three random numbers between them (including the min/max values).

28. What is the output of the following 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 25%

Answer ALL questions in this section

29. You are to design abasic 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 thengo 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:

a.   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.                                                                                 [8 marks]

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

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

ii.       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.

iii.       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.).

c.  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. [3 marks]