C++ Mid Term

  • ISO/IEC 14882
  • IEEE 830-1998
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 Tcarteronw
T
Tcarteronw
Community Contributor
Quizzes Created: 38 | Total Attempts: 32,005
| Attempts: 470 | Questions: 47
Please wait...
Question 1 / 47
0 %
0/100
Score 0/100
1. The expression static_cast<int>(3) is called a

Explanation

The expression static_cast(3) is called a type cast because it is used to convert the value 3 from its original type (which is likely a floating-point or enumeration type) to the integer type. The static_cast keyword is used in C++ to perform explicit type conversions. By using static_cast(3), we are explicitly telling the compiler to treat the value 3 as an integer.

Submit
Please wait...
About This Quiz
C++ Mid Term - Quiz

The 'C++ Mid Term' quiz assesses understanding of C++ functions, including call-by-reference, void functions, and output interpretation. It tests practical coding skills and theoretical knowledge, essential for students and programmers aiming to master C++.

Tell us your name to personalize your report, certificate & get on the leaderboard!
2. What is the value of x after the following statements? int x, y, z; y = 10; z = 3; x = y * z + 3;

Explanation

The value of x is 33. This is because the expression y * z + 3 is evaluated first, which is equal to 30 + 3. Therefore, x is assigned the value of 33.

Submit
3. Call-by-reference should be used

Explanation

Call-by-reference should be used when the function needs to change the value of one or more arguments. This is because call-by-reference allows the function to directly access and modify the original variables, rather than creating copies of them. This is particularly useful when the function needs to make permanent changes to the variables that should be reflected outside of the function's scope.

Submit
4. What is the output of the following code? float value; value = 33.5; cout << value << endl;

Explanation

The code initializes a variable named "value" as a float with the value 33.5. Then, it prints the value of "value" followed by an end line character. Therefore, the output of the code will be 33.5.

Submit
5. If you need to write a function that will compute the cost of some candy, where each piece costs 25 cents, which would be an appropriate function declaration?

Explanation

The appropriate function declaration would be "int calculateCost(int count)" because it specifies the return type as an integer and takes an integer parameter "count". This matches the requirement of computing the cost of candy based on the number of pieces, where each piece costs 25 cents.

Submit
6. What is the value of x after the following code fragment executes? float x = 36.0; x = sqrt(x);

Explanation

The code fragment calculates the square root of the initial value of x, which is 36.0. The square root of 36.0 is 6.0. Therefore, the value of x after the code executes is 6.0.

Submit
7. An algorithm is

Explanation

The correct answer is "A finite set of steps to solve a problem." An algorithm refers to a step-by-step procedure or a finite set of instructions that are followed to solve a problem or perform a specific task. It is a systematic approach that outlines the necessary steps or operations needed to achieve a desired outcome. Algorithms can be implemented in various programming languages and are crucial in computer science and problem-solving.

Submit
8.
  1. Which boolean operation is described by the following table?
A B Operation
True True True
True False True
False True True
False False False

Explanation

The boolean operation described by the table is "or". This is because the result is true when either A or B (or both) are true. When both A and B are true, the result is true. When only one of A or B is true, the result is also true. However, when both A and B are false, the result is false.

Submit
9. What is the output of the following code fragement?   double size, volume=16.0; size = sqrt(sqrt(volume)) / 3; cout << fixed << showpoint <precision(2);

Explanation

The code fragment calculates the size of an object based on its volume. It uses the sqrt() function to find the square root of the volume, and then divides it by 3. The output statement uses the fixed, showpoint, and precision(2) manipulators to format the output with 2 decimal places. Therefore, the output will be 0.67.

Submit
10. What is the output of the following program fragment? cout << pow(4,2) << endl;

Explanation

The program fragment uses the pow() function to calculate the result of 4 raised to the power of 2, which is 16. The result is then printed to the console followed by an endline character.

Submit
11.
  1. Which boolean operation is described by the following table?
A B Operation
True True True
True False False
False True False
False False False
 

Explanation

The correct answer is "and". This is because the table shows that the output is only true when both inputs A and B are true. If either one or both of the inputs are false, the output is false. This behavior aligns with the logical "and" operation, where the result is true only if both conditions are true.

Submit
12. Which of the following is not a phase of the program-design process?

Explanation

The program-design process involves several phases, including problem-solving, implementation, and marketing the final program. Problem-solving refers to identifying and defining the problem that the program aims to solve. Implementation involves the actual development and coding of the program. Marketing the final program refers to promoting and distributing the program to the intended audience. However, marketing the final program is not a phase of the program-design process as it focuses on the post-development stage and is more related to the business and marketing aspects rather than the design and development of the program itself.

Submit
13. Information Hiding is analogous to using

Explanation

Information hiding is analogous to using a black-box methodology because both involve hiding the internal details of a system or process. In information hiding, the internal implementation details of a module or class are hidden, and only the necessary information is exposed to the outside world. Similarly, in a black-box methodology, the internal workings of a system or process are hidden, and only the inputs and outputs are visible. Both approaches prioritize encapsulation and abstraction, allowing for easier understanding, maintenance, and modification of the system or process.

Submit
14. Testing your program should be done

Explanation

Testing your program should be done as each function is developed because it allows for early detection and resolution of any issues or bugs that may arise during the development process. By testing each function individually, it ensures that they are working correctly before moving on to the next function. This approach helps in identifying and fixing any errors early on, leading to a more efficient and effective development process.

Submit
15. What is the value of choice after the following statements? void getChoice(int& par_choice, in par_count); int choice, count=3;   getChoice(choice, count);   void getChoice(int& par_choice, in par_count) {             if(par_count<0)                         par_choice =0;             if(par_count = 0)                         par_choice=-1;             else                         par_choice=99;             return; }

Explanation

The value of choice after the given statements is 99. This is because the function getChoice is called with the arguments choice and count. Inside the function, there are conditional statements. If par_count is less than 0, par_choice is assigned 0. If par_count is equal to 0, par_choice is assigned -1. Otherwise, par_choice is assigned 99. Since count is 3, which is not less than 0 or equal to 0, the else condition is executed and par_choice is assigned 99. Therefore, the value of choice after the statements is 99.

Submit
16. The fabs(double num) function

Explanation

The fabs(double num) function is used to calculate the absolute value of a given number. It returns the positive value of num, regardless of its original sign.

Submit
17. What is wrong with the following for loop?       for(int i=0;i<10;i--)       {                   cout << "Hello\n";       }

Explanation

The given for loop will result in an infinite loop because the condition `i--` will always evaluate to true. The loop starts with `i` initialized to 0, and then decrements `i` by 1 after each iteration. Since `i` is already less than 10, it will never reach the termination condition, resulting in an infinite loop.

Submit
18. Which of the following is not a valid identifier?

Explanation

The identifier "return" is not valid because it is a reserved keyword in many programming languages, including Python. Reserved keywords have a special meaning in the language and cannot be used as identifiers for variables, functions, or other elements of the code. Therefore, "return" cannot be used as a variable name in this context.

Submit
19. Which of the following data types may be used in a switch statement?

Explanation

In a switch statement, any of the given data types (int, char, enum, long) may be used. The switch statement allows for multiple cases to be evaluated based on the value of a variable, and it can handle different data types as long as they are compatible with the switch expression. Therefore, all of the given data types are correct options for a switch statement.

Submit
20. What is the value of x after the following statements? double x; x = 0; x += 3.0 * 4.0; x -= 2.0;

Explanation

The variable x is declared as a double and initialized to 0. Then, the value of x is updated by multiplying 3.0 and 4.0, which results in 12.0. Finally, 2.0 is subtracted from x, resulting in a final value of 10.0.

Submit
21. If a function needs to modify more than one variable, it must

Explanation

If a function needs to modify more than one variable, it must be a call by reference function. This is because call by reference allows the function to directly access and modify the original variables passed to it, rather than creating copies of the variables. This ensures that any modifications made within the function are reflected in the original variables outside of the function. Pass by value creates copies of the variables, so any modifications made within the function do not affect the original variables. A void function does not necessarily modify variables, and returning all values needed may not be practical or efficient in some cases.

Submit
22. Which of the following comments would be the best post-condition for this swap function void swap( int& left, int&right);

Explanation

