Test Series - oops

Test Number 23/78

Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: All the member functions of local classes are __________ by default.
A. Static
B. Inline
C. Abstract
D. Virtual
Solution: 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.
Q: Which language can use inheritance with local classes?
A. Kotlin
B. Java
C. SmallTalk
D. SAP ABAP
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10