Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) 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
Clarification: 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.
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
Clarification: 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
Clarification: 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.
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
Clarification: 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.
Clarification: 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.
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
Clarification: 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.
Clarification: 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.
8. The enclosing function has no special access to the members of the local class.
Clarification: 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?
d) SAP ABAP
Clarification: 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.
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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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.