C++ MCQs with explanations

FUNCTIONS IN C++

1. Which one of the following statements is a valid main function prototype?

Explanation ≡ Correct answer: option d) int main(int argc, char* argv[ ] );
The main function in C++ should have one of the two valid prototypes: - int main(int argc, char* argv[]); - int main() The "argc" parameter represents the number of command-line arguments, and "argv" is an array of pointers to the command-line arguments. The correct main function prototype is "int main(int argc, char* argv[])". The other options are invalid.

2. Which one of the following is true about a function in a C++ program?

Explanation ≡ Correct answer: option b) A function may be called as and when required.
Functions in a C++ program can be called at any point in the program when their invocation is required. They can be called from the main function or from other functions, as needed. They are not required to be called at least once, and they may or may not return a value, depending on their return type.

3. Which of the following function prototypes is invalid?

Explanation ≡ Correct answer: option b) void exchange (int x, y);
In option b), the function prototype "void exchange (int x, y);" is invalid because it is missing the data type for the "y" parameter. All function parameters must have their data types explicitly specified. The correct prototype should be "void exchange (int x, int y);".

4. Find the error in the following function prototype double area (int)

Explanation ≡ Correct answer: option a) Argument name is not specified.
In the function prototype "double area (int)", the error is that the name of the argument is not specified. The correct prototype should specify the name of the argument along with its data type, such as "double area(int side)".

5. In C++, the main function returns to the operating system

Explanation ≡ Correct answer: option a) an integer value.
In C++, the main function returns an integer value to the operating system. The return value of the main function indicates the exit status of the program. By convention, a return value of 0 indicates a successful execution, and a non-zero value indicates an error or abnormal termination.

6. The 'call by reference' approach of invoking a function enables us to

Explanation ≡ Correct answer: option b) not alter the values of the original variables in the calling program.
The 'call by reference' approach allows us to pass the memory address of the variables as arguments to a function, rather than passing the values themselves. This way, the function can directly access and modify the original variables in the calling program without creating copies. As a result, the values of the original variables remain unchanged in the calling program.

7. Following is a list of function declarations with default values. Which one of them is a valid declaration?

Explanation ≡ Correct answer: option b) float mul(int a, float b=10.0, int c);
In option b), the function declaration "float mul(int a, float b=10.0, int c);" is valid because it specifies default values for parameters "b" and "c". The function can be called with any combination of arguments. If "b" and "c" are not provided in the function call, their default values of 10.0 and 0, respectively, will be used.

8. The following is the list of function declarations. Which one of them will be invoked by the function call statement cout << mul(10.5, 10);

Explanation ≡ Correct answer: option b) double mul(float a, float b);
The function call statement "cout << mul(10.5, 10);" will invoke the function with the prototype "double mul(float a, float b);". The function will take two float arguments and return a double value. The other function declarations do not match the arguments and return type used in the function call statement.

9. The usage of macros in C may be replaced in C++ by

Explanation ≡ Correct answer: option b) inline functions
In C++, macros can be replaced by inline functions. Macros are preprocessor directives that perform simple text replacement, whereas inline functions provide a safer and more efficient way of defining small, reusable code blocks that are expanded directly at the call site, similar to macros. Inline functions avoid potential issues with macros, such as incorrect parameter evaluation and lack of type checking.

10. The function mul(void) { return(5.5 * 3); }

Explanation ≡ Correct answer: option b) 16.5
The function "mul(void) { return(5.5 * 3); }" calculates the result of 5.5 multiplied by 3, which is 16.5. Since the function does not take any arguments and directly returns the result of the multiplication, the output of the function call will be 16.5.

11. Which of the following statements is true concerning a function containing const arguments?

Explanation ≡ Correct answer: option b) The function cannot change the value of const arguments.
When a function contains const arguments, it means that the function promises not to modify the values of those arguments. Attempting to change the value of a const argument inside the function will result in a compilation error. Const arguments can be used in expressions, but they cannot be modified.

12. Which of the following will return the value -8.0, given the value of x to be -8.5?

Explanation ≡ Correct answer: option c) floor(x)
The "floor(x)" function in C++ returns the largest integer that is less than or equal to the argument "x". In this case, if x is -8.5, then "floor(-8.5)" will return -9.0, which is the largest integer less than or equal to -8.5. Therefore, the correct answer is -9.0.


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. 
Let us C++ by Famous author Yashavant Kanetkar
Indian writer


Topic wise C++ MCQs Index ≡ (Click to open)
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments