Object Oriented MCQ Set 1
1. If a function has to be called only by using other member functions of the class, what should be the access specifier used for that function?
a) Private
b) Protected
c) Public
d) Default
Answer
Answer: a [Reason:] 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.
2. Which among the following is correct for the code given below?
class student { private: student() { } public : student( int x) { marks =x; } };
a) The object can never be created
b) The object can be created without parameters
c) Only the object with only 1 parameter can be created
d) Only the object with some parameters can be created
Answer
Answer: c [Reason:] 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.
3. Which among the following is true for the code given below?
class A { private : int marks; char name[20]; public : A(int x=100) { marks=x; } };
a) Objects can be created with one parameter or without parameter
b) Object can be created only with one parameter
c) Object can be created with more than one parameter
d) Objects can be create only without parameter
Answer
Answer: a [Reason:] 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.
4. Which among the following is correct to call a private member from outside the class?
a) object.memberfunction( parameters );
b) object->memberfunction( parameters );
c) object->memberfunction( parameteres); or object.memberfunction( parameters );
d) Not possible
Answer
Answer: d [Reason:] 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 address of the member, may be you can access those members, but that is totally different case and concept.
5. If private members has to be accessed directly from outside the class but the access specifier must not be changed, what should be done?
a) Specifier must be changed
b) Friend function should be used
c) Other public members should be used
d) It is not possible
Answer
Answer: b [Reason:] 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.
6. Which access specifier is/are most secure during inheritance?
a) Private
b) Default
c) Protected
d) Private and default
Answer
Answer: a [Reason:] 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.
7. Choose the correct option for the code given below
class A{ static int c=0; public: A(){ c++; } };
a) Constructor will make c=1 for each object created
b) Constructor will make c=0 for each object created
c) Constructor will keep number of objects created
d) Constructor will just initialize c=0 then increment by 1
Answer
Answer: c [Reason:] 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.
8. Which option is false for the following code?
class A { private : int sum(int x, int y) { return x+y; } public: A() { } A(int x, int y) { cout<<sum(x,y); } };
a) Constructor can be created with zero argument
b) Constructor prints sum, if two parameters are passed with object creation
c) Constructor will give error if float values are passed
d) Constructor will take 0 as default value of parameters if not passed
Answer
Answer: d [Reason:] 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.
9. Which member will never be used from the following class?
class A() { int marks; char name[20]; public : A() { marks=100; } void disp() { cout<<”Marks= ”<'<marks; cout<<”Student”; } };
a) name variable will never be used
b) marks variable will never be used
c) constructor will never be used
d) disp() function will never be used
Answer
Answer: a [Reason:] 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.
10. Private member functions can be overloaded.
a) True
b) False
Answer
Answer: a [Reason:] 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.
11. Which among the following is true?
a) Private member functions can’t be overloaded
b) Private member functions can be overridden
c) Private member functions can’t be overloaded with a public member
d) Private member function can’t be overridden
Answer
Answer: d [Reason:] The private member functions can be overloaded but they can’t be overridden. This is because, overriding means a function with same name in derived class, gets more priority when called from object of derived class. Here, the member function is private so there is no way that it can be overridden.
12. Which data member in following code will be used whenever an object is created?
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
Answer
Answer: c [Reason:] 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.
13. Which member can be considered most secure in the code below?
class A() { int a; private : int b; protected : int c; public : int d; };
a) a
b) b
c) c
d) d
Answer
Answer: b [Reason:] 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) { } }; A a; A b(100);
a) Program will give compile time error
b) Program will run fine
c) Program will give runtime error
d) Program will give logical error
Answer
Answer: a [Reason:] 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.
15. Which among the following is correct?
a) Private specifier must be used before public specifier
b) Private specifier must be used before protected specifier
c) Private specifier must be used first
d) Private specifier can be used anywhere in class
Answer
Answer: d [Reason:] 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.
Object Oriented MCQ Set 2
1. Which among the following is true for the code given below:
class A { int marks; public : disp() { cout<<marks; } } class B: protected A { char name[20]; } A a; a.disp(); B b; b.disp();
a) Only object of class A can access disp() function
b) Only object of class B can access disp() function
c) Both instances can access disp() function
d) Accessing disp() outside class is not possible
Answer
Answer: a [Reason:] 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.
2. If the members have to be accessed from anywhere in program and other packages also, which access specifier should be used?
a) Public
b) Private
c) Protected
d) Default
Answer
Answer: a [Reason:] 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.
3. Which among the following have least security according to the access permissions allowed?
a) Private
b) Default
c) Protected
d) Public
Answer
Answer: d [Reason:] 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.
4. Which among the following can be used for outer most class access specifier in java?
a) Private
b) Public
c) Default
d) Default or Public
Answer
Answer: d [Reason:] 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.
5. We can reduce the visibility of inherited methods.
a) True
b) False
Answer
Answer: b [Reason:] 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.
6. If members of a super class are public, then________
a) All those will be available in subclasses
b) None of those will be available in subclasses
c) Only data members will be available in subclass
d) Only member functions will be available in subclass
Answer
Answer: a [Reason:] 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.
7. How many public class(s) (outermost) can be there in a java program?
a) 1
b) 2
c) 3
d) As required
Answer
Answer: a [Reason:] There can be only one public class in a java program. The public class name must match the name of 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.
8. What is output of following code?
package pack1; class A { public A() { System.out.print(“object created”); } } package pack2; import pack1.*; class B { A a=new A(); }
a) Output is: object created
b) Output is: object createdobject created
c) Compile time error
d) Run time error
Answer
Answer: c [Reason:] The program will give compile time error. Class A is defined with default access specifier. This directly means that class A will be available within package only. Even if the constructor is public, the object will not be created.
9. Which among the following for public specifier is false?
a) The static members can’t be public
b) The public members are available in other packages tooo
c) The subclasses can inherit the public members privately
d) There can be only one public class in java program
Answer
Answer: a [Reason:] 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.
10. A class has its default constructor defined as public. Class B inherits class A privately. The class:
a) B will not be able to have instances
b) Only A can have instances
c) Only B can have instances
d) Both classes can have instances
Answer
Answer: d [Reason:] Class A can have instances as it has public default constructor. Class will have its own constructors defined. Hence both classes can have instances.
11. Which specifier can be used to inherit protected members as protected in sub class but public as public in subclass?
a) Private
b) Default
c) Public
d) Protected
Answer
Answer: c [Reason:] 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.
12. Which among the following is true for public class?
a) There can be more than one public class in a single program
b) Public class members can be used without using instance of class
c) Public class is available only within the package
d) Public classes can be accessed from any other class using instance
Answer
Answer: d [Reason:] 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.
13. If a class doesn’t have public members, then________
a) None of its members will be able to get inherited
b) None of its instance creation will be allowed
c) None of its member function can be called outside the class
d) None of its data members will be able to get initial value
Answer
Answer: c [Reason:] 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.
14. In multi-level inheritance(all public) , the public members of parent/superclass will ________
a) Will continue to get inherited subsequently
b) Will not be inherited after one subclass inheritance
c) Will not be available to be called outside class
d) Will not be able to allocated with any memory space
Answer
Answer: a [Reason:] 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.
15. Which specifier allows to secure the public members of base class in inherited classes?
a) Private
b) Protected
c) Public
d) Private and Protected
Answer
Answer: d [Reason:] Both the private and protected specifiers can make the public members of base class more secure. This is useful if we stop using the parent class members and use the classes which inherited the parent class, so as to secure data better.
Object Oriented MCQ Set 3
1. What are public member functions?
a) Functions accessible outside the class but not in derived class
b) Functions accessible outside the class directly
c) Functions accessible everywhere using object of class
d) Functions that can’t be accessed outside the class
Answer
Answer: c [Reason:] The most suitable definition would be that pubic member functions are accessible everywhere using object of the class. If derived classes are using those, derived class object can be used to call those functions.
2. Which among the following is true for public member functions?
a) Public member functions doesn’t have a return type
b) Public member functions doesn’t have any security
c) Public member functions are declared outside the class
d) Public member functions can be called using object of class
Answer
Answer: d [Reason:] The public member functions can be called using object of the class. The members can’t be declared outside the class as those would become non-member functions of the class. The functions have security as those can be accessed using the class object only.
3. Which type of member functions get inherited in the same specifier in which the inheritance is done? (If private inheritance is used, those become private and if public used, those become public)
a) Private member functions
b) Protected member functions
c) Public member functions
d) All member functions
Answer
Answer: c [Reason:] The public member functions gets into the same specifier in which the inheritance is done. If protected members are involved in public inheritance, still those remain protected in the derived class but public members become public on public inheritance and protected in protected inheritance.
4. Which syntax among the following is correct for public member functions?
a) public::void functionName(parameters)
b) public void functionName(parameters)
c) public(void functionName(parameters))
d) public:-void functionName(Parameters)
Answer
Answer: b [Reason:] The public member functions declaration must be mentioned with the keyword public. The syntax given is used in java. Keyword public is followed by the usual function declaration.
5. Which syntax is applicable to declare public member functions in C++?
a) public:
b) public(
c) public void
d) public::
Answer
Answer: a [Reason:] The syntax in C++ must contain the public keyword followed by a colon. Thereafter, all the public members can be declared. But in few other language, public have to be mentioned explicitly with each member.
6. In java, which rule among the following is applicable?
a) Keyword public can’t be preceded with all the public members
b) Keyword public must be preceded with all the public members
c) Keyword public must be post mentioned the function declaration
d) Keyword public is not mandatory
Answer
Answer: b [Reason:] The public members in java must be preceded with the keyword public. It must be mentioned with each public member, unlike C++ where we mention it only once. In java, each member must have explicit declaration of specifier type.
7. How many public members are allowed in a class?
a) Only 1
b) At most 7
c) Exactly 3
d) As many as required
Answer
Answer: d [Reason:] The number of public members that can be defined in a class doesn’t have any limit. Though the programmer should not use too many functions, instead should use another class for more specific functions to reduce the readability complexity.
8. Which is not a proper way to access public members of a class?
a) Using object pointer with arrow operator
b) Using object of class in main function
c) Using object of class with arrow operator
d) Using object anywhere in the program
Answer
Answer: c [Reason:] The public members can be accessed anywhere in the program using the object of the class. And if object pointer is used, then arrow operator is used to access class members. If normal object is used with arrow operator, an error will be generated.
9. Which call is correct for public members of a nested class?
a) Can be called from object of enclosing class
b) Can be called within enclosing class only with direct names
c) Direct names should be used for the nested classes
d) Only with help of nested class object pointer
Answer
Answer: a [Reason:] The closest definition is that any public member function of the nested class can be accessed with the help of enclosing class object. The nested class object pointer can be used only within the enclosing class. It’s not mandatory to use the members of nested class only within the enclosing class.
10. Which public function call among the following is correct outside the class, if return type is void (C++)?
a) object.void functionName(parameters);
b) object.functionName(parameters);
c) object.functionName void (parameters)
d) object.void functionName();
Answer
Answer: b [Reason:] The condition given says that there is no return type hence we can call the function directly. The object name should be mentioned with a dot operator to access its class members. Then the function name with parameters, if required, can be given.
11. If public members are to be restricted from getting inherited from the subclass of the class containing that function, which alternative is best?
a) Make the function private
b) Use private inheritance
c) Use public inheritance
d) Use protected inheritance
Answer
Answer: b [Reason:] If private inheritance is used then the class containing the function will be able to use the function with rules of whichever specifier is used. Then the derived class makes those function the private members of itself. This restricts the public members of parent class from further inheritance.
12. A derived class object can access the public members of the base class.
a) True
b) False
Answer
Answer: b [Reason:] The public members of the base class can be accessed from the derived class object only if public inheritance is used. If protected or private inheritance is used then those members become public/protected in derived class and hence won’t be able to be called from object of derived class.
13. If a class have a public member function and is called directly in the main function then _____
a) Undeclared function error will be produced
b) Out of memory error is given
c) Program gives warning only
d) Program shut down the computer
Answer
Answer: a [Reason:] If the function is called directly without using any object then the compiler doesn’t gets to know that the function have to be called from a specific class. And if there are no global or in-scope function with same name then the compiler produces an error stating that the called function is undeclared.
14. The function main() must always be public.
a) True
b) False
Answer
Answer: a [Reason:] The main() function must always be public. This is because the whole function and the operating system that is out of the package have to access the main function throughout the program execution. Hence the main() function should be public so as to be available everywhere in the program.
15. All the public member functions ___
a) Can’t access the private members of a class
b) Can’t access the protected members of a class
c) Can access only public members of a class
d) Can access all the member of its class
Answer
Answer: d [Reason:] The public member function can access any private, protected and public member of its class. Not only public member function, any member function of a class can access each and every other member declared inside the class. Hence are flexible to program.
Object Oriented MCQ Set 4
1. What are constant member functions?
a) Functions which doesn’t change value of calling object
b) Functions which doesn’t change value of any object inside definition
c) Functions which doesn’t allow modification of any object of class
d) Functions which doesn’t allow modification of argument objects
Answer
Answer: a [Reason:] The constant member functions are special type of member functions. These are intended to restrict any modification in to the values of object which is used to invoke that function. This is done to ensure that there are no accidental modifications to the object.
2. Which keyword must be used to declare a member function as a constant member function?
a) Constant
b) Const
c) FunctionConst
d) Unchanged
Answer
Answer: b [Reason:] The keyword const is provided in most of the programming languages. This indicates that the member on which it is specified, remains constant with the respective values of members. The keyword must be mentioned so as to declare a member function to be constant.
3. Which objects can call the const functions?
a) Only const objects
b) Only non-const objects
c) Both const and non-const objects
d) Neither const not non-const objects
Answer
Answer: c [Reason:] All the objects of a class can call const functions for its use. Const objects can call the const functions to since those values are already constant. And the non- const objects can call the const functions to keep their values constant.
4. Non-const functions _______
a) Can be called only from non-const object
b) Can be called only from const object
c) Can be called both by const and non-const object
d) Can’t be called with object
Answer
Answer: a [Reason:] The non-const functions are able to modify the values of object which called the function. So only the non-const functions can be called. If const object is used then the compiler produces an error as the const object is being given to a function which can modify its values.
5. Which is a correct condition on const member functions?
a) Const member functions can’t call non-const member functions
b) Const member functions can’t call any other function
c) Const member functions can call only the functions which are neither const nor non-const
d) Const member functions can call only data members of call not member functions
Answer
Answer: a [Reason:] The const member functions are restricted to call any other non-const member functions. This is to ensure that the const function doesn’t have any code that might modify the calling object.
6. If a const object calls a non-const member function then ____
a) Run time error may get produced
b) Compile time error may get produced
c) Either compile time or run time error is produced
d) The program can’t be compiled
Answer
Answer: b [Reason:] The program gets compiled but produces an error. The error is produced because a constant value is being changed. Even if there is no code that can change any object value, but non-const member functions are assumed to change the values.
7. Can a constructor function be constant?
a) Yes, always
b) Yes, only if permissions are given
c) No, because objects are not involved
d) No, never
Answer
Answer: d [Reason:] The constructors can’t be made const. This is to ensure that the constructor is capable of initializing the values to the members of the object. If it is made constant then it won’t be able to initialize any data member values.
8. A function can have both the const and non-const version in the same program.
a) True
b) False
Answer
Answer: a [Reason:] The functions in a program can be made both const and non-const. This feature is made available to make programming more flexible. This ensures the security too as we can call const function whenever required.
9. How is it possible to have both const and non-const version of a function?
a) Function overriding
b) Function prototyping
c) Function overloading
d) Function declaring
Answer
Answer: c [Reason:] The functions can be declared const and non-const in the same program. The technique used is function overloading. We can define a const function and then a non-const version of same function using overloading.
10. When both the const and non-const version of a functions are required?
a) Return value have to be different in const
b) Return value have to be same in const
c) Return values have to be ignored
d) Return values have to be suppressed
Answer
Answer: a [Reason:] The return values can help to overload the functions. Also this will allow us to use a non-const function to be called inside both the const and non-const version of functions.
11. If a function is to be made const, which is the correct syntax?
a) const functionName(parameters);
b) const returnType functionName(parameters);
c) const functionName(returnType)(Parameters);
d) const (functionName(parameters));
Answer
Answer: b [Reason:] The function declaration must contain the keyword const. The const keyword makes the function const type. The usual function declaration can be given followed by the keyword. The keyword const can be given after the declaration of function and before definition.
12. Functions which differ in const-ness are considered ______
a) To have same signature
b) To have different signature
c) To produce compile time error
d) To produce runtime error
Answer
Answer: b [Reason:] The functions are considered to have different signature. This is because the const-ness also defines the type of function or the working of functions. And hence the functions can be considered different. This is the reason that we can use function overloading for const and non-const version of same function.
13. If const version of a function when overloading is used, the function ___
a) Returns reference to object
b) Returns volatile reference
c) Returns mutable reference
d) Returns const reference
Answer
Answer: d [Reason:] The function returns a const reference. This is to ensure that the value of object calling the function is not modified. This is a security feature.
14. Which among the following is recommended for const functions?
a) Const function use should be reduced in a program
b) Const function use should be more in a program
c) Const function use should not matter in a program
d) Const function use should be able to modify the values
Answer
Answer: b [Reason:] The const member functions should be used more in a program. The reason behind is to ensure there is no accidental modification of data of object. Also to ensure any unintended modification which may result in unexpected termination of program.
15. Use of const member function in a program ___
a) Is mandatory, always
b) Is optional, always
c) Is mandatory, if objects are used
d) Is optional, if const objects are used
Answer
Answer: b [Reason:] The use of const member functions is not mandatory. If const objects are involved then there is a high use of const member functions too. But there is no mandatory condition.
Object Oriented MCQ Set 5
1. In which of the following way(s) can the object be returned from a function?
a) Can only be returned by value
b) Can only be returned by reference
c) Can be returned either by value or reference
d) Can neither be returned by value nor by reference
Answer
Answer: c [Reason:] The objects can be returned either by value or reference. There is no mandatory condition for the way it should be used. The way of returning object can be decided based on the requirement.
2. Whenever an object is returned by value ____
a) A temporary object is created
b) Temporary object is not created
c) Temporary object may or may not be created
d) New permanent object is created
Answer
Answer: a [Reason:] A temporary object is created when an object is returned by value. The temporary object is used to copy the values to another object or to be used in some way. The object holds all the values of the data members of the object.
3. Where the temporary objects (created while return by value) are created?
a) Outside the function scope
b) Within the function
c) Inside the main function
d) Inside the calling function
Answer
Answer: b [Reason:] The temporary object are created within the function and are intended to return the value for specific use. Either the object can be assigned to another object or be used directly if possible.
4. Which is the correct syntax for returning an object by value?
a) void functionName ( ){ }
b) object functionName( ) { }
c) class object functionName( ) { }
d) ClassName functionName ( ){ }
Answer
Answer: d [Reason:] The class name itself should be the return type of the function. This notifies that the function will return an object of specified class type. Only the class name should be specified.
5. Which is the correct syntax for defining a function which passes an object by reference?
a) className& functionName ( )
b) className* functionName( )
c) className-> functionName( )
d) &className functionName()
Answer
Answer: a [Reason:] The function declaration must contain the class name as return type. But, a & symbol should be followed by the class name. & indicates that the object being returned will be returned by reference.
6. If an object is declared inside the function then ____ outside the function
a) It can be returned by reference
b) It can’t be returned by reference
c) It can be returned by address
d) It can’t be returned at all
Answer
Answer: b [Reason:] The object which is declared inside the class can never be returned by reference. This is because the object will be destroyed as it goes out of scope when the function is returned. The local variables get destroyed when function is returned hence the local objects can’t be returned by reference.
7. How many independent objects can be returned at same time from a function?
a) 1
b) 2
c) 3
d) 4
Answer
Answer: a [Reason:] Only one object can be returned at a time. This is because a function is only capable of returning a single value at a time. Though array of objects can be returned from a function.
8. Which error will be produced if a local object is returned by reference outside a function?
a) Out of memory error
b) Run time error
c) Compiler time error
d) No error
Answer
Answer: c [Reason:] If the local object is returned outside the function then the compile time error arises. While the program is being converted and the processes happening during compile time, the compiler won’t be able to resolve the statement.
9. If object is passed by reference ____
a) Temporary object is created
b) Temporary object is created inside the function
c) Temporary object is created for few seconds
d) Temporary object is not created
Answer
Answer: d [Reason:] The temporary object is not created. If object is returned by reference, a particular memory location will be denoted with another name and hence same address values will be used.
10. Which among the following is correct?
a) Individual data members can’t be returned
b) Individual data members can be returned
c) Individual member functions can be returned from another function
d) Individual data members can only be passed by reference
Answer
Answer: b [Reason:] It is not mandatory to return the whole object. Instead we can return a specific data member value. But the return type given must match with the data type of the data being returned.
11. Can we return an array of objects?
a) Yes, always
b) Ye, only if objects are having same values
c) No, because objects contain many other values
d) No, because objects are single entity
Answer
Answer: a [Reason:] The Object array can be returned from a function. This can be done by putting a className* as the return type of the function. This makes the return type to accept an array of objects in return.
12. If an object is passed by reference to a function then it must be returned by reference.
a) True
b) False
Answer
Answer: b [Reason:] It is not compulsory to return the object in the same way as it was passed. If the object is passed by reference then there is actually no need to return the object. Because the changes made inside the function will be visible on the original object of caller function also.
13. Which among the following is true?
a) Two objects can point to the same memory location
b) Two objects can never point to the same memory location
c) Objects not allowed to point at a location already occupied
d) Objects can’t point to any address
Answer
Answer: a [Reason:] When an object is created and instead of calling a constructor, another object is assigned to it. Both the objects point to the same memory location. This can be illustrated with help of return by reference.
14. If an object is being returned by value then ____
a) Its member values are made constant
b) Its member values have to be copied individually
c) Its member values are not used
d) Its member values are copied using copy constructor
Answer
Answer: d [Reason:] When an object is returned by value, it will be returned to another object or will be directly used. Still in all the conditions the copy constructor will be used to copy all the values from the temporary object that gets created.
15. Why temporary object is not created in return by reference?
a) Because compiler can’t create temporary objects
b) Because the temporary object is created within the function
c) Because return by reference just make the objects points to values memory location
d) Because return by reference just make the object point to null
Answer
Answer: c [Reason:] A reference to the memory location where the returned object is stored is made. This allows the new object which takes the return value, point to the memory location and hence access the same values.