Object Oriented MCQ Number 01269

Object Oriented MCQ Set 1

1. Which among the following defines single level inheritance?
a) One base class derives another class
b) One derived class inherits from one base class
c) One base class inherits from one derived class
d) One derived class derives from another derived class

Answer

Answer: b [Reason:] If only one base class is used to derive only one subclass, it is known as single level inheritance. The reason of this name is that we inherit the base class to one more level and stop the inheritance any further.

2. If class A and class B are derived from class C and class D, then ____
a) Those are 2 pairs of single inheritance
b) That is multilevel inheritance
c) Those is enclosing class
d) Those are all independent classes

Answer

Answer: a [Reason:] Since class A is derived from class C and then class B is derived from class D, there are two pairs of classes which shows single inheritance. Those two pairs are independent of each other though.

3. If single inheritance is used, program will contain ____
a) At least 2 classes
b) At most 2 classes
c) Exactly 2 classes
d) At most 4 classes

Answer

Answer: a [Reason:] The program will contain at least 2 classes in the sense of base and derived classes. At least one base class and one derived class must be there. Types of inheritance remains the same though.

4. Single level inheritance supports _______ inheritance.
a) Run time
b) Compile time
c) Multiple inheritance
d) Language independency

Answer

Answer: a [Reason:] The run time inheritance is done when object of a class is created to call a method. At run time the function is searched if it is in class of object. If not, it will search in its parent classes and hierarchy for that method.

5. Which method in the code below is single level inherited?

class A
{
protected int a, b;
public: void show()
{ 
cout<<a<<b;
}
};
class B: public A
{
public: void disp()
{ 
cout<<a++<<b++; 
}
};
class C: private A, public B
{
void avg()
{ 
cout<<(a+b)/2; 
}
};

a) Class A
b) Class B
c) Class C
d) None

Answer

Answer: b [Reason:] Class B is using single level inheritance. Class C is using multiple inheritance. And class A is parent of other two classes.

6. If single level inheritance is used and an abstract class is created with some undefined functions, can its derived class also skip some definitions?
a) Yes, always possible
b) Yes, possible if only one undefined function
c) No, at least 2 undefined functions must be there
d) No, the derived class must implement those methods

Answer

Answer: d [Reason:] The derived class must implement those methods. This is because the parent class is abstract and hence will have some undefined functions which has to be defined in derived classes. Since we are using single level inheritance, if derived class doesn’t implement those functions then one more class has to be there which will become multi-level inheritance.

7. Which among the following is false for single level inheritance?
a) There can be more than 2 classes in program to implement single inheritance
b) There can be exactly 2 classes to implement single inheritance in a program
c) There can be more than 2 independent classes involved in single inheritance
d) The derived class must implement all the abstract method if single inheritance is used

Answer

Answer: c [Reason:] If more than 2 independent classes are involved to implement the single level inheritance, it won’t be possible as there must be only one child and one parent class and none other related class.

8. Which concept will result in derived class with more features (consider maximum 3 classes)?
a) Single inheritance
b) Multiple inheritance
c) Multilevel inheritance
d) Hierarchical inheritance

Answer

Answer: b [Reason:] If single inheritance is used then only feature of a single class are inherited, and if multilevel inheritance is used, the 2nd class might have use private inheritance. Hence only multiple inheritance can result in derived class with more features. This is not mandatory but in a case if we consider same number of features in each class, it will result the same.

9. Single level inheritance is safer than _______
a) Multiple inheritance
b) Interfaces
c) Implementations
d) Extensions

Answer

Answer: a [Reason:] Interfaces also represent a way of inheritance but is a wide word to decide which inheritance we ate talking about in it, hence can’t be considered. Implementation and extensions also doesn’t match that level of specific idea. And multiple inheritance not so safe as it might result in some ambiguity.

10. Which language doesn’t support single level inheritance?
a) Java
b) C++
c) Kotlin
d) All languages support it

Answer

