Professional Documents
Culture Documents
Questions & Answers On Access Specifiers
Questions & Answers On Access Specifiers
sanfoundry.com
Answer: c
Explanation: Only 3 types of access specifiers are available.
Namely, private, protected and public. All these three can
be used according to the need of security of members.
1 of 8 3/19/18, 9:32 AM
Access Specifiers - Object Oriented Programmi... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: All the classes can use any of the specifiers as
needed. There is no restriction on how many of them can
be used together.
Answer: a
Explanation: Private members of a class can’t be inherited.
These members can only be accessible from members of
its own class only. It is used to secure the data.
Answer: b
Explanation: Default access is used if the programmer
2 of 8 3/19/18, 9:32 AM
Access Specifiers - Object Oriented Programmi... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: Protected access is used to make the
members private. But those members can be inherited. This
gives both security and code reuse capability to a program.
Answer: c
Explanation: The default members can be inherited.
Provided that they are in same package. It works in a little
different way from private access specifier.
3 of 8 3/19/18, 9:32 AM
Access Specifiers - Object Oriented Programmi... about:reader?url=https://www.sanfoundry.com/...
c) Protected
d) Default
View Answer
Answer: b
Explanation: The implicit constructor will always be public.
Otherwise the class wouldn’t be able to have instances. In
turn, no objects will be created and the class can only be
used for inheritance.
Answer: d
Explanation: Class B object will not be able to call any of
the private, protected and public members of class A. It is
not only about the function add(), but all the members of
class A will become private members of class B.
4 of 8 3/19/18, 9:32 AM
Access Specifiers - Object Oriented Programmi... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: b
Explanation: All the constructors must be made private.
This will restrict the instance of class to be made anywhere
in the program. Since the constructors are private, no
instance will be able to call them and hence won’t be
allocated with any memory space.
Answer: a
Explanation: All the data members are counted to calculate
the size of an object of a class. The data member’s access
specifier doesn’t play any role here. Hence all the data size
will be added.
5 of 8 3/19/18, 9:32 AM
Access Specifiers - Object Oriented Programmi... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: c
Explanation: The friend function of class B will not be able
to access private members of class A. Since B is inheriting
class A privately, the members will become private in class
B. But private members of class A won’t be inherited at all.
Hence it won’t be accessible.
Answer: a
Explanation: The classes which inherit the abstract class,
won’t be able to implement the members of abstract class.
The private members will not be inherited. This will restrict
the subclasses to implement those members.
6 of 8 3/19/18, 9:32 AM
Access Specifiers - Object Oriented Programmi... about:reader?url=https://www.sanfoundry.com/...
d) Public
View Answer
Answer: d
Explanation: All the members must be of public access. So
that the members can be inherited easily. Also, the
members will be available from outside the class.
Answer: a
Explanation: All the data members should be made private
to ensure the highest security of data. In special cases we
can use public or protected access, but it is advised to keep
the data members private always.
Answer: d
Explanation: It is always advised that the member functions
should be kept public so that those functions can be used
7 of 8 3/19/18, 9:32 AM
Access Specifiers - Object Oriented Programmi... about:reader?url=https://www.sanfoundry.com/...
from out of the class. This is usually done to ensure that the
features provided by the class can be used at its maximum.
8 of 8 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: a
Explanation: The function should be made private. In this
way, the function will be available to be called only from the
class member functions. Hence the function will be secure
from outside world.
1 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
below?
class student
{
private: student()
{
}
public : student( int x)
{
marks =x;
}
};
Answer: c
Explanation: For creating object without parameters, the
default constructor must be defined in public access. But
here, only parameterized constructor is public, hence the
objects being created with only one parameter will only be
allowed.
class A
{
private : int marks; char name[20];
public :
2 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
A(int x=100)
{
marks=x;
}
};
Answer: a
Explanation: The constructor here has a default argument
constructor. Hence we can pass one parameter, but that is
optional. If an object is created without parameter, the
default value will be used in the constructor definition.
Answer: d
Explanation: The private member function will not be
accessible from outside the class. Hence any syntax will not
work to access the private members. If you have the
3 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: For calling the function directly, we can’t use
another function because that will be indirect call. Using
friend function, we can access the private members directly.
Answer: a
Explanation: The private members are most secure in
inheritance. The default members can still be in inherited in
special cases, but the private members can’t be accessed
in any case.
4 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The constructor is using a static member to
keep the count of the number of objects created. This is
done because the variable c is static and hence the value
will be common for all the objects created.
class A
{
private : int sum(int x, int y)
{
return x+y;
}
public: A()
{
}
A(int x, int y)
{
cout<<sum(x,y);
}
};
5 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: Constructor is not having any default
arguments hence no default value will be given to any
parameters. Only integer values must be passed to the
constructor if we need the sum as output, otherwise if float
values are passed, type mismatch will be shown as error.
class A()
{
int marks; char name[20];
public : A()
{
marks=100;
}
void disp()
{
cout<<”Marks=
”<'<marks;
cout<<”Student”;
}
};
6 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: Variable name will never be used. It is a
private member. None other than class members can
access name, also, neither the constructor nor the disp()
function are accessing the variable name. Hence it will
never be accessible.
Answer: a
Explanation: The private functions can also be overloaded.
This can be done in usual way by having the same name of
the member function and having different signature. Only
thing is, they must be accessed from members of class
only.
Answer: d
Explanation: The private member functions can be
7 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Class A
{
int x; int y; int z;
public : A()
{
y=100; x=100*y;
}
};
a) x will be used
b) y will be used
c) z will be used
d) All will be used
View Answer
Answer: c
Explanation: Whenever an object will be created, the
constructor will be called. Inside constructor we are using
the data members x and y. Hence these two will always be
used with each object creation.
8 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
class A()
{
int a;
private : int b;
protected : int c;
public : int d;
};
a) a
b) b
c) c
d) d
View Answer
Answer: b
Explanation: The default variables can be inherited in some
special cases but the public members can never be
inherited. Hence the most secure data member in the class
is b.
14. Which among the following is correct for the code given
below?
class A
{
private : A()
{
}
public : A(int x)
{
}
};
9 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
A a;
A b(100);
Answer: a
Explanation: The program will be giving a compile time error
as the default constructor is private in class. And, the logical
errors are usually runtime so we can’t say that the program
will give logical error. The program will not run.
Answer: d
Explanation: The private specifier can be used anywhere in
the class as required. It is not a rule to mention the private
members first and then others. It is just followed to write
first for better readability.
10 of 11 3/19/18, 9:32 AM
Private Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
11 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: c
Explanation: The members which are made protected, are
most secure if inheritance is not used. But, this facility is
provided to keep those members private and with that, they
can be inherited by other classes. This is done to make the
code more flexible.
1 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The instances will be allowed to be created
only inside the sub classes. This is because the protected
members will be inherited and hence the constructor too.
This will allow the subclasses to call the constructor
whenever an object is created.
class A
{
int marks;
protected : A()
{
marks=100;
}
public : A( int x)
{
marks=x;
}
};
2 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: d
Explanation: The instances can be created anywhere in the
program. The only restriction will be on which constructor
will have to be called. The instances with zero arguments
will be allowed to be created only inside the subclasses, but
the instances with one argument can be created anywhere
in the program.
Answer: a
Explanation: The sub class should use private inheritance.
This will allow only the nearest sub classes to inherit the
protected members and then those members will become
private. Hence further inheritance of those members will not
be possible.
class A
{
int marks;
3 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
protected : A(int x)
{
marks=x;
}
public : A()
{
marks=100;
}
}
class B
{
A a;
A b=100;
};
main()
{
A a, b=100;
B c;
}
Answer: c
Explanation: The objects being created with assignment
value are allowed, if the constructor accepts only 1
argument. But main() function will not be able to create the
4 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
class A
{
protected : int marks;
public :
A()
{
marks=100;
}
disp()
{
cout<<”marks=”<<
marks;
}
};
class B: protected A
{
};
B b;
b.disp();
5 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The object of class B can’t access the
members of A outside the class. This is because the class
is being inherited in protected access, so all the members
will become protected in subclass B.
Answer: a
Explanation: The protected members are allowed in the
same package but can also be accessed in other packages
using inheritance. But the default members can never be
accessible in other packages.
Answer: b
6 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
class A
{
private: int marks;
A()
{
}
Public : disp()
{
cout<< marks;
}
};
class B: public A
{
}
B b;
Answer: a
Explanation: Instance of B will not be created. When you try
7 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: The protected and public members of the
parent class will become the protected members in
subclass. This is predefined rule of inheritance. The reason
behind is to maintain the level of security in subclass too.
Answer: d
Explanation: The members must be made public, otherwise
it is not possible. In every case, the protected members will
act as private members if it’s about access specifier. It will
8 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The members which have to be secure and
might get used in other packages or subclasses can use
protected access. This also allows the members to be safe
from accidental modification.
Answer: b
Explanation: The statement given is true. The clear
difference is protected members are available in other
packages also, but the default members are available within
the package only.
9 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: If a new constructor is defined in public
access. That will be available to whole program. Only
restriction will be of the way to use it.
class A
{
Public : A(int a=0)
{
cout<<”new A”;
}
};
A a;
A b;
A c;
10 of 11 3/19/18, 9:32 AM
Object Oriented Programming Question Bank about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The constructor has a default argument.
Whenever the object is created, the constructor will be
called and print the message in its definition. Since the
argument is default valued, it is not mandatory to pass
anything to the new object.
11 of 11 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
class A
{
int marks;
public : disp()
{
cout<<marks;
}
}
class B: protected A
{
char name[20];
}
1 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
A a; a.disp();
B b; b.disp();
Answer: a
Explanation: The object of class A can access the disp()
function. This is because the disp() function is public in
definition of class A. But it can’t be accessed from instance
of class B because the disp() function is protected in class
B, since it was inherited as protected.
Answer: a
Explanation: The access specifier must be public so as to
access the members outside the class and anywhere within
the program without using inheritance. This is a rule,
predefined for the public members.
2 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: d
Explanation: The public members are available to whole
program. This makes the members most vulnerable to
accidental changes, which may result in unwanted
modification and hence unstable programming.
Answer: d
Explanation: Either default or public access specifier must
be used for outer most classes. Private can be used with
inner classes. This is done so that all the members can
access and use the outmost class and that program
execution can be done from anywhere. Inner classes can
be made private for security.
3 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The statement given is false. This is because
when we inherit the members they can either be made
more secure or be at same access. But the visibility
reduction is not possible, for example, if a member is
protected in parent class, then it can only be made
protected or private in sub class and not public in any case.
Answer: a
Explanation: All the members will be available in sub
classes. Though it is not guaranteed whether the members
will be available in subsequent subclasses from the first
subclass.
Answer: a
Explanation: There can be only one public class in a java
program. The public class name must match the name of
4 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
file. And there can’t be more than one class with same
name in a single program in same scope. Hence it is not
possible to have more than one public class in java
program.
package pack1;
class A
{
public A()
{
System.out.print(“object
created”);
}
}
package pack2;
import pack1.*;
class B
{
A a=new A();
}
Answer: c
Explanation: The program will give compile time error.
Class A is defined with default access specifier. This
5 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The static members are not property of any
object of the class. Instead, those are treated as property of
class. This allows us to have public static members too.
Answer: d
Explanation: Class A can have instances as it has public
default constructor. Class will have its own constructors
defined. Hence both classes can have instances.
6 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
b) Default
c) Public
d) Protected
View Answer
Answer: c
Explanation: The specifier that can make protected
member’s protected in subclass and public member’s public
in subclass, is public. This is done to maintain the security
level of protected members of parent class.
Answer: d
Explanation: The public class is a usual class. There is no
special rule but the members of the class can be accessed
from other classes using instance of the class. This is
usually useful to define main() function.
7 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: According to rule of private, protected and
default access specifiers, none of the members under these
specifiers will be able to get invoked outside the class. We
are not sure about the members of class specifically so
other options doesn’t give a fixed answer.
Answer: a
Explanation: The public inheritance makes the public
members of base class, public in derived classes. This can
be used when the same feature have to be redefined with
each new class inheriting the base class.
Answer: d
8 of 9 3/19/18, 9:32 AM
Public Access Specifier - Object Oriented Prog... about:reader?url=https://www.sanfoundry.com/...
9 of 9 3/19/18, 9:32 AM