Test Series - oops

Test Number 61/78

Q: Which among the following best defines static variables members?
A. Data which is allocated for each object separately
B. Data which is common to all the objects of a class
C. Data which is common to all the classes
D. Data which is common to a specific method
Solution: The static data members are made common to all the object of a class. They doesn’t change from object to object. Those are property of class rather than of any individual object.
Q: If static data members have to be used inside a class, those member functions _____
A. Must not be static member functions
B. Must not be member functions
C. Must be static member functions
D. Must not be member function of corresponding class
Solution: Only the static member functions can access the static data members. The definition of static members is made common and hence the member function should be capable of manipulating the static data members.
Q: The syntax for defining the static data members is __________
A. dataType className :: memberName = value;
B. dataType className : memberName = value;
C. dataType className . memberName = value;
D. dataType className -> memberName =value;
Solution: The syntax doesn’t contain the static keyword. Since it is already been declared as static inside the class. The data type and the corresponding class name must be there to allocate the variable to a class. The value is assigned using scope resolution operator for the member name.
Q: Any changes made to static data member from one member function _____________
A. Is reflected to only the corresponding object
B. Is reflected to all the variables in a program
C. Is reflected to all the objects of that class
D. Is constant to that function only
Solution: The changes made from any function to static data member will be a common change for all the other objects also. If the change is made with respect to one object and change is printed from another object, the result will be same.
Q: Which among the following is wrong syntax related to static data members?
A. className :: staticDataMember;
B. dataType className :: memberName =value;
C. static dataType memberName;
D. className : dataType -> memberName;
Solution: The syntax given in option d doesn’t belong to any particular declaration or definition. First one is to access the static members using the class name. Second is to define the static data outside the class. Third syntax id to declare a data member as static in a class.
Q: The static data member ______________________
A. Must be defined inside the class
B. Must be defined outside the class
C. Must be defined in main function
D. Must be defined using constructor
Solution: The static data members must be defined outside the class. Since these are common to all the objects and should be created only once, they must not be defined in the constructor.
Q: Which keyword should be used to declare static variables?
A. static
B. stat
C. common
D. const
Solution: The keyword used to declare static variables is static. This is must be used while declaring the static variables. The compiler can make variables static if and only if they are mentioned with static keyword.
Q: Which is the correct syntax for declaring static data member?
A. static mamberName dataType;
B. dataType static memberName;
C. memberName static dataType;
D. static dataType memberName;
Solution: The syntax must firstly be mentioned with the keyword static. Then the data type of the member followed by the member name should be given. This is general form of declaring static data members.
Q: The static data member __________________________
A. Can be accessed directly
B. Can be accessed with any public class name
C. Can be accessed with dot operator
D. Can be accessed using class name if not using static member function
Solution: The static data members can be accessed using the class name also. If the member functions is not used or is not to be used then we can call the static data members directly by using its corresponding class name.
Q: Which among the following is the correct syntax to access static data member without using member function?
A. className -> staticDataMember;
B. className :: staticDataMember;
C. className : staticDataMember;
D. className . staticDataMember;
Solution: For accessing the static data members without using the static member functions, the class name can be used. The class name followed by scope resolution, indicating that static data members is member of this class, and then the data member name.

You Have Score    /10