Object Oriented Programming using C++ Questions and Answers – Local Class

«
»

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Local Class”.

1. What are local classes?
a) Classes declared inside a package
b) Classes declared inside a function
c) Classes declared inside a class
d) Classes declared inside structure
View Answer

Answer: b
Explanation: The classes declared inside a package are available to all the functions and classes, hence can’t be called local. This is somewhat similar concept that we use to denote variables of a function. The classes declared inside functions will be local to them.
advertisement

2. All member functions of a local class must be ___________
a) Defined outside class body
b) Defined outside the function definition
c) Defined inside the class body
d) Defined at starting of program
View Answer

Answer: c
Explanation: There is a restriction on where the member functions of the local class should be define. Those must be defined inside the class body only. This is to reduce the ambiguity and complexity of program.

3. Can local class members access/use the general local variables (except static, abstract etc.) of the function in which it is defined?
a) Yes, it can access with arrow operator
b) No, it can’t access with dot operator
c) Yes, it can access using dot operator
d) No, it can’t access In anyway
View Answer

Answer: d
Explanation: The local variables of the functions are not available to the member functions of the class. This is done to reduce the ambiguity in variables and their access rules.
advertisement
advertisement

4. Which type of data can a local class access from the function in which it is defined?
a) Static and extern
b) Abstract and static
c) Void and extern
d) Const and static
View Answer

Answer: a
Explanation: The local classes have this feature to access the static and extern variables of the function in which those are defined. This feature is available since these type of data are common to the program and is created only one time. Run time creation and destruction of these variables is not done. The only restriction that may apply is those members must be constants.

5. Local classes can access the type names and enumerators defined by the enclosing function.
a) True
b) False
View Answer

Answer: a
Explanation: This is a little tricky part with local classes. Though the local class can’t access the general variables of the function but can access the types that are defined inside the function. This is because the whole definition of that type would be existing inside the class.
advertisement

6. Can static variables be declared inside a local class?
a) Yes, with public access specifier
b) Yes, anywhere as required
c) No, not possible in private access specifier
d) No, not possible anyway
View Answer

Answer: d
Explanation: No, the static variables can’t be declared inside a local class. This is because each time the function is called, all the variables get created again and are destroyed as soon as the function is returned. This would have been possible id the static variable was of function.

7. All the member functions of local classes are __________ by default.
a) Static
b) Inline
c) Abstract
d) Virtual
View Answer

Answer: c
Explanation: All the members are defined inside the class body. And when the member functions are defined inside the class body, they are made inline by default. If the definition is too complex, those are made normal functions.
advertisement

8. The enclosing function has no special access to the members of the local class.
a) True
b) False
View Answer

Answer: a
Explanation: This is a rule that the enclosing function doesn’t have any special access to the members of the local class. This is done to maintain the security of class members. And to adhere to the rules of OOP.

9. Which language can use inheritance with local classes?
a) Kotlin
b) Java
c) SmallTalk
d) SAP ABAP
View Answer

Answer: d
Explanation: Other language might support inheritance with local classes but those doesn’t provide all the proper features of inheritance. Language SAP ABAP provides a way to implement inheritance with local classes efficiently.
advertisement

10. How many local classes can be defined inside a single function?
a) Only 1
b) Only 3
c) Only 5
d) As many as required
View Answer

Answer: d
Explanation: The local classes can be defined as required. There is no restriction on the number of local classes that can be defined inside a function. But all those classes must follow the rules and restrictions.

11. All the data members of local class must be ___________
a) Defined with declaration
b) Defined in constructor
c) Declared and defined in constructor
d) Declared using a member function
View Answer

Answer: b
Explanation: The data members follow the same rules as of simple classes. Hence the data members must be declared first. Then their definition must be given using the constructors.

12. Can two different functions have local class with same name?
a) Yes, since local
b) No, names must be different
c) No, scope doesn’t work here
d) No, ambiguity arises
View Answer

Answer: a
Explanation: The local classes can have same name if they belong to different functions. The classes would be local to those specific functions and hence can have same name. This is same as that of local variables concept.

13. What is the scope of local class?
a) Within the class only
b) Within the function
c) Within the program
d) One time creation and live till end of program
View Answer

Answer: b
Explanation: The scope of a local class is limited only within the function definition. The function can use the class as usual as local variables. The class gets destroyed as soon as the function is returned.

14. Can a function, other than the enclosing function of local class, access the class members?
a) Yes, using object
b) Yes, using direct call
c) Yes, using pointer
d) No, can’t access
View Answer

Answer: d
Explanation: The local classes are local to the specific enclosing function. Other functions can’t access the class. Even if the pointers are used, the class must be alive when the pointer is used. But this will not happen if the enclosing function is returned.

15. Which among the following is the main advantage of using local classes?
a) Make program more efficient
b) Makes program execution faster
c) Helps to add extra functionality to a function
d) Helps to add more members to a function
View Answer

Answer: c
Explanation: The closest answer is to add more functionalities to a function or to make some specific functions to be generic. Adding more members to a function can be done directly but to add some special functionality that are encapsulated, can be done using local classes.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter