1.
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;
}
Correct Answer
D. The cost for 15 items is 3.00, and the tax for 3.00 is 0.00;
Explanation
The function calculateCost takes in three parameters: count, subTotal, and taxCost. Inside the function, it checks if the count is less than 10. If it is, it multiplies the count by 0.50 and assigns the result to subTotal. If the count is not less than 10, it multiplies the count by 0.20 and assigns the result to subTotal. Then, it calculates the taxCost by multiplying 0.1 with the subTotal.
In the given function call, calculateCost(15, subtotal, tax), the count is 15 which is not less than 10. Therefore, subTotal is calculated as 15 * 0.20 = 3.00. The taxCost is calculated as 0.1 * 3.00 = 0.30.
So, the output of the function call is "The cost for 15 items is 3.00, and the tax for 3.00 is 0.30."
2.
Which of the following is not an example of a program bug?
Correct Answer
B. Operator error
Explanation
An operator error refers to a mistake made by the user while interacting with a program, such as entering incorrect input or selecting the wrong option. It is not considered a program bug because it is not an issue with the program's code or functionality. Instead, it is a mistake made by the user that leads to unexpected or incorrect results. Program bugs, on the other hand, refer to errors or flaws in the program's code that cause it to behave incorrectly or produce incorrect results.
3.
The set of instructions that a computer will follow is known as:
Correct Answer
C. Program
Explanation
A program refers to the set of instructions that a computer will follow. This set of instructions can be written in a programming language and is responsible for telling the computer what tasks to perform and how to perform them. It includes a series of steps that the computer executes in order to complete a specific task or achieve a desired outcome. Therefore, program is the correct answer as it accurately describes the set of instructions that a computer will follow.
4.
Which of the following is not a valid identifier?
Correct Answer
A. Return
Explanation
The identifier "return" is not a valid identifier because it is a reserved keyword in many programming languages. Reserved keywords have special meanings in the language and cannot be used as identifiers for variables, functions, or other elements in the code.
5.
What is the value of x after the following statements?
int x, y, z;
y = 10;
z = 3;
x = y * z + 3;
Correct Answer
D. 33
Explanation
The value of x is 33 because the expression y * z + 3 evaluates to 33. The value of y is 10 and the value of z is 3, so when we multiply y and z we get 30, and then we add 3 to get the final result of 33.
6.
What is the value of x after the following statements?
int x;
x = x + 30;
Correct Answer
D. Garbage
Explanation
The value of x is not initialized before the statement x = x + 30. Therefore, when this statement is executed, the value of x is undefined or garbage.
7.
What is the output of the following code?
float value;
value = 33.5;
cout << value << endl;
Correct Answer
A. 33.5
Explanation
The code initializes a variable named "value" with the value 33.5. Then, it prints the value of "value" followed by a line break. Therefore, the output of the code will be "33.5".
8.
Which of the following statements is NOT legal?
Correct Answer
D. Char ch="cc";
Explanation
The statement "char ch="cc";" is not legal because it is assigning a string value to a char variable. In C++, a char variable can only store a single character, not a string of characters.
9.
An algorithm is
Correct Answer
C. A finite set of steps to solve a problem
Explanation
The correct answer is "A finite set of steps to solve a problem". This answer accurately describes an algorithm, which is a precise and unambiguous set of instructions or steps that can be followed to solve a specific problem or perform a specific task. An algorithm can be implemented in any programming language and is independent of the inputs and outputs of a program, the processing unit of a computer, or being a complete computer program.
10.
Which of the following is not a phase of the program-design process?
Correct Answer
C. Marketing the final program
Explanation
The program-design process consists of several phases, including problem-solving, implementation, and marketing the final program. However, marketing the final program is not considered a phase of the program-design process. This phase typically occurs after the program has been designed and developed, and it involves promoting and advertising the program to potential users or customers. It focuses on creating awareness and generating interest in the program, rather than the actual design and development of the program itself.
11.
Which of the following is not part of the Software Life Cycle?
Correct Answer
C. Data Entry
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 essential steps in developing and deploying software applications. Data Entry, on the other hand, is a separate task that involves entering data into a system, but it is not directly related to the overall software development process.
12.
What is the value of x after the following statements?
int x;
x = 15 %4;
Correct Answer
C. 3
Explanation
The value of x after the given statements is 4. The expression "15 % 4" calculates the remainder when 15 is divided by 4, which is 3. Therefore, x is assigned the value of 3.
13.
What is the value of x after the following statement?
float x;
x = 3.0 / 4.0 + 3 + 2 / 5
Correct Answer
D. 3.75
Explanation
The value of x is 3.75. This is because the expression on the right side of the assignment operator (=) is evaluated from left to right. First, the division 3.0 / 4.0 is performed, resulting in 0.75. Then, the addition 0.75 + 3 is calculated, giving 3.75. Finally, the division 2 / 5 is performed, resulting in 0.4, but since x is declared as a float, the decimal part is retained, giving the final value of 3.75.
14.
-
Which boolean operation is described by the following table?
A
B
Operation
True
True
True
True
False
True
False
True
True
False
False
False
Correct Answer
A. Or
Explanation
The correct answer is "or". This is because the operation returns "True" when at least one of the inputs is "True". In the given table, whenever either A or B is "True", the result is "True". Only when both A and B are "False", the result is "False".
15.
Which of the following data types may be used in a switch statement?
Correct Answer
E. All answers are correct
Explanation
In a switch statement, any of the given data types (int, char, enum, long) can be used. This is because a switch statement allows for multiple cases to be evaluated, and these data types can all be used as the expression to evaluate the cases. Therefore, all of the given answers are correct.
16.
What is wrong with the following for loop?
for(int i=0; i<10; i--)
{
cout << "Hello\n";
}
Correct Answer
C. Infinite loop
Explanation
The given for loop is an infinite loop because the condition for the loop to continue is i < 10, but inside the loop, the value of i is decremented by i--. This means that i will never be greater than or equal to 10, causing the loop to run indefinitely.
17.
What is the value of x after the following code fragment executes?
float x = 36.0;
x = sqrt(x);
Correct Answer
B. 6.0
Explanation
The code fragment starts by initializing the variable x with the value 36.0. Then, the sqrt() function is applied to x, which calculates the square root of x. The square root of 36.0 is 6.0. Therefore, the value of x after the code fragment executes is 6.0.
18.
What is the output of the following program fragment?
cout << pow(4,2) << endl;
Correct Answer
D. 16
Explanation
The program fragment uses the pow() function to calculate the result of raising 4 to the power of 2. The pow() function takes two arguments, the base (4 in this case) and the exponent (2 in this case), and returns the result of the exponentiation. The result, 16, is then printed to the console followed by a newline character.
19.
Which boolean operation is described by the following table?
A
B
Operation
True
True
True
True
False
False
False
True
False
False
False
False
Correct Answer
B. And
Explanation
The given table represents the boolean operation "and". In this operation, the result is true only when both inputs are true. In all other cases, the result is false.
20.
When overloading a function, what must be true?
Correct Answer
B. The names should be the same with different number and/or types of parameters.
Explanation
When overloading a function, the names of the functions should be the same, but the number and/or types of parameters should be different. This allows multiple functions with the same name to be defined, but with different parameter lists. This enables the programmer to perform different operations based on the arguments passed to the function.
21.
When parameters are passed between the calling code and the called function, parameters and arguments are matched by:
Correct Answer
B. Their relative positions in the parameter and argument lists
Explanation
When parameters are passed between the calling code and the called function, the matching of parameters and arguments is based on their relative positions in the parameter and argument lists. This means that the first parameter in the parameter list will be matched with the first argument in the argument list, the second parameter with the second argument, and so on. The order in which the parameters and arguments are listed is crucial for the correct matching to occur.
22.
The expression static_cast(3) is called a
Correct Answer
A. Type cast
Explanation
The expression static_cast(3) is called a type cast. Type casting is a way to convert one data type into another. In this case, the static_cast is used to explicitly convert the value 3 into a different type.
23.
What is the value of the following?
sqrt(pow(2,4));
Correct Answer
C. 4
Explanation
The given expression calculates the square root of the result of raising 2 to the power of 4. In other words, it calculates the square root of 16. The square root of 16 is 4.
24.
Information Hiding is analogous to using
Correct Answer
B. A black-box methodology
Explanation
Information hiding is analogous to using a black-box methodology because both involve the concept of encapsulating details and complexity. In information hiding, the internal workings of a module or object are hidden from the outside world, allowing for easier maintenance and modification. Similarly, in a black-box methodology, the inner workings of a system or process are not visible or accessible, and only the inputs and outputs are considered. Both approaches promote abstraction and modularization, making it easier to understand and work with complex systems.
25.
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);
Correct Answer
B. 0
Explanation
The output of the function call factorial(4) is 0. This is because the variable "product" is initialized to 0 and is never updated within the while loop. Therefore, when the loop ends, the value of "product" remains 0 and is returned as the output.
26.
Which of the following are equivalent to (!(x<15 && y>=3))?
Correct Answer
C. (x>=15 || y < 3)
Explanation
The given expression is a negation of (x=3). In other words, it is equivalent to the opposite of (x=3). The opposite of (x=3) is (x>=15 || y=15 || y
27.
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;
}
}
Correct Answer
B. 4
Explanation
The final value of i is 4. The outer loop iterates 4 times (i=0, i=1, i=2, i=3), and the inner loop iterates 3 times for each iteration of the outer loop. However, when i=2, the inner loop is terminated with the break statement. Therefore, the inner loop only runs twice when i=2. As a result, i reaches its final value of 4.
28.
What is the value returned by the following function?
int function()
{
int value = 35;
return value + 5;
value += 10;
}
Correct Answer
B. 40
Explanation
The function returns the value of "value" variable plus 5, which is 40. The code after the return statement is not executed, so the line "value += 10;" does not affect the returned value.
29.
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?
Correct Answer
D. Int calculateCost(int count);
Explanation
The appropriate function declaration for computing the cost of candy would be "int calculateCost(int count);" because it takes an integer parameter "count" which represents the number of candy pieces, and it returns an integer value representing the total cost in cents. The other function declarations are not suitable because they either have the wrong parameter type or missing parameter type.
30.
When a variable is local to a function, we say that it has ___ of the function
Correct Answer
C. Scope
Explanation
When a variable is local to a function, it means that it can only be accessed and used within that specific function. This concept is known as the scope of the variable. The scope defines the visibility and accessibility of the variable, ensuring that it does not interfere with other variables outside of the function. Therefore, the correct answer is "scope".
31.
Testing your program should be done
Correct Answer
A. As each function is developed
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 becomes easier to isolate and identify any errors, making the debugging process more efficient. Additionally, testing at this stage helps ensure that each function works correctly before moving on to the next one, ultimately leading to a more robust and reliable final program.
32.
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;
}
}
Correct Answer
B. Function definitions may not be nested
Explanation
The given code contains two function definitions, f1 and f2, which are nested within each other. However, in C++ (and most programming languages), function definitions cannot be nested. Each function should be defined separately and not within another function. This is why the correct answer is "Function definitions may not be nested."
33.
If a function needs to modify more than one variable, it must
Correct Answer
D. Be a call by reference function
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, rather than creating copies of them. By passing the variables by reference, any changes made within the function will be reflected in the original variables outside of the function. This is necessary when multiple variables need to be modified within a function.
34.
When a void function is called, it is known as
Correct Answer
C. An executable statement.
Explanation
When a void function is called, it is known as an executable statement because it performs a specific task or action within the program. Unlike functions that return a value, a void function does not provide any output or returned value. Instead, it executes a series of statements or commands to carry out a particular operation. Therefore, calling a void function is considered an executable statement within the program's flow of execution.
35.
Call-by-reference should be used
Correct Answer
B. When the function needs to change the value of one or more arguments
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 passed as arguments, rather than creating copies of them. By using call-by-reference, any changes made to the arguments inside the function will be reflected outside of the function as well. This is particularly useful when the function needs to update the values of variables in the calling code.
36.
A simplified main program used to test functions is called
Correct Answer
A. A stub
Explanation
A stub is a simplified main program used to test functions. It is a placeholder that simulates the behavior of a real program or function, allowing developers to test individual components before integrating them into the larger system. Stubs are often used when certain dependencies or components are not yet implemented or available, allowing developers to focus on testing specific functionality without the need for a complete and fully functional program.
37.
x = 10;
y = 15;
mystery(x, y);
void mystery (int& one, int two)
{
int temp;
temp = one;
one = two;
two = temp;
}
What are the values of x and y after the statements above execute? (Assume that variables are properly declared.)
Correct Answer
D. X = 15; y = 15
Explanation
The values of x and y after the statements above execute are x = 15 and y = 15. This is because the mystery function swaps the values of the two input parameters. So, the value of x becomes equal to the initial value of y (which is 15) and the value of y becomes equal to the initial value of x (which is also 15).
38.
A function that is associated with an object is called a _________ function.
Correct Answer
C. Member
Explanation
A function that is associated with an object is called a member function. This type of function is defined within a class and can access the object's data members and other member functions. It is used to perform specific operations or actions on the object.
39.
After a stream is opened, before it is used for input and output, it should be
Correct Answer
C. Checked to see if it opened correctly
Explanation
Before a stream is used for input and output, it should be checked to see if it opened correctly. This is important because if the stream did not open successfully, attempting to use it for input and output operations may result in errors or unexpected behavior. By checking if the stream opened correctly, any potential issues can be identified and handled appropriately before proceeding with further operations.
40.
If the user types in the characters 10, and your program reads them into an integer variable, what is the value stored into that integer?
Correct Answer
C. 10
Explanation
When the user types in the characters "10", the program reads them into an integer variable. Since the characters "10" represent the numeric value of ten, the value stored into that integer variable will be 10.
41.
What is the output of the following code?
char ch='G';
cout << tolower(ch) << endl;
Correct Answer
C. The integer value of 'g'
Explanation
The output of the code is the integer value of 'g'. The function tolower() is used to convert the character 'G' to its lowercase equivalent, which is 'g'. The lowercase 'g' is then printed to the console.
42.
A simplified version of a function which is used to test the main program is called
Correct Answer
A. A stub
Explanation
A simplified version of a function used to test the main program is called a stub. A stub is a placeholder or a temporary implementation of a function that allows the main program to be tested without fully implementing all the functionality. It provides a basic structure or interface for the function, allowing the program to compile and run, but the actual logic or implementation of the function is not fully developed. Stubs are commonly used in software development to simulate the behavior of complex functions or modules during testing.
43.
When a void function is called, it is known as
Correct Answer
C. An executable statement
Explanation
When a void function is called, it is known as an executable statement because it performs a specific task or action when it is executed. Unlike functions that return a value, void functions do not have a return type and therefore do not return any value. Instead, they are used to perform actions or operations without returning a result.
44.
The following code:
int *p;
declares p to be a(n) ____ variable.
Correct Answer
C. Pointer
Explanation
The given code declares a variable named "p" to be a pointer. A pointer is a variable that stores the memory address of another variable. In this case, "p" is declared as a pointer to an integer, as indicated by the use of the "*" symbol before the variable name.
45.
Given the following statements:
int x = 25;
int *p = &x;
*p = 46;
what is the value of x?
Correct Answer
D. 46
Explanation
The value of x is 46 because the variable p is a pointer that points to the memory address of x. By dereferencing the pointer using the * operator, the value at that memory address is changed to 46. Therefore, the value of x is also updated to 46.
46.
In C++, you declare a pointer variable by using the ____ symbol.
Correct Answer
A. *
Explanation
In C++, you declare a pointer variable by using the "*" symbol. The "*" symbol is used to indicate that a variable is a pointer and can store the memory address of another variable. This allows you to indirectly access and manipulate the value of the variable that the pointer is pointing to.
47.
Double sales [50];
int j;
Consider the declaration above. Which of the following correctly initializes the array sales?
Correct Answer
C. For(j = 0; j
Explanation
The correct answer is "for(j = 0; j < 50; j++)". This initializes the array sales with a loop that starts from 0 and goes up to 49 (50 times in total), incrementing the value of j by 1 each time. This ensures that each element of the array is assigned a value.
48.
X = 10;
y = 15;
mystery (x, y);
void mystery(int& one, int two)
{
int temp;
temp = one;
one = two;
two = temp;
}
What are the values of x and y after the statements above execute? (Assume that variables are properly declared.)
Correct Answer
D. X = 15; y = 15
Explanation
The values of x and y after the statements above execute are x = 15 and y = 15. This is because the function "mystery" swaps the values of the two input variables. In this case, the initial values of x and y are 10 and 15 respectively. After the function is called, the value of x becomes 15 (which was the initial value of y) and the value of y becomes 15 (which was the initial value of x).
49.
Cout << fixed << showpoint;
cout <<setprecision(2);
cout << x << " " << y << " " << z << endl;
Suppose that x = 25.67, y = 356.876, and z = 7623.9674. What is the output of the above statements?
Correct Answer
C. 25.67 356.88 7623.97
Explanation
The code snippet uses the fixed manipulator to set the floating-point output format to fixed-point notation. The showpoint manipulator ensures that the decimal point is always displayed, even if there are no decimal places. The setprecision(2) function sets the precision of the output to 2 decimal places. Therefore, when the values of x, y, and z are printed using cout, they will be displayed with 2 decimal places. The correct answer is "25.67 356.88 7623.97" because it matches the expected output format.
50.
C++ programs should contain comments to explain the code.
Correct Answer
A. True
Explanation
Including comments in C++ programs is considered good practice because it helps in understanding and explaining the code. Comments provide additional information about the purpose, functionality, and logic of the code, making it easier for other developers (or even the original developer) to read and maintain the code. They can also serve as documentation for future reference. Therefore, it is recommended to include comments in C++ programs to enhance code readability and maintainability.