Answer: d [Reason:] All the languages support single level inheritance. Since any class can inherit other classes as required, if single level inheritance was not allowed it would result in failing a lot of features of OOP.

11. Output of following program?

class A
{
protected: int a,b;
public: void disp()
{ 
cout<<a<<b; 
}
};
class B:public A
{
int x,y;
};

a) Garbage value
b) Compile time error
c) Run time error
d) Runs but gives random values as output

Answer

Answer: b [Reason:] The compiler doesn’t find the main function and hence will throw an error main() missing. This program is using single level inheritance but the program is incomplete. Every program must implement main function.

12. Output of following program?

class A
{  
float sal=40000;  
}  
class B extends A
{  
int salBonus=10000;  
public static void main(String args[])
{  
		B p=new B();  
		System.out.println("B salary is:"+p.sal);  
		System.out.println("Bonus of B is:"+p.bonus);  
}  
}

a) B salary is: 4000.0
Bonus of B is: 10000
b) B salary is 10000
Bonus of B is: 4000.0
c) Compile time error
d) Run time error

Answer

Answer: a [Reason:] The program gives output as in option a. The program have used single level inheritance and hence have access to the parent class methods and variables. This program simply prints the value from parent class and from the child class.

13. Single level inheritance will be best for_____
a) Inheriting a class which performs all the calculations
b) Inheriting a class which can print all the calculation results
c) Inheriting a class which can perform and print all calculations
d) Inheriting all the classes for different calculations

Answer

Answer: b [Reason:] Inheriting a class which can perform the most common task will be more efficient. If class which can perform all the calculations is inherited then there won’t be any problem to print the result too. But if a class which can do the most common task for all the other tasks, it will make real use of inheritance.

14. Which constructor will be called first from the classes involved in single inheritance from object of derived class?
a) Base class constructor
b) Derived class constructor
c) Both class constructors at a time
d) Run time error

Answer

Answer: a [Reason:] The base class constructor will be called first from the object of derived class constructor. When the derived class members are to be initialized and allocated memory, the base class members should also be confirmed with the memory allocation.

15. If base class contains 2 nested classes, will it be possible to implement single level inheritance?
a) Yes, always
b) Yes, only if derived class also have nested classes
c) No, it will use more than 2 classes which is wrong
d) No, never

Answer

Answer: a [Reason:] The nested classes are also members of a class. They behave as a used defined data type with some methods implementation. So the inheritance will be as usual with the nested classes being member of base class and of derived class if not private.

Object Oriented MCQ Set 2

1. Which among the following best defines static variables members?
a) Data which is allocated for each object separately
b) Data which is common to all the objects of a class
c) Data which is common to all the classes
d) Data which is common to a specific method

Answer

Answer: b [Reason:] The static data members are made common to all the object of a class. They doesn’t change from object to object. Those are property of class rather than of any individual object.

2. Which keyword should be used to declare static variables?
a) static
b) stat
c) common
d) const

Answer

Answer: a [Reason:] The keyword used to declare static variables is static. This is must be used while declaring the static variables. The compiler can make variables static if and only if they are mentioned with static keyword.

3. Any changes made to static data member from one member function _______
a) Is reflected to only the corresponding object
b) Is reflected to all the variables in a program
c) Is reflected to all the objects of that class
d) Is constant to that function only

Answer

Answer: c [Reason:] The changes made from any function to static data member will be a common change for all the other objects also. If the change is made with respect to one object and change is printed from another object, the result will be same.

4. Which is the correct syntax for declaring static data member?
a) static mamberName dataType;
b) dataType static memberName;
c) memberName static dataType;
d) static dataType memberName;

Answer

Answer: d [Reason:] The syntax must firstly be mentioned with the keyword static. Then the data type of the member followed by the member name should be given. This is general form of declaring static data members.

5. The static data member ______
a) Must be defined inside the class
b) Must be defined outside the class
c) Must be defined in main function
d) Must be defined using constructor

Answer

