1.
In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
Correct Answer
C. "I am a boy\n\0"
Explanation
The correct answer is "I am a boy\0".
The fgets() function reads the line from the file and stores it in the array str. It stops reading when it encounters a new line character (\n) or reaches the maximum number of characters specified. In this case, the line "I am a boy\r" is read into the array str, including the carriage return character (\r). The null character (\0) is automatically added at the end of the string to mark the end of the string. Therefore, the resulting value of str would be "I am a boy\0".
2.
What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("source.txt", "rb");
Correct Answer
A. Open "source.txt" in binary mode for reading
Explanation
The purpose of "rb" in the fopen() function is to open the file "source.txt" in binary mode for reading.
3.
What does fp point to in the program?
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("trial", "r");
return 0;
}
Correct Answer
B. A structure which contains a char pointer which points to the first character of a file.
Explanation
In this program, the variable 'fp' is declared as a pointer to a FILE structure. The 'fopen' function is used to open the file named "trial" in read mode. This function returns a pointer to the FILE structure that represents the opened file. Therefore, 'fp' points to a structure which contains a char pointer that points to the first character of the file.
4.
Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp;
fp = fopen("NOTES.TXT", "r+");
Correct Answer
D. Read and Write
Explanation
The code snippet opens the file "NOTES.TXT" with the mode "r+" which allows both reading and writing operations on the file. Therefore, the correct answer is "Read and Write".
5.
To print out a and b given below, which of the following printf() statement will you use?
#include<stdio.h>
float a=3.14;
double b=3.14;
Correct Answer
A. Printf("%f %lf", a, b);
Explanation
The correct answer is printf("%f %lf", a, b). This is because the variable "a" is of type float and the variable "b" is of type double. The format specifier "%f" is used to print a float value and the format specifier "%lf" is used to print a double value. Therefore, this printf statement is the correct one to print the values of "a" and "b".
6.
Which files will get closed through the fclose() in the following program?#include<stdio.h>
int main()
{
FILE *fs, *ft, *fp;
fp = fopen("A.C", "r");
fs = fopen("B.C", "r");
ft = fopen("C.C", "r");
fclose(fp, fs, ft);
return 0;
}
Correct Answer
D. Error in fclose()
Explanation
The correct answer is "Error in fclose()". This is because the fclose() function only takes one file pointer as an argument, not multiple file pointers. In the given program, the fclose() function is called with three file pointers (fp, fs, ft), which is incorrect syntax and will result in an error.
7.
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"?#include<stdio.h>
int main()
{
int i, fss;
char ch, source[20] = "source.txt", target[20]="target.txt", t;
FILE *fs, *ft;
fs = fopen(source, "r");
ft = fopen(target, "w");
while(1)
{
ch=getc(fs);
if(ch==EOF)
break;
else
{
fseek(fs, 4L, SEEK_CUR);
fputc(ch, ft);
}
}
return 0;
}
Correct Answer
B. Trh
Explanation
The program opens a source file named "source.txt" and a target file named "target.txt". It then reads each character from the source file using the getc() function. If the character is not the end-of-file character (EOF), it moves the file pointer of the source file forward by 4 characters using fseek(). It then writes the character to the target file using the fputc() function. This process continues until the end of the source file is reached. Therefore, the contents of the "target.txt" file will be "Trh" as the characters "T", "r", and "h" are the ones that are written to the file.
8.
To scan a and b given below, which of the following scanf() statement will you use?#include<stdio.h>
float a;
double b;
Correct Answer
D. Scanf("%f %lf", &a, &b);
Explanation
The correct answer is scanf("%f %lf", &a, &b) because the format specifier %f is used for scanning a float variable and %lf is used for scanning a double variable. Since a is a float and b is a double, this scanf statement will correctly scan both variables.
9.
Out of fgets() and gets() which function is safe to use?
Correct Answer
B. Fgets()
Explanation
The correct answer is fgets(). The gets() function is considered unsafe because it does not perform any bounds checking, which means it can easily lead to buffer overflow vulnerabilities. On the other hand, fgets() allows you to specify the maximum number of characters to read, preventing buffer overflow issues. Therefore, fgets() is the safer option to use.
10.
Consider the following program and what will be content of t?#include<stdio.h>
int main()
{
FILE *fp;
int t;
fp = fopen("DUMMY.C", "w");
t = fileno(fp);
printf("%d\n", t);
return 0;
}
Correct Answer
B. The handle associated with "DUMMY.C" file
Explanation
The fileno() function in C returns the file descriptor associated with the given file pointer. In this program, the file "DUMMY.C" is opened in write mode using the fopen() function. Then, the file descriptor of the opened file is obtained using the fileno() function and stored in the variable t. Finally, the value of t is printed, which represents the handle associated with the "DUMMY.C" file. Therefore, the content of t will be the handle associated with the "DUMMY.C" file.