CS Object oriented programming - Unit 1 - MCQs

 

UNIT I

Object oriented programming concepts – objects-classes- methods and messages-abstraction and encapsulation-inheritance- abstract classes- polymorphism.Introduction to C++- objects- classes- constructors and destructors

 

 

1)  Which of the following language was developed as the first purely object programming language?

 

a.   SmallTalk

b.   C++

c.   Kotlin

d.   Java

 

Answer: a. SmallTalk

 

Explanation: This programming language was invented as the first pure OOPS (object-oriented) language. This language was designed by Alan Kay in the early 1970s.

 

2.  Who developed object-oriented programming?

 

a.      Adele Goldberg

b.      Dennis Ritchie

c.       Alan Kay

d.      Andrea Ferro

 

Answer: c. Alan Kay

 

Explanation: In the year 1970, Alan Kay gave Object-Oriented programming. He coined the concept of OOPS at a grad school in the year 1966 or 1967. Alan kay, Adele Goldberg, Dan Ingalls and others developed the first Smalltalk programming language, which follows the OOPS concept.

 

3.  Which of the following is not an OOPS concept?

 

a.      Encapsulation

b.      Polymorphism

c.       Exception

d.      Abstraction

 

Answer: c. Exception


4.  Which feature of OOPS described the reusability of code?

 

a.      Abstraction

b.      Encapsulation

c.       Polymorphism

d.      Inheritance

 

Answer: d. Inheritance

 

Explanation: Inheritance is the feature of OOPS, which allows the users of OOPS to reuse the code which is already written. This OOPS feature inherits the features of another class in the programs. This mechanism actually inherits the fields and methods of the superclass.

 

5.  Which of the following language supports polymorphism but not the classes?

 

a.      C++ programming language

b.      Java programming language

c.       Ada programming language

d.      C# programming language

 

Answer: c. Ada programming language

 

Explanation: It is a programming language that disapproves of the concept of polymorphism but supports the concept of classes. It is an object-based language. So, it does not follow the Object-oriented programming concepts.

 

6.  Which among the following feature is not in the general definition of OOPS?

 

a.      Modularity

b.      Efficient Code

c.       Code reusability

d.      Duplicate or Redundant Data

 

Answer: d. Duplicate or Redundant Data

 

Explanation: Duplicacy or Redundancy of data is a feature which totally dependent on the programmers. So, it cannot be created by the OOPS.

 

7.  Which feature of OOPS derives the class from another class?

 

a.      Inheritance

b.      Data hiding


c.       Encapsulation

d.      Polymorphism

 

Answer: a. Inheritance

 

Explanation: Inheritance is an important OOPS feature which derives the class from the base class or superclass. This OOPS feature inherits the features of another class in the programs. This mechanism actually inherits the fields and methods of the superclass.

 

8.  Define the programming language, which does not support all four types of inheritance?

 

a.      Smalltalk

b.      Kotlin

c.       Java

d.      C++

 

Answer: c. Java

 

Explanation: Java is a programming language that disapproves of the concept of 'multiple inheritance'. So, it does not agree with all types of inheritance. But, we can implement 'multiple inheritance' in Java language using the interface concept

 

9.  A single program of OOPS contains               classes?

 

a.  Only 1

 

b.      Only 999

c.       Only 100

d.      Any number

 

Answer: d. Any number

 

Explanation: We can define any number of classes with different names in a single program of OOPS.

 

10.  Which operator from the following can be used to illustrate the feature of polymorphism?

 

a.      Overloading <<

b.      Overloading &&

c.       Overloading | |

d.      Overloading +=


Answer: a. Overloading <<

 

Explanation: << is an insertion operator which is used for overloading (polymorphism).

 

11.  Which two features of object-oriented programming are the same?

 

a.      Abstraction and Polymorphism features are the same

b.      Inheritance and Encapsulation features are the same

c.       Encapsulation and Polymorphism features are the same

d.      Encapsulation and Abstraction

 

Answer: d. Encapsulation and Abstraction

 

Explanation: Encapsulation and Abstraction are the same OOPS concepts. Encapsulation hides the features of the object and binds all the properties inside a single class. And abstraction is a feature that shows the required data to the user.

 

12.  Which header file is required by the C++ programming language to use the OOPS concept?

 