Answer: b [Reason:] The static data members must be defined outside the class. Since these are common to all the objects and should be created only once, they must not be defined in the constructor.

6. The syntax for defining the static data members is:
a) dataType className :: memberName =value;
b) dataType className : memberName =value;
c) dataType className . memberName =value;
d) dataType className -> memberName =value;

Answer

Answer: a [Reason:] The syntax doesn’t contain the static keyword. Since it is already been declared as static inside the class. The data type and the corresponding class name must be there to allocate the variable to a class. The value is assigned using scope resolution operator for the member name.

7. If static data members have to be used inside a class, those member functions ___
a) Must not be static member functions
b) Must not be member functions
c) Must be static member functions
d) Must not be member function of corresponding class

Answer

Answer: c [Reason:] Only the static member functions can access the static data members. The definition of static members is made common and hence the member function should be capable of manipulating the static data members.

8. The static data member ____
a) Can be accessed directly
b) Can be accessed with any public class name
c) Can be accessed with dot operator
d) Can be accessed using class name if not using static member function

Answer

Answer: d [Reason:] The static data members can be accessed using the class name also. If the member functions is not used or is not to be used then we can call the static data members directly by using its corresponding class name.

9. Which among the following is correct syntax to access static data member without using member function?
a) className -> staticDataMember;
b) className :: staticDataMember;
c) className : staticDataMember;
d) className . staticDataMember;

Answer

Answer: b [Reason:] For accessing the static data members without using the static member functions, the class name can be used. The class name followed by scope resolution, indicating that static data members is member of this class, and then the data member name.

10. Which data members among the following are static by default?
a) extern
b) integer
c) const
d) void

Answer

Answer: c [Reason:] The const data members of any class are made static by default. This is an implicit meaning given by the compiler to the member. Since const values won’t change from object to object, hence are made static instead.

11. What is the output of the following program?

class Test
{
private:static int x;
public: static void fun()
{
cout << ++x << “ ”;
}
};
int Test :: x =20;
void main()
{
	Test x;
	x.fun();
	x.fun();
}

a) 20 22
b) 20 21
c) 21 22
d) 22 23

Answer

Answer: c [Reason:] The static member is initialized with 20. Then the function is called which used pre-increment and printed value of x. The function is called twice. Hence we get 21 22 as output.

12. Whenever any static data member is declared in a class ______
a) Only one copy of the data is created
b) New copy for each object is created
c) New memory location is allocated with each object
d) Only one object uses the static data

Answer

Answer: a [Reason:] The static data is same for all the objects. Instead of creating the same data each time an object is created, the compiler created only one data which is accessed by all the objects of the class. This saves memory and reduces redundancy.

13. If object of class are created, then the static data members can be accessed ______
a) Using dot operator
b) Using arrow operator
c) Using colon
d) Using dot or arrow operator

Answer

Answer: d [Reason:] The static data members can be accessed in usual way as other members are accessed using the objects. The dot operator is used generally. Arrow can be used with the pointers.

14. What will be the output of the following program?

class Test
{
public: Test() 
{ 
cout  << "Test's Constructor is Called " << endl;  
}
};
 
class Result
{
static Test a;
public:
	Result() 
{ 
cout  << "Result's Constructor is Called " << endl; 
}
}; 
 
void main() 
{ 
	Result b; 
}

a) Test’s Constructor is Called
b) Result’s Constructor is Called
c) Result’s Constructor Called Test’s Constructor is Called
d) Test’s Constructor Called Result’s Constructor is Called

Answer

Answer: b [Reason:] The output is the message printed from the constructor of class Result. There is no inheritance used hence only one constructor is called. Since static members are declared once in class declaration and are not defined. The constructor of class Test will not be called.

15. Which among the following is wrong syntax related to static data members?
a) className :: staticDataMember;
b) dataType className :: memberName =value;
c) static dataType memberName;
d) className : dataType -> memberName;

Answer

