Find Output Of Program

Question 6 :

What will be the output of the program ?

#include < stdio.h >
int main()
{
    printf("%c\n", ~('C'*-1));
    return 0;
}


A). A
B). B
C). C
D). D
Answer : Option B

Question 7 :

What will be the output of the program ?

#include < stdio.h >
int main()
{
    FILE *fp;
    unsigned char ch;
     /* file 'abc.c' contains "This is Indiaparinam " */
    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("\n", ch);
    return 0;
}


A). This is Indiaparinam
B). This is
C). Infinite loop
D). Error
Answer : Option C

Explanation :

The macro EOF means -1.
while((ch=getc(fp)) != EOF) Here getc function read the character and convert it to an integer value and store it in the variable ch, but it is declared as an unsigned char. So the while loop runs infinitely.


Question 8 :

What will be the output of the program ?

#include < stdio.h >

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


A). 21
B). 23
C). Error
D). No output
Answer : Option B

Question 9 :

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


A). Print the contents of file "myfile.c"
B). Print the contents of file "myfile.c" upto NULL character
C). Infinite loop
D). Error in program
Answer : Option C

Explanation :

The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.


Question 10 :

What will be the output of the program ?

#include < stdio.h >

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


A). %%%%%
B). %%
C). No output
D). Error
Answer : Option B