The best post-condition for the swap function void swap( int& left, int& right) would be "//Postcondition: the values of left and right are exchanged." This post-condition accurately describes the expected outcome of the swap function, which is to exchange the values of the variables left and right.

Submit
23. What is the output of the following function call? //function body int factorial(int n) {       int product=0;       while(n > 0)       {                   product = product * n;                   n—;       }       return product; }   //function call cout << factorial(4);

Explanation

The output of the function call is 0. This is because the variable "product" is initially assigned a value of 0 and is never updated within the while loop. Therefore, the final value of "product" remains 0 and is returned as the output.

Submit
24. Which of the following is not an example of a program bug?

Explanation

An operator error is not considered a program bug because it is caused by a mistake made by the user, rather than a flaw in the program's code. Operator errors occur when the user enters incorrect or inappropriate input, or performs an action in an unintended manner. These errors are typically the result of human error or misunderstanding, rather than a problem with the program itself.

Submit
25. The set of instructions that a computer will follow is known as:

Explanation

A program refers to a set of instructions that a computer follows to perform a specific task or solve a problem. It is a sequence of commands that tells the computer what operations to execute and in what order. Programs are created by programmers using programming languages and are stored in the computer's memory or storage devices. The computer reads and executes these instructions to carry out the desired tasks. Therefore, the correct answer is Program.

Submit
26. What is the value of x after the following statements? int x; x = x + 30;

Explanation

The value of x is uninitialized before the statement x = x + 30. Therefore, when we try to add 30 to x, we are essentially adding 30 to an unknown value. This results in an undefined or garbage value for x.

Submit
27. When a void function is called, it is known as

Explanation

When a void function is called, it is considered as an executable statement because it performs a specific task or action within the program. Unlike functions that return a value, void functions do not produce any output or returned value. Instead, they are designed to execute a set of instructions or operations without returning any result. Therefore, the correct answer is "An executable statement."

Submit
28. What is the output of the following code fragments? int trial( int& a, int b) {             if(b > a)             {                         a=b;                         return –a;             }             else             {                         return 0;             } }   float x=0, y=10,z; z=trial(y,x); cout << z << " " << x <<" " << y << endl;

Explanation

The code defines a function called "trial" that takes two integer parameters, "a" and "b". Inside the function, it checks if "b" is greater than "a". If it is, it assigns the value of "b" to "a" and returns the negative value of "a". If "b" is not greater than "a", it simply returns 0.

In the main code, it declares three float variables, "x", "y", and "z". "x" is initialized to 0, "y" is initialized to 10. Then, it calls the "trial" function with "y" and "x" as arguments and assigns the return value to "z". Finally, it prints the values of "z", "x", and "y".

The output of the code will be "0 0 1 0" because the value of "z" will be 0, the value of "x" will remain 0, and the value of "y" will remain 10.

Submit
29. What is the value returned by the following function? int function() {       int value = 35;       return value + 5;       value += 10; }

Explanation

The function returns the value of "value" variable incremented by 5, which is 40. The line "value += 10;" is never executed because it comes after the return statement, so it does not affect the returned value.

Submit
30. When overloading a function, what must be true?

Explanation

When overloading a function, the names should be the same, but the number and/or types of parameters should be different. This allows the compiler to distinguish between different versions of the function based on the arguments passed to it.

Submit
31. What is the value of the following? sqrt(sqrt(pow(2,4)));

Explanation

The given expression evaluates the square root of the square root of 2 raised to the power of 4. First, 2 raised to the power of 4 is calculated, resulting in 16. Then, the square root of 16 is taken, resulting in 4. Finally, the square root of 4 is calculated, resulting in 2. Therefore, the value of the given expression is 2.

Submit
32. Which of the following is not part of the Software Life Cycle?

Explanation

Data Entry is not part of the Software Life Cycle because it refers to the process of inputting data into a computer system, which is not a specific phase or activity in the software development process. The Software Life Cycle typically includes phases such as Analysis, Design, Implementation, and Testing, which are focused on the development and testing of software applications. Data Entry, on the other hand, is a separate task that involves entering data into a system and does not directly contribute to the software development process.

Submit
33. Which of the following statements is NOT legal?

