0% found this document useful (0 votes)
14K views

Questions On Memory Allocation & Scope of Variable

The document discusses the new operator in C++ object oriented programming. It provides multiple choice questions and answers about new. Key points: - The new operator allocates memory for an object or array and returns a pointer. It can fail by throwing an exception or returning zero. - A custom exception handler can be set using _set_new_handler. Constructors are called after memory allocation. - New can allocate pointers but not references, functions, or arrays with initializers. Allocated objects are not automatically destroyed and must be freed manually. - New implicitly invokes the operator new function for memory allocation.

Uploaded by

kibrom atsbha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14K views

Questions On Memory Allocation & Scope of Variable

The document discusses the new operator in C++ object oriented programming. It provides multiple choice questions and answers about new. Key points: - The new operator allocates memory for an object or array and returns a pointer. It can fail by throwing an exception or returning zero. - A custom exception handler can be set using _set_new_handler. Constructors are called after memory allocation. - New can allocate pointers but not references, functions, or arrays with initializers. Allocated objects are not automatically destroyed and must be freed manually. - New implicitly invokes the operator new function for memory allocation.

Uploaded by

kibrom atsbha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...

sanfoundry.com

New Operator - Object Oriented


Programming Questions and
Answers
by Manish
7-9 minutes

This set of Object Oriented Programming (OOPs) Multiple


Choice Questions & Answers (MCQs) focuses on “New
Operator”.

1. What is new operator?


a) Allocates memory for an object or array
b) Allocates memory for an object or array and returns a
particular pointer
c) Used as return type when an object is created
d) Used to declare any new thing in a program
View Answer

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.

2. Microsoft C++ Components extensions support new key


word to _____________
a) Modify a vtable

1 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...

b) Replace a vtable slot entry


c) Add new vtable slot entries
d) Rearrange vtable slot entries
Answer: c
View Answer

Explanation: The new keyword is used for adding new


vtable slot entries. This is an additional feature in Microsoft
C++. It can use predefined class object for this work.

3. What happens when new fails?


a) Returns zero always
b) Throws an exception always
c) Either throws an exception or returns zero
d) Terminates the program
View Answer

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.

4. If new throws an error, which function can be called to


write a custom exception handler?
a) _set_handler
b) _new_handler
c) _handler_setter
d) _set_new_handler
View Answer

Answer: d

2 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...

Explanation: If the default exception handler have to be


replaced by a user defined handler, we can call
_set_new_handler run-time library function with the function
name as an argument. This lets the programmer to give a
custom definition for handling new operator failure.

5. In C++, if new operator is used, when is the constructor


called?
a) Before the allocation of memory
b) After the allocation of memory
c) Constructor is called to allocate memory
d) Depends on code
View Answer

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.

6. Which among the following is correct syntax to declare a


2D array using new operator?
a) char (*pchar)[10] = new char[][10];
b) char (pchar) = new char[][10];
c) char (*char) = new char[10][];
d) char (*char)[][10]= new char;
View Answer

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.

7. For declaring data by using new operator


____________________
a) Type name can’t contain const
b) Type name can’t contain volatile
c) Type name can’t contain class declarations
d) Type name can’t contain const, volatile, class declaration
or enumerations
View Answer

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.

8. The new operator _____________


a) Can allocate reference types too
b) Doesn’t allocate reference types
c) Can allocate reference to objects
d) Doesn’t allocate any data
View Answer

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.

9. Which among the following is true?


a) New operator can’t allocate functions but pointer to

4 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...

functions can be allocated


b) New operator can allocate functions as well as pointer to
functions
c) New operator can allocate any type of functions
d) New operator is not applicable with functions allocation
View Answer

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.

10. Which among the following is added in grammar of new


operator?
a) Finalize
b) Arg
c) Initializer
d) Allocator
View Answer

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.

11. Initializers __________________


a) Are used for specifying arrays
b) Are used to defined multidimensional arrays
c) Can’t be specified for arrays

5 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...

d) Can’t be specified for any data


View Answer

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.