a.      stdio.h

b.      iostream.h

c.       stdlib.h

d.      We can easily use the OOPS concepts in c++ programs without using any header file.

 

Answer: d. We can easily use the OOPS concepts in c++ programs without using any header file.

 

Explanation: There is no need to use any particular header file for using the OOPS concept in the C++ programs. The C++ functions and variables have their respective header files, which should be defined in the program.

 

13.  Which of the following is not correct for virtual function in C++ ?.

 

A.  Virtual function can be static.

B.  Virtual function should be accessed using pointers

C.  Virtual function is defined in base class

D.  Must be declared in public section of class

 

Ans : A

 

Explanation: Virtual function is can’t be static in C++.


14.  How can we make a class abstract?

A.  By declaring it abstract using the static keyword

B.  By declaring it abstract using the virtual keyword.

C.  By making at least one member function as pure virtual function

D.  By making all member functions constant

 

Ans : C

 

Explanation: We can make a class abstract by making at least one member function as pure virtual function.

 

15.  How many specifiers are present in access specifiers in class?

 

A.  2

B.  1

C.  4

D.  3

 

Ans : D

 

Explanation: There are three types of access specifiers. They are public, protected and private.

 

16.  Which of these following members are not accessed by using direct member access operator?

 

A.  Public

B.  Private

C.  Protected

D.  Both B & C

 

Ans : D

 

Explanation: Because of the access is given to the private and protected, We can’t access them by using direct member access operator.

17.  Which other keywords are also used to declare the class other than class?

 

A.  Struct

B.  Union

C.  Object

D.  Both struct & union


Ans : D

 

Explanation: Struct and union take the same definition of class but differs in the access techniques.

18.  Which of the following is true?

 

A.  All objects of a class share all data members of class

B.  Objects of a class do not share non-static members. Every object has its own copy

C.  Objects of a class do not share codes of non-static methods, they have their own copy

D.  None of these

 

Ans : B

 

Explanation: very object maintains a copy of non-static data members. For example, let Student be a class with data members as name, year, batch. Every object of student will have its own name, year and batch. On a side note, static data members are shared among objects. All objects share codes of all methods

 

19.  Which of the following can be overloaded?

 

A.  Object

B.  Operators

C.  Both A & B

D.  None of the above

 

Ans : C

 

Explanation: Object and Operators can be overloaded.

 

20.  Which is also called as abstract class?

 

A.  Virtual function

B.  Derived class

C.  Pure virtual function

D.  None of the mentioned

 

Ans : C

 

Explanation: Classes that contain at least one pure virtual function are called as abstract base classes.


 

21.  What will be the output of the following program? #include <iostream>

 

using namespace std; class LFC

{

 

static int x; public:

static void Set(int xx)

 

{

 

x = xx;

 

}

 

void Display()

 

{

 

cout<< x ;

 

}

 

};

 

int LFC::x = 0; int main()

{

 

LFC::Set(33);

 

LFC::Display(); return 0;

}


A.  The program will print the output 0.

B.  The program will print the output 33.

C.  The program will print the output Garbage.

D.  The program will report compile time error.

 

Ans : D

 

 

Explanation: The program will report compile time error: cannot call member function "void LFC::Display()" without object

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

 

Note:Includes all required header files class course

{

 

int x, y; public: course(int xx)

{

 

x = ++xx;

 

}

 

void Display()

 

{

 

cout<< --x << " ";

 

}

 

};

 

int main()

 

{

 

course obj(20); obj.Display();


int *p = (int*)&obj ;

 

*p = 5; obj.Display(); return 0;

}

 

A. 20 4

B. 21 4

C. 20 5

D. 21 5

 

Ans : A

 

Explanation: 20 4 will be the output of the following program

22.   In a class, member variables are often called its                  , and its member functions are sometimes referred to as its behaviour, or                         .

 

1.                  attributes, methods

2.      none of these

3.      values, morals

4.      data, activities

5.      attributes, activities

 

Answer: (1)

 

23.Which of these keywords are access specifiers?

1.                  near and far

2.      opened and closed

3.      table and row

4.      none of these

5.      private and public

 

Answer: (5)

 

24. Use of                      protects data from inadvertent modifications.

1.                  protect() member function

