CPS 150 Final Exam Study Questions From Test #2

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Silentj38
S
Silentj38
Community Contributor
Quizzes Created: 2 | Total Attempts: 234
| Attempts: 90 | Questions: 50
Please wait...
Question 1 / 50
0 %
0/100
Score 0/100
1. 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

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.

Submit
Please wait...
About This Quiz
CPS 150 Final Exam Study Questions From Test #2 - Quiz

This quiz assesses understanding of C++ programming concepts such as loops, value parameters, and variable roles. It's designed to prepare students for the CPS 150 final exam, testing... see moretheir ability to analyze and predict code behavior. see less

2. 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

Explanation

The while loop condition should be "number

Submit
3. 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

Explanation

The variable "number" is being outputted on each line using the "cout" statement.

Submit
4. 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

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.

Submit
5. 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

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.

Submit
6. 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 ;

Explanation

The missing condition in the for statement should be "number

Submit
7. 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 ;

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.

Submit
8. 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 ;

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.

Submit
9. 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 <<______ ;

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.

Submit
10. 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

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.

Submit
11. 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

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

Submit
12. 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

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.

Submit
13. 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

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.

Submit
14. 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

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.

Submit
15. 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

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.

Submit
16. 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

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.

Submit
17. 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

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.

Submit
18. 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

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.

Submit
19. 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

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.

Submit
20. 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

Explanation

The blank should be filled with "score

Submit
21. 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

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.

Submit
22. 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

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.

Submit
23. 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

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).

Submit
24. 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

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.

Submit
25. A function (other than main) can call another function.

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.

Submit
26. A function must have a return statement.

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.

Submit
27. A program contains the line x = x + y ; inside a loop. What is the best description of variable x?

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.

Submit
28. A program must contain another function in addition to the one called main.

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.

Submit
29. The function prototype
     double mySqrt( int x ) ;

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.

Submit
30. What is the output from this program fragment?

for (K = 4 ; K >= 1 ; K--) {
   sum = 0 ;
   sum = sum + K ;
} // end for
cout << sum << endl ;

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.

Submit
31. The arguments must never have the same names as the parameters.

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.

Submit
32. A function that does not return a value to the calling statement has a special return type called?

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.

Submit
33. 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 ;

Explanation

The correct answer is only R. Fragment R is an infinite loop because the condition in the while loop is always true (z

Submit
34. What is the output from the following fragment?

z = 15 ;
while (z <= 20)
     z = z + 2 ;
cout << z << endl ;

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.

Submit
35. Which of the following statements about const value parameters is true?

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.

Submit
36. Using the following function definition layout :

A B( C ) {
     D
}

the parameter list is represented by?

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.

Submit
37. Which of the following is an advantage of using functions in writing 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.

Submit
38. 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?

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.

Submit
39. 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?

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.

Submit
40. 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?

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.

Submit
41. 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 main

Input:
39
41
44
36
12
5
54
10
0

What is the value displayed by the cout statement?

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.

Submit
42. 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 main

Input:
39
41
44
36
12
5
54
10
0

How many times is the variable m assigned a value within the loop?

Explanation

Within the given program, the variable m is assigned a value within the loop three times. This happens when the condition n

Submit
43. 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

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.

Submit
44. 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

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.

Submit
45. The function that appears first within the program is the first one to be activated.

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.

Submit
46. Which of the following program fragment(s) calculate(s) the sum
1 + 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

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

Submit
47. Which of the following statements about value parameters is true?

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.

Submit
48. 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 ;

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.

Submit
49. How many lines are printed by this program segment?

for (J = 1 ; J <= 5 ; J++) ;
for (K = 13 ; K >= 11 ; K--)
   cout << "Hello\n" ;

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.

Submit
50. 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 ;

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.

Submit
View My Results

Quiz Review Timeline (Updated): Jul 1, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jul 01, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 11, 2010
    Quiz Created by
    Silentj38
Cancel
  • All
    All (50)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Complete the program fragment using while statement that will display...
Complete the program fragment using while statement that will display...
Complete the program fragment using while statement that will display...
Complete the program fragment using while statement that will display...
Complete the program fragment using while statement that will display...
Complete the program fragment using a for statement that...
Complete the program fragment using a for statement that...
Complete the program fragment using a for statement that...
Complete the program fragment using a for statement that...
Complete the following partially written program which is supposed to...
Complete the following partially written program which is supposed to...
Complete the following partially written program which is supposed to...
Complete the following partially written program which is supposed to...
Complete the following partially written program which is supposed to...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
Complete the following program which is supposed to read the test...
A function (other than main) can call another function.
A function must have a return statement.
A program contains the line x = x + y ; inside a loop. What is the...
A program must contain another function in addition to the one called...
The function prototype     double mySqrt( int x )...
What is the output from this program fragment?for (K = 4 ; K >= 1 ;...
The arguments must never have the same names as the parameters.
A function that does not return a value to the calling statement has a...
Which of the following program fragments is an infinite loop?/*...
What is the output from the following fragment?z = 15 ;while (z <=...
Which of the following statements about const value parameters is...
Using the following function definition layout :A B( C )...
Which of the following is an advantage of using functions in writing...
A program contains the line x = y ;This is the first mention of...
A program contains the line int x ; The variable x does not appear...
You want to write a loop to read a large number of data values.The end...
Consider the program given below with the inputs in the order...
Consider the program given below with the inputs in the order...
What is the output from this program fragment?for (int j = 0 ; j <...
What is the value returned by the function sum for this call?sum...
The function that appears first within the program is the first one to...
Which of the following program fragment(s) calculate(s) the sum1 + 2 +...
Which of the following statements about value parameters is true?
How many lines are printed by the program segment?(*Note two values on...
How many lines are printed by this program segment?for (J = 1 ; J...
Complete the program fragment using a for statement that...
Alert!

Advertisement