12. The objects allocated using new operator


________________
a) Are destroyed when they go out of scope
b) Are not destroyed even if they go out of scope
c) Are destroyed anytime
d) Are not destroyed throughout the program execution
View Answer

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.

13. The new operator _________________


a) Invokes function operator new
b) Doesn’t invoke function operator new
c) Invokes function operator only if required
d) Can’t invoke function operator new implicitly
View Answer

Answer: a

6 of 8 3/19/18, 10:25 AM
New Operator - Object Oriented Programming ... about:reader?url=https://www.sanfoundry.com/...

Explanation: The new operator invokes function operator


new. This is done to allocate the storage to an object.
::operator new is called for storage allocation impicitly.

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.

15. How does compiler convert “::operator new” implicitly?


a) ::operator new( sizeof( type ) )
b) ::operator new( sizeof( ) )
c) new operator :: type sizeof( type )
d) new sizeof( type ) operator
View Answer

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/...

Sanfoundry Global Education & Learning Series –


Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming


(OOPs), here is complete set of 1000+ Multiple Choice
Questions and Answers.

8 of 8 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...

sanfoundry.com

Delete Operator - Object Oriented


Programming Questions and
Answers
by Manish
6-8 minutes

This set of Object Oriented Programming (OOPs) Multiple


Choice Questions & Answers (MCQs) focuses on “Delete
Operator”.

1. What is delete operator?


a) Deallocates a block of memory
b) Deallocates whole program memory
c) Deallocates only primitive data memory
d) Deallocates all the data reserved for a class
View Answer

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.

2. If an object is allocated using new operator


____________

1 of 7 3/19/18, 10:25 AM
Delete Operator - Object Oriented Programmin... about:reader?url=https://www.sanfoundry.com/...

a) It should be deleted using delete operator


b) It can’t be deleted using delete operator
c) It may or may not be deleted using delete operator
d) The delete operator is not applicable
View Answer

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.

3. Does delete return any value?


a) Yes, positive value
b) Yes, negative value
c) Yes, zero value
d) No
View Answer

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.

4. Which type of value is resulted from the delete operator?


a) void
b) void pointer
c) null pointer
d) null
View Answer

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/...

values returned is of no use to the program or any other


system function hence the return type is not defined for the
delete operator.

5. If delete is used to delete an object which was not


allocated using new _______________
a) Then out of memory error arises
b) Then unreachable code error arises
c) Then unpredictable errors may arise
d) Then undefined variable error arises
View Answer

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.

6. Delete operator _________________


a) Can be used on pointers with null value
b) Can be used on pointers with void value
c) Can be used on pointer with value 0
d) Can be used on pointer with any value
View Answer

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/...

7. When delete operator is used ___________________ (If


object has a destructor)
a) Object destructor is called after deallocation
b) Object destructor is called before deallocation
c) Object destructor is not used
d) Object destructor can be called anytime during
destruction
View Answer

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.

8. If delete is applied to an object whose l-value is


modifiable, then _______________ after the object is
deleted.
a) Its value is defined as null
b) Its value is defined as void
c) Its value is defined as 0
d) Its value is undefined
View Answer

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.

9. How many variants of delete operator are available?


a) Only 1

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.

10. Which is the correct syntax to delete a single object?


a) delete *objectName;
b) objectName delete;
c) delete objectName;
d) objectName *delete;
View Answer

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.

11. Which is the correct syntax to delete array of objects?


a) delete [] objectName;
b) delete * objectName;
c) objectName[] delete;
d) delete objectName[];
View Answer

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/...

12. Which cases among the following produces the


undefined result?
a) delete [] on an independent object
b) delete on an object array
c) delete [] on an object and delete on object array
d) Undefined result is never produced
View Answer

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.

13. The delete operator __________________


a) Invokes function operator delete
b) Invokes function defined by user to delete
c) Invokes function defined in global scope to delete object
d) Doesn’t invoke any function
View Answer

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.

14. For objects that are not of class type ______________


a) Global delete operator is invoked
b) Local delete operator is invoked
c) Global user defined function is invoked
d) Local function to delete object is called

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.

15. The delete operator __________________________


a) Can be defined for each class
b) Can’t be defined for each class
c) Can be defined globally only
d) Can’t be defined in a program explicitly
View Answer

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.

Sanfoundry Global Education & Learning Series –


Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming


(OOPs), here is complete set of 1000+ Multiple Choice
Questions and Answers.

7 of 7 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...

sanfoundry.com

Automatic Variable - Object


Oriented Programming Questions
and Answers
by Manish
7-9 minutes

This set of Object Oriented Programming (OOPs) Multiple


Choice Questions & Answers (MCQs) focuses on
“Automatic Variable”.

1. What are automatic variables?


a) Global variables
b) Implicit/temporary variables
c) Local variables
d) System variables
View Answer

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.

2. The memory for automatic variables


___________________
a) Have to be allocated and deallocated explicitly

1 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...

b) Are allocated and deallocated automatically


c) Is never actually allocated
d) Are never safe
View Answer

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.

3. Scope of an automatic variable _______________


a) Is actually the whole program
b) Is actually never fixed
c) Is always equal to the whole program execution
d) Is actually function or block in which it is defined
View Answer

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.

4. Which among the following is true for automatic variables


in general?
a) Automatic variables are invisible to called function
b) Automatic variables are always visible to the called
function
c) Automatic variables can’t interact with the called function
d) Automatic variables can’t be variable
View Answer

Answer: a

2 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...

Explanation: The automatic variables are hidden from the


called function. Even if passed by reference or address, the
address of the variable is used and not the actual variable
of calling function. Automatic variables can be const or
variable.

5. If an automatic variable is created and then a function is


called then ________________
a) The automatic variable created gets destroyed
b) The automatic variable doesn’t get destroyed
c) The automatic variable may or may not get destroyed
d) The automatic variable can’t be used in this case
View Answer

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.

6. Where are the automatic variables stored if another


function is called in between the execution of program?
a) Heap
b) Queue
c) Stack
d) Temp variable
View Answer

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/...

function is called, the present data is saved in stack and


new entry in stack is made for the called function. When the
function returns, the automatic variables are used again
from where those were left.

7. The static variables of a function ________________


a) Are also automatic variables
b) Are not automatic variables
c) Are made automatic by default
d) Can be made automatic explicitly
View Answer

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.

8. All variables declared within a block


____________________
a) Are not always automatic
b) Can be made non-automatic
c) Are static by default
d) Are automatic by default
View Answer

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/...

9. What values does uninitialized automatic variables


contain?
a) Null value
b) Void value
c) Undefined/Garbage
d) Zero value
View Answer

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.

10. Constructor of automatic variables is called


____________________
a) When execution reaches the place of declaration of
automatic variables
b) When the program is compiled
c) When the execution is just started
d) Just before the execution of program
View Answer

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.

11. Does java contain auto or register keywords?

5 of 8 3/19/18, 10:25 AM
Automatic Variable - Object Oriented Programm... about:reader?url=https://www.sanfoundry.com/...

a) Yes, for declaring every type of variable


b) Yes, only to declare cache registers
c) No, because java doesn’t support automatic variables
d) No, java supports local variable concept
View Answer

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.

12. The automatic variables _________________________


a) Must be declared after its use
b) Must be declared before using
c) Must be declared, can be anytime
d) Must not be initialized
View Answer

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.

13. Which error is produced if the automatic variables are


used without declaration?
a) Undefined symbol
b) Memory error
c) Type mismatch
d) Statement missing
View Answer

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.

14. In perl, using which operator are the local variables


created?
a) Dot
b) Arrow
c) Scope resolution
d) my
View Answer

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.

15. How are automatic variables different from the instance


variables?
a) Automatic variables are initialized automatically but
instances are not
b) Automatic variables are given zero values initially and not
instances
c) Instance variables have to be initialized explicitly and
automatic implicitly
d) Instance variables are initialized implicitly while automatic
are not
View Answer

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.

