1.
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 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.
2.
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 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."
3.
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;
Correct Answer
D. 0 0 1 0
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.
4.
Which of the following comments would be the best post-condition for this swap function void swap( int& left, int&right);
Correct Answer
B. //Postcondition: the values of left and right are exchanged.
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.
5.
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, 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.
6.
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 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.
7.
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. 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.
8.
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 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.
9.
If you write a function that should use call-by-reference, but forget to include the ampersand,
Correct Answer
D. The program will run but probably not give you the correct information.
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.
10.
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 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.
11.
In the following function, what is passed to the first parameter?
void f1( int& value1, int value2);
int x,y;
f1(x,y);
Correct Answer
D. The variable x (or its memory location)
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.
12.
the fabs(double num) function
Correct Answer
D. Returns the absolute value of num
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.
13.
What is the output of the following program fragment?
cout << static_cast<int>(3/4) << endl;
Correct Answer
C. 0
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.
14.
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 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.
15.
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 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.
16.
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 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.
17.
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, 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.
18.
The expression static_cast<int>(3) is called a
Correct Answer
A. Type cast
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.
19.
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);
Correct Answer
C. 3
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.
20.
What is the value of the following?
sqrt(sqrt(pow(2,4)));
Correct Answer
B. 2
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.
21.
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);
Correct Answer
D. 0
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.
22.
What is the output of the following code fragement?
double size, volume=16.0;
size = sqrt(sqrt(volume)) / 3;
cout << fixed << showpoint <precision(2);
Correct Answer
A. 0.67
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.
23.
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 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.
24.
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 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.
25.
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 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
26.
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 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.
27.
Which of the following data types may be used in a switch statement?
Correct Answer
E. All are correct
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.
28.
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 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.
29.
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 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.
30.
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 4 raised to the power of 2, which is 16. The result is then printed to the console followed by an endline character.
31.
-
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 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.
32.
-
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 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.
33.
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. 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.
34.
What is the value of x after the following statements?
double x;
x = 0;
x += 3.0 * 4.0;
x -= 2.0;
Correct Answer
C. 10.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.
35.
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 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.
36.
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." 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.
37.
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 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.
38.
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 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.
39.
Which of the following is not an example of a program bug?
Correct Answer
B. Operator error
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.
40.
The set of instructions that a computer will follow is known as:
Correct Answer
C. Program
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.
41.
Which of the following is not a valid identifier?
Correct Answer
A. Return
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.
42.
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. 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.
43.
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 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.
44.
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" 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.
45.
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 Java, a char can only hold a single character, not a string of characters.
46.
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;
}
Correct Answer
D. 99
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.
47.
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 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."