

InterviewSolution
Saved Bookmarks
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
1. |
Point out the error/warning in the program? |
Answer» Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value. | |
2. |
What will be the output of the program if value given to ? |
Answer» The scanf function returns the number of input is given. printf("%d\n", scanf("%d", &i)); The scanf function returns the value 1(one). Therefore, the output of the program is '1'. | |
3. |
On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"? |
Answer» The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human". Inside the while loop, ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF. if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops. If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt. fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt. The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file. | |
4. |
Out of and which function is safe to use? |
Answer» Because, In fgets() we can specify the size of the buffer into which the string supplied will be stored. | |
5. |
are FILE pointers |
Answer» Yes, these will be declared like The corresponding stdio.h variable is FILE* stdin; The corresponding stdio.h variable is FILE* stdout; The corresponding stdio.h variable is FILE* stderr; | |
6. |
A file written in text mode can be read back in binary mode. |
Answer» The difference is that text files contain lines (or records) of text and each of these has an end-of-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line. Binary files are not broken up into separate lines or records so the end-of line marker is not written when writing to a binary file. So, we cannot read the correct the data in binary mode. | |
7. |
Can we specify a variable filed width in a format string? |
Answer» In scanf() a * in a format string after a % sign is used for the suppression of assignment. That is, the current input field is scanned but not stored. | |
8. |
While calling the function in the format string conversion specifier can be used to write a character string in capital letters. |
Answer» The %s format specifier tells the compiler the given input was string of characters. | |
9. |
A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. |
Answer» True, each line may contain zero or more characters terminated by a newline character. | |
10. |
Offset used in function call can be a negative number. |
Answer» True, offset in fseek() function can be a negative number. It makes the file pointer to move backwards from the current position. Declaration: retval = fseek( fp, offset, from ); Where: FILE *fp; = points to the file on which I/O is to be repositioned. long offset; = is an integer giving the number of bytes to move forward or backward in the file. This may be positive or negative. int from; = is one of the manifests SEEK_SET, SEEK_CUR, or SEEK_END. int retval; = is non-zero if the seek operation was invalid (e.g. on a file not opened with a "b" option); otherwise, the return value is zero. | |
11. |
We should not read after a write to a file without an intervening call to |
Answer» True, we should not be able to read a file after writing in that file without calling the below functions. int fflush ( FILE * stream ); If the given stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file. int fseek ( FILE * stream, long int offset, int origin ); Its purpose is to change the file position indicator for the specified stream. void rewind ( FILE * stream ); Sets the position indicator associated with stream to the beginning of the file. | |
12. |
In a call to function the format specifier %b can be used to print binary equivalent of an integer. |
Answer» There is no format specifier named %b in c. | |
13. |
Point out the correct statements about the program? |
Answer» This program get the input string from the user through gets function and store it in the file f1.txt using fputs function. | |
14. |
In a file contains the line "I am a boy\r\n" then on reading this line into the array using . What will contain? |
Answer» Declaration: char *fgets(char *s, int n, FILE *stream); fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first. Therefore, the string str contain "I am a boy\n\0" | |
15. |
What is the purpose of in function used below in the code? |
Answer» The file source.txt will be opened in the binary mode. | |
16. |
What does point to in the program ? |
Answer» The fp is a structure which contains a char pointer which points to the first character of a file. | |