Answer: d [Reason:] The syntax given in option d doesn’t belong to any particular declaration or definition. First one is to access the static members using the class name. Second is to define the static data outside the class. Third syntax id to declare a data member as static in a class.

Object Oriented MCQ Set 3

1. Which among the following is correct definition for static member functions?
a) Functions created to allocate constant values to each object
b) Functions made to maintain single copy of member functions for all objects
c) Functions created to define the static members
d) Functions made to manipulate static programs

Answer

Answer: b [Reason:] The functions which are made common, with respect to definition and data usage, to all the objects. These functions are able to access the static data members of a class.

2. The static member functions ______
a) Have access to all the members of a class
b) Have access to only constant members of a class
c) Have access to only the static members of a class
d) Have direct access to all other class members also

Answer

Answer: c [Reason:] The static member functions are common for all the objects. These functions can use only the static members of a class in which those are defined. This is because other members change with respect to each object created.

3. The static member functions ____
a) Can be called using class name
b) Can be called using program name
c) Can be called directly
d) Can’t be called outside the function

Answer

Answer: a [Reason:] The static members can be accessed using class name also. This is because the static members remain common to all the objects. Hence objects are not required.

4. Which is correct syntax to access the static member functions with class name?
a) className . functionName;
b) className -> functionName;
c) className : functionName;
d) className :: functionName;

Answer

Answer: d [Reason:] The scope resolution operator must be used to access the static member functions with class name. This indicates that the function belongs to the corresponding class.

5. Which among the following is not applicable for the static member functions?
a) Variable pointers
b) void pointers
c) this pointer
d) Function pointers

Answer

Answer: c [Reason:] Since the static members are not property of objects, they doesn’t have this pointer. Every time the same member is referred from all the objects, hence use of this pointer is of no use.

6. Which among the following is true?
a) Static member functions can’t be virtual
b) Static member functions can be virtual
c) Static member functions can de declared virtual if it is pure virtual class
d) Static member functions can be used as virtual in Java

Answer

Answer: a [Reason:] The static member functions can’t be virtual. This is a restriction on static member functions, since the definition should not change or should not be overridden by any other function of derived class. The static members must remain same for all the objects.

7. The static members are ______
a) Created with each new object
b) Created twice in a program
c) Created as many times a class is used
d) Created and initialized only once

Answer

Answer: d [Reason:] The static members are created only once. Then those members are reused whenever called or invoked. Memory is allocated only once.

8. Which among the following is true?
a) Static member functions can be overloaded
b) Static member functions can’t be overloaded
c) Static member functions can be overloaded using derived classes
d) Static member functions are implicitly overloaded

Answer

Answer: b [Reason:] The static member functions can’t be overloaded because the definition must be the same for all the instances of a class. If an overloaded function have many definitions, none of them can be made static.

9. The static member functions ___
a) Can’t be declared const
b) Can’t be declared volatile
c) Can’t be declared const or volatile
d) Can’t be declared const, volatile or const volatile

Answer

Answer: d [Reason:] The static member functions can’t be made const, since any object or class itself should be capable of making changes to the function. And the function must retain all changes common to all the objects.

10. Which keyword should be used to declare the static member functions?
a) static
b) stat
c) const
d) common

Answer

Answer: a [Reason:] The member functions which are to be made static, must be preceded with the keyword static. This indicates the compiler to make the functions common to all the objects. And a new copy is not created with each of the new object.

11. The keyword static is used ___
a) With declaration inside class and with definition outside the class
b) With declaration inside class and not with definition outside the class
c) With declaration and definition wherever done
d) With each call to the member function

Answer

Answer: b [Reason:] The keyword is used only inside the class while declaring the static member. Outside the class, only definition with proper syntax is given. There is no need of specifying the keyword static again.

12. Which among the following can’t be used to access the members in any way?
a) Scope resolution
b) Arrow operator
c) Single colon
d) Dot operator

Answer

Answer: c [Reason:] The single colon can’t be used in any way in order to access the static members of a class. Other symbols can be used according to the code and need.

