Object Oriented Programming using C++ Questions and Answers – Data Members

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

1. What is the term used to indicate the variable and constants of a class?
a) Data members
b) Variables of class
c) Data characters
d) Constants
View Answer

Answer: a
Explanation: The variables inside a class are termed data members of the class. It is not a mandatory rule but variables are used to refer to usual variables used in functions or globally. The term is given because the values stored in those variables represent some kind of data related to class.

2. Data members ________________ (C++)
a) Can be initialized with declaration in classes
b) Can be initialized only with help of constructors
c) Can be initialized either in declaration or by constructor
d) Can’t be initialized
View Answer

Answer: b
Explanation: The data members are not property of class, those are property of the instances of the class. And the memory for the data members are not reserved until a constructor is called. Hence we use constructors for their initialization after the memory is reserved.

3. Which among the following is true for data members?
a) Private data members can be initialized with declaration in class
b) Static members are initialized in constructors
c) Protected data members can be initialized in class directly
d) Static data members are defined outside class, not in constructor
View Answer

Answer: d
Explanation: Static members are not property of instances of classes. Those are shared by all the object of classes. Hence those are defined outside the constructor, so as to make them common for all the objects.
advertisement
advertisement

4. What should be done for data member to be of user defined structure type?
a) The structure must have been defined before class.
b) The structure must have been defined after the class definition
c) The structure must be predefined
d) The structure type data members can’t be used
View Answer

Answer: a
Explanation: The structure must have been defined prior to its use. If the structure is not defined, then the memory space will not be allocated for its members. This leads to undefined use of new data types.

5. How many data members can a class contain?
a) 27
b) 255
c) 1024
d) As many as required
View Answer

Answer: d
Explanation: Any class can have as many data members as required. The only restriction that may arise is when there is not enough memory space. This gives flexibility to define a class with best properties possible.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. How to access data members of a class?
a) Dot operator
b) Arrow operator
c) Dot or arrow as required
d) Dot, arrow or direct call
View Answer

Answer: c
Explanation: The data members can never be called directly. Dot operator is used to access the members with help of object of class. Arrow is usually used if pointers are used.

7. To create a pointer to a private data member of a class, outside the class, which among the following is correct?
a) Return the address of the private data member using a member function
b) Access the private member using a pointer outside class
c) Declare the member as pointer inside the class
d) Not possible to create pointer to a private member
View Answer

Answer: a
Explanation: We can call a public member function and return the address of any private data member. Though the pointer being returned must be defined inside class itself. And the returned address can be stored in a pointer.
advertisement

8. Which among the following is true for use of setter() and getter() function?
a) Considered best for manipulating data values
b) Considered the only proper way to manipulate the values
c) Considered specially for private members manipulation
d) Considered a red flag, and not recommended for large scale use
View Answer

Answer: d
Explanation: This concept of getter and setter functions is not acceptable if used too much. This is considered to be inappropriate in OOP perspective. Though it is commonly used, it doesn’t work according to OOP concepts at some higher level of understanding.

9. What is the output of following code?

advertisement
int n=10;		// global
class A()
{
	private : int n;
	public : int m;
	A()
	{ 
		n=100; m=50;
	}
void disp()
{
	cout<<”n”<<m<<n;
};

a) 1050100
b) 1005010
c) n5010
d) n50100
View Answer

Answer: d
Explanation: In cout we have specified n as a string to be printed. And m is a variable so its value gets printed. And global variable will not be used since local variable have more preference.

10. The static member functions can only use ________
a) Static data members
b) Private data members
c) Protected data members
d) Constant data members
View Answer

Answer: a
Explanation: The static member functions can only access static data members. This is because the static member function can’t work with the properties that change object to object. It is mandatory that only the common properties of all the objects be used. And only static data members are common to all as those are property of class.

11. A class can have self-referential data members.
a) True
b) False
View Answer

Answer: b
Explanation: The data members in a class can never refer to own class type. This is not possible because the data members should have some memory allocated for its object before the self-reference is used, but class must call constructor for that. Hence not possible.

12. What is the keyword used to make data members have same value?
a) static
b) const
c) double
d) abstract
View Answer

Answer: b
Explanation: The keyword const can be used anywhere to make the variable have same value all the time. This restriction is made to use the same value whenever required. Also, this can restrict accidental changes.

13. Which data members can be inherited but are private to a class?
a) Private
b) Protected
c) Protected and Static
d) Privately inherited
View Answer

Answer: b
Explanation: Static members inheritance also depends on the type of specifier they have. Only the protected members can be inherited but remain private to class. If static members are defined in private access, they won’t be allowed for inheritance.

14. The arguments passed to member functions by reference are considered as data members of class.
a) True
b) False
View Answer

Answer: b
Explanation: This is a wrong statement. As only the data defined inside class is considered as its member. But even if a variable is passed by reference it would be the same variable that is outside the class. Hence it can’t be considered class member.

15. Which among the following is not allowed for data member declaration?
a) int a;
b) static int a;
c) abstract a;
d) Boolean a;
View Answer

Answer: c
Explanation: The abstract keyword in the declaration of data members is not allowed. This is because the abstract keyword features can’t be used with the data members of the class. We can have all other syntax given, but not abstract.

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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.