2.      private access specifier

3.      class protection operator, @

4.      none of these

5.      public access specifier


Answer: (2)

 

25. Which is private member functions access scope?

 

a)  Member functions which can only be used within the class

 

b)  Member functions which can used outside the class

 

c)  Member functions which are accessible in derived class

 

d)  Member functions which can’t be accessed inside the class

 

Answer: a

 

Explanation: The member functions can be accessed inside the class only if they are private. The access is scope is limited to ensure the security of the private members and their usage.

 

26.  Which among the following is true?

 

a)  The private members can’t be accessed by public members of the class

 

b)  The private members can be accessed by public members of the class

 

c)  The private members can be accessed only by the private members of the class

 

d)  The private members can’t be accessed by the protected members of the class

 

Answer: b

 

Explanation: The private members are accessible within the class. There is no restriction on use of private members by public or protected members. All the members can access the private member functions of the class.

 

27.  Which member can never be accessed by inherited classes?

 

a)  Private member function

 

b)  Public member function

 

c)  Protected member function

 

d)  All can be accessed

 

Answer: a


Explanation: The private member functions can never be accessed in the derived classes. The access specifiers is of maximum security that allows only the members of self class to access the private member functions.

 

28.  Which syntax among the following shows that a member is private in a class?

 

a)  private: functionName(parameters)

 

b)  private(functionName(parameters))

 

c)  private functionName(parameters)

 

d)  private::functionName(parameters)

 

Answer: c

 

Explanation: The function declaration must contain private keyword follower by the return type and function name. Private keyword is followed by normal function declaration.

29.  If private member functions are to be declared in C++ then                          

 

a)  private: <all private members>

 

b)  private <member name>

 

c)  private(private member list)

 

d)  private :- <private members>

 

Answer: a

 

Explanation: The private members doesn’t have to have the keyword with each private member. We only have to specify the keyword private followed by single colon and then private member’s are listed.

 

30.  In java, which rule must be followed?

 

a)  Keyword private preceding list of private member’s

 

b)  Keyword private with a colon before list of private member’s

 

c)  Keyword private with arrow before each private member

 

d)  Keyword private preceding each private member


Answer: d

 

Explanation: The private keyword must be mentioned before each private member. Unlike the rule in C++ to specify private once and list all other private member’s, in java all member declarations must be preceded by the keyword private.

 

31.  How many private member functions are allowed in a class?

 

a)  Only 1

 

b)  Only 7

 

c)  Only 255

 

d)  As many as required

 

Answer: d

 

Explanation: There are no conditions applied on the number of private member functions that can be declared in a class. Though the system may restrict use of too many functions depending on memory.

 

32. How to access a private member function of a class?

 

a)  Using object of class

 

b)  Using object pointer

 

c)  Using address of member function

 

d)  Using class address

 

Answer: c

 

Explanation: Even the private member functions can be called outside the class. This is possible if address of the function is known. We can use the address to call the function outside the class.

 

33.  Private member functions                         

 

a)  Can’t be called from enclosing class

 

b)  Can be accessed from enclosing class


c)  Can be accessed only if nested class is private

 

d)  Can be accessed only if nested class is public

 

Answer: a

 

Explanation: The nested class members can’t be accessed in the enclosed class even though other members can be accessed. This is to ensure the class members security and not to go against the rules of private members.

 

34. Which function among the following can’t be accessed outside the class in java in same package?

 

a)  public void show()

 

b)  void show()

 

c)  protected show()

 

d)  static void show()

 

Answer: c

 

Explanation: The protected members are available within the class. And are also available in derived classes. But these members are treated as private members for outside the class and inheritance structure. Hence can’t be accessed.

 

35.  If private members are to be called outside the class, which is a good alternative?

 

a)  Call a public member function which calls private function

 

b)  Call a private member function which calls private function

 

c)  Call a protected member function which calls private function

 

d)  Not possible

 

Answer: a

 

Explanation: The private member functions can be accessed within the class. A public member function can be called which in turn calls the private member function. This maintains the security and adheres to the rules of private members.

 

36.  If a function in java is declared private then it                                    


a)  Can’t access the standard output

 

b)  Can access the standard output

 

c)  Can’t access any output stream

 

d)  Can access only the output streams

 

