AmbitionBox

Discover Best Places to work in India

C Programming

C Programming Questions and Answers

Showing 1 - 4 of 4 questions

1

Point out the error in the program?

#include < stdio.h >#include < stdlib.h >int main()
{
    unsignedchar;
    FILE *fp;
    fp=fopen("trial", "r");
    if(!fp)
    {
        printf("Unable to open file");
        exit(1);
    }
    fclose(fp);
    return0;
}
a

Error: in unsigned char statement

b

Error: unknown file pointer

c

No error

d

None of above

correct answer c
Answer :

Option C

Explanation :

This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file" and then terminate the program.
If file exists, it simply close the file and then terminates the program.

2

Point out the error in the program?

#include < stdio.h >/* Assume there is a file called 'file.c' in c:\tc directory. */int main()
{
    FILE *fp;
    fp=fopen("c:\tc\file.c", "r");    
    if(!fp) 
      printf("Unable to open file.");        

    fclose(fp);
    return0;
}
a

No error, No output.

b

Program crashes at run time.

c

Output: Unable to open file.

d

None of above

correct answer c
Answer :

Option C

Explanation :

The path of file name must be given as "c:\\tc\file.c"

3

Point out the error/warning in the program?

#include < stdio.h >int main()
{
    unsignedchar ch;
    FILE *fp;
    fp=fopen("trial", "r");
    while((ch = getc(fp))!=EOF)
        printf("%c", ch);
    fclose(fp);
    return0;
}
a

Error: in unsigned char declaration

b

Error: while statement

c

No error

d

It prints all characters in file "trial"

correct answer a
Answer :

Option A

Explanation :

Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value.

4

Point out the correct statements about the program?

#includeint 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("\n", fptr);
        }
        fclose(fptr);
    }
    return0;
}
a

The code copies the content of one file to another

b

The code writes strings that are read from the keyboard into a file.

c

The code reads a file

d

None of above

correct answer b
Answer :

Option B

Explanation :

This program get the input string from the user through gets function and store it in the file f1.txt using fputs function.

Select a company to compare with

vs

Similar Companies