1.
What is the output from the following fragment?z = 15 ;while (z <= 20) z = z + 2 ;cout << z << endl ;
Correct Answer
E. 21
Explanation
The code initializes the variable z to 15. The while loop condition checks if z is less than or equal to 20. Inside the loop, z is incremented by 2. The loop continues until z becomes greater than 20. After the loop, the value of z, which is 21, is printed followed by a new line. Therefore, the output from the given fragment is 21.
2.
Which of the following program fragments is an infinite loop?/* fragment P */z = 10 ;while (z <= 20) z = z + 1 ;/* fragment Q */z = 30 ;while (z <= 20) z = z - 1 ;/* fragment R */z = 10 ;while (z <= 20) ;z = z + 1 ;
Correct Answer
C. Only R
Explanation
The correct answer is only R. Fragment R is an infinite loop because the condition in the while loop is always true (z
3.
Which of the following statements about value parameters is true?
Correct Answer
A. The argument is never modified by execution of the called function
Explanation
The correct answer is that the argument is never modified by the execution of the called function. This means that when a function is called with a value parameter, the value of the argument remains unchanged throughout the execution of the function. The function can perform operations on the parameter, but it cannot modify the original argument that was passed to it.
4.
Which of the following statements about const value parameters is true?
Correct Answer
E. Both a and b
Explanation
Both a and b are true statements about const value parameters. The first statement, "The argument is never modified by execution of the called function," means that the value of the argument passed to the function remains unchanged throughout the function's execution. The second statement, "The parameter is never modified by execution of the called function," means that the parameter itself, which is a local variable within the function, is also not modified. Therefore, both the argument and the parameter remain constant and unmodified during the function's execution.
5.
A program contains the line x = x + y ; inside a loop. What is the best description of variable x?
Correct Answer
C. Accumulator
Explanation
In this program, the line "x = x + y;" suggests that the variable x is being used to accumulate or store the sum of its current value and the value of variable y. This indicates that x is acting as an accumulator, keeping track of the running total.
6.
A program contains the line x = y ;This is the first mention of variable x in the program. What is the best description of variable x?
Correct Answer
D. Undeclared variable
Explanation
The line x = y indicates that variable x is being assigned the value of variable y. However, since this is the first mention of variable x in the program, it implies that variable x has not been declared or defined before. Hence, the best description for variable x in this context is an undeclared variable.
7.
A program contains the line int x ; The variable x does not appear anywhere else in the program. What is the best description of variable x?
Correct Answer
E. Uninitialized variable
Explanation
The variable x is considered an uninitialized variable because it is declared but not assigned a value. It is not used or referenced anywhere else in the program.
8.
How many lines are printed by this program segment?for (J = 1 ; J <= 5 ; J++) ;for (K = 13 ; K >= 11 ; K--) cout << "Hello\n" ;
Correct Answer
D. 3
Explanation
The program segment consists of two for loops. The first for loop has an empty body and will execute 5 times. The second for loop has a cout statement that prints "Hello" and will execute 3 times. Therefore, the total number of lines printed by the program segment is 3.
9.
The function prototype double mySqrt( int x ) ;
Correct Answer
A. Declares a function called mySqrt which takes an integer as an argument and returns a double
Explanation
The given correct answer is "declares a function called mySqrt which takes an integer as an argument and returns a double". This is because the function prototype specifies the name of the function (mySqrt), the type of the argument (int x), and the return type (double). Therefore, it declares a function called mySqrt that takes an integer as an argument and returns a double.
10.
Using the following function definition layout :A B( C ) { D}the parameter list is represented by?
Correct Answer
C. C
Explanation
The parameter list is represented by C because in the given function definition layout, the function B is defined with a parameter C inside the parentheses. The parameter list is a way to pass values or variables to a function, and in this case, C represents the parameter that will be used within the function.
11.
Which of the following is an advantage of using functions in writing programs?
Correct Answer
E. We could use the same functions in different programs
Explanation
Using functions in writing programs allows for code reusability, which is an advantage. This means that the same functions can be used in different programs, saving time and effort in rewriting the same code. It also promotes modular programming, making the code more organized and easier to maintain. Additionally, using functions can improve code readability and understandability as they break down complex tasks into smaller, manageable parts.
12.
What is the output from this program fragment?for (K = 4 ; K >= 1 ; K--) { sum = 0 ; sum = sum + K ;} // end forcout << sum << endl ;
Correct Answer
A. 1
Explanation
The program fragment uses a for loop to iterate from 4 to 1. Inside the loop, the variable "sum" is set to 0 and then incremented by the value of "K" in each iteration. However, since the variable "sum" is declared inside the loop, it will be re-initialized to 0 in each iteration. Therefore, when the loop ends, the value of "sum" will be 1, which is the output of the program.
13.
A function that does not return a value to the calling statement has a special return type called?
Correct Answer
B. Void
Explanation
A function that does not return a value to the calling statement has a special return type called "void". The "void" return type indicates that the function does not have a return value and it is used when the function is intended to perform a task without returning any specific result.
14.
Consider the program given below with the inputs in the order they would be entered into the program.#include <iostream>using namespace std ;void main () { int m, n ; cin >> n ; m = n ; while (n > 0) { if (n < m) m = n ; cin >> n ; } // end while cout << m << endl ;} // end mainInput:3941443612554100What is the value displayed by the cout statement?
Correct Answer
D. 5
Explanation
The program takes input numbers and assigns the first input number to both variables m and n. Then, it enters a while loop where it checks if the current input number (n) is less than the value of m. If it is, it updates the value of m to be equal to n. This process continues until the input number becomes 0. Finally, the program displays the value of m, which is the smallest input number entered. In this case, the smallest input number is 5, so the value displayed by the cout statement is 5.
15.
Consider the program given below with the inputs in the order they would be entered into the program.#include <iostream>using namespace std ;void main () { int m, n ; cin >> n ; m = n ; while (n > 0) { if (n < m) m = n ; cin >> n ; } // end while cout << m << endl ;} // end mainInput:3941443612554100How many times is the variable m assigned a value within the loop?
Correct Answer
D. 3
Explanation
Within the given program, the variable m is assigned a value within the loop three times. This happens when the condition n < m is true, and in each iteration, the value of n is read from the input. The variable m is initially assigned the value of n before entering the loop, and then it is updated whenever a smaller value of n is encountered. Therefore, the loop assigns a value to m three times.
16.
What is the output from this program fragment?for (int j = 0 ; j < 3 ; j ++) { for (int k = j ; k < 3 ; k ++) cout << ' * ' ; for (int k = 0 ; k < j ; k ++) cout << ' ' ; cout << endl ;} // end for j
Correct Answer
B. ***
**
*
Explanation
The program uses nested for loops to print a pattern of asterisks and spaces. The outer loop iterates three times, representing the rows of the pattern. The first inner loop prints asterisks in a row, starting from the current row number and going up to the third row. The second inner loop prints spaces in a row, starting from 0 and going up to the current row number. After each row is printed, a new line is added. The pattern formed by the program is three rows of asterisks, followed by two rows of asterisks and spaces, and finally one row of asterisks.
17.
What is the value returned by the function sum for this call?sum (67.58, 50.94)The code for function sum is given below:int sum( int num1, int num2 ) { return( num1 + num2 ) ;} // end sum
Correct Answer
D. 117
Explanation
The function sum takes two integer arguments, num1 and num2, and returns their sum. In this case, the function is called with the arguments 67.58 and 50.94. However, since the function is defined to take integer arguments, the decimal parts of the arguments are truncated. Therefore, the function will add 67 and 50, resulting in the value 117.
18.
Which of the following program fragment(s) calculate(s) the sum1 + 2 + 3 + ...10 correctly?/* fragment P */sum = 10 ;x = 1 ;while (x < 11) { sum = sum + x ; x = x + 1 ;}/* fragment Q */sum = 10 ;x = 1 ;while (x <= 10) { sum = sum + x ; x = x + 1 ;}/* fragment R */sum = 10 ;for (x = 1 ; x < 10 ; x++) sum = sum + x
Correct Answer
C. Only R
Explanation
Fragment R is the only program fragment that correctly calculates the sum 1 + 2 + 3 + ...10. In fragment P, the condition in the while loop is x < 11, which means the loop will run 11 times instead of 10. In fragment Q, the condition in the while loop is x
19.
You want to write a loop to read a large number of data values.The end of input os to be signaled by a sentinel value.Which of the following is a true statement?
Correct Answer
C. Sentinel can be anything that does not appear in the actual input, but only at its end
Explanation
The correct answer is that the sentinel can be anything that does not appear in the actual input, but only at its end. This means that the sentinel value can be any value that is not part of the actual data being inputted, but is used to signal the end of input. It does not have to be a very large number, zero, negative, or appear multiple times within the input. As long as the sentinel value is unique and does not appear in the actual input, it can be used to indicate the end of input in the loop.
20.
How many lines are printed by the program segment?(*Note two values on one line count only as one line)for (J = 1 ; J <= 5 ; J ++) for (K = 13 ; K >= 11 ; K --) cout << J << ' ' << K << endl ;
Correct Answer
C. 15
Explanation
The program segment consists of two nested loops. The outer loop iterates from 1 to 5, while the inner loop iterates from 13 to 11. Each time the inner loop completes one iteration, it prints the values of J and K followed by a newline character. Since the inner loop iterates 3 times (from 13 to 11), and the outer loop iterates 5 times, the total number of lines printed by the program segment is 15.
21.
A function (other than main) can call another function.
Correct Answer
A. True
Explanation
A function (other than main) can call another function. This means that within the body of a function, we can include a call to another function to execute a specific set of instructions. This allows for modular programming, where different functions can be created to perform specific tasks and then called as needed within the program. By calling another function, we can reuse code and make our program more organized and efficient.
22.
The function that appears first within the program is the first one to be activated.
Correct Answer
B. False
Explanation
The statement "The function that appears first within the program is the first one to be activated" is false. In most programming languages, the order in which functions appear in the program does not determine the order of their activation. The execution of functions is typically determined by the flow of control within the program, such as function calls and conditional statements. Functions can be called from other functions, and their activation depends on when they are called during the program's execution.
23.
A program must contain another function in addition to the one called main.
Correct Answer
B. False
Explanation
This statement is false. A program does not necessarily need to contain another function in addition to the main function. The main function is the entry point of a program and it can be the only function in a program. Additional functions can be included in a program depending on the requirements and complexity of the program, but it is not a mandatory requirement.
24.
The arguments must never have the same names as the parameters.
Correct Answer
B. False
Explanation
The statement is false because arguments can have the same names as the parameters. In programming, arguments are the values that are passed into a function when it is called, while parameters are the variables that are declared in the function definition. The names of the arguments and parameters can be the same or different, depending on the programming language and the specific requirements of the function.
25.
A function must have a return statement.
Correct Answer
B. False
Explanation
A function does not necessarily have to have a return statement. Some functions are designed to perform a task or modify data without returning a value. These functions are called "void" functions and are commonly used for actions such as printing output or updating variables. In such cases, the function is not required to have a return statement.
26.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = _____ ;while (number <= 19) { cout << number << endl ; number = number + 3 ;} // end while
Correct Answer
1
Explanation
The given program fragment uses a while loop to display numbers from 1 to 19 in steps of 3. The variable "number" is initialized to 1, and the while loop continues as long as "number" is less than or equal to 19. Inside the loop, the current value of "number" is printed using the cout statement, and then "number" is incremented by 3. This process repeats until "number" exceeds 19, resulting in the numbers 1, 4, 7, ..., 19 being displayed on separate output lines.
27.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (___________) { cout << number << endl ; number = number + 3 ;} // end while
Correct Answer
number <= 19
Explanation
The while loop condition should be "number
28.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (number <= 19) { cout << ________ << endl ; number = number + 3 ;} // end while
Correct Answer
number
Explanation
The variable "number" is being outputted on each line using the "cout" statement.
29.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (number <= 19) { cout << number << _______ ; number = number + 3 ;} // end while
Correct Answer
endl
Explanation
The correct answer is "endl" because it is used to insert a new line character after each number is displayed, ensuring that each number is printed on a separate line.
30.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (number <= 19) { cout << number << endl ; _________________;} // end while
Correct Answer
number = number + 3
number = (number + 3)
Explanation
The correct answer is "number = number + 3" or "number = (number + 3)". These statements increment the value of the variable "number" by 3 in each iteration of the while loop. This ensures that the loop continues until the value of "number" reaches or exceeds 19.
31.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (____________; number <= 19 ; number = number + 3) cout << number << endl ;
Correct Answer
int number = 1
Explanation
The correct answer is "int number = 1" because it initializes the variable "number" with the value 1, which is the starting point for the sequence of numbers to be displayed.
32.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ;____________; number = number + 3) cout << number << endl ;
Correct Answer
number <= 19
Explanation
The missing condition in the for statement should be "number
33.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ; number <= 19 ; _______________) cout << number << endl ;
Correct Answer
number = number + 3
number = (number + 3)
Explanation
The correct answer is "number = number + 3" or "number = (number + 3)". These statements increment the value of the variable "number" by 3 in each iteration of the for loop. This ensures that the program will display the numbers 1 to 19 in steps of 3, as required by the question.
34.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ; number <= 19 ; number = number + 3) cout <<__________<< endl ;
Correct Answer
number
Explanation
The given correct answer is "number". In the for loop, the variable "number" is being incremented by 3 in each iteration, starting from 1 and ending at 19. So, when we print the value of "number" in each iteration, it will display the numbers 1, 4, 7, ..., 19.
35.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of 3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ; number <= 19 ; number = number + 3) cout << number <<______ ;
Correct Answer
endl
Explanation
The "endl" statement is used to insert a new line after each number is displayed. This ensures that each number is printed on a separate line, as required by the question.
36.
Complete the following partially written program which is supposed to read 10 integer values using a loop, and find the sum total of those numbers.void main () { int k, number ; int sum = 0 ; ______; while (k <= 10) { cout << "Enter a number: " ; cin >> number ; sum = sum + number ; k = k + 1 ; } // end while cout << "Result = " << sum << endl ;} // end main
Correct Answer
k = 1
Explanation
The correct answer is "k = 1" because the program is using a while loop to iterate 10 times and read 10 integer values. The variable "k" is used as a counter to keep track of the number of iterations. By setting "k = 1" initially, the loop will run 10 times as long as "k" is less than or equal to 10.
37.
Complete the following partially written program which is supposed to read 10 integer values using a loop, and find the sum total of those numbers.void main () { int k, number ; int sum = 0 ; k = 1 ; while (_____ <= 10) { cout << "Enter a number: " ; cin >> number ; sum = sum + number ; k = k + 1 ; } // end while cout << "Result = " << sum << endl ;} // end main
Correct Answer
k
Explanation
The variable "k" is used as a counter to keep track of the number of iterations in the while loop. It starts with a value of 1 and increases by 1 with each iteration. The condition in the while loop is "k
38.
Complete the following partially written program which is supposed to read 10 integer values using a loop, and find the sum total of those numbers.void main () { int k, number ; int sum = 0 ; k = 1 ; while (k <= 10) { cout << "Enter a number: " ; _____________; sum = sum + number ; k = k + 1 ; } // end while cout << "Result = " << sum << endl ;} // end main
Correct Answer
cin >> number
Explanation
The missing code in the program is "cin >> number". This line of code is used to read an integer value from the user and store it in the variable "number". By including this line inside the loop, the program will prompt the user to enter a number 10 times and each time the entered number will be added to the sum variable. Finally, the program will display the sum of all the entered numbers as the result.
39.
Complete the following partially written program which is supposed to read 10 integer values using a loop, and find the sum total of those numbers.void main () { int k, number ; int sum = 0 ; k = 1 ; while (k <= 10) { cout << "Enter a number: " ; cin >> number ; _________________; k = k + 1 ; } // end while cout << "Result = " << sum << endl ;} // end main
Correct Answer
sum = sum + number
sum = (sum + number)
sum = number + sum
sum = (number + sum)
Explanation
The correct answer is "sum = sum + number, sum = (sum + number), sum = number + sum, sum = (number + sum)". In order to find the sum total of the 10 integer values, the program needs to add each number to the current value of the sum variable. This can be done by using the expression "sum = sum + number" or "sum += number". Both expressions are equivalent and will update the value of the sum variable by adding the current number.
40.
Complete the following partially written program which is supposed to read 10 integer values using a loop, and find the sum total of those numbers.void main () { int k, number ; int sum = 0 ; k = 1 ; while (k <= 10) { cout << "Enter a number: " ; cin >> number ; sum = sum + number ; k = k + 1 ; } // end while cout << "Result = " <<_______<< endl ;} // end main
Correct Answer
sum
Explanation
The program reads 10 integer values using a loop and finds the sum total of those numbers. It initializes a variable "sum" to 0 and a variable "k" to 1. Then, it enters a while loop that executes as long as "k" is less than or equal to 10. Inside the loop, it prompts the user to enter a number, reads the number from the user, and adds it to the current value of "sum". After each iteration, it increments the value of "k" by 1. Finally, outside the loop, it prints the value of "sum" as the result.
41.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> __________; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && score <= 100) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
classSize
Explanation
The variable "classSize" is used to store the number of students in the class. It is initialized by taking input from the user using the "cin" statement. This value is then used in the while loop to iterate through each student's test score. The size of the class determines the number of times the loop will execute.
42.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; ________; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && score <= 100) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
k = 1
Explanation
The variable "k" is used as a counter to keep track of the number of test scores entered. It is initially assigned a value of 1 because the loop starts from 1. As the loop iterates, the value of "k" is incremented by 1 in each iteration until it reaches the value of "classSize". This ensures that the loop will run for the specified number of students in the class.
43.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (________<= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && score <= 100) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
k
Explanation
The variable "k" is used as a counter to keep track of the number of test scores entered by the user. It is initialized to 1 before the while loop starts, and it is incremented by 1 at the end of each iteration of the loop. This ensures that the loop continues until the number of iterations reaches the class size entered by the user. Therefore, "k" is the correct answer.
44.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; _______________; if (score >= 0 && score <= 100) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
cin >> score
Explanation
The missing code in the program is "cin >> score". This line of code is used to input the test score from the user. It allows the user to enter a test score, which is then stored in the variable "score". This input is necessary in order to calculate the class average.
45.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (__________ && score <= 100) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
score >= 0
Explanation
The blank in the if statement should be filled with "score >= 0" to ensure that the test score is within the range of 0 to 100. This condition checks if the score is greater than or equal to 0, which means it is a valid score. If the score is less than 0 or greater than 100, it will not be considered for the calculation of the class average.
46.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && __________) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
score <= 100
Explanation
The blank should be filled with "score
47.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && score <= 100) { ________________________; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
validCount = validCount + 1
validCount = (validCount + 1)
validCount++
validCount ++
Explanation
The correct answer is validCount++. This statement increments the value of validCount by 1, indicating that a valid test score has been entered. It is used to keep track of the number of valid scores entered in order to calculate the class average correctly.
48.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && score <= 100) { validCount = validCount + 1 ; __________________; } // end if k = k + 1 ; } // end while classAvg = sum / validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
sum = sum + score
sum = (sum + score)
sum = score + sum
sum = (score + sum)
Explanation
The correct answer is "sum = sum + score". This statement adds the value of the current score to the running total sum. It is used to calculate the sum of all valid scores entered by the user.
49.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && score <= 100) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = _________/ validCount ; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
sum
Explanation
The variable "sum" is used to accumulate the total sum of valid test scores entered by the user. It is initialized before the while loop and updated inside the if statement whenever a valid score is entered. Finally, the class average is calculated by dividing the sum by the number of valid scores (validCount).
50.
Complete the following program which is supposed to read the test scores of students in a class and compute the class average. The individual test scores should be within the range 0 to 100. If a test score is entered that is outside of the range, then it should be ignored and not used for the calculation of the class average so that the class average is based on valid scores only.int main () { int k, score, validCount = 0, classSize ; cout << "Enter number of students in class: " ; cin >> classSize ; k = 1 ; while (k <= classSize) { cout << "Enter a test score: " ; cin >> score ; if (score >= 0 && score <= 100) { validCount = validCount + 1 ; sum = sum + score ; } // end if k = k + 1 ; } // end while classAvg = sum / ___________; // S1 cout << "The class average is " << classAvg << endl ; return 0;} // end main
Correct Answer
validCount
Explanation
The variable "validCount" keeps track of the number of valid test scores entered by the user. It is incremented by 1 each time a valid score is entered. This variable is used to calculate the class average by dividing the sum of valid scores by the number of valid scores. Therefore, "validCount" is the correct answer as it represents the total number of valid scores entered by the user.