Answer: b

 

Explanation: The private members can access any standard input or output. There is no restriction on access to any input or output stream. And since standard input can also be used hence only accessing the output stream is not true.

 

37.  Which among the following best describes abstract classes?

 

a)  If a class has more than one virtual function, it’s abstract class

 

b)  If a class have only one pure virtual function, it’s abstract class

 

c)  If a class has at least one pure virtual function, it’s abstract class

 

d)  If a class has all the pure virtual functions only, then it’s abstract class

 

Answer: c

 

Explanation: The condition for a class to be called abstract class is that it must have at least one pure virtual function. The keyword abstract must be used while defining abstract class in java.

 

38.  Can abstract class have main() function defined inside it?

 

a)  Yes, depending on return type of main()

 

b)  Yes, always

 

c)  No, main must not be defined inside abstract class

 

d)  No, because main() is not abstract function

 

Answer: b

 

Explanation: This is a property of abstract class. It can define main() function inside it. There is no restriction on its definition and implementation.


39.  If there is an abstract method in a class then, _                             

 

a)  Class must be abstract class

 

b)  Class may or may not be abstract class

 

c)  Class is generic

 

d)  Class must be public

 

Answer: a

 

Explanation: It is a rule that if a class have even one abstract method, it must be an abstract class. If this rule was not made, the abstract methods would have got skipped to get defined in some places which are undesirable with the idea of abstract class.

 

40.    If a class is extending/inheriting another abstract class having abstract method, then


 

a)  Either implementation of method or making class abstract is mandatory

 

b)  Implementation of the method in derived class is mandatory

 

c)  Making the derived class also abstract is mandatory

 

d)  It’s not mandatory to implement the abstract method of parent class

 

Answer: a

 

Explanation: Either of the two things must be done, either implementation or declaration of class as abstract. This is done to ensure that the method intended to be defined by other classes gets defined at every possible class.

 

41.   Abstract class A has 4 virtual functions. Abstract class B defines only 2 of those member functions as it extends class A. Class C extends class B and implements the other two member functions of class A. Choose the correct option below.

 

a)  Program won’t run as all the methods are not defined by B

 

b)  Program won’t run as C is not inheriting A directly

 

c)  Program won’t run as multiple inheritance is used

 

d)  Program runs correctly


Answer: d

 

Explanation: The program runs correctly. This is because even class B is abstract so it’s not mandatory to define all the virtual functions. Class C is not abstract but all the virtual functions have been implemented will that class.

 

42.  Abstract classes can                                          instances.

 

a)  Never have

 

b)  Always have

 

c)  Have array of

 

d)  Have pointer of

 

Answer: a

 

Explanation: When an abstract class is defined, it won’t be having the implementation of at least one function. This will restrict the class to have any constructor. When the class doesn’t have constructor, there won’t be any instance of that class.

 

43.  We                                        to an abstract class.

 

a)  Can create pointers

 

b)  Can create references

 

c)  Can create pointers or references

 

d)  Can’t create any reference, pointer or instance

 

Answer: c

 

Explanation: Even though there can’t be any instance of abstract class. We can always create pointer or reference to abstract class. The member functions which have some implementation inside abstract itself can be used with these references.

 

44.  Which among the following is an important use of abstract classes?

 

a)  Header files

 

b)  Class Libraries

 

c)  Class definitions


d)  Class inheritance

 

Answer: b

 

Explanation: The abstract classes can be used to create a generic, extensible class library that can be used by other programmers. This helps us to get some already implemented codes and functions that might have not been provided by the programming language itself.

 

45.  Can abstract classes have static methods (Java)?

 

a)  Yes, always

 

b)  Yes, but depends on code

 

c)  No, never

 

d)  No, static members can’t have different values

 

Answer: a

 

Explanation: There is no restriction on declaring static methods. The only condition is that the virtual functions must have some definition in the program.

 

46.  Encapsulation helps in writing                        classes in java a.Mutable

b.Abstract c.Wrapper d.Immutable Answer: (d)

47.  Which among the following should be encapsulated? a.The data which is prone to change is near future

b.The data prone to change in long terms c.The data which is intended to be changed d.The data which belongs to some other class


Answer: (a)

 

48.  How can Encapsulation be achieved? a.Using Access Specifiers

