Midterm Exam Practice Questions
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Midterm Exam Practice Questions
Important requirements for all questions:
• All data members must be declared as “private”
• Please make sure when it asks to “return” and it should return the values using return type or pass-by-reference parameters and not printing them out.
• Make sure that you clearly show how the C++ class and methods are being called and print out its return value and its results properly as expected
• There is no use of cin and cout in the class or the functions in questions unless it states explicitly to permit. cin and cout should be used in main() or testing functions
• No multiple return statements in one function or method are allowed.
• Please make sure to show how each question is being tested and produce correct values.
1. Define a new “SumOfTwo ” class that manages a simple addition expression: operand-1 (integer) and operand-2
such as 10 + 5 or 20 + 2
The class must provide only the following public methods (no more and no less):
o toString method must return all the SumOfTwo information as a string in the following format:
SUM( operand1, operand2 ) = <sum>
such as
SUM(10, 5) = 15
Or
SUM(20, 2) = 22
o isGreater method that compares with another SumOfTwo object and return true if the sum of the current SumOfTwo object is greater than the score of the other SumOfTwo object.
o hasOneOperandTheSame method that compares with another SumOfTwo object and returns true if they have at least one operand with the same value.
For example, SUM(10, 5) and SUM(5, 2) will return true.
o isTheSame method that compares with another SumOfTwo object and returns true if they both have the same sum
SUM(10, 5) and SUM(4, 11) will return true.
o createDouble method that creates a brand-new SumOfTwo
object with each operand having the double value of the current operands and return its pointer.
SUM(10, 5) will create and return the object for SUM(20, 10)
Requirements:
o Please do not provide any other public method including get the sum of the two operands. Private methods are ok.
o The class must not provide the default constructor. It must require the two operand values to initialize the object.
o You must not declare the sum as one of the data members as it can be calculated when needed.
Show how this SumOfTwo class being used to create objects and how these methods are being called and return proper values.
2. Define a function named “getLargestSum ” that accepts an array of
SumOfTwo objects and its size. It will return the following information
to the caller:
- The index of the array element where it has the largest sum (if there are multiple sums with the same value, it needs to return the index of the first one)
- How many elements in the array have the same sum as the largest. Show how this function is being called and return proper information.
Here are test data:
{ {10, 5} } ;
{ {10, 5}, {20, 2}, {30, 1} } ;
{ {10, 5}, {13, 2}, {4, 11} } ;
{ {30, 5}, {40, 2}, {10, 1} } ;
{ {10, 5}, {5, 10} } ;
{ {10, 5}, {20, 2}, {10, 1}, {10, 12}, {2, 20}, {4, 3} } ;
And its expected returned values:
Index: 2 Largest count: 1
Index: 0 Largest count: 3
Index: 1 Largest count: 1
Index: 0 Largest count: 2
Index: 1 Largest count: 3
3. Define a function named “searchSumInfo ” that accepts an array of
SumOfTwo object pointers, its size and another pointer to the search SumOfTwo object. It will go through the array and return two pieces of information
- how many elements in the array has the same sum value as the search object
- how many elements in the array have at least one same operand as in the search SumOfTwo object.
Show how this function is being called and returning proper values.
4. Write a function named "getQuestionMarkCount" that accepts an array of pointers to C-string (an array of characters terminated by a NULL character) and its size.
- How many C-strings in that array that contains exactly one question mark ('?') character in the entire array. For example, if the C-string is ['a', '?', 'b', '\0'], it will be counted.
- How many C-strings in the array that contains exactly two characters of ‘/’ followed by ‘?’
- How many C-strings in the array ends with a ‘?’
For this question, you are not allowed to use array notation, string class and its method or string function such as strlen or strstr. You must use pointer and pointer arithmetic.
Please show how this function is being called and tested.
Hint: you can start out with array notation and convert them into pointer notation
2024-02-24