1Oops Viva Questions

  • Upload
    9chand3

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

  • 8/13/2019 1Oops Viva Questions

    1/24

    &

  • 8/13/2019 1Oops Viva Questions

    2/24

    Page 2 of 24

    Oops Viva Questions & Answers

    Note: With the following question & answers below, Study the complete operator overloading

    concepts, i.e. how to overload each operator & Stream handling concepts completely.

    Basics Concepts

    B1. What are the basic concepts of OOPS?

    Ans.OOPs concepts are1. Object2. Class3. Encapsulation4. Abstraction5. Polymorphism6. Inheritance7. Message passing8. Dynamic binding (Dynamic Polymorphism)

    These all are partially supported by c++, java supports all the above features.

    B2. What is an Object?

    Ans. An object is an abstraction of a real world entity. It may represent a person, a place number

    and icons or something else that can be modeled. Any data in an object occupy some space in

    memory and can communicate with each other.

    B3. What are Classes?

    Ans. A class is a collection of objects having common features .It is a user defined data types which

    has data members as well functions that manipulate these data.

    B4.What is Data Abstraction?

    Ans. It can be defined as the separation of unnecessary details or explanation from system

    requirements so as to reduce the complexities of understanding requirements.

    B5.What is Encapsulation?

    Ans. It is a mechanism that puts the data and function together. It is the result of hidingimplementation details of an object from its user .The object hides its data to be accessed by only

    those functions which are packed in the class of that object.

    B6. What is Inheritance?

    Ans. It is the relationship between two classes of object such that one of the classes, the child takes

    all the relevant features of other class -the parent. Inheritance brings about reusability.

  • 8/13/2019 1Oops Viva Questions

    3/24

    Page 3 of 24

    B7. What is Polymorphism?

    Ans. Polymorphism means having many forms that in a single entity can takes more than one form.

    Polymorphism is implemented through operator overloading and function overloading.

    B8. What is Dynamic Binding?

    Ans. Dynamic binding is the process of resolving the function to be associated with the respective

    functions calls during their runtime rather than compile time.

    B9. What do you mean by Message Passing?

    Ans. Every data in an object in oops that is capable of processing request known as message .All

    object can communicate with each other by sending message to each other

    Advanced Concepts:

    1. What do you mean by Procedure Oriented Programming (POPS)?

    Ans.a. This approach divides the code into functions& the data is passed from one function to

    another.

    b. Focus is on procedures (functions) i.e. Function centric.c. Procedures are dissociated from data & are not part of it.d. Data is not secured.e. Any Data can be manipulated by any function; there is no binding between function & its

    related data.

    f. Debugging will involve a visual inspection of the entire code.g. Compilers that implement Procedure Oriented Programming systems do not prevent

    unauthorized functions from accessing & manipulating structure variables or structure datamembers.

    h. Every piece of code that access the structure will have to be visually inspected & tested againto ensure that it does not corrupt the data or data members which it should not even access.

    i. It is the lack of data security that led to the new system of programming called Object-Oriented Programming.

    2. What do you mean by Object-Oriented Programming (OOPS)?

    Ans.

    a.

    In this approach we try to bring real-world object into programming.b. What is real-world object? All the real-world object has got two main integral section, Firstthe Internal parts (Properties) & Second the Interfaces (Functionalities), which we use to

    operate on them.

    c. If a perfect interface is required to work on an object, the Functions should perfectly interactwith the Properties with an exclusive rights.

    d. In Oops, with the help of new programming construct & new keywords, associated functionscan be given exclusive rights to work upon only its variables without corrupting the variables

    which it should not access.

  • 8/13/2019 1Oops Viva Questions

    4/24

    Page 4 of 24

    e. Complier that implement Oops enable data security by enforcing restrictions on theunauthorized functions to corrupt unrelated data.

    f. This enforce makes the structure to behave like real-world object.g. Using some new techniques & concepts we can bring in the Code-Reusability in the

    programming environment. Example: Inheritance, Polymorphism, etc...

    3. Difference between C & C++?Ans.

    Sl.No C ( Pops ) C++ ( Oops )

    1Procedure Oriented Programming

    approach.Object Oriented Programming approach.

    2Focuses mainly on Function ( Function

    Centric ).Focuses mainly on Data ( Data Centric ).

    3Variables should be declared in the

    declaration part only.

    Variables can be declared in any part of

    the program but before its use .

    4Structure allows only the data members

    to be declared in it.

    Structure allows data members &

    member functions to be declared in it.

    5No restrictions to access the data

    members (Data is not Secured) .

    Data members can be accessed

    depending upon the access specifiers

    (Data is not Secured).

    6 Constructor & Destructor do not exist. Constructor & Destructor do exist.

    7Variables declared to structure are

    called as Structure Variables.

    Variables declared to structure are called

    as Objects.

    8C compiler cannot compile C++

    program.C++ compiler can compile C program.

    4. What do you mean by Console Input/Output in C++?

    Ans.

    I. Console Output:-a. The Output function in C language, such as printf(), can be included in C++

    program because they are anyway defined in the standard library.

    b. There are some more ways of outputting to the console in C++, i.e. cout.c. cout console output.d. cout is an object of the class ostream_withassign the class which has been

    inherited from the base class ostream & which in turn inherited from the base class

    ios. All these classes are defined in the header file iostream.h.

    e. The

  • 8/13/2019 1Oops Viva Questions

    5/24

    Page 5 of 24

    c. cin console input.d. cin is an object of the class istream_withassign the class which has been inherited

    from the base class istream & which in turn inherited from the base class ios. All

    these classes are defined in the header file iostream.h.

    e. The >) which accepts

    operands.

    4

    On success returns number of

    characters it has read from console &

    on failure returns 0.

    No return value.

    5Should pass format specifers as the

    leading parameter.No need to use any format specifers.

    6

    Address operator should be used with

    the variable to store the read value at

    the exact location.

    No need to use any Address operator.

    6. Difference between printf & cout.

    Ans.

    Sl.No printf cout

    1printf is inbuilt function in the header

    file stdio.h.

    cout is a predefined object in the

    header file iostream.h.

    2 Function call statement. Expression statement.

    3 Accepts parameters & works on it.

    Works with the binary operator called

    insertion operator (

  • 8/13/2019 1Oops Viva Questions

    6/24

    Page 6 of 24

    7. What is a Reference Variables in C++?

    Ans.

    a. A Reference variable is nothing but a reference for the existing variable.b. Syntax: & =;c. The reference variable & existing variable are although separate entity in the OS, their address

    are same.

    d.

    Reference variable must be initialized at the time of declaration.e. After creation, reference variables just act & can be used same as like other normal variables.Example:

    int num; num = 10;

    int &ref_num = num;

    ref_num = 20;

    8. Difference between Reference variable & Pointer variable.

    Ans.

    Sl.No Pointer Variable Reference Variable

    1

    It holds the address of another existing

    variable. It points at the existing variable.

    2An extra memory is required to store

    the address of another existing variable.

    No extra memory is required as it

    directly points at the already allocated

    memory.

    3

    Dereference operator (*) is required to

    access the value stored in the stored

    address.

    It acts as the normal variable & can be

    used without any additional operator

    with it.

    4

    Address for the pointer variable can be

    initialized at the time of declaration or

    any where in the program.

    Address for the reference variable

    should be initialized at the time of

    declaration only.

    9. What is Function prototyping?

    Ans.

    a. Function prototyping is necessary in C++.b. A prototype describes the functions interface to the complier.c. It tells the return_type, function name, number of arguments & datatype of each argument

    that it takes.

    d. Syntax: ();e. Function prototype is also a statement, hence semicolon must follow it.

  • 8/13/2019 1Oops Viva Questions

    7/24

    Page 7 of 24

    f. Providing names to the formal arguments is optional in function prototyping, even ifprovided they need not match with the name providing in the function definition & function

    call.

    10. Why prototyping is important in C++?

    Ans.

    a.

    The return value of a function is handled correctly.b. Correct number & type of arguments are passed to a function.c. To make use of function overloading.

    11. What is Function Overloading?

    Ans.

    a. Function overloading is also known as Function Polymorphism.b. C++ allows two or more functions to have same name, but with different signatures.c. Signature of a function means the number, type & sequence of arguments of the function.d. The compiler decides which function is to be called based upon the number, type & sequence of

    parameters that are passed to the function call.

    e. Function Overloading exhibits static polymorphism.12. Explain Default Values for Formal Arguments of Functions.

    Ans.

    a. It is possible in C++ to specify default values for some or all of the formal arguments of afunction.

    b. If no value is passed for an argument when the function is called, the default value specified forit is passed.

    c. If parameters are passed in the normal fashion for such an argument, the default value isignored.

    d. Default values must be supplied starting from the rightmost argument.e. Default values must be specified in function prototypes only, they should not be specified in

    function definition.

    13. What is an Inline Function?

    Ans.

    a. Inline functions are used to increase the speed of execution of executable files.b. An inline function is a function is a function whose compiled code is in line with the rest of the

    program.

    c. With inline code, the program does not have to jump to another location to execute the code andthen jump back.

    d. The executable program itself becomes so large that occupies a lot of space in the computersmemory during run-time when the inline function is called repeatedly.

    e. Under some circumstance the complier issues warning on the inline functions:-a. If the function is recursive.b. If there are looping constructs in the function.c. If there are static variables in the function.

  • 8/13/2019 1Oops Viva Questions

    8/24

    Page 8 of 24

    14. Explain Structure in C++ ?

    Ans.

    a. Structure in C++ has been redefined even to allow functions to be part of it as MemberFunctions.

    b. This means not only the data members can be accessed from the variables of the structure nutalso the members functions.

    c.

    In a member function, one can refer directly without using variable-to-member access operatorto the members of the structure variable for which the member function is called.

    d. Each structure variable contains a separate copy of the member data within itself, however onlyone copy of the member function exits for all the structure variables.

    e. There is a use of access specifiers to protect the data members.f. By default the access specifier of members of structures are public.

    15. What is Class?

    Ans.

    a. C++ introduces a new keyword class as a substitute for the keyword struct.b. Class can be used to create the user defined datatype which consist not only data member but

    also the member functions too.

    c. In a member function, one can refer directly without using variable-to-member access operatorto the members of the class objects for which the member function is called.

    d. Each object contains a separate copy of the member data within itself, however only one copy ofthe member function exits for all the objects of the same class.

    e. There is a use of access specifiers to protect the data members.f. By default the access specifier of members of class are private.

    16. Difference between Structures in C++ & Classes?

    Ans.

    Sl.NO Structure in C++ Class1 The Keyword used here is struct. The Keyword used here is class.

    2By default the structure members are

    public.

    By default the structure members are

    private.

    17. Difference between Structure in C & Structure in C++.

    Ans.

    Sl.No Structure in C Structure in C++

    1

    Only data members can be declared

    inside the structure.

    Data members & Member functions can

    be declared inside the structure.

    2Variables declared to structure are called

    as Structure-Variables.

    Variables declared to structure are called

    as Objects.

    3No restriction to access the data members

    by any function the program.

    Depending upon the access specifiers the

    functions are allowed to access the data

    members.

    4 There is data security Data security is high.

  • 8/13/2019 1Oops Viva Questions

    9/24

    Page 9 of 24

    18. Difference between Structure in C & Class.

    Ans.

    Sl.No Structure in C Class

    1 The Keyword used here is struct. The Keyword used here is class.

    2Only data members can be declared

    inside the structure.

    Data members & Member functions can

    be declared inside the class.

    3 Variables declared to structure are calledas Structure-Variables.

    Variables declared to class are called asObjects.

    4No restriction to access the data members

    by any function the program.

    Depending upon the access specifiers the

    functions are allowed to access the data

    members.

    5 There is data security Data security is high.

    19. What do you mean by Access Specifiers?

    Ans.

    a. First of all there are 3 types of Access Specifiers:-i.

    Public.ii. Private.iii. Protected.

    b. This access specifiers puts some restriction on the function & member function for accessing thedata members or member function, the concept Data Security comes alive from this above

    statement.

    20. What is an Object?

    Ans.

    a. First to be clear, if we talk generally an Object is a real world entity which holds someproperties & some functionalities, these properties & functionalities are binded together to

    perform some task.

    b. The main motto of the OOPS is bringing the real-world object into the programming, as theevery object in the world contains 2 major parts, even the Object inside the programming should

    contain the same 2 major parts, that is what we declare the Data Members (Properties) &

    Member Functions (Functionalities) using class.

    c. Any variable declared to class are called as Object, as it contains the 2 major part, what a real-world object should contain.

    d. Objects are called as Instances of the Class.21. Relation Between Class & Objects.

    Ans.a. Objects are instance of the Class.b. Class is the Basic Structure of Objects.

    22. What is Scope Resolution Operator?

    Ans.

    a. As the name indicates it resolves the scope of any member of the class.b. The scope resolution operator is used to define the scope of the member when we use or define

    the member outside the class.

  • 8/13/2019 1Oops Viva Questions

    10/24

    Page 10 of 24

    c. As per the C++ constructs two or more classes can contain same name to the member function,when ever this same name members are used outside the class, clearly to tell which Function or

    which Data belongs to which class is done by Scope resolution operator.

    23. What is this pointer?

    Ans.

    a.

    The 2 major terms to remember of this pointer are:-i. It is a Constant pointer.ii. It points at the address of the calling object.

    b. Any member function is called the leading parameter of the calling function is going to be thispointer of the type of the class for which the calling object belongs.

    c. With the help of this pointer we can access the members of the same class without usingaccess-tomember operator.

    24. How many this pointer exists in the C++ program?

    Ans. Number this pointer is equal the number of Objects created, at any point of time only one this

    pointer is used.

    25. What is Data Abstraction?

    Ans.

    a. Based on some function calls, some data can be changed.b. The change in one data can reflect or can change some other data internally without manual

    interaction.

    c. The Dependency of one data on other which reacts internally is known as Data Abstraction.26. What is Arrow Operator (->)?

    Ans.

    a. When we create object for the class, we use Access-to-Member Operator (.) to access the anymember of the class.

    Exp: class Sample; Sample Obj1;

    Obj1.Member1 or Obj1.Fun();

    b. When we create pointer object for the class, we use Arrow Operator (->) to access the anymember of the class.

    Exp: class Sample; Sample *Obj1;

    Obj1->Member1 or Obj1->Fun();

    27. What do you mean by Overloaded Member functions?

    Ans. Defining or declaring two or more member function in the same class is known as OverloadedMember Functions.

    28. What do you mean by Inline Member functions?

    Ans. We Can create the Inline Member function of the class in two ways:-

    a. By defining the function inside the Class.b. By using the keyword inline as the prefix in the function definition header outside the class.

  • 8/13/2019 1Oops Viva Questions

    11/24

    Page 11 of 24

    29. What is Constant Member function?

    Ans. By making the member function as Constant the data of the data member inside this class can be

    only read but cannot change the value.

    30. What is Mutable Data Members?

    Ans. If the Data Member should be even changed in the constant function than it should be declared asconstant data member by prefixing the keyword mutable.

    31. What is a Friend Function?

    Ans. A friend function is a non-member function that has special rights to acces private data members

    of any object of the class of whom it is a friend.

    32. What is a Friend Class?

    Ans.

    a. A Class can be friend of another Class.b. Member functions of a friend class can access the private data member of objects of the class

    of which it is a friend.

    33.What is Static Data members?

    Ans.

    a. Static Data member hold global data that is common to all objects of the class.b. To a Data member as Static prefix the keyword static.c. Change of this value from one object reflects in all other objects of the class.d. This is a member of class but not the member of any object of the class.e. There is only one copy of static data member in the memory for n-objects of the class.f. By Creating object for the class, the memory is allocated for all the member of the class except

    Static data members.g. The memory for static data member should be allocated by the user explicitly, using the

    following syntax:-

    i. ::;ii. ::=;

    h. Static data members can be initialized inside the class itself.i. If the static data member has been initialized inside the class the same member is not supposed

    to be initialized at the time of allocation of memory for it.

    class Sample

    {int Num;

    static int Count;

    public:

    . . . . . . . .

    . . . . . . . .

    . . . . . . . .

    };

    Sample Obj1,Obj2,Obj3;

  • 8/13/2019 1Oops Viva Questions

    12/24

    Page 12 of 24

    34. What is Static Member function?

    Ans.

    a. Value of Static Data member can be changed only through Static Member Functions.b. Static Member functions can be called without a calling object.c. Static Member functions can be called without respect to any object of the Class.d. Static Member functions can be called without respect to class.

    35. Relation between Objects & Functions.

    Ans. Mainly 3 relation exists between Objects & Functions:-

    a. Functions Can be called with respect to object.b. Objects can be passed as parameters to functions.c. Objects can be returned from the functions.

    36. Relation between Objects & Arrays.

    Ans. Mainly 2 relation exists between Objects & Arrays:-

    a. Arrays of Objects.- Declaring array of objects: Sample Obj[10];

    b. Arrays inside Objects.- Declaring array of any type inside the class:

    class Sample

    {

    int Num[10];

    . . . . . . . . . . .

    . . . . . . . . . . .

    };

    37. What is Namespaces?

    Ans.a. Namespaces enable the C++ programmer to prevent pollution of the global namespace that leads

    to name clashes.

    b. The Name of each class is visible in the entire source code, i.e. in the global namespace.c. Whenever the class name is same in 2 or more header files than it lends to a problem for the

    compiler.

    d. To overcome this problem each header file or class should be placed in a separate space &should be given unique name space to it, and it is known as Namesapce.

    38. What is Nested Classes?

    Ans.a. A Class can be defined inside another class, which is known as Nested class.b. The Class Containing nested class is known as Enclosing class.c. Nested class can be defined in the Public, Private or Protected portions.d. By defining the class as a nested class, we avoid a name collision.e. The size of an enclosing class is not affected by the presence of the nested class.f. The member function of nested class can be defined outside the enclosing class by using the

    enclosing class name followed by scope resolution operator followed by name of the nested

    class followed by scope resolution operator followed by member function name.

  • 8/13/2019 1Oops Viva Questions

    13/24

    Page 13 of 24

    class Out

    {

    class In

    {

    void Set();

    };

    }

    void Out::In::Set()

    {

    . . . . . .

    }

    g. An object of the nested class can be member of enclosing class.h. Creation of the nested class does not happen by creating an object for enclosing class.

    39. What is the difference between new operator & malloc() ?

    Ans.

    Sl.NO mallloc() new operator

    1 It is an inbuilt function. It is an operator.

    2Number of the bytes which should be

    allocated is passed as parameter.

    No need of passing the number of bytes it

    should allocate, by having the type of data

    it allocates.

    3

    Explicit typecasting is required to convert

    the Void pointer type into desired pointer

    type.

    The typecasting happens implicitly.

    4To allocate n-blocks of memory calloc()

    is used.

    To allocate n-blocks of memnory new[] is

    used.

    5If the desired block of memory is not

    available the function returns NULL.

    If the desired block of memory is not

    available it calls the set_new_handler()

    function.

    40. What is the difference between delete operator & free()?

    Ans.

    Sl.NO free() delete operator

    1 It is an inbuilt function. It is an operator.

    2 It just deallocates the memory block.It deallocates the memory block & returns

    to the OS.

    3 Results in Memory leak. No memory leak

    4To deallocate n-blocks of memory the

    same function is used.

    To deallocate n-blocks of memory new[]

    is used.

    41. Explain set_new_handler() function.

    Ans.

    a. It is a global function.b. Whenever there is an out-of-memory condition this function is called.

  • 8/13/2019 1Oops Viva Questions

    14/24

    Page 14 of 24

    c. This function on call return the address of the previous handler function.d. User can handle the error as he wants by making use of set_new_handler function.e. This function takes function pointer as the formal argument.

    42. What is a Constructor?

    Ans.

    a.

    It is a member function which has same name as the class.b. It has no return type.c. It is called automatically whenever the object is created.d. The Constructor cannot be a static member function.e. There are 4types of Constructors:-

    i. Default Constructors.ii. Zero argument Constructors.

    iii. Parameterized Constructors.iv. Copy Constructors.

    43. What is Default Constructors?

    Ans.

    a. It is the Constructors which is declared by the complier whenever user defined Constructorsdoest exist.

    b. It is a Constructors with empty body, which does nothing.44. What is Zero argument Constructors?

    Ans.

    a. It is a constructor which is defined by the user.b. It is a Constructor which takes no argument or no parameter.c. This can be a empty body constructor or even the user can define anything statement in it.d. Usually this is used to initialize the data members by literal values.

    45. What is Parameterized Constructor?

    Ans.

    a. It is a constructor which is defined by the user.b. It is a Constructor which takes one or more argument or parameter.c. This Constructor should not be defined without declaring Zero Argument Constructor..d. Usually this is used to initialize the data members by passing the actual value at the time of

    object declaration.

    46. What is Copy Constructor?Ans.

    a. It is a constructor which is defined by the user.b. It is constructor which takes the leading parameter of the pointer type of the same class of it.c. This Constructor is used to copy Object to other of the same class at the time of declaration.d. This Constructor should not be defined without declaring Zero Argument Constructor..

    47. When the Copy Constructor will be called?

    Ans. The Copy Constructor will be called on 3 situation:-

  • 8/13/2019 1Oops Viva Questions

    15/24

    Page 15 of 24

    a. When existing object is initialized with the new object declaration.b. When we pass object as parameter to any function.c. When an object is returned from the function.

    48. What is a Destructor?

    Ans.

    a.

    It is a member function which has same name as the class with tilde (~) character prefixed..b. It has no return type.c. It is called automatically whenever the object is going out of scope or deleted.d. The Destructor cannot be a static member function.e. Usually this is used to deallocate the memory which is allocated outside the object.

    49. What do you mean by Inheritance?

    Ans.

    a. Inheritance is a concept where a class may be created in such a way that it automaticallyincludes or inherits the data member & member functions of an exiting class.

    b. The class to which the data members & member functions are inherited is known as Derived-Class or Child-Class or Sub-Class.

    c. The class from which the data members & member functions will be inherited is known asBase-Class or Parent-Class or Super-Class.

    d. There is no any relation between derived class object & base class object.e. By creating derived class object, it turn it will never create base class object.f. The Members of base class can be accessed from derived class object the same way as we

    access the members of derived class.

    50. What are the Effects of Inheritance?

    Ans. Inheritance affects the size & behavior of derived class objects in 2 ways:-

    a. The size of the derived class object will be always greater than the size of the base class, i.e.Size of derived class = Sum of size of data members + size of base class.

    b. The member functions of derived class can access the member of base class.51. What are the Benefits of Inheritance?

    Ans.

    a. Inheritance is another feature of C++ that enables code reusability.b. The base class can have only the generic common definition.c. The data & functions that are common to more than one class can be put together in the base

    class.

    d. While only the special ones can be put in the derived class.52. Base Class & Derived Class Pointers.

    Ans.

    a. Base class pointer can point at the object of derived class.b. Derived class pointer cannot point at the object of base class.

  • 8/13/2019 1Oops Viva Questions

    16/24

    Page 16 of 24

    Reason:- For Example that the following code of inheritance-

    class Base

    {

    int x;

    public:

    . . . . . . . .

    . . . . . . . .};

    class Derived:public Base

    {

    int y;

    public:

    . . . . . . . .

    . . . . . . . .

    };

    Base *Obj1,Obj2;

    Derived *Obj3,Obj4;

    a. Base class pointer can point at the object of derived class:- As the memory allocation of thederived class object even contains the memory of the base class members, the pointer of the base

    class can point at the portion of memory of the base class itself.

    Obj1 = &Obj4;

    b. Derived class pointer cannot point at the object of base class:-As the memory allocation of thebase class object doest contains the memory of the derived class members, the pointer of the

    derived class cant find its portion to point at.Obj3 = &Obj2;

    x

    y

    1002

    Obj4:1002

    *Obj1

    1004

    1002

    Obj2:1002

    x*Obj3

    ?

  • 8/13/2019 1Oops Viva Questions

    17/24

    Page 17 of 24

    53. What is Function Overriding?

    Ans.

    a. When the derived class declares the member function with same name & same signature as thefunction declared in the base class, the function overriding occurs.

    b. Now, the derived class contains two member functions of the same name & same signature.c. If that function is called with respect to the object of derived class, the compiler calls the

    function one declared in the derived class by overriding the function one declared in the baseclass.

    54. How to initialize the base class data members using the constructor of derived class?

    Ans.

    a. While passing the values to the constructor of derived class, we need to know how manyvalues are required by the base class constructor for initialization.

    b. While creating the object of the derived class the user should pass the number of valuesrequired by the base class constructor plus the number of values required by the derived class

    constructor.

    c. While defining the constructor of the derived class, we need to pass the required values withthe base class name to the base class constructor.

    Syntax: Derived(int x,int y):Base(y)

    55. Explain deriving a class by different access specifiers.

    Ans. The Derived Class can be derived by 3 different access specifiers:-

    Public Protected Private

    a. Deriving by Public specifier:-i. Member functions of derived class can access.

    ii. Member functions of subsequently derived class can access.iii. Non-member functions can access.

    b. Deriving by Protected specifier:-i. Member functions of derived class can access.

    ii. Member functions of subsequently derived class can access.iii. Non-member functions cannot access.

    c. Deriving by Private specifier:-i. Member functions of derived class cannot access.

    ii. Member functions of subsequently derived class cannot access.iii. Non-member functions cannot access.

    Deriving class by different Access

    Specifers

    Public Protected Private

    Public Public Protected Private

    Protected Protected Protected PrivateAccess

    Specifers

    ofData

    Members

    Private Private Private Private

  • 8/13/2019 1Oops Viva Questions

    18/24

    Page 18 of 24

    56. What are the Different Kinds of Inheritance?

    Ans. There are 4 types of Inheritance:-

    1. Multiple Inheritance.2. Multi-level Inheritance.3. Hierarchical Inheritance.4. Hybrid Inheritance.

    57. What is a Virtual base Class?

    Ans. Whenever there is a multiple copy is inherited to derived class, the Virtual class avoids the

    complier to copy the same copy of data member & member functions to the derived class.

    58. What are the ambiguities in Multiple Inheritance?

    Ans.

    a. Identical Members in more than One base class:- The first ambiguity arises if two or more of thebase class have a member of the same name.

    b. Diamond-Shaped Inheritance:- Ambiguities can also arise if two or more base classes in turninherit from common base class.

    Note: The above 2 ambiguity can be solved using Scope resolution Operator & Function

    overriding, even the third solution also available, i.e. by declaring the top base class as Virtual

    base class.

    59. What do you mean by Diamond-Shaped Inheritance & what is the importance of it?

    Ans.

    a. It is a hybrid inheritance, which is combination of Hierarchical & Multiple Inheritance.b. The importance of the Diamond-Shaped Inheritance is that, it yields to a ambiguity of having

    inherited same copy of data members & member functions twice to the same derived class.

    60. What is the order of Invocation of Constructors & Destructors?Ans. Constructors are called in the following order:-

    1. Virtual base class constructors in the order of declaration.2. Non-Virtual base class constructors in the order of declaration.3. Member objects constructors in the order of declaration.4. Derived class constructor.

    Destructors are called in the reverse order.

    61. What is a Virtual Functions?

    Ans. Virtual function provides one of the most useful & powerful features of C++ called Dynamic

    polymorphism.

    62. What is Dynamic polymorphism?

    Ans.

    1. Basically there are 2 types of polymorphism, i.e. (i). Static Polymorphism & (ii). DynamicPolymorphism.

    2. Static Polymorphism is also known as Function Overloading.3. In static polymorphism the binding of function call & function definition is done at compile

    time. i.e. Which function call calls which function is known before execution.

  • 8/13/2019 1Oops Viva Questions

    19/24

    Page 19 of 24

    4. In Dynamic polymorphism the binding of function call & function definition is done at run time.i.e. Which function call calls which function is known only during execution.

    63. Explain Mechanism of Virtual Function?

    Ans. The 3 key terms should be remembered to understand Mechanism of Virtual Function:-

    1. Generic class: The class which contains one or more virtual functions.2.

    VTAB: Virtual TABle.3. VPTR: Virtual PoinTeR.

    a. Every Generic Class in C++ program will have a Virtual Table.b. The Object Created to Generic Class acts as a Virtual Pointer which points at the Virtual Table.c. The VTAB contains the addresses of the Virtual Functions defined in the class.d. Addresses of Non-Virtual function doest appears in the VTAB.e. The Address in the VTAB will be set or created based on the following 3 condition:-

    i. The Address of the Virtual function will be retained from the base class VTAB in theVTAB of the derived class if the Virtual function of the same name is not declared in the

    derived class.

    ii. The Address of the Virtual function will be created newly in the derived class VTAB ifthe Virtual Function is declared of the same name as the base class Virtual Function.

    iii. The address in the derived class VTAB will be created newly for the Virtual functiondeclared newly in the derived class.

    64. What do you mean by Pure Virtual Functions?

    Ans. The Virtual Function which has no body but equated to Zero.

    65. What do you mean by Abstract Base Class?

    Ans. Any Base class which has got one or more Pure Virtual Functions is known as Abstract Base

    Class or in short ABC.

    66. What is Virtual Destructors?

    Ans. If we declare Destructor as virtual in the base class, the virtual Destructor will be not called with

    respect to the object of Derived class.

    67. What is Virtual Constructor?

    Ans. First of all it should be clear that, we cant define Virtual Constructor.

    Reason: Declaring as virtual constructor results in compile-time error, as the base class constructor

    will not be called with respect to the object of derived class, and if some initialization will be

    missed in the base class constructor.

    68. What do you mean by Streams?

    Ans. Streams means flow of data.

    There are 2 types of Streams:-

    i. Input Streams:- It is the flow of data from a file to program variables, keyboard can also actas source file.

    ii. Output Streams:- It is the flow of data from program variables to a file, the monitor is alsotreated as a target for output stream.

  • 8/13/2019 1Oops Viva Questions

    20/24

    Page 20 of 24

    69. Give the Class Hierarchy for Handling Streams.

    Ans.

    i. The Class ios is the base class in the hierarchy.ii. The class ostream os derived from the class ios and handles the general output stream.

    iii. The class istream os derived from the class ios and handles the general input stream.iv. The insertion operator () is defined in the istream class.

    vi. The class ostream_withassign is derived from class ostream.vii. The class istream_withassign is derived from class istream.

    viii. cout is an object of the class ostream_withassign.ix. cin is an object of the class istream_withassign.x. The class iostream is derived by multiple inheritance from the classes istream & ostream, it

    has the functionality to handle both input & output streams.

    xi. The class ofstream is derived from the class ostream, It has the functionality to handle outputstream to disk files.

    Example:-

    ofstream ofile(first.dat);

    ofilex;

    70. What are the different types of methods used to achieve Text output in C++?Ans. There are 2 methods:-

    1. The insertion operator.2. The put() function.

    71. Explain outputting different type of data on to the text file using Insertion operator?

    Ans.

    a. The insertion operator has been defined & overloaded in the class ostream.

    ios

    istream ostream

    ifstream iostream

    istream_withassi n

    ofstream

    ostream_withassi nfstream

  • 8/13/2019 1Oops Viva Questions

    21/24

    Page 21 of 24

    b. It takes an object of the class ostream or an object of a class that is derived from the classostream as its left-hand side operand.

    c. On its right-hand side it takes a value of one of the fundamental data types.d. It copies the value on its right into the file that is associated with the object on its left.

    Working process of Insertion operator on different datatypes:-

    a.

    Inserting characters into output streams:-1. A character type value occupies one byte in the memory.2. It occupies one byte in the output file too.3. The bit setting of both the bytes is identical.

    b. Inserting integers into output streams:-1. An integer type value occupies four bytes in the memory.2. The number of bytes it occupies in the output file depends upon the value.3. Depending upon the number of digits the value holds, that many characters will be copied to

    text file, if its negative value number of digits + 1 characters.

    c. Inserting floats & doubles into output streams:-1. An integer type value occupies four bytes in the memory.2. The number of bytes it occupies in the output file depends upon the value.3. Depending upon the number of digits in both the parts of the value with the decimal point

    character, that many characters will be copied to text file, if its negative value (number of

    digits in both the part + decimal point character + 1) characters.

    d. Inserting strings into output streams:-1. A character array is allocated a fixed number of bytes in the memory during runtime.2. The actual string contained in it usually occupies only a part of that memory.3. In the output text file the number of characters in the actual string is occupied in the file.

    72. Explain put() function?

    Ans.a. The put() function is a member of the ostream class.b. This function copies the character that is passed as a parameter to it into the output file

    associated with the object with respect to which the function is called.

    73. What are the different types of methods used to achieve Text output in C++?

    Ans. There are 3 methods:-

    1. The Extraction operator.2. The get() function.3. The getline() function.

    71. Explain inputting different type of data from the text file using Extraction operator?

    Ans.

    a. The extraction operator has been defined & overloaded in the class istream.b. It takes an object of the class istream or an object of a class that is derived from the class

    istream as its left-hand side operand.

    c. On its right-hand side it takes a variable of one of the fundamental data types.d. It copies the value into its right from the file that is associated with the object on its left.

  • 8/13/2019 1Oops Viva Questions

    22/24

    Page 22 of 24

    Working process of Extraction operator on different datatypes:-

    a. Extracting characters into output streams:-1. It reads one byte from the input file that is attached with the object on its left and writes it

    into the variable.

    b. Extracting integers into output streams:-1. It reads bytes from the input file that is attached with the object on its left until it finds a

    white space.2. It reckons that the read set of bytes represents an integer in base 10 format.3. Therefore, the extraction operator converts the read value into base 2 format.4. Finally, it writes the converted value into the variable.

    c. Extracting floats & doubles into output streams:-Values for float & double type variables are extracted in the same as they are for integers.

    d. Extracting strings into output streams:-1. As in the case of integers, the extraction operator reads from the file until it finds a white

    space, even the string will be read until it encounters whitespace.

    72. Explain get() function?

    Ans.

    a. The get() function is a member of the istream class.b. This function reads one byte from the input file & stores it in the character variable that is

    passed as a parameter to it.

    73. Explain getline() function?

    Ans.

    a. The getline() function reads one line from the input file.b. It has been defined in the class istream.c. It takes 3 parameters:

    i. Name of the character array in which the read line will be stored.ii. An integer, signifies the number of bytes that will be read from the input file.

    iii. Delimiting character.74. What do you mean by operator overloading?

    Ans. Overloading an operator means programming an operator to work on operands of the types it has

    not yet been designed to operate.

    75. How does the compiler interpret the Operator-Overloading functions?

    Ans.

    a. The statement:-S3 = S1 + S2;is interpreted as,

    S3 = S1.operator + (S2); If the function is member function.

    S3 = operator + (S1,S2); If the function is friend function.

  • 8/13/2019 1Oops Viva Questions

    23/24

    Page 23 of 24

    76. Why are operators overloaded?

    Ans.

    a. The operator-overloading functions can be easily substituted by member functions or friendfunction with ordinary but meaningful and relevant names.

    b. However, operator overloading becomes mandatory under the following circumstances:1. Objects of the class acquire resources dynamically during run time & no two objects should

    share the same copy of the resources.2. Objects of the class acquire some resources dynamically during run time & no two objectsshould share even different copies of the resources.

    3. Objects need to be passed as parameters in function templates & the operators being used ontemplate class objects within the template functions should work in the same way on objects

    of the class.

    4. The default action of the dynamic memory management operators (new & delete) areunsuitable for the class being designed.

    5. Change in the implementation of the class forces an undesirable change in its interface inturn necessitating the rewriting & recompiling of the application programs.

    77. What are rules for Operator-Overloading?

    Ans.

    1. New operators cannot be created.2. Meaning of existing operators cannot be changed.3. Some of the existing operators cannot be overloaded:-

    a. ::b. .c. .*d. ?:e. Sizeoff. typeid

    4. Some operators can be overloaded using non-static member functions only:a. ==b. ()c. []d. ->

    5. Number of arguments that an existing operator takes cannot be changed.6. Overloaded operators cannot take default arguments.

    78. How to create pre-increment/decrement & post-increment/decrement using increment/decrement

    operator?Ans.

    a. The post-increment/decrement operator can be overloaded by zero argument if it is amember function.

    b. The pre-increment/decrement operator can be overloaded by passing one dummyargument if it is a member function.

    c. The post-increment/decrement operator can be overloaded by passing one object as anargument if it is a friend function.

  • 8/13/2019 1Oops Viva Questions

    24/24

    d. The pre-increment/decrement operator can be overloaded by passing object as a leadingargument & one dummy argument if it is a friend function.

    79. What is the syntax for overloading Insertion () operator. Explain the complete

    syntax format.

    Ans.

    a.

    Insertion Operator ((istream&, object);

    The first parameter should be reference object of the type istream/ostream, the return type should be

    void or it should be reference object of the type istream/ostream, the 2nd

    parameter should the object of

    the type class & in extraction operator overloading the object of the class should be of the reference

    type.

    80. What do you mean by smart pointer?

    Ans. If we overload arrow operator (->) it is known as smart pointer.

    81. Mention types of templates?

    Ans. There are 2 types of templates:-

    i. Class Templates.ii. Function Templates.

    82. How does the Template concept work?

    Ans.

    a. The Compiler generates an actual function/class from a template only once for a givendata type.

    b. Subsequent calls with the same data type will not generate the definition again.c. Reason is that the compiler search for the exact match before looking for template.d. Since, the first function call itself generates the function/class definition, subsequent calls

    do not do so.