Skip to main content

Object Oriented Programming MCQs with Answers

Practice important Object Oriented Programming MCQs with answers and explanations.

Chapter: 38
Q581:
In the resolution order of template specializations, which specialization is checked first?
A. Partial specialization
B. General template
C. Complete specialization
D. None of the above
Correct Answer: C
Chapter: 38
Q582:
When the compiler searches for template specializations, it starts from more ______ to more ______.
A. specific, general
B. specific, specialized
C. general, specific
D. specialized, specific
Correct Answer: A
Chapter: 38
Q583:
Rule 1 states that when an ordinary function or class is declared as a friend of a class template, it becomes a friend of ______.
A. only the class template instantiated for the same type
B. all instantiations of that template class
C. none of the instantiations of that template class
D. some instantiations of that template class
Correct Answer: B
Chapter: 38
Q584:
Which of the following is true about Rule 2 in template and friend functions/classes?
A. A template friend function or class template is instantiated with the same type parameters of the class template granting friendship.
B. All instantiations of the friend function or class are friends of the class template.
C. Only one specific instantiation of the friend function or class is considered a friend.
D. None of the above
Correct Answer: A
Chapter: 38
Q585:
In the context of template and friend functions, what happens if you instantiate a friend template function with a different type than the class template?
A. The instantiation of the friend function will be a friend of the class template.
B. The instantiation of the friend function will not be a friend of the class template.
C. The friend function will be instantiated with the same type parameters as the class template.
D. None of the above
Correct Answer: B
Chapter: 38
Q586:
Rule 3 of templates and friends says that if a friend function or class template takes different type parameters from the class template granting friendship, then ______.
A. its each instantiation is a friend of each instantiation of the class template
B. its each instantiation is not a friend of the class template
C. its instantiation must match the type parameters of the class template
D. none of the above
Correct Answer: A
Chapter: 38
Q587:
When a template class is declared as a friend of another class template, which types of specializations become friends?
A. Only explicit specializations
B. Only implicit specializations
C. All kinds of specializations including explicit, implicit, and partial
D. None of the above
Correct Answer: C
Chapter: 38
Q588:
If a template function is declared as a friend of a class template, which of the following is true?
A. All instantiations of that function become friends of the class template only for specific types
B. All instantiations of the function become friends of the class template for all types
C. Only the specific instantiation of the function that is used is a friend
D. None of the above
Correct Answer: B
Chapter: 38
Q589:
In a template class, when you declare a template function as a friend, what type of template function becomes a friend of the class?
A. Only the exact template function instantiated with the same type parameters
B. Only general template functions
C. Only partial template functions
D. All template functions including explicit, implicit, and partial
Correct Answer: D
Chapter: 38
Q590:
In Rule 4, when a template is declared as a friend of any class, which of the following also becomes friends of the class?
A. Only the explicit specializations of that function or class
B. Only the implicit specializations of that function or class
C. All specializations including explicit, implicit, and partial
D. None of the above
Correct Answer: C
Chapter: 38
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
Correct Answer: A
Chapter: 38
Q592:
Templates and their specializations can use ______ to access private data of the class.
A. friend functions
B. inheritance
C. friend classes
D. overloading
Correct Answer: A
Chapter: 38
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
Correct Answer: B
Chapter: 38
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
Correct Answer: A
Chapter: 38
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
Correct Answer: A
Chapter: 38
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
Correct Answer: A
Chapter: 38
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
Correct Answer: A
Chapter: 38
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
Correct Answer: C
Chapter: 38
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
Correct Answer: A
Chapter: 39
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
Correct Answer: A

Test Your Knowledge

Take a timed quiz on Object Oriented Programming

🚀 Start Quiz Now