Sanfoundry Global Education & Learning Series –


Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming


(OOPs), here is complete set of 1000+ Multiple Choice
Questions and Answers.

8 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...

sanfoundry.com

Object Oriented Programming


Questions for Campus Interviews
by Manish
7-9 minutes

This set of Object Oriented Programming Questions and


Answers for Campus interviews focuses on “Extern
Variable”.

1. What is extern variable?


a) Variables to be used that are declared in another object
file
b) Variables to be used that are declared in another source
file
c) Variables to be used that are declared in another
executable file
d) Variables to be used that are declared in another
program
View Answer

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/...

2. Which among the following is a correct statement for


variables?
a) Variable can be declared many times
b) Vairbale can be declared only one time
c) Variable declaration can’t be done more than ones
d) Variable declaration is always done more than one time
View Answer

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.

3. Which among the following is true for the variables?


a) Variable can be defined only once
b) Variable can be defined any number of times
c) Variable must be defined more than one time
d) Variable can be defined in different files
View Answer

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.

4. To use extern variable _____________________


a) The source file must not be included in the new file code
b) The source file itself must be used for new program
c) The source file must be included in the new file
d) The source file doesn’t matter for extern variables

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.

5. What does a header file contain for an extern variable?


a) Only declaration of variables
b) Only definiton of variables
c) Both declaration and definiton of variables
d) Neither declaration nor definition
View Answer

Answer: a
Explanation: The header file only contains the declaration of
variables that are extern. It doesn’t contain any static
variable definitions.

6. Which condition is true if extern variable is used in a file?


a) All the header files declare it
b) Only few required files declare it
c) All header files declared it if required
d) Only one header file should declare it
View Answer

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/...

7. Whenever a funcion is declared in a program


_____________________
a) extern can be used only in some special cases
b) extern can’t be used
c) function is extern by default
d) it cant be made extern
View Answer

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.

8. Even if a variable is not declared as extern, it is extern by


default. (True/False)
a) True
b) False
View Answer

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.

9. Due to what, is the memory allocated for the extern


variables?
a) Declaration
b) Definition
c) Including file

4 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...

d) Memory is not allocated for extern variables


View Answer

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.

10. Which is the correct syntax for extern variable


declaration?
a) extern data_type variable_name;
b) extern variable_name;
c) data_type variable_name extern;
d) extern (data_type)variable_name;
View Answer

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.

11. Which is the correct syntax for extern function


declaration?
a) extern function_name(argument_list);
b) extern return_type function_name(argument_list);
c) extern (return_type)function_name(argument_list);
d) return_type extern function_name(argument_list);
View Answer

Answer: b

5 of 8 3/19/18, 10:25 AM
Object Oriented Programming Questions for C... about:reader?url=https://www.sanfoundry.com/...

Explanation: The syntax must contain the keyword extern


first, to denote that the function is extern. Though the
function are extern by default but among the given choices,
it should contain the keyword, for explicit declaration. Then
the usual function declaration follows.

12. What will be the output of the program?

extern int var;


int main(void)
{
var = 10;
var++;
cout<<var;
}

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.

13. If the definition is given in the header file that we include


then ________________
a) The program can run successfully
b) Also the program should define the extern variable
c) The extern variable must contain two definitions
d) Extern variable can’t be used in the program

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.

14. If extern variable is initialized with the declaration then


_______________________
a) Also the header file with definition is required
b) The header file with definition must be included
c) There is no need to include any other header file for
definition
d) The extern variable produces compile time error
View Answer

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.

15. Why are functions extern by default?


a) Because functions are always private
b) Because those are not visible throughout the program
c) Because those can’t be accessed in all parts of program
d) Because those are visible throughout the program
View Answer

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/...

value that a function must contain. Hence the functions are


extern by default.

Sanfoundry Global Education & Learning Series –


Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming for


Campus Interviews, here is complete set of 1000+ Multiple
Choice Questions and Answers.

8 of 8 3/19/18, 10:25 AM

You might also like