In: Computer Science
QUESTION 40
When you plan the test runs for a program, you should do all but one of the following. Which one is it?
a. |
list the invalid entries and unexpected user actions for each test run |
|
b. |
list the expected exceptions for each test run |
|
c. |
list the expected results for each test run |
|
d. |
list the valid entries for each test run |
1.5 points
QUESTION 41
Which of the following for loops could you use to iterate the data in a vector named days if you need to be able to work with iterators within the loop?
a. |
for (auto p: days) { ... } |
|
b. |
for (int i = 0; i < days.size(); ++i) { ... } |
|
c. |
for (auto iter = days.begin(); iter != days.end(); ++iter) { ... } |
|
d. |
all of the above |
1.5 points
QUESTION 42
Code Example 8-2
1. /* This application displays a student's score after a
5-point curve */
2.
3. void display_info(string fname, string lname, int score) {
4. cout << "Hello, "
<< fname << ' ' << Lname;
5. cout << "Your score on
this is " << score;
6. score = score + 5;
7. }
8.
9. int main() {
10. string first, last;
11. int score = 0;
12. cout << "first name: ";
13. cin >> first;
14. cout << "last name: ";
15. cin >> last;
16. cout << "score: ";
17. cin >> grade;
18. display_info(first, last,
score);
19. }
(Refer to Code Example 8-2.) What is the error in the main() function?
a. |
The cin statement on line 17 stores the input in an undefined variable named grade. |
|
b. |
The cin statement on line 17 does not define grade as an int. |
|
c. |
The function call on line 18 should send in fname and lname as arguments, not first and last. |
|
d. |
There are no errors in main(). |
1.5 points
QUESTION 43
Which of the following techniques could you use to define an array of strings named products with 5 elements?
a. |
const int size = 5; |
|
b. |
array<string, 5> products; |
|
c. |
int size; |
|
d. |
all of the above |
|
e. |
a and b only |
1.5 points
QUESTION 44
Which type of error lets the program run but produces the wrong results?
a. |
user |
|
b. |
runtime |
|
c. |
syntax |
|
d. |
logic |
1.5 points
QUESTION 45
How would you modify the following enumeration to change the
underlying enumerator type to char?
enum class Suit {
diamonds = 'd',
hearts = 'h',
clubs = 'c',
spades = 's'
};
a. |
enum class : char Suit { |
|
b. |
enum class Suit : char { |
|
c. |
enum class Suit char { |
|
d. |
enum class Suit { |
1.5 points
QUESTION 46
If you don’t specify the values of the enumerators in an enumeration, they are stored as
a. |
sequential int values starting at 1 |
|
b. |
sequential int values starting at 0 |
|
c. |
sequential char values starting at 'A' |
|
d. |
sequential char values starting at 'a' |
1.5 points
QUESTION 47
Given the following code, if the user worked 45 hours at
$10.00/hour, the output is as shown below.
1. int main() {
2. double hours, rate,
pay;
3. cout << "How many hours
did you work? ";
4. cin >> hours;
5. cout << "What is your
hourly rate? ";
6. cin >> rate;
7. if (hours > 40 &&
rate < 15)
8. pay =
(40 * rate) + (hours - 40 * rate * 1.5);
9. else
10. pay =
hours * rate;
11. cout << "Your pay is: $" <<
round(pay * 100) / 100;
12. }
Output:
Your pay is: $-105
Which line should be changed to fix this logic error?
a. |
change line 8 to: pay = (40 * rate) + ((hours - 40) * rate * 1.5); |
|
b. |
change line 7 to: if (hours > 40 || rate < 15) |
|
c. |
change line 2 so hours and rate are integer values instead of floating-point values |
|
d. |
change line 11 to: cout << "Your pay is: $" << ceil(pay * 100) / 100; |
1.5 points
QUESTION 48
Which of the following techniques could you use to initialize an array of ints named ages so its 4 elements contain the values 57, 33, 45, and 0?
a. |
array<int, 4> ages { 57, 33, 45, 0 }; |
|
b. |
array<int, 4> ages { 57, 33, 45 }; |
|
c. |
array<int, 4> ages; |
|
d. |
all of the above |
|
e. |
a and c only |
1.5 points
QUESTION 49
Which of the following is not an advantage of using a for loop and subscripting to iterate the data in a container?
a. |
You can easily skip elements by changing the loop criteria. |
|
b. |
It uses a counter variable that can be useful for tasks such as displaying a number for each element. |
|
c. |
It’s available to all containers. |
|
d. |
You can get the value of an element without dereferencing an iterator that points to it. |
Q 40) -> b.list the expected exceptions for each test run
Explaination :: We have to check all the exceptions that can be occured during execution which causes program to terminate abnormally.
Q 41)-->> c. for (auto iter = days.begin(); iter != days.end(); ++iter) { ... }
Explaination :: We use
vector<
int>::iterator ptr;
.
Iterators are used to point at the memory addresses of STL
containers. They are primarily used in sequence of numbers,
characters etc. They reduce the complexity and execution time of
program.
Q 42)-->> a. The cin statement on line 17 stores the input in an undefined variable named grade.
Explaination :: variable grade is not declared due to this we will get the error.
Q43)-->> d. all of the above
Q 44-->> d. logic
Explaination :: If we run program without any error but still getting wrong result then the logic of the program is wrong.
Q45) --> b. enum class Suit : char {
diamonds = 'd',
hearts = 'h',
clubs = 'c',
spades = 's'
Q46--> b. sequential int values starting at 0
Explaination :: By default it starts with index 0
Q47-->> a. change line 8 to: pay = (40 * rate) + ((hours - 40) * rate * 1.5);
Explaination :: Operator precedance
Q 47-->> e. a and c only
Q 48-->> c. It’s available to all containers.