Explanation

The statement "char ch="cc";" is not legal because it is assigning a string value to a char variable. In Java, a char can only hold a single character, not a string of characters.

Submit
34. If the variable x has the original value of 3.4, what is the value in x after the following? cout << static_cast<int>(x);

Explanation

The code snippet is using the static_cast function to convert the variable x from a float to an integer. This function truncates the decimal part of the number and returns the integer value. Therefore, the value of x after the code is executed will be 3.

Submit
35. What is the output of the following program fragment? cout << static_cast<int>(3/4) << endl;

Explanation

The program fragment is using static_cast to convert the result of the division operation 3/4 to an integer. Since both 3 and 4 are integers, the division operation will result in an integer division. In integer division, the fractional part is truncated, so the result of 3/4 is 0. Therefore, the output of the program fragment will be 0.

Submit
36. What is the value of x after the following statements? int x; x = 15 %4;

Explanation

The value of x after the given statements is 3. The expression "15 % 4" calculates the remainder when 15 is divided by 4, which is 3. Therefore, the value of x is assigned as 3.

Submit
37. If you write a function that should use call-by-reference, but forget to include the ampersand,

Explanation

When a function is supposed to use call-by-reference, it means that the function should be able to modify the values of the variables passed to it. However, if the ampersand (&) is forgotten while passing the variables to the function, the function will not be able to access the original variables and modify their values. As a result, the program will still run, but the function will not give the correct information because it is not actually modifying the original variables.

Submit
38. When parameters are passed between the calling code and the called function, parameters and arguments are matched by:

Explanation

When parameters are passed between the calling code and the called function, they are matched by their relative positions in the parameter and argument lists. This means that the first parameter in the function's parameter list will be matched with the first argument passed in the function call, the second parameter will be matched with the second argument, and so on. The matching is based on the order in which the parameters and arguments are listed, ensuring that the correct values are assigned to the corresponding parameters in the function.

Submit
39. What is the value of x after the following statement? float x; x = 3.0 / 4.0 + 3  + 2 / 5

Explanation

The value of x is 3.75. In the given statement, the expression is evaluated from left to right. First, the division operation 3.0 / 4.0 is performed, resulting in 0.75. Then, the addition operation 0.75 + 3 is performed, resulting in 3.75. Finally, the division operation 2 / 5 is performed, resulting in 0.4, but since the variable x is of type float, the result is converted to a float before being assigned to x. Therefore, the final value of x is 3.75.

Submit
40. When a variable is local to a function, we say that it has ___ of the function

Explanation

When a variable is local to a function, it means that it can only be accessed and used within that specific function. The scope of a variable refers to the portion of the code where the variable is visible and can be used. In this case, the variable's scope is limited to the function in which it is defined, and it cannot be accessed from outside of that function.

Submit
41. In the following function, what is passed to the first parameter? void f1( int& value1, int value2);   int x,y; f1(x,y);

Explanation

In the given code, the function f1 is called with two arguments, x and y. The first parameter of the function is passed by reference, indicated by the use of the ampersand (&) symbol. This means that the function will receive the memory location of the variable x, rather than its value. Therefore, the correct answer is that the variable x (or its memory location) is passed to the first parameter of the function.

Submit
42. A simplified main program used to test functions is called

Explanation

A stub is a simplified main program used to test functions. It is typically used when a function or module is not yet fully implemented or when the main program is not yet available. A stub provides a basic framework for testing the functionality of individual functions before integrating them into the main program.

Submit
43. What is wrong with the following code? int f1( int x, int y) {             x = y * y;             return x;             int f2( float a, float& b) {             if(a < b)                         b = a;             else                         a=b;             return 0.0;        } }

Explanation

The code provided contains two function definitions, f1 and f2, which are nested within each other. However, in C++, function definitions cannot be nested. Each function should be defined separately and outside of any other function. Therefore, the correct answer is that function definitions may not be nested.

Submit
44. What is the value of i after the following function call? //function definition int doSomething(int value) {             value = 35;             return value;             value = 13 }   //fragment of main program int i=0; cout << doSomething(i);

Explanation

