Skip to main content

Introduction to Programming MCQs with Answers

Practice important Introduction to Programming MCQs with answers and explanations.

Multiple Choice Questions

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
View Answer
B

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
View Answer
A

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
View Answer
B

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
View Answer
B

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
View Answer
B

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
View Answer
B

Q657: What is the syntax to define a member function of a template class outside the class definition?
  • A: template class name::function_name (argument_list) { /* body */ }
  • B: class name::function_name (argument_list) { /* body */ }
  • C: template void name::function_name (argument_list) { /* body */ }
  • D: template function_name (argument_list) { /* body */ }
View Answer
A

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
View Answer
B

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
View Answer
A

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
View Answer
C

Test Your Knowledge

Take a timed quiz on Introduction to Programming

🚀 Start Quiz Now