In context of C++, which of the following is a valid statement?

2022

In context of C++, which of the following is a valid statement?

  1. A.

    Friend function of a class is always member function of that class.

  2. B.

    Value of static data member cannot be changed once assigned.

  3. C.

    Keywords private, protected and public may appear in any order and any number of times in a class.

  4. D.

    Static data member of a class can be accessed only by static member function of that class.

Attempted by 108 students.

Show answer & explanation

Correct answer: C

Step 1: Evaluate each option based on C++ language rules.

Option A: A friend function is not a member function of the class. It is declared using the 'friend' keyword and can access private and protected members, but it is defined outside the class. Hence, this statement is incorrect.

Option B: Static data members can be initialized and modified after initialization, as long as the modification is done in a valid context. For example, they can be changed in a non-const member function. Therefore, this statement is incorrect.

Option C: In C++, the access specifiers private, protected, and public can appear multiple times and in any order within a class definition. This is syntactically valid and allowed by the language standard. Hence, this statement is correct.

Option D: Static data members can be accessed by both static and non-static member functions, as well as by non-member functions. The access is not restricted to only static member functions. Therefore, this statement is incorrect.

हिन्दी उत्तर:

चरण 1: प्रत्येक विकल्प का मूल्यांकन C++ भाषा नियमों के आधार पर करें।

विकल्प A: एक मित्र फलन क्लास का सदस्य फलन नहीं होता है। इसे 'friend' कीवर्ड का उपयोग करके घोषित किया जाता है और यह निजी और संरक्षित सदस्यों तक पहुँच सकता है, लेकिन यह क्लास के बाहर परिभाषित होता है। इसलिए, यह कथन गलत है।

विकल्प B: स्टेटिक डाटा सदस्यों को प्रारंभिक रूप से निर्धारित किया जा सकता है और उन्हें बाद में परिवर्तित किया जा सकता है, जब तक कि परिवर्तन एक वैध संदर्भ में किया जाता है। उदाहरण के लिए, वे एक गैर-const सदस्य फलन में बदले जा सकते हैं। इसलिए, यह कथन गलत है।

विकल्प C: C++ में, एक क्लास परिभाषा में एक्सेस स्पेसिफायर्स private, protected और public कई बार और किसी भी क्रम में आ सकते हैं। यह वाक्यांशिक रूप से वैध है और भाषा मानक द्वारा अनुमति दी जाती है। इसलिए, यह कथन सही है।

विकल्प D: स्टेटिक डाटा सदस्यों को न तो स्टेटिक सदस्य फलन और न ही गैर-स्टेटिक सदस्य फलन द्वारा एक्सेस किया जा सकता है, बल्कि गैर-सदस्य फलन द्वारा भी एक्सेस किया जा सकता है। एक्सेस को केवल स्टेटिक फलनों तक सीमित नहीं किया जाता है। इसलिए, यह कथन गलत है।

Explore the full course: Rssb Basic Computer Instructor