13. We can use the static member functions and static data member ______
a) Even if class object is not created
b) Even if class is not defined
c) Even if class doesn’t contain any static member
d) Even if class doesn’t have complete definition

Answer

Answer: a [Reason:] The static members are property of class as a whole. There is no need of specific objects to call static members. Those can be called directly or with class name.

14. The static data member _____
a) Can be mutable
b) Can’t be mutable
c) Can’t be integer
d) Can’t be characters

Answer

Answer: b [Reason:] The static data members can never be mutable. There copies are not made. Since those are common and created only once.

15. If static data member are made inline, ________
a) Those should be initialized outside the class
b) Those can’t be initialized with the class
c) Those can be initialized within the class
d) Those can’t be used by class members

Answer

Answer: c [Reason:] Since the members are created once and are common for all the instances, those can be initialized inside the class. Those doesn’t change with each object being created hence can be defined inside the class once for all.

Object Oriented MCQ Set 4

1. Which is a true statement for object of String class?
a) Object are immutable
b) Object are mutable
c) Object are created only once
d) Object can’t be created

Answer

Answer: a [Reason:] The object of string class are mostly immutable. This means that the String objects are constant. These can’t be changed once created.

2. How to declare an object of class String?
a) String object_Name = value;
b) String object_name = new;
c) String object_name= new value;
d) String object_name= value new;

Answer

Answer: a [Reason:] The class name String is given. And then the object name is mentioned. There are two ways to declare and initialize the string. Either by giving direct string value or by using new keyword. But if new operator is used, constructor of String class have to be called. From the given options, the direct string value declaration is correct.

3. What does function length do in String class?
a) Returns length of string including null character
b) Returns length of string excluding null character
c) Returns length of substring
d) Returns size of string in bytes

Answer

Answer: b [Reason:] The length function returns the length of string. The length is the number of characters in the string but the last null character is not counted. The string length can be used to loop through each character in the string.

4. Which is the function to get the character present at a particular index in the string?
a) char charAt(index);
b) char charIn(StringName);
c) char charAt(StringName);
d) char charIn(index);

Answer

Answer: a [Reason:] The function can be called using dot operator with the string object. Char is the return type of the function to return the character at specified position. The index must be an integer value, less than the length of string.

5. If only one parameter is passed to substring function then ______
a) It returns the character at the specified position
b) It returns the string of length 1 from the specified index
c) It returns the string from specified index till the end
d) It returns the string from starting of string till the specified index

Answer

Answer: c [Reason:] The substring function returns a string value. The string is the substring starting from the specified index till the end. The substring function have to be called with the object of string class.

6. If two index are given as argument to substring function then ___
a) String of length equal to sum of two arguments is returned
b) String starting from first index and of length equal to send argument
c) String starting from first index and of length equal to sum of two arguments
d) String starting from first index and ending at second index position

Answer

Answer: d [Reason:] A value of string type is returned from this function. The returned string is a substring that starts from the first argument position, till the second index position. The indices must be less than the length of actual string.

7. String class have a concat() function that is used to _____
a) Replace old string by new string
b) Add two strings
c) Append one string at end of another string
d) Remove a string from end of one string

Answer

Answer: c [Reason:] The concat function is used to append string into another string. The new string is always appended at the end of source string. The target string is appended as it is and the whole string is then ended by null character.

8. The function lastIndexOf() is used to ___
a) Get the index of last occurrence of specified character in argument
b) Get the index of first occurrence of specified character in argument
c) Get the index of last occurrence of first character in string
d) Get the index of last occurrence of last character of string

Answer

Answer: a [Reason:] The function is used to get the last occurrence index of a character present in a string. The return type is char. Single character is returned. The function is used with a string object and the target character is passed as its argument.

9. Function equals() is ___ and equalIgnoreCase() is _____
a) Case Insensitive, case insensitive
b) Case sensitive, Case insensitive
c) Case sensitive, case sensitive
d) Case insensitive, case sensitive

Answer

Answer: b [Reason:] Both the functions return Boolean value. The function equal() is case sensitive and returns false even if a single character is case different in two strings. The other function ignores the case sensitivity and only checks if the spellings are same.

10. The compareTo() function is used to ____
a) Compare strings value to string object
b) Compare string value to string value
c) Compare string object to another string object
d) Compare string object to another string value

Answer

Answer: c [Reason:] The source and target must be objects of the string class. The compare is always case sensitive. To compare two string objects without case sensitivity then we can use compareToIgnoreCase() function.

11. String class provides function toUpper() to _____
a) Convert first character to upper case
b) Convert last character to upper case
c) Convert the whole string characters to upper case
d) Convert upper case to lower and lower to upper cases

Answer

Answer: c [Reason:] The function is used to convert each character of the string. If the character is already upper case then it remains the same. But if some character is in lower case then it will be converted to upper case.

12. String trim() function is used to _______
a) Remove all the white spaces from the string
b) Remove white space from start of string
c) Remove white space at end of string
d) Remove white space from both the ends of string

Answer

Answer: d [Reason:] The function is used to remove any white space from both the ends of a given string. The white space include space, tab, next line etc. It will be removed both from the starting of string and from end of string.

13. Function replace() accepts _______ arguments.
a) 1
b) 2
c) 3
d) 4

Answer

Answer: b [Reason:] The first argument is the target character. This target character will be replaced by another character. The new character is the second argument to the function. Only the characters can be passed as argument, not a string.

14. If two arguments are passed to the indexOf() function then ___
a) Second argument indicates the occurrence number of specified character from starting
b) Second argument indicates the occurrence number of specified character from end
c) Second argument indicates the index of the character in first argument
d) Second argument indicates the index of the character from the last of the string

Answer

Answer: a [Reason:] The string may have more than one occurrence of a character. We use this function to get the index at which the specified number of times a specific character has occurred in a string. For example, we can get the index of 5th occurrence of character “j” in a string.

15. The string class deals with string of only character type.
a) True
b) False

Answer

Answer: a [Reason:] The string class objects can be used for any string consisting of characters. The characters include numbers, alphabets and few special characters. String class is not necessary to be used but provides a huge set of inbuilt functions to make the string operations easier.

Object Oriented MCQ Set 5

1. A template class can have _______
a) More than one generic data type
b) Only one generic data type
c) At most two data types
d) Only generic type of integers and not characters

Answer

Answer: a [Reason:] The template class can support more than one data type. The only thing is to add all the data types required in a list separated by comma within template specification.

2. Which among the following is the proper syntax for the template class?
a) template ;
b) Template ;
c) template T named(T x, T y){ }
d) Template T1 named(T1 x, T2 y){ }

Answer

Answer: c [Reason:] The syntax must start with keyword template, case sensitive. Then it should include the typename and a variable to denote it. Then whenever that variable is used, it replaces it with the data type needed.

3. Can default arguments be used with the template class?
a) Yes, in some special cases
b) Yes, always
c) No, it must satisfy some specific conditions first
d) No, it can’t be done

Answer

Answer: b [Reason:] The template class can use default arguments. This is used to specify the data type to be considered if it is not specified while passing to the generic class. The default type will be used.

4. What is the syntax to use explicit class specialization?
a) template class myClass<>{ }
b) template class myClass{ }
c) template <> class myClass<>{ }
d) template <> class myClass{ }

Answer

Answer: d [Reason:] The class specialization is creation of explicit specialization of a generic class. We have to use template<> constructor for this to work. It works in the same way as with explicit function specialization.

5. Which is the most significant feature that arises by using template classes?
a) Code readability
b) Ease in coding
c) Code reusability
d) Modularity in code

Answer

Answer: c [Reason:] The code reusability is the feature that becomes more powerful with use of template classes. You can generate a single code that can be used in variety of programming situation.

6. A template class defines the form of a class _____ it will operate.
a) With full specification of the data on which
b) With full specification of the functions on which
c) Without full specification of the data on which
d) Without full specification of the functions on which

Answer

Answer: c [Reason:] The template classes can accept all types of data types. There is no need to specify the data on which the class has to operate. Hence it gives us flexibility to code without worrying about the type of data that might be used in the code.

7. What are the two specializations of I/O template classes in C++?
a) 16-bit character and wide characters
b) 8-bit character and wide characters
c) 32-bit character and locale characters
d) 64-bit characters and locale characters

Answer

Answer: b [Reason:] The I/O specialization is made with wide character and 8-bit characters. Wide characters are used to store the characters that might take more than 1 byte of space in memory or any size that is different from the one that the machine is using.

8. Can typeid() function be used with the object of generic classes?
a) Yes, only if default type is given
b) Yes, always
c) No, generic data can’t be determined
d) No, never possible

Answer

Answer: b [Reason:] The typeid() function can be used with the objects of generic classes. An instance of a template class will take the type of data that is being used with it. Hence when typeid() function is used, the data type would have already been defined and hence we can get desired result from typeid() function.

9. The _______ class is a specialization of a more general template class.
a) String
b) Integer
c) Digit
d) Math

Answer

Answer: a [Reason:] The string class is more specialized. Since the string must be able to store any kind of data that is given to the string. Hence it need maximum specialization.

10. How is function overloading different from template class?
a) Overloading is multiple function doing same operation, Template is multiple function doing different operations
b) Overloading is single function doing different operations, Template is multiple function doing different operations
c) Overloading is multiple function doing similar operation, Template is multiple function doing identical operations
d) Overloading is multiple function doing same operation, Template is same function doing different operations

Answer

Answer: c [Reason:] The function overloading is multiple functions with similar or different functionality but generic class functions perform the same task on given different types of data.

11. What if static members are declared inside template classes?
a) All instances will share the static variable
b) All instances will have their own static variable
c) All the instances will ignore the static variable
d) Program gives compile time error

Answer

Answer: b [Reason:] The generic class have a special case with static members. Each instance will have its own static member. The static members are not shared usually.

12. What is the output of following program?

template <typename T>
void test(const T&x) 
{
    static int count = 0;
    cout &lt;&lt; "x = " &lt;&lt; x &lt;&lt; " count = " &lt;&lt; count &lt;&lt; endl;
    ++count;
    return;
}
 
void main() 
{
    test<int> (2);
    test<int>(2);
    test<double>(2.2);
}

a) x = 2 count = 0
x = 2.2 count = 0
x = 2.2 count = 0
b) x = 2 count = 0
x = 2 count = 0
x = 2.2 count = 0
c) x = 2 count = 0
x = 2 count = 1
x = 2.2 count = 0
d) x = 2 count = 0
x = 2 count = 1
x = 2.2 count = 2

Answer

Answer: c [Reason:] For each new type, the class will have separate instance. Here two instances will be created and hence counter for integer goes to 1. And for float value, the count remains 0 for the output.

13. If template class is defined, is it necessary to use different types of data for each call?
a) No, not necessary
b) No, but at least two types must be there
c) Yes, to make proper use of template
d) Yes, for code efficiency

Answer

Answer: a [Reason:] It is not necessary to use different type with each call to the generic function. Data may be of same type with each call but still the function works. We don’t consider other properties like efficiency with this concept because it is made generic to all data type, hence always works.

14. How many generic types can be given inside a single template class?
a) Only 1
b) Only 3
c) Only 7
d) As many as required

Answer

Answer: d [Reason:] There is no restriction on the number of types to be used for making the class generic. There can be any number of generic types with a single class. Hence giving flexibility to code with all the data types.

15. Template classes must have at least one static member.
a) True
b) False

Answer

Answer: b [Reason:] There is no mandatory condition to have static members inside template class. Not only template, it is not mandatory to have static members anywhere. We can use them as required in the code.

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.