Introduction to Programming MCQs with Answers
Practice important Introduction to Programming MCQs with answers and explanations.
Multiple Choice Questions
Q641: How does the compiler handle template functions?
- A: Generates separate functions at runtime
- B: Creates function versions for different data types at compile time
- C: Manually writes functions for each data type
- D: Uses pre-defined functions for each data type
View Answer
B
Q642: What keyword is used to define a template function in C++?
- A: template
- B: macro
- C: define
- D: generic
View Answer
A
Q643: Which of the following is true about function templates?
- A: They require explicit definition for every data type used
- B: They can only be used for built-in data types
- C: They allow the compiler to generate functions for specific data types at compile time
- D: They cannot be used with user-defined data types
View Answer
C
Q644: How would you define a template function that swaps two values of any data type?
- A: template
void swap(T &a, T &b) { T temp = a; a = b; b = temp; } - B: template
void swap(T a, T b) { T temp = a; a = b; b = temp; } - C: template
void swap(T a, T b) { T temp; temp = a; a = b; b = temp; } - D: template
void swap(T &a, T &b) { T temp; temp = a; a = b; b = temp; }
View Answer
D
Q645: What happens if you try to use a template function with mismatched argument types?
- A: The program compiles successfully but may produce incorrect results
- B: The compiler generates an error at compile time
- C: The compiler automatically converts types to match
- D: The function call will be ignored
View Answer
B
Q646: Can a template function have multiple generic types?
- A: No, only one generic type is allowed
- B: Yes, up to two generic types are allowed
- C: Yes, templates can have multiple generic types
- D: No, templates are limited to built-in types only
View Answer
C
Q647: When defining a template function that needs to handle different data types, which part should be generic?
- A: Both the function name and the return type
- B: Only the function name
- C: At least one argument in the function should be of generic type
- D: Only the return type
View Answer
C
Q648: What is a limitation of template functions related to file separation?
- A: Template functions cannot be split into separate header and implementation files
- B: Template functions can be declared in one file and defined in another
- C: Template functions can only be used within the same file
- D: Template functions need to be explicitly declared in multiple files
View Answer
A
Q649: How does the reverse template function handle different data types?
- A: It generates a version of the function for each data type at runtime
- B: It uses a fixed type for all operations
- C: It uses the same function definition for all data types
- D: It generates a specific version for each data type at compile time
View Answer
D
Q650: What is an example of an overloaded template function?
- A: A template function that performs different operations based on data types
- B: A function that swaps two values and takes one or more arguments
- C: A function that generates different code paths based on number of arguments
- D: A function template that can handle different argument types and counts
View Answer
A