C++ Programming Questions and Answers – Access Control

This section on advanced C++ interview questions focuses on “Access Control”. One shall practice these advanced C++ questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our advanced C++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of advanced C++ interview questions on “Access Control” along with answers, explanations and/or solutions:

1. Which access specifier is used where one wants data members to be accessed by other classes but not from outside objects?
a) private
b) protected
c) public
d) both protected and public
View Answer

Answer: b
Explanation: Protected and public members are accessible from derived classes but public members can be accessed by objects of the class so protected specifier is the answer.

2. Which of the following describes the protected access specifier?
a) The variable is visible only outside inside the block
b) The variable is visible everywhere
c) The variable is visible to its block and to it’s derived class
d) The variable is not visible to its block
View Answer

Answer: c
Explanation: Protected members are visible to its block and to the derived classes and not visible to outside objects or variables.

3. To which of the following access specifiers are applicable?
a) Member data
b) Functions
c) Both Member data & Functions
d) Protected members
View Answer

Answer: c
Explanation: The access specifiers can be applicable to the member data and functions because they need to be accessed outside the block.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class student
  4.     {
  5.         public:
  6.         int rno , m1 , m2 ;
  7.         protected:
  8.         void get()
  9.         {
  10.             rno = 15, m1 = 10, m2 = 10;
  11.         }
  12.     };
  13.     class sports
  14.     {
  15.         public:
  16.         int sm;
  17.         void getsm()
  18.         {
  19.             sm = 10;
  20.         }
  21.     };
  22.     class statement : public student, public sports
  23.     {
  24.         int tot, avg;
  25.         public:
  26.         void display()
  27.         {
  28.             tot = (m1 + m2 + sm);
  29.             avg = tot / 3;
  30.             cout << tot;
  31.             cout << avg;
  32.         }
  33.         void setObject()
  34.         {
  35.             get();
  36.         }
  37.     };
  38.     int main()
  39.     {
  40.         statement obj;
  41.         obj.setObject();
  42.         obj.getsm();
  43.         obj.display();
  44.     }

a) 3010
b) 1010
c) 2100
d) Error
View Answer

Answer: a
Explanation: In this program we setting values of m1 and m2 using obj.setObject() function derived from student class. setting calue of sm using getsm() derived from sports function and then displaying the outputs using display() function in statement class.

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std; 
  3.     struct A 
  4.     {
  5.         int i;
  6.         char j;
  7.         float f;
  8.         void func();
  9.     };
  10.     void A :: func() {}
  11.     struct B 
  12.     {
  13.         public:
  14.         int i;
  15.         char j;
  16.         float f;
  17.         void func();
  18.     };
  19.     void B :: func() {}
  20.     int main() 
  21.     {
  22.         A a; B b;
  23.         a.i = b.i = 1; 
  24.         a.j = b.j = 'c';
  25.         a.f = b.f = 3.14159;
  26.         a.func();
  27.         b.func();
  28.         cout << "Allocated"; 
  29.         return 0;
  30.     }

a) Allocated
b) Error
c) 3.14159
d) 1
View Answer

Answer: a
Explanation: In this program, We used access specifiers for structures, As we declared all methods as public, The values can be allocated.
Output:

advertisement
$ g++ acc.cpp
$ a.out
Allocated

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct A 
  4.     {
  5.         private:
  6.         int i, j, k;
  7.         public:
  8.         int f();
  9.         void g();
  10.     };
  11.     int A :: f() 
  12.     {
  13.         return i + j + k;
  14.     }
  15.     void A :: g() 
  16.     {
  17.         i = j = k = 0;
  18.     }
  19.     class B 
  20.     {
  21.         int i, j, k;
  22.         public:
  23.         int f();
  24.         void g();
  25.     };
  26.     int B :: f() 
  27.     {
  28.         return i + j + k; 
  29.     }
  30.     void B :: g() 
  31.     {
  32.         i = j = k = 0;
  33.     }
  34.     int main() 
  35.     {
  36.         A a;
  37.         B b;
  38.         a.f(); 
  39.         a.g();
  40.         b.f(); 
  41.         b.g();
  42.         cout << "Identical results would be produced";
  43.     }

a) 50
b) Identical results would be produced
c) Error
d) Runtime error
View Answer

Answer: b
Explanation: In this program, We apply the access specifiers to both the class and the structure.
Output:

$ g++ acc1.cpp
$ a.out

Identical results would be produced

7. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Cat
  4.     {
  5.         public:
  6.         int age;
  7.         int weight;
  8.     };
  9.     int main()
  10.     {
  11.         Cat f;
  12.         f.age = 56;
  13.         cout << "Gates is " ;
  14.         cout << f.age << " years old.\n";
  15.     }

a) Gates is
b) Gates is 56 years old
c) Error
d) Gates is 53 years old
View Answer

Answer: b
Explanation: In this program, We passed the value from main function to class and returning it to the main and then printing it.
Output:

$ g++ acc2.cpp
$ a.out
Gates is 56 years old

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct X;
  4.     struct Y 
  5.     {
  6.         void f(X*);
  7.     };
  8.     struct X 
  9.     {
  10.         private:
  11.         int i;
  12.         public:
  13.         void initialize(); 
  14.         friend void g(X* , int);
  15.         friend void Y :: f(X*);
  16.         friend struct Z;
  17.         friend void h();
  18.     };
  19.     void X :: initialize() 
  20.     {
  21.         i = 0;
  22.     }
  23.     void g(X* x, int i) 
  24.     {
  25.         x -> i = i;
  26.     }
  27.     void Y :: f(X * x) 
  28.     {
  29.         x -> i = 47;
  30.         cout << x->i;
  31.     }
  32.     struct Z 
  33.     {
  34.         private:
  35.         int j;
  36.         public:
  37.         void initialize();
  38.         void g(X* x);
  39.     };
  40.     void Z::initialize() 
  41.     {
  42.         j = 99;
  43.     }
  44.     void Z::g(X* x) 
  45.     {
  46.         x -> i += j;
  47.     }
  48.     void h() 
  49.     {
  50.         X x;
  51.         x.i = 100;
  52.         cout << x.i;
  53.     }
  54.     int main() 
  55.     {
  56.         X x;
  57.         Z z;
  58.         z.g(&x);
  59.         cout << "Data accessed";
  60.     }

a) 99
b) 47
c) Data accessed
d) 67
View Answer

Answer: c
Explanation: In this program, We are using the access specifiers to friend function to manipulate the values.
Output:

$ g++ acc3.cpp
$ a.out
Data accessed

9. Members of which access specifiers are not inherited?
a) Public
b) Protected
c) Private
d) None of the mentioned
View Answer

Answer: d
Explanation: All the data members and member functions of a class are private by default.

10. What is the importance of mutable keyword?
a) It allows the data member to change within a const member function
b) It will not allow the data member to change within a const member function
c) It will copy the values of the variable
d) It allows the data member to change outside a const member function
View Answer

Answer: a
Explanation: Mutable keyword allows assigning values to a data member belonging to a class defined as “Const” or constant.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.