Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.56 KB | None | 0 0
  1. skip to main content
  2. Quiz Submissions - PRACTICE Final Test
  3. TrongNghia Dao (username: trod)
  4. Attempt 1
  5. Written: Apr 9, 2019 6:12 PM - Apr 10, 2019 9:28 PM
  6. Submission View
  7. Your quiz has been submitted successfully.
  8.  
  9. Theory Questions
  10. For the questions in this section, you are not permitted to use a C++ compiler.
  11.  
  12. Question 1 1 / 1 point
  13. What is the output of the following C++ code?
  14.  
  15. const int NUM_ROWS = 3;
  16.  
  17. const int NUM_COLS = 2;
  18.  
  19.  
  20.  
  21. int stuff[NUM_ROWS][NUM_COLS] = { {2, 6}, {3, 1}, {5, 4} };
  22.  
  23. int row = 0, col = 0;
  24.  
  25.  
  26.  
  27. for (int i = 0; i < NUM_ROWS; i++)
  28.  
  29. {
  30.  
  31. for (int j = 0; j < NUM_COLS; j++)
  32.  
  33. {
  34.  
  35. if (stuff[i][j] > stuff[row][col])
  36.  
  37. {
  38.  
  39. row = i;
  40.  
  41. col = j;
  42.  
  43. }
  44.  
  45. }
  46.  
  47. }
  48.  
  49. cout << row << " " << col << endl;
  50.  
  51.  
  52. 1 0
  53.  
  54.  
  55. 1 2
  56.  
  57.  
  58. 2 1
  59.  
  60.  
  61. 0 1
  62.  
  63. Question 2 0 / 2 points
  64. Fill in the missing parts (marked as ???) of the XML comment for the following function.
  65.  
  66. /// <summary>Number of columns in the 2-D array of doubles</summary>
  67.  
  68. const int COL_SIZE = 3;
  69.  
  70.  
  71.  
  72. /// <summary>
  73.  
  74. /// ???
  75.  
  76. /// </summary>
  77.  
  78. /// <param name="numbers">2-D array of doubles</param>
  79.  
  80. /// <param name="row_size">Number of rows in the 2-D array of doubles</param>
  81.  
  82. /// <returns>???</returns>
  83.  
  84. int doSomething(double numbers[][COL_SIZE], int row_size)
  85.  
  86. {
  87.  
  88. int count = 0;
  89.  
  90.  
  91.  
  92. for (int i = 0; i < row_size; i++)
  93.  
  94. {
  95.  
  96. for (int j = 0; j < COL_SIZE; j++)
  97.  
  98. {
  99.  
  100. if ((numbers[i][j] - (int)numbers[i][j]) > 0)
  101.  
  102. {
  103.  
  104. count++;
  105.  
  106. numbers[i][j] = (int)numbers[i][j];
  107.  
  108. }
  109.  
  110. }
  111.  
  112. }
  113.  
  114.  
  115.  
  116. return count;
  117.  
  118. }
  119.  
  120. 1st ?:
  121. This question has not been graded.
  122. The correct answer is not displayed for Written Response type questions.
  123. View Feedback
  124. Question 3 0 / 2 points
  125. Fill in the missing parts (marked as ???) of the XML comment for the following function.
  126.  
  127. /// <summary>
  128.  
  129. /// ???
  130.  
  131. /// </summary>
  132.  
  133. /// <param name="numbers">Integer array</param>
  134.  
  135. /// <param name="size">Number of elements in the integer array</param>
  136.  
  137. void doSomething(int numbers[], int size)
  138.  
  139. {
  140.  
  141. int temp;
  142.  
  143.  
  144.  
  145. for (int pass = 0; pass < size - 1; pass++)
  146.  
  147. {
  148.  
  149. for (int i = 0; i < size - pass - 1; i++)
  150.  
  151. {
  152.  
  153. if (numbers[i] > numbers[i + 1])
  154.  
  155. {
  156.  
  157. temp = numbers[i];
  158.  
  159. numbers[i] = numbers[i + 1];
  160.  
  161. numbers[i + 1] = temp;
  162.  
  163. }
  164.  
  165. }
  166.  
  167. }
  168.  
  169. }
  170.  
  171. - No text entered -
  172. This question has not been graded.
  173. The correct answer is not displayed for Written Response type questions.
  174. View Feedback
  175. Question 4 4.5 / 5 points
  176. Which of the following statements are valid array declarations? Select all that apply.
  177.  
  178. Correct Answer
  179. int numbers[10] = { 1, 2, 3, 4 };
  180.  
  181. Correct Answer
  182. short* heights = new short[50];
  183.  
  184. Correct Answer
  185. char food[] = "bacon";
  186.  
  187.  
  188. int nums[] = (3, 4, 3, 2);
  189.  
  190.  
  191. bool cool[];
  192.  
  193. Correct Answer
  194. double points[] = { 2.3, 4.5, 6.6, 4.6, 5.9 };
  195.  
  196. Correct AnswerIncorrect Response
  197. string names[30];
  198.  
  199.  
  200. double[15] scores;
  201.  
  202.  
  203. char name[8] = "Aloysius";
  204.  
  205.  
  206. int* grades = new int[];
  207.  
  208. View Feedback
  209. Question 5 2.5 / 4 points
  210. Which of the following statements are true? Select all that apply.
  211.  
  212.  
  213. A destructor is a special member function that is called automatically whenever an object is created.
  214.  
  215.  
  216. Instance variables are class data members that are shared by all objects of the class.
  217.  
  218. Correct AnswerIncorrect Response
  219. A "magic number" is a literal constant with unexplained meaning.
  220.  
  221. Correct Answer
  222. Where possible, it is a good idea to eliminate duplicated code.
  223.  
  224. Correct AnswerIncorrect Response
  225. Data members that are shared by all objects of a class must be static.
  226.  
  227. Incorrect Response
  228. static member functions can access instance data members, as long as the instance data member values are not modified.
  229.  
  230. Correct Answer
  231. Class member functions that do not modify any data members should be const.
  232.  
  233.  
  234. Any attempt to modify the value of a const data member will result in a run-time error.
  235.  
  236. View Feedback
  237. Question 6 5 / 5 points
  238. Consider the following C++ code:
  239.  
  240. int nums[] = { 1, 2, 3, 4 };
  241.  
  242. int *iPtr;
  243.  
  244.  
  245.  
  246. iPtr = nums;
  247.  
  248. (*iPtr)++;
  249.  
  250.  
  251.  
  252. iPtr++;
  253.  
  254. (*iPtr) += 10;
  255.  
  256.  
  257.  
  258. iPtr += 2;
  259.  
  260. (*iPtr) *= 10;
  261.  
  262.  
  263.  
  264. After this code has executed, what would be the printed output of each of the following statements?
  265.  
  266. std::cout << nums[0];
  267.  
  268. ___2___(20 %)
  269. std::cout << nums[1];
  270.  
  271. ___12___(20 %)
  272. std::cout << nums[2];
  273.  
  274. ___3___(20 %)
  275. std::cout << nums[3];
  276.  
  277. ___40___(20 %)
  278. std::cout << *iPtr;
  279.  
  280. ___40___(20 %)
  281. View Feedback
  282. Question 7 1 / 1 point
  283. What is the output of the following C++ code?
  284.  
  285. int stuff[10] = {1, 2, 3, 4};
  286.  
  287.  
  288.  
  289. std::cout << stuff[9];
  290.  
  291.  
  292. 9
  293.  
  294.  
  295. 0
  296.  
  297.  
  298. Output is unpredictable
  299.  
  300.  
  301. 4
  302.  
  303. View Feedback
  304. Question 8 1 / 1 point
  305. Which of the following is a declaration for a function that returns nothing and accepts the following parameters:
  306.  
  307. a character passed by value
  308. an integer passed by pointer
  309. a float passed by reference
  310.  
  311. void func(char&, int, float*);
  312.  
  313.  
  314. void func(char, int*, float&);
  315.  
  316.  
  317. void func(char*, int&, float);
  318.  
  319.  
  320. void func(char, int*, float*);
  321.  
  322. Question 9 4 / 4 points
  323. Fill in the blanks to complete the output of the following C++ program:
  324.  
  325. #include <iostream>
  326.  
  327.  
  328.  
  329. int func(int x, int *y, int &z)
  330.  
  331. {
  332.  
  333. int* p = &x;
  334.  
  335.  
  336.  
  337. x += 1;
  338.  
  339. *p += 1;
  340.  
  341. *y += 2;
  342.  
  343. z += 3;
  344.  
  345.  
  346.  
  347. return (x + *p + *y + z);
  348.  
  349. }
  350.  
  351.  
  352.  
  353. int main(void)
  354.  
  355. {
  356.  
  357. int a = 1, b = 1, c = 1, d = 1;
  358.  
  359.  
  360.  
  361. d = func(a, &b, c);
  362.  
  363.  
  364.  
  365. std::cout << "The value of a is " << a << "\n";
  366.  
  367. std::cout << "The value of b is " << b << "\n";
  368.  
  369. std::cout << "The value of c is " << c << "\n";
  370.  
  371. std::cout << "The value of d is " << d << "\n";
  372.  
  373.  
  374.  
  375. system("pause");
  376.  
  377. return 0;
  378.  
  379. }
  380.  
  381.  
  382.  
  383. Program output:
  384.  
  385.  
  386. The value of a is ___1___(25 %)
  387.  
  388. The value of b is ___3___(25 %)
  389.  
  390. The value of c is ___4___(25 %)
  391.  
  392. The value of d is ___13___(25 %)
  393. Question 10 2.5 / 5 points
  394. Consider the following class definition and object declaration.
  395.  
  396. class CricketBowler
  397.  
  398. {
  399.  
  400. private:
  401.  
  402. static int numBowlers;
  403.  
  404. int wickets;
  405.  
  406. int runs;
  407.  
  408. public:
  409.  
  410. CricketBowler();
  411.  
  412. ~CricketBowler();
  413.  
  414. void addWicket(void);
  415.  
  416. void addRun(void);
  417.  
  418. float getAverage(void) const;
  419.  
  420. static int getNumBowlers(void);
  421.  
  422. private:
  423.  
  424. void setWickets(int wickets);
  425.  
  426. void setRuns(int runs);
  427.  
  428. };
  429.  
  430. CricketBowler ZaheerZhan;
  431.  
  432. Which of the following expressions and statements are valid? Select all that apply.
  433.  
  434.  
  435. ZaheerZhan.runs = 12;
  436.  
  437. Incorrect Response
  438. ZaheerZhan.addRun(5);
  439.  
  440. Incorrect Response
  441. CricketBowler::numBowlers
  442.  
  443. Correct Answer
  444. ZaheerZhan.getNumBowlers()
  445.  
  446.  
  447. CricketBowler AnilKumble(0, 0);
  448.  
  449. Correct AnswerIncorrect Response
  450. ZaheerZhan.getAverage()
  451.  
  452. Incorrect Response
  453. CricketBowler::addRun();
  454.  
  455. Correct Answer
  456. ZaheerZhan.addWicket();
  457.  
  458.  
  459. ZaheerZhan.setWickets(20);
  460.  
  461. Correct AnswerIncorrect Response
  462. CricketBowler::getNumBowlers()
  463.  
  464. View Feedback
  465. Practical Questions
  466. For the questions in this section, you are permitted to use a C++ compiler.
  467.  
  468. Question 11 0 / 30 points
  469. Implement a C++ class named Player that represents a player on a sports team.
  470.  
  471. The Player class must have the following data members:
  472.  
  473. A string named firstName that represents the player's first name.
  474. A string named lastName that represents the player's last name.
  475. An int named goals that represents the total number of goals the player has scored.
  476. An int named assists that represents the total number of assists the player has earned.
  477. An int named gamesPlayed that represents the total number of games the player has played.
  478. An int named numPlayers that represents the total number of Player objects that currently exist.
  479. The Player class must have the following member functions:
  480.  
  481. 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.
  482. 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.
  483. A constructor that accepts (in order) the player's first name, last name, goals, assists, and games played.
  484. Getter functions for all data members.
  485. Setter functions for all data members (except numPlayers).
  486. A function addGoal() that increments the player's goals by one.
  487. A function addAssist() that increments the player's assists by one.
  488. A function addGamePlayed() that increments the number of games the player has played by one.
  489. A function getPoints() that returns an int value representing how many points (goals + assists) the player has.
  490. A function getFullName() that returns the player's full name as a single string.
  491. A function getPointsPerGame() that returns a double value representing the average number of points the player earns each game.
  492. A function printStats() that prints the player's goals, assists, points, and points per game, formatted as shown in the program output below.
  493. Additional requirements:
  494.  
  495. Separate the definition and implementation of the Player class into a .cpp file and a .h file.
  496. Designate the members of the Player class as private or public as appropriate.
  497. Use the const keyword for the members of the Player class as appropriate.
  498. Use the static keyword for the members of the Player class as appropriate.
  499. 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.
  500. Include logic to prevent against invalid arithmetic operations, for example, dividing by zero.
  501. Use the following program to test your Player class.
  502.  
  503. #include <iostream>
  504.  
  505. #include <string>
  506.  
  507. #include "Player.h"
  508.  
  509.  
  510.  
  511. int main(void)
  512.  
  513. {
  514.  
  515. Player p1;
  516.  
  517.  
  518.  
  519. Player p2("John", "Tavares");
  520.  
  521. p2.setGoals(47);
  522.  
  523. p2.setAssists(41);
  524.  
  525. p2.setGamesPlayed(81);
  526.  
  527.  
  528.  
  529. Player p3("Mitchell", "Marner", 25, 66, 80);
  530.  
  531. p3.addGoal();
  532.  
  533. p3.addAssist();
  534.  
  535. p3.addGamePlayed();
  536.  
  537.  
  538.  
  539. Player p4("Astin", "Mathew", 37, 35, 67);
  540.  
  541. p4.setFirstName("Auston");
  542.  
  543. p4.setLastName("Matthews");
  544.  
  545.  
  546.  
  547. std::cout << "Number of players: " << Player::getNumPlayers() << "\n";
  548.  
  549. p1.printStats();
  550.  
  551. p2.printStats();
  552.  
  553. p3.printStats();
  554.  
  555. p4.printStats();
  556.  
  557.  
  558.  
  559. system("pause");
  560.  
  561. return 0;
  562.  
  563. }
  564.  
  565.  
  566.  
  567. Program Output:
  568.  
  569. Number of players: 4
  570.  
  571. Player Name: John Doe
  572. GP G A PTS PTS/G
  573. 0 0 0 0 0
  574.  
  575. Player Name: John Tavares
  576. GP G A PTS PTS/G
  577. 81 47 41 88 1.08642
  578.  
  579. Player Name: Mitchell Marner
  580. GP G A PTS PTS/G
  581. 81 26 67 93 1.14815
  582.  
  583. Player Name: Auston Matthews
  584. GP G A PTS PTS/G
  585. 67 37 35 72 1.07463
  586.  
  587. Copy/paste only the source code for your Player class below.
  588.  
  589. // Paste code from Player.h below this
  590.  
  591. class Player
  592. {
  593. public:
  594. Player(int value);
  595. ~Player();
  596. string firstName(void) const;
  597. string lastName(void) const;
  598. int goals(void) const;
  599. void setValue(int);
  600. int assists(void) const;
  601. int gamesPlayed(void) const;
  602. bool equals(int) const;
  603. bool equals(const MyInteger&) const;
  604. int numsPlayed(void) const;
  605. static bool isEven(int);
  606. static bool isOdd(int);
  607. private:
  608. int value;
  609. };
  610. // Paste code from Player.cpp below this
  611.  
  612.  
  613. This question has not been graded.
  614. The correct answer is not displayed for Written Response type questions.
  615. View Feedback
  616. Question 12 0 / 20 points
  617. Implement a C++ function named getPlayerNames() that:
  618.  
  619. Accepts the following four parameters (in order):
  620. A string named teamName that represents the name of the team
  621. An integer named numPlayers that represents the number of players on the team
  622. A string named longestName that represents the player's name that has the most characters
  623. A string named shortestName that represents the player's name that has the least characters
  624.  
  625. Returns a pointer to a 1-dimensional array of string objects that represent the player's names.
  626.  
  627. Implements the following functionality:
  628. 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
  629. Asks the user to enter the names of the player on the specified team, and puts the names into the array
  630. Determines which name in the array has the most characters and assigns the name to longestName
  631. Determines which name in the array has the least characters and assigns the name to shortestName
  632. 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.
  633. Additional requirements:
  634.  
  635. Pass parameters to the getPlayerNames() function by value and by reference as appropriate.
  636. Use the const and static keywords as appropriate.
  637. Document the getPlayerNames() function using XML comments.
  638. Use the following program to test your getPlayerNames() function.
  639.  
  640. #include <iostream>
  641.  
  642. #include <string>
  643. #include <limits>
  644.  
  645. using namespace std;
  646.  
  647.  
  648.  
  649. // *** Put your getPlayerNames() function here ***
  650.  
  651.  
  652.  
  653. int main(void)
  654.  
  655. {
  656.  
  657. string* playerNames, longName, shortName, teamName;
  658.  
  659. int numPlayers;
  660.  
  661.  
  662.  
  663. // Get team name from the user
  664.  
  665. cout << "Enter the team name: ";
  666.  
  667. getline(cin, teamName);
  668.  
  669.  
  670.  
  671. // Get number of players from the user
  672.  
  673. cout << "Enter number of players: ";
  674.  
  675. cin >> numPlayers;
  676.  
  677.  
  678.  
  679. // Flush the input buffer
  680.  
  681. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  682.  
  683.  
  684.  
  685. // Get player's names from the user
  686.  
  687. playerNames = getPlayerNames(teamName, numPlayers, longName, shortName);
  688.  
  689.  
  690.  
  691. // Output longest/shortest player names
  692.  
  693. std::cout << "Longest player name: " << longName << "\n";
  694.  
  695. std::cout << "Shortest player name: " << shortName << "\n";
  696.  
  697.  
  698.  
  699. delete[] playerNames;
  700.  
  701.  
  702.  
  703. system("pause");
  704.  
  705. return 0;
  706.  
  707. }
  708.  
  709.  
  710.  
  711. Example Program Output:
  712. Enter the team name: Peterborough Petes
  713. Enter number of players: 5
  714. Enter the names of 5 players on the Peterborough Petes (press Enter after each name):
  715. Ryan Merkley
  716. Zach Gallant
  717. John Parker-Jones
  718. Liam Kirk
  719. Semyon Der-Arguchintsev
  720. Longest player name: Semyon Der-Arguchintsev
  721. Shortest player name: Liam Kirk
  722.  
  723. // Copy/paste only the source code for your getPlayerNames() function here.
  724.  
  725. This question has not been graded.
  726. The correct answer is not displayed for Written Response type questions.
  727. View Feedback
  728. Attempt Score:
  729. 21.5 / 80 - 26.88 %
  730. Overall Grade (highest attempt):
  731. 21.5 / 80 - 26.88 %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement