1. What will be the output of the program, if a short int is 2 bytes wide?

#include<stdio.h>
int main()
{
    short int i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
        printf("%u,", i);
    return 0;
}
  • 1 … 65535
  • Expression syntax error
  • No output
  • 0, 1, 2, 3, 4, 5

Explanation for(i5 && i>=-1; ++i; i>0) so expression i5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression.

In for( i 5 && i >= -1; ++i; i>0) expression i5 && i>=-1 evaluates to one.

Loop condition always get evaluated to true. Also at this point it increases i by one.

An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie. 65535)


2. What will be the output of the program?

#include<stdio.h>
int main()
{
    float a = 0.7;
    if(0.7 > a)
        printf("Hi\n");
    else
        printf("Hello\n");
    return 0;
}
  • Hi
  • Hello
  • Hi Hello
  • None of above

Explanation if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is greater than the float variable a. Hence the if condition is satisfied and it prints ‘Hi’


12. What will be the output of the program ?

#include<stdio.h>
 
int main()
{
    char str1[] = "Hello";
    char str2[10];
    char *t, *s;
    s = str1;
    t = str2;
    while(*t=*s)
        *t++ = *s++;
    printf("%s\n", str2);
    return 0;
}
  • Hello
  • HelloHello
  • No output
  • ello

Explanation

  • Iteration 1:
    • Condition: Copies ‘H’ from str1[0] to str2[0]. (Pointers don’t move).
    • Body: Copies ‘H’ from str1[0] to str2[0] again.
    • Increment: s moves to ‘e’, t moves to the next slot.
  • Iteration 2:
    • Condition: Copies ‘e’ from str1[1] to str2[1].
    • Body: Copies ‘e’ again.
    • Increment: s moves to ‘l’, t moves to the next slot.

18. Point out the error in the program.

#include<stdio.h>
const char *fun();
 
int main()
{
    char *ptr = fun();
    return 0;
}
const char *fun()
{
    return "Hello";
}
  • Error: Lvalue required
  • Error: cannot convert ‘const char *’ to ‘char *’.
  • No error and No output
  • None of above

20. What will be the output of the program?

#include<stdio.h>
 
int main()
{
    int i;
    char c;
    for(i=1; i<=5; i++)
    {
        scanf("%c", &c); /* given input is 'a' */
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}
  • aaaa
  • aaaaa
  • Garbage value.
  • Error in ungetc statement.

Explanation for(i=1; i5; i++) Here the for loop runs 5 times.

Loop 1:
scanf(“%c”, &c); Here we give ‘a’ as input.
printf(“%c”, c); prints the character ‘a’ which is given in the previous “scanf()” statement.
ungetc(c, stdin); “ungetc()” function pushes character ‘a’ back into input stream.

Loop 2:
Here the scanf(“%c”, &c); get the input from “stdin” because of “ungetc” function.
printf(“%c”, c); Now variable c = ‘a’. So it prints the character ‘a’.
ungetc(c, stdin); “ungetc()” function pushes character ‘a’ back into input stream.

This above process will be repeated in Loop 3Loop 4Loop 5.