February 18, 2019

Srikaanth

Samsung SDS Frequently Asked C++ Interview Questions Answers

Carry Out Conversion Of One Object Of User-defined Type To Another?

To perform conversion from one user-defined type to another we need to provide conversion function. Following program demonstrates how to provide such conversion function.

class circle
{
    private
       int radius ;
       public
          circle ( int r = 0 )
          {
             radius = r ;
          }
} ;
class rectangle
{
     private
        int length, breadth ;
        public
           rectangle( int l, int b )
           {
              length = l ;
              breadth = b ;
           }
           operator circle( )
           {
               return circle ( length ) ;
           }
} ;
void main( )
{
    rectangle r ( 20, 10 ) ;
    circle c;
    c = r ;
}

Here, when the statement c = r ; is executed the compiler searches for an overloaded assignment operator in the class circle which accepts the object of type rectangle. Since there is no such overloaded assignment operator, the conversion operator function that converts the rectangle object to the circle object is searched in the rectangle class. We have provided such a conversion function in the rectangle class. This conversion operator function returns a circle object. By default conversion operators have the name and return type same as the object type to which it converts to. Here the type of the object is circle and hence the name of the operator function as well as the return type is circle.

Write A Program That Implements A Date Class Containing Day, Month And Year As Data Members. Implement Assignment Operator And Copy Constructor In This Class.

This is shown in following program

#include

class date
{
     private
        int day ;
        int month ;
        int year ;
     public
        date ( int d = 0, int m = 0, int y = 0 )
        {
            day = d ;
            month = m ;
            year = y ;
        }
        // copy constructor
        date ( date &d )
        {
            day = d.day ;
            month = d.month ;
            year = d.year ;
        }
        // an overloaded assignment operator
        date operator = ( date d )
        {
            day = d.day ;
            month = d.month ;
            year = d.year ;
            return d ;
        }
        void display( )
        {
            cout << day << "/" << month << "/" << year ;
        }
} ;
void main( )
{
        date d1 ( 25, 9, 1979 ) ;
        date d2 = d1 ;
        date d3 ;
        d3 = d2 ;
        d3.display( ) ;
}
Samsung SDS Frequently Asked C++ Interview Questions Answers
Samsung SDS Frequently Asked C++ Interview Questions Answers

Write About C++ Storage Classes?

The storage classes are qualifiers that are used to specify the lifetime of a variable. The lifetime of a variable relates to the portion of the program that has access to the variable. Storage class directs the compiler about how to store the variable in memory and how to access variable within the program.
The following are the different types of storage classes

 Auto —Identifies the local variable as automatic, which means that each invocation of the statement block in which the variable is defined gets a fresh copy with its own memory space and with re-initialization each time.
 Static —Refers that the scope of a static local variable begins inside the statement block in which the variable is declared and ends when the block terminates. The variable itself retains its value between executions of the statement block.
 Extern —Declares a global variable in one program that can be accessed by another program. The default value of an extern variable is zero. This variable is useful in a scenario where we divide one large program into different small programs and use external variable in each small program. The main advantage of using external variable is that the complexity of a program can be reduced by separating a large program into smaller programs and using external variable, which is shared by all the programs.
 Register —Refers that a variable declared with the register storage class is the same as an auto variable except that the program cannot take the variable's address. Its purpose is to allow the programmer to specify conditions under which the program's performance would be improved if certain local and automatic variables were maintained in one of the computer's hardware registers.

What Are The Advantages Of Using Const Reference Arguments In A Function?

The following are the advantages of using the const reference arguments in a function
a. Protects against errors that result in altering data of a program.
b. Allows the processing of const and non-const actual arguments by the function. On the contrary, in the prototype, the acceptance of only non-constant arguments is done by the function without a const.
c. Allows the generation and usage of a temporary variable by the function appropriately.

What Kind Of Problems Can Be Solved By A Namespace?

The namespace feature is used to avoid the name collision caused due to the use of the global identifiers by multiple providers of libraries. The provider of libraries avoids such name collisions by assigning the unique namespace to the libraries. The namespace feature is a logical space which uniquely identifies a resource, such as a program or class.
The declaration of a namespace is given as follows

namespace[identifier]
{
namespace-body
}

Why Do We Use The Using Declaration?

A using declaration specifies that all the identifiers in the namespace are available to the program within the scope of the using declaration. It makes all the namespace's identifiers available to the program in the context of their own outer scope. The principle use of the using declaration is to support standard library interfaces that are well known.

Write About An Iterator Class?

Iterator class provides an access to the classes which are inside the containers (it holds a group of objects in an organized way). The containers include the data structure, class, and abstract data type. Each container type supports one category of iterator depending on the container's requirements. The categories are input, output, forward, bidirectional, and random access. These properties specify the behavior that the iterator must exhibit in order to support the container. Iterators can be initialized, incremented, and decremented, and their bounds can be limited to the current extent of the containers. If you can cause an iterator to be equal to another iterator by incrementing the first one, the second iterator is reachable from the first. The two iterators are also known to refer to the same container. The two iterators can therefore define a range of objects in the container.

Write About The Stack Unwinding?

Stack unwinding is a process in which a destructor is invoked in a particular program for destroying all the local objects in the stack between throwing and catching of an exception.

In Which Situation The Program Terminates Before Reaching The Breakpoint Set By The User At The Beginning Of The Mainq Method?

