Skip to main content

Object Oriented Programming MCQs with Answers

Practice important Object Oriented Programming MCQs with answers and explanations.

Multiple Choice Questions

Q591: When using friend functions in a template class, which instantiation is OK if the function is instantiated for a different type?
  • A: Instantiation of the function for the same type as the class
  • B: template functions must be instantiated for the same type as the class
  • C: template functions must be instantiated for a different type
  • D: None of the above
View Answer
A

Q592: Templates and their specializations can use ______ to access private data of the class.
  • A: friend functions
  • B: inheritance
  • C: friend classes
  • D: overloading
View Answer
A

Q593: The template class B in the provided example grants friendship to which of the following?
  • A: Only specific template classes and functions
  • B: All template classes and functions
  • C: Only general template classes and functions
  • D: None of the above
View Answer
B

Q594: A class template can inherit from another class template, provided that ______.
  • A: all derived classes are also template classes and take at least as many template parameters as the base class
  • B: require only one template parameter for the base class
  • C: none of the derived classes are template classes
  • D: the base class and derived class do not need to match template parameters
View Answer
A

Q595: The following code will produce an error if you try to access the private member `data` of class B in which of the following scenarios?
  • A: If you instantiate `doSomething` with a type different from that of `class B`
  • B: When `doSomething` and `class B` use the same type parameter
  • C: When `doSomething` and `class B` use different type parameters
  • D: None of the above
View Answer
A

Q596: A function template declared as a friend of a class template can be instantiated with any type as long as ______.
  • A: it matches the type parameters of the class template
  • B: it does not match the type parameters of the class template
  • C: it uses different type parameters than the class template
  • D: it must be a general template function
View Answer
A

Q597: When a class template has a friend class template with the same type parameters, ______.
  • A: the friend class template can access all private members of the class template for the same type
  • B: only the specific instantiation is able to access private members
  • C: all instantiations are able to access private members
  • D: for no instantiations can access private members
View Answer
A

Q598: In a template class B, if a friend function is instantiated with a type different from the class template, ______.
  • A: the friend function can access private members of the class template
  • B: for the same type is required for access to private members
  • C: the access to private members is denied
  • D: none of the above
View Answer
C

Q599: When a template function or class is declared a friend, all its specializations, including partial ones, ______.
  • A: become friends of the class template
  • B: are not friends of the class template
  • C: must match the exact type of the class template
  • D: do not need to match the type of the class template
View Answer
A

Q600: What is the output of the following code where static members of a template class are demonstrated? A<int> ia; A<char> ca; ia.data = 5; ca.data = 7; cout << "ia.data = " << ia.data << endl << "ca.data = " << ca.data;
  • A: ia.data = 5 and ca.data = 7
  • B: ia.data = 7 and ca.data = 5
  • C: ia.data = 5 and ca.data = 5
  • D: ia.data = 7 and ca.data = 7
View Answer
A

Test Your Knowledge

Take a timed quiz on Object Oriented Programming

🚀 Start Quiz Now