Access Control(C++ Programming ) Questions and Answers
What is the output of this program?
1.
#include
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 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
Explanation:-
Answer: Option C. -> Data accessedIn this program, We are using the access specifiers to friend function to manipulate the values.
Output:
$ g++ acc3.cpp
$ a.out
Data accessed
Explanation:-
Answer: Option B. -> Gates is 56 years oldIn 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
What is the output of this program?
1.
#include
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
Explanation:-
Answer: Option B. -> Identical results would be producedIn 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
What is the output of this program?
1.
#include
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
Explanation:-
Answer: Option A. -> 3010None.
What is the output of this program?
1.
#include
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
Explanation:-
Answer: Option A. -> AllocatedIn this program, We used access specifiers for structures, As we declared all methods as public,
The values can be allocated.
Output:
$ g++ acc.cpp
$ a.out
Allocated