In C++, the initialization of variables is allowed before the calling of the main() method. If a function is invoked by the global variable's initialization and termination is done, then it results in the error before entering of the main() method.

If I Is An Integer Variable, Which Is Faster ++i Or I++?

In ++i which is a pre-increment method, there is only one instruction which is needed for incrementing the variable for returning a new value. In the case of i++ which is a post-incremental method, there is a need for two instructions -one for saving the old compiler which is used in the expression and the other for the incrementation of the variable. In the post-incremental method, first the old value is returned and then the variable is incremented. This process is slower than the pre-incremental process.

How Can I Disable The "echo" Feature?

The disabling of the echo feature is done with the help of the getpassQ function. The character keys are turned off by using the getpass(3) function.

How Can You Link A C Program With A C Function?

A C program can be linked with a C function with the help of the extern "C" linkage specification. The name mangling in C++ encodes all the codes into the symbol and therefore results in errors. Therefore, the references of these symbols should be avoided by the C compiler. One should have the knowledge about the mangled function and type-safe linkages. During the compilation, with the extern "C" linkage specification, the name mangling feature is turned off to ensure the proper linkage of the C program to the C functions.

How Are The Features Of C++ Different From C?

All the features of C are similar to C++ except some features, such as polymorphism, inheritance, operator overloading which are supported in C++ but not in C language. Both C and C++ language is similar in their functionality but C++ provides with more tools and options.

How Can A Struct In C++ Differs From A Struct In C?

The differences between struct in C++ and C are listed in the following points

 In C and C++, the variables of the structures are public; however, in C, the variable cannot be declared as private or protected. On the contrary, in C++, the variables can be declared as private or protected.
On declaring a struct in C, the addition of the struct keyword is must. On the contrary, there is no need of the struct keyword on declaring struct in C++.
 In C, structures do not have direct functions or methods (procedures). Example, pointers inside the functions. On the other hand, C++ has direct functions or methods, for example classes.
 In C, the initialization cannot be done outside the scope of a structure. However, in C++, the initialization can be done outside the scope of a structure.
 In C, the concept of inheritance is not supported. In C++, the concept of inheritance is fully supported.

What Happens When The Extern "c" Char Func (char*,waste) Executes?

On executing the extern "C" char func (char*,waste), the link to the code compiled by the C compiler can be established by the programmer because the "name mangling" (which includes the tokens that identify the function's return type and the types of its arguments) will be turn off for the function.

Write About The Access Privileges In C++ And Also Mention About Its Default Access Level?

There are three access privileges provided in C++ which are private, protected, and public. The members of a class have private access by default.The public members can be accessed by member functions and by other functions that declare an instance of the class. The private members can be accessed by member functions of the same class. The protected members are included by using the class inheritance. The protected members can be accessed only within the class.

Describe The Role Of The C++ In The Tradeoff Of Safety Vs. Usability?

Earlier in C, encapsulation is meant to hide the information. Encapsulation is included in C by making other class members static in a class to prevent accessing the members by another module. However, the concept of multiple instances is not supported by the encapsulation because it is impossible to directly make multiple instances inside the static class module. If there is a need of multiple instances, a struct is used. On the contrary, encapsulation is not supported by structs.
In C++, a class supports both encapsulation and multiple instances. The class's interface that contains the public member functions and friend functions of the class can be accessed by multiple users. On the other hand, an implementation of the class is defined by the private and protected parts of a class. This encapsulated part forms a struct; thereby, reducing the tradeoff (loosing of one quality as a result of gaining of another quality) between the encapsulation and usability.

Is Recursion Allowed In Inline Functions?

Syntactically, the recursion (calling of the function by itself) is allowed in inline function but practically, the inline functions and their properties do not remain inside the program. Moreover, the compiler is not sure about the depth of the recursion at the time of compilation.

Write About The Scope Resolution Operator?

The scope resolution operator (::) can be used to define the member functions of a program outside the boundary of a class and not within the class specifier. The global scope resolution operator, which is coded as a prefix to the variable's name (for example,:: varname), lets you explicitly reference a global variable from a scope in which a local variable has the same name.

A program can use scope resolution operator (::) to bypass the override of a base class member that a derived class has overridden.

Is It Possible For A Member Function To Delete The Pointer, Named This?

It is possible for a member function to use delete this pointer but on certain conditions.
The conditions which allow the member function to use delete this are listed in the following order

Make sure that the allocation of this (current) object is done through new [] operator only.
Make sure that the invocation of your member function done on this (current) object will be the last member function. It made the calling of the last member function by the current object easy.
Make sure that calling of other member functions and data members should not be done after the line of code which includes delete this.
Examination or comparison of this pointer with other pointers and with NULL, printing or casting it, must be avoided after using the delete this line.

Name The Debugging Methods That Are Used To Solve Problems?

The following are the debugging methods used by C++ to solve problems

 GOB—Allows debugging of the global objects which have the reference
 Forte —Used as a debugger for building the application based on high performance
 Visual ^:udio —Traces the program's source code and supports break-points, watchpoints, and threads
 Tusc —Traces the last call of the system before its termination

Write About The Various Sections Of The Executable Image?

The sections of an executable image are as follows
a. The different sections of data, such as the section of the data which is not initialized and the initialized section of the variable of the data
b. The specific section of the code
c. The allocation of all the static variables is done in the variable section which is initialized.

Subscribe to get more Posts :