Here are few of the C and C++ basic written test questions and mostly asked / popular technical – tech hr interview questions from IT / software development companies placement papers. See more placement exam questions with solutions and complete details here.
-
Are the expressions *ptr++ and ++*ptr same?
Ans: No. *ptr++ increments the pointer and not the value pointed by it, whereas ++*ptr increments the value being pointed to by ptr. -
strpbrk( )
The function strpbrk( ) takes two strings as parameters. It scans the first string, to find, the first occurrence of any character appearing in the second string. The function returns a pointer to the first occurrence of the character it found in the first string. The following program demonstrates the use of string function strpbrk( ).
#include
main( )
{
char *str1 = “Hello!” ;
char *str2 = “Better” ;
char *p ;
p = strpbrk ( str1, str2 ) ;
if ( p )
printf ( “The first character found in str1 is %c”, *p ) ;
else
printf ( “The character not found” ) ;
}
The output of the above program would be the first character found in str1 is e -
Can we convert an unsigned long integer value to a string?
Ans: The function ultoa( ) can be used to convert an unsigned long integer value to a string. This function takes three arguments, first the value that is to be converted, second the base address of the buffer in which the converted number has to be stored (with a string terminating null character ‘\0′) and the last argument specifies the base to be used in converting the value. Following example demonstrates the use of this function.
#include
void main( )
{
unsigned long ul = 3234567231L ;
char str[25] ;ultoa ( ul, str, 10 ) ;
printf ( “str = %s unsigned long = %lu\n”, str, ul ) ;
} -
ceil( ) and floor( )
The math function ceil( ) takes a double value as an argument. This function finds the smallest possible integer to which the given number can be rounded up. Similarly, floor( ) being a math function, takes a double value as an argument and returns the largest possible integer to which the given double value can be rounded down. The following program demonstrates the use of both the functions.
#include
void main( )
{
double no = 1437.23167 ;
double down, up ;
down = floor ( no ) ;
up = ceil ( no ) ;
printf ( “The original number %7.5lf\n”, no ) ;
printf ( “The number rounded down %7.5lf\n”, down ) ;
printf ( “The number rounded up %7.5lf\n”, up ) ;
}
The output of this program would be,
The original number 1437.23167
The number rounded down 1437.00000
The number rounded up 1438.00000 -
How do I use function ecvt( ) in a program?
Ans: The function ecvt( ) converts a floating-point value to a null terminated string. This function takes four arguments, such as, the value to be converted to string, the number of digits to be converted to string, and two integer pointers. The two-integer pointer stores the position of the decimal point (relative to the string) and the sign of the number, respectively. If the value in a variable, used to store sign is 0, then the number is positive and, if it is non-zero, then the number is negative. The function returns a pointer to the string containing digits. Following program demonstrates the use of this function.
#include
main( )
{
char *str ;
double val ;
int dec, sign ;
int ndig = 4 ;
val = 22 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( “string = %s dec = %d sign = %d\n”, str, dec, sign ) ;
val = -345.67 ;
ndig = 8 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( “string = %s dec = %d sign = %d\n”, str, dec, sign ) ;
// number with a scientific notation
val = 3.546712e5 ;
ndig = 5 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( “string = %s dec = %d sign = %d\n”, str, dec, sign ) ;
}
The output of this program would be
string = 2200 dec = 2 sign = 0
string = 34567000 dec = 3 sign = 1
string = 35467 dec = 6 sign = 0