By PDMACpayday loans

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

Texas Instruments India latest placement paper written theory / theoretical questions for b.tech engineering ece – electronics and communication, cse – computer science, IT branches test paper questions. Technical / tech interview detailed questions and answers for IT companies papers for computer languages C, C++ and other subjects.

What is Data Conversion?
Ans: Assignments between types whether they are basic or user-defined, are handled by the compiler. If the variables are of different basic types compiler calls a special routine to convert the value. But if we want to convert between user-defined data type and basic types we have to write conversion routine ourselves. A conversion routine to convert user-defined data type string to integer is shown below:
class string
{
private :
char str[20] ;
public :
string( )
{
}
string ( char *s )
{
strcpy ( str, s ) ;
}
operator int( )
{
return 123 ; // Write logic to convert string to integer
}
} ;
main( )
{
string s2 = “123″ ;
int i1 = int ( s2 ) ;
cout << endl << i1 ;
}

How to obtain type information using typeid( ) operator?
Ans: typeid( ) operator takes an object, a reference or a pointer and returns its type. Following program shows how to use the typeid( ) operator.
#include
#include
class Base
{
public :
virtual void show( )
{
}
};

class Der1 : public Base
{
} ;
void main( )
{
Base *b1 ;
cout << endl << typeid ( b1 ).name( ) ;

Der1 d1 ;
b1 = &d1 ;
cout << endl << typeid ( *b1 ).name( ) ;
cout << endl << typeid ( 12 ).name( ) << endl << typeid ( 12.5 ).name( ) ;
}
The output of this program will be
Base*
Der1
int
double
RTTI operators must be used for polymorphic class (class having virtual function) only. For non-polymorphic class static type information is returned.

How to use RTTI with class templates?
Ans: Templates can generate different classes. We may wish to get the type of class, which we are working in. The following program shows how to use RTTI operator typeid( ) with class template.
#include
#include
template
class base
{
public :
base( )
{
cout << typeid ( *this ).name( ) << “Constructor” << endl ;
}
T add ( T a, T b )
{
return a + b ;
}
~base( )
{
cout << typeid ( *this ).name( ) << “Destructor” << endl ;
}
} ;
void main( )
{
base b1 ;
cout << b1.add ( 10, 20 ) << endl ;
base b2 ;
cout << b2.add ( 5.5, 10.5 ) << endl ;
}

We can use following C++ operators for typecasting.static_cast is used for castless conversions, narrowing conversions, conversion from void* and implicit type conversions. const_cast is used to convert a const to a non-const. reinterpret_cast is used to assign one kind of pointer to another.

What will be the output of the following program?
#include
class A
{
public :
A( )
{
cout << “Reached in Constructor\n” ;
}
} ;
void main( )
{
A a( ) ;
A b ;
}
Output : Reached in Constructor

Constructor gets called only once when the object b is created. When the statement A a( ) ; gets executed constructor does not get called. This is because compiler takes this statement as a prototype declaration of function a( ) that returns an object of class A. However, if we pass arguments like
A a ( 10 ) ;
Compiler would search for one argument constructor and if not found would flash an error.

What is a container?
Ans: A container is an object that holds other objects. Various collection classes like List, Hash Table, AbstractArray, etc. are the examples of containers. We can use the classes to hold objects of any derived classes. The containers provide various methods using which we can get the number of objects stored in the container and iterate through the objects stored in it.

One can declare several function templates with the same name and even declare a combination of function templates and ordinary functions with the same name. When an overloaded function is called, overload resolution is necessary to find the right function or template function to invoke.
For example:
template < class T > T sqrt ( T ) ;
template < class T > complex < T > sqrt ( complex < T > ) ;double sqrt (
double ) ;
void f ( complex < double > z )
{
sqrt ( 2 ) ; // sqrt < int > ( int )
sqrt ( 2.0 ) ; // sqrt ( double )
sqrt ( z ) ; // sqrt < complex < double > ( complex < double > )
}
In the same way that a template function is a generalization of the notion of a function, the rules for resolution in the presence of function templates are generalizations of the function overload resolution rules. Basically, for each template we find the specialization that is best for the set of function arguments. Then we apply the usual function overload resolution rules to these specializations and all ordinary functions.

