Subscribe:Posts Comments | Read Articles on Various Topics - Pro Articles

By PDMACpayday loans

You Are Here: Home » Sample Placement Papers, Texas Instruments Test Papers » Texas Instruments Technical Theoretical Questions Part five

  1. Why is it necessary to use a reference in the argument to the copy constructor?
    Ans : If we pass the copy constructor the argument by value, its copy would get constructed using the copy constructor. This means the copy constructor would call itself to make this copy. This process would go on and on until the compiler runs out of memory. This can be explained with the help of following example:
    class sample
    {
    int i ;
    public :
    sample ( sample p )
    {
    i = p.i ;
    }
    } ;

    void main( )
    {
    sample s ;
    sample s1 ( s ) ;
    }
    While executing the statement sample s1 ( s ), the copy constructor would get called. As the copy construct here accepts a value, the value of s would be passed which would get collected in p. We can think of this statement as sample p = s. Here p is getting created and initialized. Means again the copy constructor would get called. This would result into recursive calls. Hence we must use a reference as an argument in a copy constructor.

  2. 46. Virtual Multiple Inheritance:
    A class b is defined having member variable i. Suppose two classes d1 and d2 are derived from class b and a class multiple is derived from both d1 and d2. If variable i is accessed from a member function of multiple then it gives error as ‘member is ambiguous’. To avoid this error derive classes d1 and d2 with modifier virtual as shown in the following program.
    #include
    class b
    {
    public :
    int i ;
    public :
    fun( )
    {
    i = 0 ;
    }
    } ;
    class d1 : virtual public b
    {
    public :
    fun( )
    {
    i = 1 ;
    }
    } ;
    class d2 : virtual public b
    {
    public :
    fun( )
    {
    i = 2 ;
    }
    } ;
    class multiple : public d1, public d2
    {
    public :
    fun( )
    {
    i = 10 ;
    }
    } ;
    void main( )
    {
    multiple d ;
    d.fun( ) ;
    cout << d.i ;
    }

  3. Can we use this pointer in a class specific, operator-overloading function for new operator?
    Ans: No! The this pointer is never passed to the overloaded operator new() member function because this function gets called before the object is created. Hence there is no question of the this pointer getting passed to operator new( ).

  4. Can we allocate memory dynamically for a reference?
    Ans: No! It is not possible to allocate memory dynamically for a reference. This is because, when we create a reference, it gets tied with some variable of its type. Now, if we try to allocate memory dynamically for a reference, it is not possible to mention that to which variable the reference would get tied.

  5. When should I overload new operator on a global basis or a class basis?
    Ans: We overload operator new in our program, when we want to initialize a data item or a class object at the same place where it has been allocated memory. The following example shows how to overload new operator on global basis.
    #include
    #include
    void * operator new ( size_t s )
    {
    void *q = malloc ( s ) ;
    return q ;
    }
    void main( )
    {
    int *p = new int ;
    *p = 25 ;
    cout << *p ;
    }
    When the operator new is overloaded on global basis it becomes impossible to initialize the data members of a class as different classes may have different types of data members. The following example shows how to
    overload new operator on class-by-class basis.
    #include
    #include
    class sample
    {
    int i ;
    public :
    void* operator new ( size_t s, int ii )
    {
    sample *q = ( sample * ) malloc ( s ) ;
    q -> i = ii ;
    return q ;
    }
    } ;
    class sample1
    {
    float f ;
    public :
    void* operator new ( size_t s, float ff )
    {
    sample1 *q = ( sample1 * ) malloc ( s ) ;
    q -> f = ff ;
    return q ;
    }
    } ;
    void main( )
    {
    sample *s = new ( 7 ) sample ;
    sample1 *s1 = new ( 5.6f ) sample1 ;
    }
    Overloading the operator new on class-by-class basis makes it possible to allocate memory for an object and initialize its data members at the same place.

  6. How would you define a pointer to a data member of the type pointer to pointer?
    Ans: The following program demonstrates this…
    #include
    class sample
    {
    public :
    sample ( int **pp )
    {
    p = pp ;
    }
    int **p ;
    } ;
    int **sample::*ptr = &sample::p ;
    void main( )
    {
    int i = 9 ;
    int *pi = &i ;
    sample s ( &pi ) ;
    cout << ** ( s.*ptr ) ;
    }
    Here, ptr is the pointer to data member p of class sample, which in turn is a pointer pointing to an int.

  7. How do I write a code to catch multiple types of exceptions in one single catch block?
    Ans: The following program demonstrates the use of a single catch block to catch multiple exceptions.
    #include
    class test
    {
    } ;
    class sample
    {
    public :
    void fun1( )
    {
    throw 99 ;
    }
    void fun2( )
    {
    throw 3.14f ;
    }
    void fun3( )
    {
    throw “error” ;
    }
    void fun4( )
    {
    throw test( ) ;
    }
    } ;
    void main( )
    {
    try
    {
    sample s ;
    s.fun4( ) ;
    s.fun1( ) ;
    s.fun2( ) ;
    s.fun3( ) ;
    }
    catch ( … )
    {
    cout << “strange” ;
    }
    }
    Here, different types of exceptions are thrown by the member functions of the class sample. While catching the exception instead of four different catch blocks we can as well define one single catch block. Note the syntax for defining the catch block, where we have used three dots (?) in the formal parameter list. This indicates that any thrown exception should get caught in the same catch block. When the exception is thrown from the fun4( ) control reaches the catch block, ignoring the rest of the calls.

  8. Can we return an error value from the constructor of a class?
    Ans: No. We cannot return any error value from the constructor, as the constructor doesn’t have any return type. However, by throwing an exception we can pass value to catch block. This is shown in the following example:
    #include
    class sample
    {
    public :
    sample ( int i )
    {
    if ( i == 0 )
    throw “error” ;
    }
    } ;
    void main( )
    {
    try
    {
    sample s ( 0 ) ;
    }
    catch ( char * str )
    {
    cout << str ;
    }
    }
    In this program, the statement throw “error” ; would throw an exception when an object s of the class sample would get created. The catch block would collect the string error.

  9. How do I define the member function of a template class, which has to be defined outside the template class. The function receives an object of its own class as a parameter and returns the value of the same type.
    Ans: The following example shows how we can define such a function.
    sample sample::fun ( sample s )
    {
    // code
    }
    Here, the first sample indicates the return type of the function and the next sample is used for the scope of function.

  10. How name mangling can be prevented?
    Ans: To avoid name mangling the function should be declared with an extern “C” attribute. Functions declared as extern “C” are treated as C-style functions. Hence the compiler does not mangle them. The following code snippet shows how to declare such a function.
    #include
    extern “C” void display( )
    {
    cout << “See the effect of C in C++ ” ;
    }
    void main( )
    {
    display( ) ;
    }

  11. Can we allocate memory dynamically for a reference?
    Ans: No, it is not possible to allocate memory dynamically for a reference. A reference is initialized at the time of creation. Trying to allocate memory dynamically for a reference creates a problem in initializing it. Thus, the compiler does not allow us to dynamically allocate the memory for references.

  12. What is RTTI?
    Ans: RTTI stands for ‘Run Time Type Information’. We use virtual function mechanism where we can call derived class’s member functions using base class’s pointer. However, many times we wish to know the exact type of the object. We can know the type of the object using RTTI. A function that returns the type of the object is known as RTTI functions. C++ supports two ways to obtain information about the object’s class at run time, they are typeid( ) operator and dynamic_cast operator.

Its the texas instruments part five of the series of programming and technical written test theory questions / theoretical questions.  Texas Instruments Placement Paper technical questions and tech interview questions.

Leave a Reply

© Copyright 2010 Latestexams. All Rights Reserved | Privacy Policy | Terms & Conditions