C Programming

C Programming

Input-Output has 40 Questions
In a file contains the line "I am a boy " then on reading this line into the array str using fgets(). what will str contain?

"I am a boy "
"I am a boy "
"I am a boy"
"I am a boy"
What will be the content of 'file.c' after executing the following program?
#include<stdio.h>

int main()
{
    FILE *fp1, *fp2;
    fp1=fopen("file.c", "w");
    fp2=fopen("file.c", "w");
    fputc('A', fp1);
    fputc('B', fp2);
    fclose(fp1);
    fclose(fp2);
    return 0;
}

B
A
B
B
B
Error in opening file 'file1.c'
Point out the error in the program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    unsigned char;
    FILE *fp;
    fp=fopen("trial", "r");
    if(!fp)
    {
        printf("Unable to open file");
        exit(1);
    }
    fclose(fp);
    return 0;
}

Error: in unsigned char statement
Error: unknown file pointer
No error
None of above
Which of the following statement is correct about the program?
#include<stdio.h>

int main()
{
    FILE *fp;
    char ch;
    int i=1;
    fp = fopen("myfile.c", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '')
            i++;
    }
    fclose(fp);
    return 0;
}

The code counts number of characters in the file
The code counts number of words in the file
The code counts number of blank lines in the file
The code counts number of lines in the file
While calling the fprintf() function in the format string conversion specifier %s can be used to write a character string in capital letters.

True
False
stderr, stdin, stdout are FILE pointers

Yes
No
What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("source.txt", "rb");

open "source.txt" in binary mode for reading
open "source.txt" in binary mode for reading and writing
Create a new file "source.txt" for reading and writing
None of above
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int k=1;
    printf("%d == 1 is" "%s", k, k==1?"TRUE":"FALSE");
    return 0;
}

k == 1 is TRUE
1 == 1 is TRUE
1 == 1 is FALSE
K == 1 is FALSE
Point out the error in the program?
#include<stdio.h>

int main()
{
    char ch;
    int i;
    scanf("%c", &i);
    scanf("%d", &ch);
    printf("%c %d", ch, i);
    return 0;
}

Error: suspicious char to in conversion in scanf()
Error: we may not get input for second scanf() statement
No error
None of above
Which of the following statement is correct about the program?
#include<stdio.h>

int main()
{
    FILE *fp;
    char str[11], ch;
    int i=0;
    fp = fopen("INPUT.TXT", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '' || ch == ' ')
        {
            str[i]='';
            strrev(str);
            printf("%s", str);
            i=0;
        }
        else
            str[i++]=ch;
    }
    fclose(fp);
    return 0;
}

The code writes a text to a file
The code reads a text files and display its content in reverse order
The code writes a text to a file in reverse order
None of above
A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character.

True
False
A file written in text mode can be read back in binary mode.

Yes
No
What does fp point to in the program ?
#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    return 0;
}

The first character in the file
A structure which contains a char pointer which points to the first character of a file.
The name of the file.
The last character in the file.
What will be the output of the program ?
#include<stdio.h>
char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}";

int main()
{
    printf(str, 34, str, 34);
    return 0;
}

char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; main(){ printf(str, 34, str, 34);}
char *str = %c%s%c; main(){ printf(str, 34, str, 34);}
No output
Error in program
Point out the error in the program?
#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    fseek(fp, "20", SEEK_SET);
    fclose(fp);
    return 0;
}

Error: unrecognised Keyword SEEK_SET
Error: fseek() long offset value
No error
None of above
Point out the correct statements about the program?
#include<stdio.h>

int main()
{
    FILE *fptr;
    char str[80];
    fptr = fopen("f1.dat", "w");
    if(fptr == NULL)
        printf("Cannot open file");
    else
    {
        while(strlen(gets(str))>0)
        {
            fputs(str, fptr);
            fputs("", fptr);
        }
        fclose(fptr);
    }
    return 0;
}

The code copies the content of one file to another
The code writes strings that are read from the keyboard into a file.
The code reads a file
None of above
Offset used in fseek() function call can be a negative number.

True
False
Will the following program work?
#include<stdio.h>

int main()
{
    int n=5;
    printf("n=%*d", n, n);
    return 0;
}

Yes
No
Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp;
fp = fopen("NOTES.TXT", "r+");