Exception Handling in C++
In C++ we can handle run-time errors generated by c++ classes by using three new keywords: throw, catch, and try. We also have to create an exception class. If during the course of execution of a member function of
this class a run-time error occurs, then this member function informs the application that an error has occurred. This process of informing is called ‘throwing’ an exception. The following code shows how to deal with exception handling.
class sample
{
public :
class errorclass
{
} ;
void fun( )
{
if ( some error occurs )
throw errorclass( ) // throws exception
}
} ;
//application
void main( )
{
try
{
sample s ;
s.fun( ) ;
}
catch ( sample::errorclass )
{
// do something about the error
}
}

Consider the following code:
#include
class base
{
public :
int data ;
} ;
class d1 : public base
{
} ;
class d2 : public base
{
} ;
class der : public d1, public d2
{
public :
void showdata( )
{
cout << data ;
}
} ;
void main( )
{
der d ;
d.showdata( ) ;
}
If you run this program it is bound to give you errors. This is because of the rules of inheritance:

1. Each base class not specified virtual will have its own sub-object representing it. In the above program, if we create object of d1 it will have a sub-object of class base containing a data member data. If we create an object of class der it will have sub-objects of classes d1 and d2 and both the sub-objects will refer to a separate copy of data. Hence, to access data from class der we will have to mention the class name. For example, d1::data or d2::data.

2. If we want that only one sub-object should exist we must use the concept of virtual base class. The single object of this will represent every base class of given name that is specified to be virtual
class. After making d1 and d2 as virtual base class if we create an object of der only one sub-object would exist and so accessing data would no longer give us errors.

How to declare a pointer to a member function?
Ans: Suppose, I wish to declare a pointer to a member function that receives an int and returns an int. I will have to declare it as int (A::* ) ( int ). Following is an example.
#include
class A
{
public :
int fun ( int f )
{
cout << “in fun\n” ;

return f * f ;
}
} ;
typedef int ( A:: *pfun ) ( int ) ;

void main( )
{
pfun p = A::fun ;
A a ;
int s = ( a.*p ) ( 6 ) ;
cout << s ;
}

What is the disadvantage of a template function?
Ans: A template function cannot be distributed in the obj form. This is because, with which parameters the template function is going to be called is decided at the run time only. Therefore an obj form of a template function cannot be made by merely compiling it.

How to declare a pointer to the data members of a class?
Ans: Following program shows how to declare a pointer to non-function members of a class.
#include
class A
{
public :

int a ;
void print( )
{
cout << a ;
}
} ;
void main( )
{
int A::*pa = &A::a ;

A obj ;
obj.*pa = 20 ;
obj.print( ) ;
}
Here, we have initialised the data member a using the pointer pa.

How to allocate memory for a multidimensional array dynamically?
Ans: Many times we need to allocate memory for a multidimensional array dynamically. Because of complexity of pointers many find this difficult. Following program allocates memory for a 3 x 3 array dynamically, copies contents of a 3 x 3 array in it and prints the contents using the pointer.
#include
#include
int a[ ][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
} ;
void main( )
{
int **p ;
p = new int *[3] ;
for ( int i = 0 ; i < 3 ; i++ )
p[i] = new int[3] ;
for ( i = 0 ; i < 3 ; i++ )
for ( int j = 0 ; j < 3 ; j++ )
p[i][j] = a[i][j] ;
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
cout << p[i][j] ;
cout << “\n” ;
}
}

Complex programming questions from IT, Electronics, Chip Design Company placement paper with programs, error finding questions from latest electronics related test paper, etc.