Texas Instruments Placement Paper technical questions – C, C++ language coding, programming questions with programs, output and detailed explanation
How do I write code to make an object work like a 2-D array?
Ans: Take a look at the following program.
int a ;
int c = 1 ;
for ( int i = 0 ; i <= 2 ; i++ )
for ( int j = 0 ; j <= 2 ; j++ )
a[i][j] = c ;
int* operator ( int i )
return a[i] ;
void main( )
emp e ;
cout << e ;
The class emp has an overloaded operator [ ] function. It takes one argument an integer representing an array index and returns an int pointer. The statement cout << e ; would get converted into a call to the overloaded [ ] function as e.operator[ ] ( 0 ). 0 would get collected in i. The function would return a[i] that represents the base address of the zeroeth row. Next the statement would get expanded as base address of zeroeth row that can be further expanded as *( base address + 1 ). This gives us a value in zeroth row and first column.
What are formatting flags in ios class?
Ans: The ios class contains formatting flags that help users to format the stream data. Formatting flags are a set of enum definitions. There are two types of formatting flags:
Flags that work in-group
The On/Off flags are turned on using the setf( ) function and are turned off using the unsetf( ) function. To set the On/Off flags, the one argument setf( ) function is used. The flags working in groups are set through the two-argument setf( ) function. For example, to left justify a string we can set the flag as,
cout.setf ( ios::left ) ;
cout << “KICIT Nagpur” ;
To remove the left justification for subsequent output we can say,
cout.unsetf ( ios::left ) ;
The flags that can be set/unset include skipws, showbase, showpoint,
uppercase, showpos, unitbuf and stdio. The flags that work in a group can have only one of these flags set at a time.
What is the purpose of ios::basefield in the following statement?
cout.setf ( ios::hex, ios::basefield ) ;
Ans: This is an example of formatting flags that work in a group. There is a flag for each numbering system (base) like decimal, octal and hexadecimal. Collectively, these flags are referred to as basefield and are specified by ios::basefield flag. We can have only one of these flags on at a time. If we set the hex flag as setf ( ios::hex ) then we will set the hex bit but we won’t clear the dec bit resulting in undefined behavior. The solution is to call setf( ) as setf ( ios::hex, ios::basefield ). This call first clears all the bits and then sets the hex bit.
Can we get the value of ios format flags?
Ans: Yes! The ios::flags( ) member function gives the value format flags. This function takes no arguments and returns a long ( typedefed to fmtflags) that contains the current format flags.
Is there any function that can skip certain number of characters present in the input stream?
Ans: Yes! This can be done using cin::ignore( ) function. The prototype of this function is as shown below:
istream& ignore ( int n = 1, int d =EOF ) ;
Sometimes it happens that some extra characters are left in the input stream while taking the input such as, the ?\n? (Enter) character. This extra character is then passed to the next input and may pose problem.
To get rid of such extra characters the cin::ignore( ) function is used. This is equivalent to fflush ( stdin ) used in C language. This function ignores the first n characters (if present) in the input stream, stops if delimiter d is encountered.
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.
Ans: This is shown in following program:
int day ;
int month ;
int year ;
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( ) ;
C, C++ complex technical / tech interview questions with detailed solutions, answers for engineering students placement papers – electronics and communication engineering students ece, computer science cse, electrical engineering students, mechanical, IT, etc. All research engineers recruitment exam test paper questions and Texas Instruments technical hr interview questions are provided.
When should I use unitbuf flag?
Ans: The unit buffering (unitbuf) flag should be turned on when we want to ensure that each character is output as soon as it is inserted into an output stream. The same can be done using unbuffered output but unit buffering provides a better performance than the unbuffered output.
What are manipulators?
Ans: Manipulators are the instructions to the output stream to modify the output in various ways. The manipulators provide a clean and easy way for formatted output in comparison to the formatting flags of the ios class. When manipulators are used, the formatting instructions are inserted directly into the stream. Manipulators are of two types, those that take an argument and those that don?t.
What is the difference between the manipulator and setf( ) function?
Ans: The difference between the manipulator and setf( ) function are as follows:
The setf( ) function is used to set the flags of the ios but manipulators directly insert the formatting instructions into the stream. We can create user-defined manipulators but setf( ) function uses data members of ios class only. The flags put on through the setf( ) function can be put off through unsetf( ) function. Such flexibility is not available with manipulators.
How do I get the current position of the file pointer?
Ans: We can get the current position of the file pointer by using the tellp( ) member function of ostream class or tellg( ) member function of istream class. These functions return (in bytes) positions of put pointer and get pointer respectively.
What are put and get pointers?
Ans: These are the long integers associated with the streams. The value present in the put pointer specifies the byte number in the file from where next write would take place in the file. The get pointer specifies the byte number in the file from where the next reading should take place.
This is part two of Texas Instruments India Part one of Technical questions for test paper and tech interview. See all part one to four.