Introduction to Programming MCQs with Answers
Practice important Introduction to Programming MCQs with answers and explanations.
Chapter: 41
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
Correct Answer:
B
Chapter: 41
Q642:
What keyword is used to define a template function in C++?
A. template
B. macro
C. define
D. generic
Correct Answer:
A
Chapter: 41
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
Correct Answer:
C
Chapter: 41
Q644:
How would you define a template function that swaps two values of any data type?
A. template<class T> void swap(T &a, T &b) { T temp = a; a = b; b = temp; }
B. template<class T> void swap(T a, T b) { T temp = a; a = b; b = temp; }
C. template<class T> void swap(T a, T b) { T temp; temp = a; a = b; b = temp; }
D. template<class T> void swap(T &a, T &b) { T temp; temp = a; a = b; b = temp; }
Correct Answer:
D
Chapter: 41
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
Correct Answer:
B
Chapter: 41
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
Correct Answer:
C
Chapter: 41
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
Correct Answer:
C
Chapter: 41
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
Correct Answer:
A
Chapter: 41
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
Correct Answer:
D
Chapter: 41
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
Correct Answer:
A
Chapter: 42
Q651:
What is the main purpose of template functions in C++?
A. To create functions for specific data types only
B. To handle different data types using the same function
C. To write functions in a non-generic manner
D. To prevent code reuse
Correct Answer:
B
Chapter: 42
Q652:
What does the template <class T> declaration do?
A. Declares a function that works with any data type
B. Defines a fixed data type for a function
C. Creates multiple functions with different names
D. Specifies the return type of the function
Correct Answer:
A
Chapter: 42
Q653:
How does the compiler handle a template function when a specific data type is used?
A. It generates multiple copies of the function with different names
B. It replaces the generic type T with the specific data type and compiles
C. It creates a single copy of the function for all data types
D. It ignores the template and uses a default data type
Correct Answer:
B
Chapter: 42
Q654:
Which of the following is true about template classes?
A. They can only be used with built-in data types
B. They allow creating classes that can handle any data type
C. They are used to define constants for classes
D. They can only be used for creating arrays
Correct Answer:
B
Chapter: 42
Q655:
What is the key property of a stack data structure?
A. First-in, first-out (FIFO)
B. Last-in, first-out (LIFO)
C. Random access
D. None of the above
Correct Answer:
B
Chapter: 42
Q656:
When a template class is instantiated with int, what happens to the T in the class definition?
A. It remains as T and is not replaced
B. It is replaced by int
C. It generates a new template with int as a parameter
D. It creates an error
Correct Answer:
B
Chapter: 42
Q657:
What is the syntax to define a member function of a template class outside the class definition?
A. template <class T> class name::function_name (argument_list) { /* body */ }
B. class name<T>::function_name (argument_list) { /* body */ }
C. template <class T> void name::function_name (argument_list) { /* body */ }
D. template <T> function_name (argument_list) { /* body */ }
Correct Answer:
A
Chapter: 42
Q658:
How does a static member in a template class behave when the class is instantiated with different data types?
A. It creates a single copy shared by all instances
B. It creates a separate static member for each data type
C. It causes an error if different types are used
D. It has no effect on the class
Correct Answer:
B
Chapter: 42
Q659:
What is the role of friend functions in template classes?
A. They can access private data of the template class
B. They define new data types
C. They replace the generic type T
D. They act as member functions of the class
Correct Answer:
A
Chapter: 42
Q660:
What is the major advantage of using templates in C++?
A. They make the code less readable
B. They require more manual adjustments for each data type
C. They promote code reuse and reduce redundancy
D. They make the code slower
Correct Answer:
C