Professional Documents
Culture Documents
sanfoundry.com
Answer: a
Explanation: The variables inside a class are termed data
members of the class. It is not a mandatory rule but
variables are used to refer usual variables used in functions
or globally. The term is given because the values stored in
those variables represent some kind of data related to
class.
1 of 8 3/19/18, 9:34 AM
Data Members - Object Oriented Programming... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The data members are not property of class,
those are property of the instances of the class. And the
memory for the data members are not reserved until a
constructor is called. Hence we use constructors for their
initialization after the memory is reserved.
Answer: d
Explanation: Static members are not property of instances
of classes. Those are shared by all the object of classes.
Hence those are defined outside the constructor, so as to
make them common for all the objects.
2 of 8 3/19/18, 9:34 AM
Data Members - Object Oriented Programming... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The structure must have been defined prior to
its use. If the structure is not defined, then the memory
space will not be allocated for its members. This leads to
undefined use of new data types.
Answer: d
Explanation: Any class can have as many data members as
required. The only restriction that may arise is when there is
not enough memory space. This gives flexibility to define a
class with best properties possible.
3 of 8 3/19/18, 9:34 AM
Data Members - Object Oriented Programming... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The data members can never be called
directly. Dot operator is used to access the members with
help of object of class. Arrow is usually used if pointers are
used.
Answer: a
Explanation: We can call a public member function and
return the address of any private data member. Though the
pointer being returned must be defined inside class itself.
And the returned address can be stored in a pointer.
Answer: d
Explanation: This concept of getter and setter functions is
4 of 8 3/19/18, 9:34 AM
Data Members - Object Oriented Programming... about:reader?url=https://www.sanfoundry.com/...
a) 1050100
b) 1005010
c) n5010
d) n50100
View Answer
Answer: d
Explanation: In cout we have specified n as a string to be
printed. And m is a variable so its value gets printed. And
global variable will not be used since local variable have
more preference.
5 of 8 3/19/18, 9:34 AM
Data Members - Object Oriented Programming... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The static member functions can only access
static data members. This is because the static member
function can’t work with the properties that change object to
object. It is mandatory that only the common properties of
all the objects be used. And only static data members are
common to all as those are property of class.
Answer: b
Explanation: The data members in a class can never refer
to own class type. This is not possible because the data
members should have some memory allocated for its object
before the self-reference is used, but class must call
constructor for that. Hence not possible.
6 of 8 3/19/18, 9:34 AM
Data Members - Object Oriented Programming... about:reader?url=https://www.sanfoundry.com/...
d) abstract
View Answer
Answer: b
Explanation: The keyword const can be used anywhere to
make the variable have same value all the time. This
restriction is made to use the same value whenever
required. Also, this can restrict accidental changes.
Answer: b
Explanation: Static member’s inheritance also depends on
the type of specifier they have. Only the protected members
can be inherited but remain private to class. If static
members are defined in private access, they won’t be
allowed for inheritance.
Answer: b
Explanation: This is a wrong statement. As only the data
defined inside class is considered as its member. But even
7 of 8 3/19/18, 9:34 AM
Data Members - Object Oriented Programming... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The abstract keyword in declaration of data
members is not allowed. This is because the abstract
keyword features can’t be used with the data members of
the class. We can have all other syntax given, but not
abstract.
8 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: b
Explanation: We can’t say that only functions which are
defined inside class are member functions. There can be
some inherited functions. Though they doesn’t belong to the
class but are property of the objects once inheritance is
used. So the nearest definition is functions belonging a
class.
1 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: There are 5 types of member functions that
are generally provided in C++. Namely, simple, static,
const, inline and friend member functions. Member
functions are specific to classes.
Answer: d
Explanation: The member functions can be called using
only the dot operator or the arrow operator. But the static
members can be called using directly the class name
followed by the scope resolution operator and static
member function name. This is useful when you don’t have
any object to call the member.
2 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The member functions whose definition is
expanded at the call, and no jump to function and return
happened, are termed as inline functions. This is used to
make the program faster and more efficient.
Answer: a
Explanation: There must be specific memory space
allocated for the data members before the static member
functions uses them. But the space is not reserved if object
is not declared. Hence only if static members are not used,
it leads to compile time error.
3 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The static member functions doesn’t contain
“this” pointer. Static member functions can’t be defined as
const or volatile also. These are restrictions on static
member functions.
Answer: d
Explanation: The members of a class can be used directly
inside a member function. We can use this pointer when
there is a conflict between data members of class and
arguments/local function variable names.
4 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
function
View Answer
Answer: c
Explanation: For overloading those operators for a class,
the class must use non-static member function so that
doesn’t remain common to all the objects, and each object
can use it independently. The friend functions is also
restricted so as to keep the security of data.
Answer: a
Explanation: The virtual functions are defined using virtual
keyword. These are made in order to make all the classes
to define them as the class gets inherited. Increases code
understanding.
Answer: b
Explanation: When generic type is used in a class, the
5 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The member functions has access to all the
members of the class. Whenever data members of a class,
which might be private, have to be modified, we make use
of these member functions. This is more secure way to
manipulate data.
class A
{
int a,b;
public : void disp();
}
a) void disp::A(){ }
b) void A::disp(){ }
c) void A:disp() { cout<<a<<b ; }
d) void disp:A(){ cout<<a<<b; }
6 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: b
Explanation: The syntax in option b is correct. We use
scope resolution to represent the member function of a
class and to write its definition. It is not necessary for a
function to have anything in its definition.
Answer: a
Explanation: We can call one function inside another
function to access some data of class. A public member
function can be used to call a private member function
which directly manipulates the private data of class.
Answer: d
Explanation: All the member functions work same as normal
functions with syntax. But the constructor and destructor
7 of 8 3/19/18, 9:34 AM
Member Functions - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: A member function of a class can only have
the access to the members of its own class and parent
classes if inheritance used. Otherwise a member function
can never access the members of a subclass. Accessing
static members of a class is possible by normal and static
member functions.
8 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: b
Explanation: The classes declared inside a package are
available to all the functions and classes, hence can’t be
called local. This is somewhat similar concept that we use
to denote variables of a function. The classes declared
inside functions will be local to them.
1 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: There is a restriction on where the member
functions of the local class should be define. Those must be
defined inside the class body only. This is to reduce the
ambiguity and complexity of program.
Answer: d
Explanation: The local variables of the functions are not
available to the member functions of the class. This is done
to reduce the ambiguity in variables and their access rules.
2 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: a
Explanation: The local classes have this feature to access
the static and extern variables of the function in which those
are defined. This feature is available since these type of
data are common to the program and is created only one
time. Run time creation and destruction of these variables is
not done. The only restriction that may apply is those
members must be constants.
Answer: a
Explanation: This is a little tricky part with local classes.
Though the local class can’t access the general variables of
the function but can access the types that are defined
inside the function. This is because the whole definition of
that type would be existing inside the class.
Answer: d
Explanation: No, the static variables can’t be declared
3 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: All the members are defined inside the class
body. And when the member functions are defined inside
the class body, they are made inline by default. If the
definition is too complex, those are made normal functions.
Answer: a
Explanation: This is a rule that the enclosing function
doesn’t have any special access to the members of local
class. This is done to maintain the security of class
members. And to adhere to the rules of OOP.
4 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
b) Java
c) SmallTalk
d) SAP ABAP
View Answer
Answer: d
Explanation: Other language might support inheritance with
local classes but those doesn’t provide all the proper
features of inheritance. Language SAP ABAP provides a
way to implement inheritance with local classes efficiently.
Answer: d
Explanation: The local classes can be defined as required.
There is no restriction on the number of local classes that
can be defined inside a function. But all those classes must
follow the rules and restrictions.
5 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The data members follow same rules as of
simple classes. Hence the data members must be declared
first. Then their definition must be given using the
constructors.
12. Can two different functions have local class with same
name?
a) Yes, since local
b) No, names must be different
c) No, scope doesn’t work here
d) No, ambiguity arises
View Answer
Answer: a
Explanation: The local classes can have same name if they
belong to different functions. The classes would be local to
those specific functions and hence can have same name.
This is same as that of local variables concept.
Answer: b
Explanation: The scope of a local class is limited only within
the function definition. The function can use the class as
usual as local variables. The class gets destroyed as soon
as the function is returned.
6 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: The local classes are local to the specific
enclosing function. Other functions can’t access the class.
Even if the pointers are used, the class must be alive when
the pointer is used. But this will not happen if the enclosing
function is returned.
Answer: c
Explanation: The closest answer is to add more
functionalities to a function or to make some specific
functions to be generic. Adding more members to a function
can be done directly but to add some special functionality
that are encapsulated, can be done using local classes.
7 of 8 3/19/18, 9:34 AM
Local Class - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
8 of 8 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: a
Explanation: If a class is defined inside another class, the
inner class is termed as nested class. The inner class is
local to the enclosing class. Scope matters a lot here.
1 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
b) Inheritance
c) Binding
d) Abstraction
View Answer
Answer: b
Explanation: Using inheritance we can have the security of
the class being inherited. The sub class can access the
members of parent class. And have more feature than a
nested class being used.
Answer: a
Explanation: The nested classes are divided into two main
categories. Namely, Static and non-static. The categories
define how the classes can be used inside another class.
Answer: d
Explanation: The non-static nested class can access all the
2 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: The static nested class doesn’t have access to
any other members of the enclosing class. This is a rule
that is made to ensure that only the data which can be
common to all the object is being accessed by the static
nested class.
Answer: d
Explanation: The nested class can be declared with any
specifier, unlike the outer classes which can only be
declared public or package private. This is a flexibility given
for the nested class being a member of enclosing class.
3 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The use of nested class increases
encapsulation as the inner class is getting even more
grouped into the enclosing class. Firstly the class
encapsulate the data, having nested classes can increase
the encapsulation even further.
Answer: c
Explanation: The use of nested classes make the code
more streamed towards a single concept. This allows to
group the most similar and related classes together and
makes it even more efficient and readable.
4 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
d) OuterClass[StaticNestedClass].
View Answer
Answer: a
Explanation: Like any other member of the class, the static
nested class uses the dot operator to be accessed. The
reason behind is, the static classes can’t work with
instances, hence we use enclosing class name to access
static nested class.
Answer: b
Explanation: The nested classes are associated with the
object of the enclosing class. Hence have direct access to
the members of that object. Hence the inner class can’t
have any static members of its own. Otherwise the rule of
static members would be violated using enclosing class
instance.
Answer: a
Explanation: An instance of inner class can exist only within
5 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
System.out.println(“value of x = ” + x);
System.out.println(“value of this.x = ” +
this.x);
System.out.println(“value of Test.this.x = ”
+ Test.T=this.x);
}
}
}
public static void main( String args[] )
{
Test t=new Test();
Test.innerClass im=t.new
innerClass();
6 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
im.innermethod(55);
}
a) value of x = 55
value of this.x = 0
value of Test.this.x = 1
b) value of x = 1
value of this.x = 0
value of Test.this.x = 55
c) value of x = 55
value of this.x = 1
value of Test.this.x = 0
d) value of x = 0
value of this.x = 55
value of Test.this.x = 1
View Answer
Answer: c
Explanation: The variable x denotes the parameter of the
function. And this.x is the variable of the inner class.
Test.this.x is the variable of the outer class. Hence we get
this output.
Answer: a
7 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The inner class will have more preference for
its local members than those of the enclosing members.
Hence it will shadow the enclosing class members. This
process is known as shadowing.
Answer: a
Explanation: Top level class encloses the other classes or
have same preference as that of other top level classes.
Having a class inside the top level class is indirectly having
8 of 9 3/19/18, 9:34 AM
Nested Class - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
9 of 9 3/19/18, 9:34 AM