This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Static Data Members”.
1. 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
View Answer
Explanation: 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.
2. Which keyword should be used to declare static variables?
a) static
b) stat
c) common
d) const
View Answer
Explanation: 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.
3. 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
View Answer
Explanation: 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.
4. 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;
View Answer
Explanation: 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.
5. 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
View Answer
Explanation: 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.
6. 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;
View Answer
Explanation: 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.
7. 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
View Answer
Explanation: 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.
8. 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
View Answer
Explanation: 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.
9. 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;
View Answer
Explanation: 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.
10. Which data members among the following are static by default?
a) extern
b) integer
c) const
d) void
View Answer
Explanation: The const data members of any class are made static by default. This is an implicit meaning given by the compiler to the member. Since const values won’t change from object to object, hence are made static instead.
11. What is the output of the following program?
class Test { private: static int x; public: static void fun() { cout << ++x << “ ”; } }; int Test :: x =20; void main() { Test x; x.fun(); x.fun(); }
a) 20 22
b) 20 21
c) 21 22
d) 22 23
View Answer
Explanation: The static member is initialized with 20. Then the function is called which used pre-increment and printed value of x. The function is called twice. Hence we get 21 22 as output.
12. Whenever any static data member is declared in a class ______________________
a) Only one copy of the data is created
b) New copy for each object is created
c) New memory location is allocated with each object
d) Only one object uses the static data
View Answer
Explanation: The static data is same for all the objects. Instead of creating the same data each time an object is created, the compiler created only one data which is accessed by all the objects of the class. This saves memory and reduces redundancy.
13. If object of class are created, then the static data members can be accessed ____________
a) Using dot operator
b) Using arrow operator
c) Using colon
d) Using dot or arrow operator
View Answer
Explanation: The static data members can be accessed in usual way as other members are accessed using the objects. The dot operator is used generally. Arrow can be used with the pointers.
14. What will be the output of the following program?
class Test { public: Test() { cout << "Test's Constructor is Called " << endl; } }; class Result { static Test a; public: Result() { cout << "Result's Constructor is Called " << endl; } }; void main() { Result b; }
a) Test’s Constructor is Called
b) Result’s Constructor is Called
c) Result’s Constructor Called Test’s Constructor is Called
d) Test’s Constructor Called Result’s Constructor is Called
View Answer
Explanation: The output is the message printed from the constructor of class Result. There is no inheritance used hence only one constructor is called. Since static members are declared once in class declaration and are not defined. The constructor of class Test will not be called.
15. 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;
View Answer
Explanation: 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.
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.
- Apply for Computer Science Internship
- Check Computer Science Books
- Check Object Oriented Programming Books
- Practice Computer Science MCQs
- Practice MCA MCQs