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 Questions and Answers

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

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

#include<stdio.h>
int main()
{
	char *str = "ZOHO";
	while (*str)
	{
		putc(*str, stdout);
		fputchar(*str);
		printf("%c", *str);
		str++;
	}
	return 0;
}

A. ZOHO

B. ZZOOHHOO

C. ZZZOOOHHHOOO

D. ZOHO ZOHO ZOHO

x

 

Option: C

Explanation

In the above C program, we have use three functions are all used for the same purpose(to print data in standard output screen). The putc() function is used to print a single character, the fputchar also used to print a single character in output screen and the printf statement is used to print a single character in a output screen.

Answer


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

#include<stdio.h>
int main()
{
	int f1, f2;
	FILE *fp;
	fp = fopen("datafile.txt", "w");
	f1 = EOF;
	f2 = feof(fp);
	if(f1 == f2)
	{
		printf("EOF and feof(), both returns the same value");
	}
	else
	{
		printf("EOF and feof() both returns different values");
	}
	return 0;
}
  

A. Nothing will be displayed

B. EOF and feof() both returns the same value

C. EOF and feof() both returns different values

D. Runtime Error

x

 

Option: C

Explanation

EOF returns -1 and feof() function returns 0. So the else part is executed.

Answer


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

/*This program uses the datafile.txt which contains the following data*/
/* Hai. This is 2braces.com. */
#include<stdio.h>
int main()
{
	char *str = "We are 2braces.com";
	fprintf(stdout, str);
	return 0;
}

A. Compilation Error

B. Runtime Error

C. Linker Error

D. We are 2braces.com

x

 

Option: D

Explanation

In C, stdout - standard output which is nothing but the output screen. In the above C program we had fprintf statement with two parameters(stdout and the variable name) which writes data into output screen.

Answer


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

/*creating new file and writing data into a file*/
#include<stdio.h>
int main()
{
	FILE *fptr;
	char name[40], name1[40];
	fptr = fopen("record.txt", "r+");
	if(fptr == NULL)
	{
		printf("File not exist");
		exit(1);
	}
	printf("Enter your name: ");
	gets(name);
	fputs(name, fptr);
	rewind(fptr);
	fgets(name1, 40, fptr);
	printf("Name: %s", name1);
	fclose(fptr);
	return 0;
}
/*say you give a name john*/

A. File Not Exist

B. Name: John

C. Name: John garbage value

D. Compilation Error

x

 

Option: A

Explanation

In the above C program we opens a file in "r+" mode. The "r+" mode doesnot create a new file. It only reads and write into a file(if already exists). So the fopen() function returns NULL to the fptr and if condition block executed.

Answer


15. 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", "w+");
	fclose(fptr);
	fptr = fopen("datafile.txt", "r");
	while((ch = fgetc(fptr))!= EOF)
	{    
		printf("%c",ch);
	}
	fclose(fptr);
	return 0;
}

A. Hello

B. Garbage values

C. Compilation Error

D. Nothing will be displayed

x

 

Option: D

Explanation

In C Files, w+ mode start writing into a file after truncate the existing data if the file is already exists otherwise it will creates a new file. In this progra we just opens "datafile.txt" in w+ mode and closes it. Its enough to truncate the data in a file. So when we trying to read from "datafile.txt", it prints nothing.

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