The value of i after the function call is 0 because the function doSomething assigns the value of 35 to the parameter value, but then it immediately returns that value without assigning it to any variable. Therefore, the original value of i, which is 0, remains unchanged.

Submit
45. Given the following code, what is the final value of i?       int i,j;       for(i=0;i<4;i++)       {                   for(j=0;j<3;j++)                   {                               if(i==2)                                           break;                   }       }

Explanation

The final value of i is 4 because the outer loop will iterate 4 times (i=0,1,2,3). However, when i=2, the inner loop will break, causing the outer loop to skip the remaining iterations and move on to the next value of i. Therefore, the inner loop will only run twice (j=0,1) for i=2, and the final value of i will be 4.

Submit
46. What is the output of the following function and function call? void calculateCost(int count, float& subTotal, float taxCost);   float tax = 0.0,   subtotal = 0.0;   calculateCost(15, subtotal,tax); cout << "The cost for 15 items is " << subtotal        << ", and the tax for " << subtotal << " is " << tax << endl; //end of fragment   void calculateCost(int count, float& subTotal, float taxCost) {       if ( count < 10)       {                   subTotal = count * 0.50;       }       else       {                   subTotal = count * 0.20;       }       taxCost = 0.1 * subTotal; }

Explanation

The function calculateCost takes three parameters: count, subTotal, and taxCost. It calculates the subtotal based on the count, where if the count is less than 10, the subtotal is count * 0.50, otherwise it is count * 0.20. After calculating the subtotal, it calculates the taxCost as 0.1 * subTotal. In the given function call, count is 15, so the else condition is executed and the subtotal is calculated as 15 * 0.20 = 3.00. The taxCost is then calculated as 0.1 * 3.00 = 0.30. Therefore, the output is "The cost for 15 items is 3.00, and the tax for 3.00 is 0.30."

Submit
47. Which of the following are equivalent to (!(x<15 && y>=3))?

Explanation

The given expression is a negation of the original expression. The original expression is "x=3". Negating this expression gives us "!(x=3)". By De Morgan's law, this can be simplified to "(x>=15 || y=15 || y

Submit
View My Results

Quiz Review Timeline (Updated): Feb 17, 2023 +

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

  • Current Version
  • Feb 17, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 11, 2011
    Quiz Created by
    Tcarteronw
Cancel
  • All
    All (47)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
The expression static_cast<int>(3) is called a
What is the value of x after the following statements? ...
Call-by-reference should be used
What is the output of the following code? ...
If you need to write a function that will compute the cost of some...
What is the value of x after the following code fragment executes? ...
An algorithm is
Which boolean operation is described by the following table?...
What is the output of the following code fragement? ...
What is the output of the following program fragment? ...
Which boolean operation is described by the following table?...
Which of the following is not a phase of the program-design process?
Information Hiding is analogous to using
Testing your program should be done
What is the value of choice after the following statements? ...
The fabs(double num) function
What is wrong with the following for loop? ...
Which of the following is not a valid identifier?
Which of the following data types may be used in a switch statement?
What is the value of x after the following statements? ...
If a function needs to modify more than one variable, it must
Which of the following comments would be the best post-condition for...
What is the output of the following function call? ...
Which of the following is not an example of a program bug?
The set of instructions that a computer will follow is known as:
What is the value of x after the following statements? ...
When a void function is called, it is known as
What is the output of the following code fragments? ...
What is the value returned by the following function? ...
When overloading a function, what must be true?
What is the value of the following? sqrt(sqrt(pow(2,4)));
Which of the following is not part of the Software Life Cycle?
Which of the following statements is NOT legal?
If the variable x has the original value of 3.4, what is the value in...
What is the output of the following program fragment? ...
What is the value of x after the following statements? ...
If you write a function that should use call-by-reference, but forget...
When parameters are passed between the calling code and the called...
What is the value of x after the following statement? ...
When a variable is local to a function, we say that it has ___ of the...
In the following function, what is passed to the first parameter? ...
A simplified main program used to test functions is called
What is wrong with the following code? ...
What is the value of i after the following function call? ...
Given the following code, what is the final value of i? ...
What is the output of the following function and function call? ...
Which of the following are equivalent to (!(x<15 &&...
Alert!

Advertisement