Reading
Writing
Appending
Read and Write
If the file 'source.txt' contains a line "Be my friend" which of the following will be the output of below program?
#include<stdio.h>

int main()
{
    FILE *fs, *ft;
    char c[10];
    fs = fopen("source.txt", "r");
    c[0] = getc(fs);
    fseek(fs, 0, SEEK_END);
    fseek(fs, -3L, SEEK_CUR);
    fgets(c, 5, fs);
    puts(c);
    return 0;
}

friend
frien
end
Error in fseek();
Point out the error in the program?
#include<stdio.h>

/* Assume there is a file called 'file.c' in c:	c directory. */
int main()
{
    FILE *fp;
    fp=fopen("c:	cfile.c", "r");    
    if(!fp) 
      printf("Unable to open file.");        

    fclose(fp);
    return 0;
}

No error, No output.
Program crashes at run time.
Output: Unable to open file.
None of above
We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()

True
False
Can we specify a variable filed width in a scanf() format string?

Yes
No
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;

printf("%f %lf", a, b);
printf("%Lf %f", a, b);
printf("%Lf %Lf", a, b);
printf("%f %Lf", a, b);
What will be the output of the program ?
#include<stdio.h>

int main()
{
    float a=3.15529;
    printf("%2.1f", a);
    return 0;
}

3.00
3.15
3.2
3
Point out the error/warning in the program?
#include<stdio.h>

int main()
{
    unsigned char ch;
    FILE *fp;
    fp=fopen("trial", "r");
    while((ch = getc(fp))!=EOF)
        printf("%c", ch);
    fclose(fp);
    return 0;
}

Error: in unsigned char declaration
Error: while statement
No error
It prints all characters in file "trial"
In a call to printf() function the format specifier %b can be used to print binary equivalent of an integer.

True
False
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;
}

"A.C" "B.C" "C.C"
"B.C" "C.C"
"A.C"
Error in fclose()
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("%c", ~('C'*-1));
    return 0;
}

A
B
C
D
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;
}

r n
Trh
err
None of above
What will be the output of the program ?
#include<stdio.h>

int main()
{
    FILE *fp;
    unsigned char ch;
     /* file 'abc.c' contains "This is ASTIWZ " */
    fp=fopen("abc.c", "r");
    if(fp == NULL)
    {
        printf("Unable to open file");
        exit(1);
    }
    while((ch=getc(fp)) != EOF)
        printf("%c", ch);

    fclose(fp);
    printf("", ch);
    return 0;
}

This is ASTIWZ
This is
Infinite loop
Error
To scan a and b given below, which of the following scanf() statement will you use?
#include<stdio.h>

float a;
double b;

scanf("%f %f", &a, &b);
scanf("%Lf %Lf", &a, &b);
scanf("%f %Lf", &a, &b);
scanf("%f %lf", &a, &b);
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *p;
    p="%d";
    p++;
    p++;
    printf(p-2, 23);
    return 0;
}

21
23
Error
No output
Out of fgets() and gets() which function is safe to use?

gets()
fgets()
What will be the output of the program ?
#include<stdio.h>

int main()
{
    FILE *ptr;
    char i;
    ptr = fopen("myfile.c", "r");
    while((i=fgetc(ptr))!=NULL)
        printf("%c", i);
    return 0;
}

Print the contents of file "myfile.c"
Print the contents of file "myfile.c" upto NULL character
Infinite loop
Error in program
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", t);
    return 0;
}

size of "DUMMY.C" file
The handle associated with "DUMMY.C" file
Garbage value
Error in fileno()
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("%%%%");
    return 0;
}

%%%%%
%%
No output
Error
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int a=250;
    printf("%1d", a);
    return 0;
}

1250
2
50
250
What will be the output of the program ?
#include<stdio.h>

int main()
{
    FILE *fp;
    char ch, str[7];
    fp=fopen("try.c", "r"); /* file 'try.c' contains "This is Nagpur" */
    fseek(fp, 9L, SEEK_CUR);
    fgets(str, 5, fp);
    puts(str);
    return 0;
}

agpur
gpur
Nagp
agpu
What will be the output of the program if value 25 given to scanf()?
#include<stdio.h>

int main()
{
    int i;
    printf("%d", scanf("%d", &i));
    return 0;
}

25
2
1
5