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
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
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
Answer: C
Explanation:
: The access specifiers can be applicable to the member data and functions because they need to be accessed outside the block.
4. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class student
{
public:
int rno , m1 , m2 ;
protected:
void get()
{
rno = 15, m1 = 10, m2 = 10;
}
};
class sports
{
public:
int sm;
void getsm()
{
sm = 10;
}
};
class statement : public student, public sports
{
int tot, avg;
public:
void display()
{
tot = (m1 + m2 + sm);
avg = tot / 3;
cout << tot;
cout << avg;
}
void setObject()
{
get();
}
};
int main()
{
statement obj;
obj.setObject();
obj.getsm();
obj.display();
}
A. 3010
B. 1010
C. 2100
D. Error
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?
#include <iostream>
using namespace std;
struct A
{
int i;
char j;
float f;
void func();
};
void A :: func() {}
struct B
{
public:
int i;
char j;
float f;
void func();
};
void B :: func() {}
int main()
{
A a; B b;
a.i = b.i = 1;
a.j = b.j = 'c';
a.f = b.f = 3.14159;
a.func();
b.func();
cout << "Allocated";
return 0;
}
A. Allocated
B. Error
C. 3.14159
D. 1
Answer: A
Explanation:
: In this program, We used access specifiers for structures, As we declared all methods as public, The values can be allocated.
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct A
{
private:
int i, j, k;
public:
int f();
void g();
};
int A :: f()
{
return i + j + k;
}
void A :: g()
{
i = j = k = 0;
}
class B
{
int i, j, k;
public:
int f();
void g();
};
int B :: f()
{
return i + j + k;
}
void B :: g()
{
i = j = k = 0;
}
int main()
{
A a;
B b;
a.f();
a.g();
b.f();
b.g();
cout << "Identical results would be produced";
}
A. 50
B. Identical results would be produced
C. Error
D. Runtime error
Answer: B
Explanation:
: In this program, We apply the access specifiers to both the class and the structure.
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Cat
{
public:
int age;
int weight;
};
int main()
{
Cat f;
f.age = 56;
cout << "Gates is " ;
cout << f.age << " years old.\n";
}
A. Gates is
B. Gates is 56 years old
C. Error
D. Gates is 53 years old
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.
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct X;
struct Y
{
void f(X*);
};
struct X
{
private:
int i;
public:
void initialize();
friend void g(X* , int);
friend void Y :: f(X*);
friend struct Z;
friend void h();
};
void X :: initialize()
{
i = 0;
}
void g(X* x, int i)
{
x -> i = i;
}
void Y :: f(X * x)
{
x -> i = 47;
cout << x->i;
}
struct Z
{
private:
int j;
public:
void initialize();
void g(X* x);
};
void Z::initialize()
{
j = 99;
}
void Z::g(X* x)
{
x -> i += j;
}
void h()
{
X x;
x.i = 100;
cout << x.i;
}
int main()
{
X x;
Z z;
z.g(&x);
cout << "Data accessed";
}
A. 99
B. 47
C. Data accessed
D. 67
Answer: C
Explanation:
: In this program, We are using the access specifiers to friend function to manipulate the values.
9. Members of which access specifiers are not inherited?
A. Public
B. Protected
C. Private
D. None of the mentioned
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
Answer: A
Explanation:
: Mutable keyword allows assigning values to a data member belonging to a class defined as “Const” or constant.