b.Using only private members c.Using inheritance

d.Using Abstraction

 

Answer: (a)

 

49.  Which among the following violates the principle of encapsulation almost always? a.Local variables

b.Global variables c.Public variables d.Array variables Answer: (b)

50.  Which among the following would destroy the encapsulation mechanism if it was allowed in programming?

 

a.Using access declaration for private members of base class b.Using access declaration for public members of base class c.Using access declaration for local variable of main() function d.Using access declaration for global variables

Answer: (a)

 

51. Which among the following can be a concept against encapsulation rules? a.Using function pointers

b.Using char* string pointer to be passed to non-member function


c.Using object array

 

d.Using any kind of pointer/array address in passing to another function

 

Answer: (d)

 

52. Consider the following code and select the correct option: class student

{

 

int marks;

 

public : int* fun()

 

{

 

return &marks;

 

}

 

};

 

main()

 

{

 

student s;

 

int *ptr=c.fun(); return 0;

}

 

a.This code is good to go

 

b.This code may result in undesirable conditions c.This code will generate error

d.This code violates encapsulation

 

Answer: (d)


53.  Consider the code and select the wrong choice: class hero

{

 

char name[10]; public : void disp()

{

 

cout<<name;

 

}

 

};

 

a.This maintains encapsulation

 

b.This code doesn’t maintain encapsulation c.This code is vulnerable

d.This code gives error

 

Answer: (a)

 

54. Encapsulation is the way to add functions in a user defined structure. a.True

b.False c.May be d.Can't say Answer: (b)

55.  Using encapsulation data security is                       

 

a.Not ensured

 

b.Ensured to some extent


c.Purely ensured d.Very low Answer: (b)

56.  How can you make the private members inheritable?

 

a)  By making their visibility mode as public only

 

b)  By making their visibility mode as protected only

 

c)  By making their visibility mode as private in derived class

 

d)  It can be done both by making the visibility mode public or protected

 

Answer: d

 

Explanation: It is not mandatory that you have to make the visibility mode either public or protected. You can do either of those. That will give you permission to inherit the private members of base class.

 

57.  If a derived class object is created, which constructor is called first?

 

a)  Base class constructor

 

b)  Derived class constructor

 

c)  Depends on how we call the object

 

d)  Not possible

 

Answer: a

 

Explanation: First the base class constructor is invoked. When we create a derived class object, the system tries to invoke its constructor but the class is derived so first the base class must be initialized, hence in turn the base class constructor is invoked before the derived class constructor.

 

58.  Members which are not intended to be inherited are declared as                                

 

a)  Public members

 

b)  Protected members


c)  Private members

 

d)  Private or Protected members

 

Answer: c

 

Explanation: Private access specifier is the most secure access mode. It doesn’t allow members to be inherited. Even Private inheritance can only inherit protected and public members.

 

59.   If a base class is inherited in protected access mode then which among the following is true?

 

a)  Public and Protected members of base class becomes protected members of derived class

 

b)  Only protected members become protected members of derived class

 

c)  Private, Protected and Public all members of base, become private of derived class

 

d)  Only private members of base, become private of derived class

 

Answer: a

 

Explanation: As the programming language rules apply, all the public and protected members of base class becomes protected members of derived class in protected access mode. It can’t be changed because it would hinder the security of data and may add vulnerability in the program.

 

60.  Which is the correct syntax of inheritance?

 

a)  class derived_classname : base_classname{ /*define class body*/ };

 

b)  class base_classname : derived_classname{ /*define class body*/ };

 

c)  class derived_classname : access base_classname{ /*define class body*/ };

 

d)  class base_classname :access derived_classname{ /*define class body*/ };

 

Answer: c

 

Explanation: Firstly, keyword class should come, followed by the derived class name. Colon is must followed by access in which base class has to be derived, followed by the base class name. And finally the body of class. Semicolon after the body is also must.

 

61.  Which one is the best description of polymorphism?

 

A.  It is the ability for undefined message/data to be processed in at least one way


B.  It is the ability for a message/data to be processed in more than one form

 

C.  It is the ability for many messages/data to be processed in many ways

 

D.  none of these

 

Ans: B

 

62.  The languages that support classes but not polymorphism is called?

 

