The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Files Interview Questions

In most of the MNC interview questions such as in ZOHO interview question, IVTL Infoview interview questions, Amazon interview questions, GOOGLE interview questions, Infosys interview questions and even in Voonik interview questions, We come across several Tricky C Questions about which 2:5 of the questions are from Files in c. Solving that kind of tricky C questions is not an easy task for all C programmers. We need more practices to solve it with ease. So we provide 25+ interesting C questions in Files to make your MNC interview very easy.

C Files Questions

16.What will be the output of the C program?

/*The file datafile.txt is already exists.*/
/*This program uses the datafile.txt which contains the following data*/
/*Hai. This is 2braces.com.*/
#include<stdio.h>
int main()
{
	char ch;
	FILE *fptr;
	fptr = fopen("datafile.txt", "r+");
	fprintf(fptr, "Welcome. ");
	fclose(fptr);
	fptr = fopen("datafile.txt", "r");
	while((ch = fgetc(fptr))!= EOF)
	{    
		printf("%c",ch);
	}
	fclose(fptr);
	return 0;
}

A. Welcome.

B. Welcome. Hai. This is 2braces.com.

C. Welcome. is 2braces.com.

D. None of the above

x

 

Option: C

Explanation

In C files, the r+ mode is used to read and write the existing file. If the file is already exists it overwrites the data.

Answer


17.What will be the output of the C program?

/*The file datafile.txt is already exists.*/
/*This program uses the datafile.txt which contains the following data*/
/*Hai. This is 2braces.com.*/
#include<stdio.h>
int main()
{
	char *str;
	fptr = fopen("datafile.txt", "r");
	fgets(str, 22, fptr);
	printf("%s", str);
	return 0;
}

A. Hai. This is 2braces.com

B. Hai. This is 2braces.

C. Hai. This is 2braces.c

D. Garbage Values

x

 

Option: B

Explanation

The fgets() function is used read string from a file. In this program we pass three parameters in fgets() function. The first parameter str is used to store the string from a file. The second parameter is used to define the length and fptr is a file pointer which points to the file.

Answer


18.What will be the output of the C program?

/*This program uses the file "int_numbers.txt"*/
/*4 5 6 5*/
#include<stdio.h>
int main()
{
	int num[4];
	int i = 0, tot = 0;
	FILE *fptr;
	fptr = fopen("int_numbers.txt", "r");
	if(fptr == NULL)
    {
		printf("File not exist");
    }
	for(i = 0; i < 4; i++)
    {
		fscanf(fptr, "%d,", &num[i]);
    }
	for(i = 0; i < 4; i++)
	{
		printf("%d ", num[i]);
		tot = tot + num[i];
	}
	printf("tot = %d", tot);
	fclose(fptr);
	return 0;
}

A. Compilation Error

B.4 5 6 tot = 15

C. 4 5 6 5

D. 4 5 6 5 tot = 20

x

 

Option: D

Explanation

In C, we can read numbers from a file and perform actions on it. In the above C program, the file function fscanf has three parameters. First parameter indicates the file. Second parameter is a format specifier which depends on the user's wish. If you want to read a string from a file, you have to use "%s", for integer "%d" and etc.

Answer


19.What will be the output of the C program?

/*This program uses the file "float_num.txt"*/
/*1.5 2.6 3.7 4.8*/
#include<stdio.h>

int main()
{
	float num[4];
	float i = 0, tot = 0;
	FILE *fptr;
	fptr = fopen("float_num.txt", "r");
	if(fptr == NULL)
    {
		printf("File not exist");
    }
	fclose(fptr);
	for(i = 0; i < 4; i++)
	{
		fscanf(fptr, "%f", &num[i]);
	}
	for(i = 0; i < 4; i++)
	{
		tot = tot + num[i];
	}
	printf("tot = %f", tot);
	return 0;
}

A. 12.60000

B. sum of garbage values

C. Compilation Error

D. Runtime Error

x

 

Option: B

Explanation

In the C program, the file has been closed before the for loop. So there is no way to access or read values from a file. It automatically assigns garbage values for the array elements. As a result the summation is also the garbage value or NULL.

Answer


20.What will be the output of the C program?

/*This program uses the file "float_num1.txt"*/
/*1.5, 2.6, 3.7, 4.8*/
#include<stdio.h>
int main()
{
	float num[4];
	float i = 0, tot = 0;
	FILE *fptr;
	fptr = fopen("float_num1.txt", "r");
	if(fptr == NULL)
    {
		printf("File not exist");
    }
	fclose(fptr);
	for(i = 0; i < 4; i++)
	{
		fscanf(fptr, "%f", &num[i]);
	}
	for(i = 0; i < 4; i++)
	{
		tot = tot + num[i];
	}
	printf("tot = %f", tot);
	return 0;
}

A. 12.600000

B. 12.000000

C. Null or Unexpected Result

D. Compilation Error

x

 

Option: C

Explanation

The values inside the file is seperated by ','(it is a unicode character. not a floating point value). When we try to read float values from a file we should specify the format specifier "%f". If you define values in separate line it will read those in smooth manner. If you define values separated by "," you should use like this "%f,". This will give a correct output.

Answer


Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report