Pointers - C Questions

Pointers Questions - 1 Pointers Questions - 2 Pointers Questions - 3 Pointers Questions - 4 Pointers Questions - 5 Pointers Questions - 6 Pointers Questions - 7 Pointers Questions - 8

Data Types - C Questions

Data Types Questions - 1 Data Types Questions - 2 Data Types Questions - 3 Data Types Questions - 4 Data Types Questions - 5

Operators - C Questions

Operators Questions - 1 Operators Questions - 2 Operators Questions - 3 Operators Questions - 4 Operators Questions - 5

Structures - C Questions

Structures Questions - 1 Structures Questions - 2 Structures Questions - 3 Structures Questions - 4 Structures Questions - 5

Files - C Questions

Files Questions - 1 Files Questions - 2 Files Questions - 3 Files Questions - 4 Files Questions - 5

printf - C Questions

printf Questions - 1 printf Questions - 2 printf Questions - 3 printf Questions - 4 printf Questions - 5

Variables - C Questions

Variables Questions - 1 Variables Questions - 2 Variables Questions - 3 Variables Questions - 4 Variables Questions - 5

Preprocessor Macros

Preprocessor Macros - 1 Preprocessor Macros - 2 Preprocessor Macros - 3 Preprocessor Macros - 4 Preprocessor Macros - 5

While Loop - C Questions

While Loop Questions - 1 While Loop Questions - 2 While Loop Questions - 3 While Loop Questions - 4 While Loop Questions - 5

for Loop - C Questions

for Loop Questions - 1 for Loop Questions - 2 for Loop Questions - 3 for Loop Questions - 4 for Loop Questions - 5

if else - C Questions

if else Questions - 1 if else Questions - 2 if else Questions - 3 if else Questions - 4 if else Questions - 5

Switch case - C Questions

Switch case Questions - 1 Switch case Questions - 2 Switch case Questions - 3 Switch case Questions - 4 Switch case Questions - 5

Array - C Questions

Array Questions - 1 Array Questions - 2 Array Questions - 3 Array Questions - 4 Array Questions - 5

Functions - C Questions

Functions Questions - 1 Functions Questions - 2 Functions Questions - 3 Functions Questions - 4 Functions Questions - 5

Memory Allocation

Memory Allocation - 1 Memory Allocation - 2
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

We to update you