A.  child Class-based language

 

B.  Class-based language

 

C.  Object-based language

 

D.  Procedure Oriented language

 

Ans: C

 

63.  Which one is the language that does not support polymorphism but support classes?

 

A.C#

 

b)  Ada

 

c)  C++

 

d)  Java

 

Ans: B

 

64.  The feature that its specification is if same message is passed to objects of several different classes and all of those can respond in a different way then it is called?

 

A.  Inheritance

 

B.  Classes

 

C.  Polymorphism

 

D.  none of these

 

Ans: C


65.  Which type of function shows polymorphism among the following?

 

A.  Inline function

 

B.  Virtual function

 

C.  Undefined functions

 

D.  Class member functions

 

Ans: B

 

66.  In case of using abstract class or function overloading, the function is supposed to be called first?

 

A.  Local function

 

B.  Function with the highest priority in the compiler

 

C.  Global function

 

D.  none of these

 

Ans: B

 

67.  Which one of the following can’t be used for polymorphism?

 

A.  Member functions overloading

 

B.  Static member functions

 

C.  global member function

 

D.  Constructor overloading

 

Ans: B

 

68.  Which one of the following can show polymorphism?

 

A.  Overloading ||

 

B.  Overloading &&

 

C.  Overloading <<


D.Overloading +=

 

Ans: C

 

69.  What is the difference between struct and class in C++?

 

(A)     All members of a structure are public and structures don’t have constructors and destructors

 

(B)   Members of a class are private by default and members of struct are public by default. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public and when deriving a class, default access specifier is private.

 

(C)  All members of a structure are public and structures don’t have virtual functions

 

(D)  All of the above

 

Answer: (B)

 

70.  Constructor is executed when          .

 

A.  An object goes out of scope.

 

B.  A class is declared

 

C.  An object is created

 

D.  An object is used

 

Ans : C

 

Explanation: Constructor is executed when An object is created.

 

71.  How many ways of reusing are there in class hierarchy?

 

A.  1

 

B.  3

 

C.  4

 

D.  2

 

Ans : D


Explanation: Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.

 

72.  Where does the object is created?

 

A.  Class

 

B.  Constructor

 

C.  Destructors

 

D.  Attributes

 

Ans : A

 

Explanation: In class, only all the listed items except class will be declared.

 

73.  Which of the following is a valid class declaration?

 

A.  Class A { int x; };

 

B.  Class B { }

 

C.  Public class A { }

 

D.  Object A { int x; };

 

Ans : A

 

Explanation: Class A { int x; }; is a valid class declaration.

 

74.  Which of the following is not correct (in C++) ?i) Class templates and function templates are instantiated in the same way ii) Class templates differ from function templates in the way they are initiated iii) Class template is initiated by defining an object using the template argument iv) Class templates are generally used for storage classes.

 

A.  i

 

B.  i & ii

 

C.  ii ,iii, iv

 

D.  iv

 

Ans : C


Explanation: In C++ class template and function template are similar in the way the are initiated. Class template are not used for storage class. Class templates and function templates are instantiated in the same way and Class template is not initiated by defining an object using the template. So (2), (3), (4) are not correct in C++.

 

75.  Which of the following keywords is used to control access to a class member?

 

A.  Default

 

B.  Break

 

C.  Protected

 

D.  Asm

 

Ans : C

 

Explanation: Protected keywords is used to control access to a class member

 

76.  Which of the following statements is incorrect?

 

A.  Destructor of base class should always be static

 

B.  Destructor of base class should always be virtual.

 

C.  Destructor of base class should not be virtual.

 

D.  Destructor of base class should always be private.

 

Ans : B

 

Explanation: Destructor of base class should always be virtual statements is incorrect

 

77.  Which operator can not be overloaded?

 

A. +

 

B.  ::

 

C.  -

 

D.  *

 

Ans : B


Explanation: :: operator can not be overloaded

 

78.  When Virtual Table is created?

 

A.  Every Class has VTable

 

B.  Class inherited from other Class

 

C.  Class has atleast one Virtual Function

 

D.  When a Class Overrides the function of Base class

 

Ans : C

 

Explanation: When Virtual Table is created Class has atleast one Virtual Function

 

79.  What is the size of empty class?

 

A.  0

 

B.  2

 

C.  4

 

D.  1

 

Ans : D

 

Explanation: When we create object of empty class at that time State of that object is nothing. Behaviour of that object is also nothing, but compiler assigns a unique address to that object. Memory in Computer is always organized in the form of bytes and minimum memory available at object address location is 1 byte. That's why size of object of empty class is 1 byte.

 

80.   Which of the followings is/are automatically added to every class, if we do not write our own.

 

A.  Copy Constructor.

 

B.  Assignment Operator

 

C.  A constructor without any parameter

 

D.  All of the above

 

Ans : D


Explanation: In C++, if we do not write our own, then compiler automatically creates a default constructor, a copy constructor and a assignment operator for every class..

 

81.  Which of the following gets called when an object is being created?

 

A.  Constuctor

 

B.  Virtual Function

 

C.  Destructors

 

D.  Main

 

Ans : A

 

Explanation: Virtual Function gets called when an object is being created.

 

82.  Destructor has a same name as the constructor and it is preceded by?

 

A.  !

 

B.  ?

 

C.  ~

 

D.  $

 

Ans : C

 

Explanation: Destructor has a same name as the constructor and it is preceded by ~.

 

83.  Like constructors, can there be more than one destructors in a class?

 

A.  Yes

 

B.  No

 

C.  May Be

 

D.  Can't Say

 

84.     State whether the following statements about the constructor are True or False. i) constructors should be declared in the private section. ii) constructors are invoked automatically when the objects are created.


A.  True,True

 

B.  True,False

 

C.  False,True

 

D.  False,False

 

Ans : C

 

Explanation: Statement 1 is false and statement 2 is true.

 

85.  Which of the following is true about constructors.

 

i)  They cannot be virtual

 

ii)  They cannot be private.

 

iii)  They are automatically called by new operator.

 

A.  All i,ii,iii

 

B.  i & iii

 

C.  ii & iii

 

D.  i & ii

 

Ans : B

 

Explanation: i) True: Virtual constructors don't make sense, it is meaningless to the C++ compiler to create an object polymorphically. ii) False: Constructors can be private

 

86.    Destructors                       for automatic objects if the program terminates with a call to function exit or function abort

 

A.  Are called

 

B.  Are not called

 

C.  Are inherited

 

D.  Are created

 

Ans : B


Explanation: Destructors Are not called for automatic objects if the program terminates with a call to function exit or function abort

 

87.  Which contructor function is designed to copy object of same class type?

 

A.  Copy constructor

 

B.  Create constructor

 

C.  Object constructor

 

D.  Dynamic constructor

 

Ans : A

 

Explanation: Copy constructor function is designed to copy object of same class type.

 

88.  What will be the output of the following program? #include <iostream>

using namespace std; class LFC

{

 

int id;

 

static int count; public:

LFC() {

 

count++; id = count;

cout << "constructor for id " << id << endl;

 

}

 

~LFC() {


cout << "destructor for id " << id << endl;

 

}

 

};

 

int LFC::count = 0; int main() {

LFC a[3];

 

return 0;

 

}

 

A.   constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 3 destructor for id 2 destructor for id 1

 

B.  constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 1 destructor for id 2 destructor for id 3

 

C.  Compiler Dependent

 

D.  constructor for id 1 destructor for id 1 Ans : D

Explanation: In the above program, id is a static variable and it is incremented with every object creation. Object a[0] is created first, but the object a[2] is destroyed first. Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object.

 

89.  What will be the output of the following program? #include <iostream>

using namespace std; class LFC {

LFC() { cout << "Constructor called"; }


};

 

int main()

 

{

 

LFC t1;

 

return 0;

 

}

 

A.  Compiler Error

 

B.  Runtime Error

 

C.  Constructor called

 

D.  destructor for id 1

 

Ans : A

 

Explanation: By default all members of a class are private. Since no access specifier is there for find(), it becomes private and it is called outside the class when t1 is construct

Comments

Popular posts from this blog

Anna University Exam Dec 224 time table

MA 6351- ANNA UNIVERSITY QUESTION PAPER TRANSFORMS AND PARTIAL DIFFERENTIAL EQUATIONS - TPDE 2013 REGULATION QUESTION PAPER NOV/DEC 2018,2019