Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- skip to main content
- Quiz Submissions - PRACTICE Final Test
- TrongNghia Dao (username: trod)
- Attempt 1
- Written: Apr 9, 2019 6:12 PM - Apr 10, 2019 9:28 PM
- Submission View
- Your quiz has been submitted successfully.
- Theory Questions
- For the questions in this section, you are not permitted to use a C++ compiler.
- Question 1 1 / 1 point
- What is the output of the following C++ code?
- const int NUM_ROWS = 3;
- const int NUM_COLS = 2;
- int stuff[NUM_ROWS][NUM_COLS] = { {2, 6}, {3, 1}, {5, 4} };
- int row = 0, col = 0;
- for (int i = 0; i < NUM_ROWS; i++)
- {
- for (int j = 0; j < NUM_COLS; j++)
- {
- if (stuff[i][j] > stuff[row][col])
- {
- row = i;
- col = j;
- }
- }
- }
- cout << row << " " << col << endl;
- 1 0
- 1 2
- 2 1
- 0 1
- Question 2 0 / 2 points
- Fill in the missing parts (marked as ???) of the XML comment for the following function.
- /// <summary>Number of columns in the 2-D array of doubles</summary>
- const int COL_SIZE = 3;
- /// <summary>
- /// ???
- /// </summary>
- /// <param name="numbers">2-D array of doubles</param>
- /// <param name="row_size">Number of rows in the 2-D array of doubles</param>
- /// <returns>???</returns>
- int doSomething(double numbers[][COL_SIZE], int row_size)
- {
- int count = 0;
- for (int i = 0; i < row_size; i++)
- {
- for (int j = 0; j < COL_SIZE; j++)
- {
- if ((numbers[i][j] - (int)numbers[i][j]) > 0)
- {
- count++;
- numbers[i][j] = (int)numbers[i][j];
- }
- }
- }
- return count;
- }
- 1st ?:
- This question has not been graded.
- The correct answer is not displayed for Written Response type questions.
- View Feedback
- Question 3 0 / 2 points
- Fill in the missing parts (marked as ???) of the XML comment for the following function.
- /// <summary>
- /// ???
- /// </summary>
- /// <param name="numbers">Integer array</param>
- /// <param name="size">Number of elements in the integer array</param>
- void doSomething(int numbers[], int size)
- {
- int temp;
- for (int pass = 0; pass < size - 1; pass++)
- {
- for (int i = 0; i < size - pass - 1; i++)
- {
- if (numbers[i] > numbers[i + 1])
- {
- temp = numbers[i];
- numbers[i] = numbers[i + 1];
- numbers[i + 1] = temp;
- }
- }
- }
- }
- - No text entered -
- This question has not been graded.
- The correct answer is not displayed for Written Response type questions.
- View Feedback
- Question 4 4.5 / 5 points
- Which of the following statements are valid array declarations? Select all that apply.
- Correct Answer
- int numbers[10] = { 1, 2, 3, 4 };
- Correct Answer
- short* heights = new short[50];
- Correct Answer
- char food[] = "bacon";
- int nums[] = (3, 4, 3, 2);
- bool cool[];
- Correct Answer
- double points[] = { 2.3, 4.5, 6.6, 4.6, 5.9 };
- Correct AnswerIncorrect Response
- string names[30];
- double[15] scores;
- char name[8] = "Aloysius";
- int* grades = new int[];
- View Feedback
- Question 5 2.5 / 4 points
- Which of the following statements are true? Select all that apply.
- A destructor is a special member function that is called automatically whenever an object is created.
- Instance variables are class data members that are shared by all objects of the class.
- Correct AnswerIncorrect Response
- A "magic number" is a literal constant with unexplained meaning.
- Correct Answer
- Where possible, it is a good idea to eliminate duplicated code.
- Correct AnswerIncorrect Response
- Data members that are shared by all objects of a class must be static.
- Incorrect Response
- static member functions can access instance data members, as long as the instance data member values are not modified.
- Correct Answer
- Class member functions that do not modify any data members should be const.
- Any attempt to modify the value of a const data member will result in a run-time error.
- View Feedback
- Question 6 5 / 5 points
- Consider the following C++ code:
- int nums[] = { 1, 2, 3, 4 };
- int *iPtr;
- iPtr = nums;
- (*iPtr)++;
- iPtr++;
- (*iPtr) += 10;
- iPtr += 2;
- (*iPtr) *= 10;
- After this code has executed, what would be the printed output of each of the following statements?
- std::cout << nums[0];
- ___2___(20 %)
- std::cout << nums[1];
- ___12___(20 %)
- std::cout << nums[2];
- ___3___(20 %)
- std::cout << nums[3];
- ___40___(20 %)
- std::cout << *iPtr;
- ___40___(20 %)
- View Feedback
- Question 7 1 / 1 point
- What is the output of the following C++ code?
- int stuff[10] = {1, 2, 3, 4};
- std::cout << stuff[9];
- 9
- 0
- Output is unpredictable
- 4
- View Feedback
- Question 8 1 / 1 point
- Which of the following is a declaration for a function that returns nothing and accepts the following parameters:
- a character passed by value
- an integer passed by pointer
- a float passed by reference
- void func(char&, int, float*);
- void func(char, int*, float&);
- void func(char*, int&, float);
- void func(char, int*, float*);
- Question 9 4 / 4 points
- Fill in the blanks to complete the output of the following C++ program:
- #include <iostream>
- int func(int x, int *y, int &z)
- {
- int* p = &x;
- x += 1;
- *p += 1;
- *y += 2;
- z += 3;
- return (x + *p + *y + z);
- }
- int main(void)
- {
- int a = 1, b = 1, c = 1, d = 1;
- d = func(a, &b, c);
- std::cout << "The value of a is " << a << "\n";
- std::cout << "The value of b is " << b << "\n";
- std::cout << "The value of c is " << c << "\n";
- std::cout << "The value of d is " << d << "\n";
- system("pause");
- return 0;
- }
- Program output:
- The value of a is ___1___(25 %)
- The value of b is ___3___(25 %)
- The value of c is ___4___(25 %)
- The value of d is ___13___(25 %)
- Question 10 2.5 / 5 points
- Consider the following class definition and object declaration.
- class CricketBowler
- {
- private:
- static int numBowlers;
- int wickets;
- int runs;
- public:
- CricketBowler();
- ~CricketBowler();
- void addWicket(void);
- void addRun(void);
- float getAverage(void) const;
- static int getNumBowlers(void);
- private:
- void setWickets(int wickets);
- void setRuns(int runs);
- };
- CricketBowler ZaheerZhan;
- Which of the following expressions and statements are valid? Select all that apply.
- ZaheerZhan.runs = 12;
- Incorrect Response
- ZaheerZhan.addRun(5);
- Incorrect Response
- CricketBowler::numBowlers
- Correct Answer
- ZaheerZhan.getNumBowlers()
- CricketBowler AnilKumble(0, 0);
- Correct AnswerIncorrect Response
- ZaheerZhan.getAverage()
- Incorrect Response
- CricketBowler::addRun();
- Correct Answer
- ZaheerZhan.addWicket();
- ZaheerZhan.setWickets(20);
- Correct AnswerIncorrect Response
- CricketBowler::getNumBowlers()
- View Feedback
- Practical Questions
- For the questions in this section, you are permitted to use a C++ compiler.
- Question 11 0 / 30 points
- Implement a C++ class named Player that represents a player on a sports team.
- The Player class must have the following data members:
- A string named firstName that represents the player's first name.
- A string named lastName that represents the player's last name.
- An int named goals that represents the total number of goals the player has scored.
- An int named assists that represents the total number of assists the player has earned.
- An int named gamesPlayed that represents the total number of games the player has played.
- An int named numPlayers that represents the total number of Player objects that currently exist.
- The Player class must have the following member functions:
- A no-arg constructor that sets the player's name to "John Doe", and sets the player's goals, assists, and games played all to zero.
- A constructor that accepts (in order) the player's first and last names as parameters, and sets the player's goals, assists, and games played all to zero.
- A constructor that accepts (in order) the player's first name, last name, goals, assists, and games played.
- Getter functions for all data members.
- Setter functions for all data members (except numPlayers).
- A function addGoal() that increments the player's goals by one.
- A function addAssist() that increments the player's assists by one.
- A function addGamePlayed() that increments the number of games the player has played by one.
- A function getPoints() that returns an int value representing how many points (goals + assists) the player has.
- A function getFullName() that returns the player's full name as a single string.
- A function getPointsPerGame() that returns a double value representing the average number of points the player earns each game.
- A function printStats() that prints the player's goals, assists, points, and points per game, formatted as shown in the program output below.
- Additional requirements:
- Separate the definition and implementation of the Player class into a .cpp file and a .h file.
- Designate the members of the Player class as private or public as appropriate.
- Use the const keyword for the members of the Player class as appropriate.
- Use the static keyword for the members of the Player class as appropriate.
- Include logic to prevent the class user from setting data members to invalid values, for example, do not allow goals to have a negative value.
- Include logic to prevent against invalid arithmetic operations, for example, dividing by zero.
- Use the following program to test your Player class.
- #include <iostream>
- #include <string>
- #include "Player.h"
- int main(void)
- {
- Player p1;
- Player p2("John", "Tavares");
- p2.setGoals(47);
- p2.setAssists(41);
- p2.setGamesPlayed(81);
- Player p3("Mitchell", "Marner", 25, 66, 80);
- p3.addGoal();
- p3.addAssist();
- p3.addGamePlayed();
- Player p4("Astin", "Mathew", 37, 35, 67);
- p4.setFirstName("Auston");
- p4.setLastName("Matthews");
- std::cout << "Number of players: " << Player::getNumPlayers() << "\n";
- p1.printStats();
- p2.printStats();
- p3.printStats();
- p4.printStats();
- system("pause");
- return 0;
- }
- Program Output:
- Number of players: 4
- Player Name: John Doe
- GP G A PTS PTS/G
- 0 0 0 0 0
- Player Name: John Tavares
- GP G A PTS PTS/G
- 81 47 41 88 1.08642
- Player Name: Mitchell Marner
- GP G A PTS PTS/G
- 81 26 67 93 1.14815
- Player Name: Auston Matthews
- GP G A PTS PTS/G
- 67 37 35 72 1.07463
- Copy/paste only the source code for your Player class below.
- // Paste code from Player.h below this
- class Player
- {
- public:
- Player(int value);
- ~Player();
- string firstName(void) const;
- string lastName(void) const;
- int goals(void) const;
- void setValue(int);
- int assists(void) const;
- int gamesPlayed(void) const;
- bool equals(int) const;
- bool equals(const MyInteger&) const;
- int numsPlayed(void) const;
- static bool isEven(int);
- static bool isOdd(int);
- private:
- int value;
- };
- // Paste code from Player.cpp below this
- This question has not been graded.
- The correct answer is not displayed for Written Response type questions.
- View Feedback
- Question 12 0 / 20 points
- Implement a C++ function named getPlayerNames() that:
- Accepts the following four parameters (in order):
- A string named teamName that represents the name of the team
- An integer named numPlayers that represents the number of players on the team
- A string named longestName that represents the player's name that has the most characters
- A string named shortestName that represents the player's name that has the least characters
- Returns a pointer to a 1-dimensional array of string objects that represent the player's names.
- Implements the following functionality:
- Creates a dynamic array of string objects that represent the player's names, sized to hold names for exactly the number of players specified by numPlayers
- Asks the user to enter the names of the player on the specified team, and puts the names into the array
- Determines which name in the array has the most characters and assigns the name to longestName
- Determines which name in the array has the least characters and assigns the name to shortestName
- Note: If there are multiple shortest/longest player names with the same number of characters, select any one of them as the shortest/longest name.
- Additional requirements:
- Pass parameters to the getPlayerNames() function by value and by reference as appropriate.
- Use the const and static keywords as appropriate.
- Document the getPlayerNames() function using XML comments.
- Use the following program to test your getPlayerNames() function.
- #include <iostream>
- #include <string>
- #include <limits>
- using namespace std;
- // *** Put your getPlayerNames() function here ***
- int main(void)
- {
- string* playerNames, longName, shortName, teamName;
- int numPlayers;
- // Get team name from the user
- cout << "Enter the team name: ";
- getline(cin, teamName);
- // Get number of players from the user
- cout << "Enter number of players: ";
- cin >> numPlayers;
- // Flush the input buffer
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- // Get player's names from the user
- playerNames = getPlayerNames(teamName, numPlayers, longName, shortName);
- // Output longest/shortest player names
- std::cout << "Longest player name: " << longName << "\n";
- std::cout << "Shortest player name: " << shortName << "\n";
- delete[] playerNames;
- system("pause");
- return 0;
- }
- Example Program Output:
- Enter the team name: Peterborough Petes
- Enter number of players: 5
- Enter the names of 5 players on the Peterborough Petes (press Enter after each name):
- Ryan Merkley
- Zach Gallant
- John Parker-Jones
- Liam Kirk
- Semyon Der-Arguchintsev
- Longest player name: Semyon Der-Arguchintsev
- Shortest player name: Liam Kirk
- // Copy/paste only the source code for your getPlayerNames() function here.
- This question has not been graded.
- The correct answer is not displayed for Written Response type questions.
- View Feedback
- Attempt Score:
- 21.5 / 80 - 26.88 %
- Overall Grade (highest attempt):
- 21.5 / 80 - 26.88 %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement