Without Book Interview Questions and Answers | Online Test | Moct Test
Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Institute Training Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Language in C Interview Questions and Answers

Ques 1. What will be the output of the following code?

void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
}

Ans. The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.

Is it helpful? Add Comment View Comments
Ques 2. Why doesn't the following code give the desired result?

int x = 3000, y = 2000 ;
long int z = x * y ;
Ans. Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below:

long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.

Is it helpful? Add Comment View Comments
Ques 3. Why doesn't the following statement work?

char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
Ans. The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below:
strcat ( str, "!" ) ;

Is it helpful? Add Comment View Comments
Ques 4. How do I know how many elements an array can hold?

Ans. The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more than 64 kb. Consider following program, which shows the maximum number of elements an array of type int, float and char can have in case of Small memory model.
main( )
{
int i[32767] ;
float f[16383] ;
char s[65535] ;
}
Is it helpful? Add Comment View Comments
Ques 5. How do I write code that reads data at memory location specified by segment and offset?

Ans. Use peekb( ) function. This function returns byte(s) read from specific segment and offset locations in memory. The following program illustrates use of this function. In this program from VDU memory we have read characters and its attributes of the first row. The information stored in file is then further read and displayed using peek( ) function.

#include <stdio.h>
#include <dos.h>

main( )
{

char far *scr = 0xB8000000 ;
FILE *fp ;
int offset ;
char ch ;

if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL )
{

printf ( "\nUnable to open file" ) ;
exit( ) ;

}

// reads and writes to file
for ( offset = 0 ; offset < 160 ; offset++ )
fprintf ( fp, "%c", peekb ( scr, offset ) ) ;
fclose ( fp ) ;

if ( ( fp = fopen ( "scr.dat", "rb" ) ) == NULL )
{

printf ( "\nUnable to open file" ) ;
exit( ) ;

}

// reads and writes to file
for ( offset = 0 ; offset < 160 ; offset++ )
{

fscanf ( fp, "%c", &ch ) ;
printf ( "%c", ch ) ;

}

fclose ( fp ) ;

}
Is it helpful? Add Comment View Comments
}
2) Why doesn't the following code give the desired result?

int x = 3000, y = 2000 ;
long int z = x * y ;
3) Why doesn't the following statement work?

char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
4) How do I know how many elements an array can hold?

5) How do I write code that reads data at memory location specified by segment and offset?

" />

Most helpful rated by users:

©2016 WithoutBook