C++ Interview Questions For Freshers

Define C++.             C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-oriented compute...

Define C++.
          C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-oriented computer language used in the development of enterprise and commercial applications.It includes a new datatype known as a class to allow object-oriented programming.
                      C++ maintains the features of C which allowed for low-level memory access but also gives the programmer new tools to simplify memory management.

Differences between C & C++.
     In case of C, the data is not secured while the data is secured(hidden) in C++
C is a low-level language while C++ is a middle-level language 
C uses the top-down approach while C++ uses the bottom-up approach
C++ supports function overloading while C does not.
C++ supports Exception Handling while C does not.

Define class
       The objects with the same data structure (attributes) and behavior (operations) are called class. 

Define object
      It is an entity which may correspond to real-world entities such as students, employees, bank account. It may be concrete such as file system or conceptual such as scheduling policies in multiprocessor operating system. Every object will have data structures called attributes and behavior called operations.

Define object based programming language.
        Object based programming language support encapsulation and object identity without supporting some important features of OOPs language. Object based language=Encapsulation + object Identity.

What are the basic concepts of object oriented programming?
          It is necessary to understand some of the concepts used extensively in object oriented programming.
These include
* Objects 
* Classes 
* Data abstraction and encapsulation 
* Inheritance 
* Polymorphism 
* Dynamic Binding 
* Message passing

What is encapsulation?
         The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Encapsulation containing and hiding information about an object, such as internal data structures and code.

Define inheritance.
        The mechanism of deriving a new class (derived) from an old class (base class) is called inheritance. It allows the extension and reuse of existing code without having to rewrite the code from scratch. Inheritance is the process by which objects of one class acquire properties of objects of another class.

What are the advantages of inheritance?
             It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.

Define polymorphism
            Polymorphism means one name, multiple forms. It allows us to have more than one function with the same name in a program.It allows us to have overloading of operators so that an operation can exhibit different behaviours in different instances.

What do you mean by inline function?
        The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

What is public, protected, private?
           Public, protected and private are three access specifier in C++. 
Public data members and member functions are accessible outside the class.
 Protected data members and member functions are only available to derived classes.
 Private data members and member functions can’t be accessed outside the class.

What is message passing?
            An object oriented program consists of a set of objects that communicate with each other. Message passing involves specifying the name of the object, the name of the function and the information to be sent.

What is the use of enumerated data type?
              An enumerated data type is another user defined type which provides a way for attaching names to numbers thereby increasing comprehensibility of the code. The enum keyword automatically enumerates a list of words by assigning them values 0,1,2, and so on.

Define Constructors
            A constructor is a member function with the same name as its class. The constructor is invoked whenever an object of its associated class is created.It is called constructor because it constructs the values of data members of the class.

Define Destuctors
            A destructor is called for a class object when that object passes out of scope or is explicitly deleted.A destructors as the name implies is used to destroy the objects that have been created by a constructors.Like a constructor , the destructor is a member function whose name is the same as the class name but is precided by a tilde.

What is Templates?
           Templates are a feature of the c++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different datatypes without being rewritten for each one.

What is a reference?
          A reference is an alias (an alternate name) for an object. It is frequently used for pass-by-reference.

What is an Iterator class?
A class that is used to traverse through the objects maintained by a container class. 
There are five categories of iterators: input iterators, output iterators, forward iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints.
                                Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order or according to some ordering relation . The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine. Iterators hide the details of access to and update of the elements of a container class. Something like a pointer.

What is a dangling pointer?
                A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.

What is a container class? What are the types of container classes?
               A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.

What is overloading?
             With the C++ language, you can overload functions and operators. Overloading is the practice of supplying more than one definition for a given function name in the same scope. - Any two functions in a set of overloaded functions must have different argument lists. - Overloading functions with argument lists of the same types, based on return type alone, is an error.

What is Overriding?
             To override a method, a subclass of the class that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list. The definition of the method overriding is: 
 * Must have same method name. 
Must have same data type. · 
* Must have same argument list. 
 Overriding a method means that replacing a method functionality in child class. To imply overriding functionality we need parent and child classes. In the child class you define the same method signature as one defined in the parent class.

What is virtual class and friend class?
             Friend classes are used when two or more classes and virtual base class aids in multiple inheritance. Virtual class is used for run time polymorphism when object is linked to procedure call at run time.

What is dynamic binding?
             Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run time.It is associated with polymorphism and inheritance.

What is an iterator?
             Iterators are like pointers. They are used to access the elements of containers thus providing a link between algorithms and containers. Iterators are defined for specific containers and used as arguments to algorithms.

What are the differences between new and malloc?
             * New initializes the allocated memory by calling the constructor. Memory allocated with new should be released with delete. 
 * Malloc allocates uninitialized memory. The allocated memory has to be released with free.
 * new automatically calls the constructor while malloc(dosen’t).

What is difference between C++ and Java?
   *  C++ has pointers Java does not. 
 * Java is the platform independent as it works on any type of operating systems. 
 * Java has no pointers where c ++ has pointers. 
 * Java has garbage collection C++ does not.

What is the difference between method overloading and method overriding?
 *Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters).
  *Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

What is the difference between realloc() and free()?
              The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. 
                                           The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.

Which is called on allocating the memory for array of objects?
                When you allocate memory for an array of objects, the default constructor must be called to construct each object. If no default constructor exists, you’re stuck needing a list of pointers to objects.

What is RTTI?
               Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many Interview Questions – Homegrown versions with a solid, consistent approach.

What is an adaptor class or Wrapper class?
              A class that has no functionality of its own. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a non-objectoriented implementation.

What is a Null object?
            It is an object of some class whose purpose is to indicate that a real object of that class does not exist. One common use for a null object is a return value from a member function that is supposed to return an object with some specified properties but cannot find such an object.

How can we access protected and private members of a class?
             In the case of members protected and private, these could not be accessed from outside the same class at which they are declared. This rule can be transgressed with the use of the friend keyword in a class, so we can allow an external function to gain access to the protected and private members of a class.

What do you mean by late binding?
             Late binding refers to function calls that are not resolved until run time. Virtual functions are used to achieve late binding. When access is via a base pointer or reference, the virtual function actually called is determined by the type of object pointed to by the pointer.

What do you mean by early binding?
               Early binding refers to the events that occur at compile time. Early binding occurs when all information needed to call a function is known at compile time. Examples of early binding include normal function calls, overloaded function calls, and overloaded operators. The advantage of early binding is efficiency.

What is an explicit constructor?
             A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It’s purpose is reserved explicitly for construction.

What problem does the namespace feature solve?
                   Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a library’s external declarations with a unique namespace that eliminates the potential for those collisions. This solution assumes that two library vendors don’t use the same namespace identifier, of course.

What are the defining traits of an object-oriented language?
The defining traits of an object-oriented langauge are: 
* encapsulation
* polymorphism


Interview Questions 5767206026671808626

Post a Comment


Follow Us

Hot in week



Side Ads