Questions On Memory Allocation & Scope of Variable
Questions On Memory Allocation & Scope of Variable
sanfoundry.com
Answer: b
Explanation: The new keyword is used to allocate memory
of an object or array. The new object or array can be of any
type. Then it return a suitable non zero pointer to the object.
1 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: While creating new objects, the new operator
may fail because of memory errors or due to permissions.
At that moment the new operator returns zero or it may
throw an exception. The exception can be handled as
usual.
Answer: d
2 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The constructor function is called after the
allocation of memory. In C++ the feature works in a bit
different way. The memory for all the data members is
allocated first and then the constructor function is called to
finalize the memory allocated.
Answer: a
Explanation: The new operator usage to declare a 2D array
requires a pointer and size of array to be declared. Data
3 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...
type and then the pointer with size of array. The left index
can be left blank or any variable can be assigned to it.
Answer: d
Explanation: The declaration of any data where we use new
operator, any of the mentioned types are not allowed. This
is because the new operator allocated memory based on
the type of data which can be allocated dynamically.
Answer: b
Explanation: The new operator doesn’t allocate reference
types. This is because the reference types are not objects.
The new operator is used to allocate memory to the direct
objects.
4 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The new operator can’t allocate functions but
can allocate pointer to the functions. This is a security
feature as well as to reduce the ambiguity in code. The new
keyword is not given functionality to directly allocate any
function.
Answer: c
Explanation: The new operator grammar is added with an
initializer field. This can be used to initialize an object with a
user defined constructor. Hence can allocate memory as
intended by the programmer.
5 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The initializers can’t be specified for arrays.
The initializers can create arrays of object if and only if the
class has a default constructor. That is a zero argument
constructor so that it can be called without any argument.
Answer: b
Explanation: It is not necessary that the objects get
destroyed when they go out of scope if allocated by using
new operator. This is because new operator returns a
pointer to object that it had allocated. A suitable pointer with
proper scope should be defined by the programmer
explicitly.
Answer: a
6 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...
14. If new operator is defined for a class and still global new
operator have to be used, which operator should be used
with the keyword new?
a) Colon
b) Arrow
c) Dot
d) Scope resolution
View Answer
Answer: d
Explanation: As usual, scope resolution operator is used to
get the scope of parent or the global entities. Hence we can
use scope resolution operator with the new operator to call
the global new operator even if new operator is defined for
the class explicitly.
Answer: a
Explanation: The compiler implicitly converts the syntax so
that the instruction can be understood by the processor and
proper machine code can be generated. The conversion is
done implicitly and no explicit syntax is required.
7 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...
8 of 8 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: a
Explanation: The delete operator is reverse process of new
operator. It deallocates all the memory allocated for an
object. The object can be of any type. The delete operator
completely destroys an object so that the resources can be
used for other purposes.
1 of 7 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The new operator allocates an object in
memory and hence the memory allocation is bit different
from usual allocation of an object. The delete operator can
be used to delete the memory allocated for an object.
Answer: d
Explanation: The delete operator doesn’t return any value.
Its function is to delete the memory allocated for an object.
This is done in reverse way as that new operator works.
Answer: a
Explanation: The result of delete operator is void. The
2 of 7 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: When the delete operator is used with the
objects that were not allocated using new operator then
unpredictable errors may arise. This is because the delete
can’t perform the required actions on the type of memory
allocated for the object.
Answer: c
Explanation: The delete operator can be used on pointers
with the value 0. This actually means that when new
operator fails and return value 0 then deleting the result of
failed new remains harmless. Hence the deletion is
possible.
3 of 7 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The destructor is called before the memory is
deallocated for any object. The destructor call initiates the
destruction process and the deallocation of memory takes
place.
Answer: d
Explanation: After performing delete operation on an object
whole l-value is modifiable, its values becomes undefined.
This is done so as to denote that the memory space is
available to be used for other puposes.
4 of 7 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...
b) Only 2
c) Only 3
d) Only 4
View Answer
Answer: b
Explanation: There are two variants of delete operator. One
is for object deletion. Other is for deletion of object array.
Answer: c
Explanation: The object to be deleted is mentioned after the
keyword delete. This deletes the object from memory and
free up the memory that was acquired by the object.
Answer: a
Explanation: The object array that has to be deleted is
mentioned after the keyword delete. But after delete, empty
square brackets have to be given to denote that the
deletion have to be done on array of objects.
5 of 7 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The undefined result is always produced when
we try to use delete [] with a single object. Because the type
of deletion mismatches. Same in case where we try to apply
delete to an object array.
Answer: a
Explanation: The delete operator invokes the function
operator delete. This function in turn performs all the delete
operations on the mentioned object. This is ensures safe
deletion.
6 of 7 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: a
Explanation: The global delete operator is called to delete
the objects that are not of class type. Class type includes
class, union or struct. All objects of these types can be
deleted using the global delete operator.
Answer: a
Explanation: The delete operator can be defined for each
class explicitly. If there is a class for which delete is not
defined then the global delete operator is used. The
definition of delete operator for each class is not necessary.
7 of 7 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: c
Explanation: The local variables are also known as
automatic variables. The variables in any local scope that
are created and destroyed as the program executes its
scope.
1 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The memory is allocated and deallocated
automatically for the automatic variables. As soon as the
variable comes in scope, the memory is allocated. The
variables are destroyed as soon as those go out of scope.
Answer: d
Explanation: The automatic variables scope is limited only
within the block or the function where those are defined.
This is property of all the automatic variables.
Answer: a
2 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The automatic variables are saved till the
called function gets executed. This is done so as to ensure
that the program can continue its execution after the called
function is returned. The automatic variables gets destroyed
only if those go out of scope.
Answer: c
Explanation: All the automatic variables are stored in a new
stack entry as soon as their scope is created. If another
3 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The static members can’t be automatic. This is
because the automatic variables are created and destroyed
with each call to a specific function. But the static members
remain throughout the execution of program once created.
Answer: d
Explanation: The variables declared inside a block, are
make automatic by default. This is to ensure that the
variables get destroyed when not required. The variables
remain live only till those are required, the life is dependent
on the scope of variable.
4 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The automatic variable which are not
initialized, contain garbage value. If we just declare a
variable and try to print its value, the result is some
unknown value. The value is garbage as that was not
expected value.
Answer: a
Explanation: Only when the execution reaches the place
where the automatic variable was declared, the constructor
is called. This is to ensure that the memory is not allocated
if not needed. The memory is allocated and then destroyed
as soon as it goes out of scope.
5 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation The auto and register keywords are not
supported in java. Though the same is allowed in java
without specifying any of those keywords. The variables are
local variables. But java makes it mandatory to initialize all
of the local variables in a program.
Answer: b
Explanation: All the automatic variables in a program must
be declared before their use. The compiler won’t allow any
use of variable if those are not declared before their use.
6 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: If the automatic variables are used without
declaration or are used before the declaration then the
compiler throws an error. The error that the symbol is
undefined. The compiler must know everything before that
can be used.
Answer: d
Explanation: The language perl supports local variables but
the concept is bit different. And if the values are not
assigned to the local variables then it contains undef value.
7 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: The automatic variables have to be initialized
explicitly. But in case of instances, those are initialized
automatically during execution of program. The conventions
are mandatory.
8 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: b
Explanation: The variables that are declared in another
source file can be accessed in other files using extern
variables. The extern variables must be mentioned
explicitly. The source file is included to use its variables.
1 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The variables can be declared any number of
times. There is no restriction on how many times a single
variables can be declared. Declaration is just an indication
that the variable will be used in the program.
Answer: a
Explanatoin: The variables can be defined only once. Once
the variable is defined, then it can’t be declared again. The
definiton of a variable is actual allocation of memory for the
variable.
2 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: c
Explanation: The source file must be included in the file
which needs to use the extern variable. This is done to
ensure that the variables that are already declared can be
used again. Onlt the declarations are used from one file to
another.
Answer: a
Explanation: The header file only contains the declaration of
variables that are extern. It doesn’t contain any static
variable definitions.
Answer: d
Explanation: Only one header file should declare the extern
variable to be used. There must not be more than one file
declaring the same extern variable. This is to ensure that
there is no ambiguity in using the extern variable.
3 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: Even if we don’t specify a function to be
extern, by default all the functions are exter. The compiler
adds the keyword at the beginning of the function
declaration. If there is an extern function to be used then it
will be used otherwise the new function only will be used.
Answerr: b
Explanation: The statement is false. The variables are not
extern by defalut. If those are made extern by default, then
the memory will never be allocated for those extern
variables. Hence we make the variables extern explicitly.
4 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The memory for the extern variables are
allocaed due to their definition. When the variables are
declared, it only indicates the compiler that the variable is
going to be used somewhere. But definition makes the
compiler to allocate the memory for the variables.
Answer: a
Explanation: The syntax firstly contains the keyword extern.
Then the data type of the variable is given. Then the
variabel name is mentioned by which it will be used in the
program.
Answer: b
5 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
a) 10
b) 11
c) Run time error
d) Compile time error
View Answer
Answer: d
Explanation: The program gives the compiler time error.
There is no definiton given for the extern variables. This is
not allowed and hence we get a compile time error.
6 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: a
Explanation: The program runs successfully. This is
because only one definition of any variable is allowed. And
hence the definition from the source file that is inlcuded will
be used.
Answer: c
Explanation: When the value for the extern variable is
defined with its declaration, then there is no need to include
any file for the definition of the variable. The Initialization
acts as a definiton for the extern variable in the file itself.
Answer: a
Explanation: The program have all of its functions visible
throughout the program usually. Also there is no specific
7 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...
8 of 8 3/19/18, 10:25 AM