The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
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.
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; }
Option: C
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.
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; }
B. EOF and feof() both returns the same value✘
C. EOF and feof() both returns different values✔
Option: C
EOF returns -1 and feof() function returns 0. So the else part is executed.
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; }
Option: D
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.
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*/
Option: A
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.
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; }
Option: D
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.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019