ExamVeda
Login
Home
21
Determine Output:
void main()
{
      int i=1, j=2;
      switch(i)
      {
            case 1: printf("GOOD"); break;
            case j: printf("BAD"); break;
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Compiler Error: Constant expression required in function main.
The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).

Note: Enumerated types can be used in case statements.

22
Determine Output:
void main()
{
      int i;
      printf("%d", scanf("%d", &i)); // value 10 is given as input here
}
Discuss
Answer & Solution
Answer: Option B
Solution:

scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.

23
Determine Output:
void main()
{
      int i=0;
      for(;i++;printf("%d", i));
      printf("%d", i);
}
Discuss
Answer & Solution
Answer: Option A
Solution:

Before entering into the for loop checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).

24
Determine Output:
void main()
{
      struct xx
      {
            int x=3;
            char name[] = "hello";
      };
      struct xx *s = malloc(sizeof(struct xx));
      printf("%d", s->x);
      printf("%s", s->name); 
}
Discuss
Answer & Solution
Answer: Option B
Solution:

Initialization should not be done for structure members inside the structure declaration.

25
Determine output:
void main()
{
      extern int i;
      i=20;
      printf("%d", sizeof(i));
}
Discuss
Answer & Solution
Answer: Option D
Solution:

Linker error: undefined symbol '_i'. Explanation:
extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn't find any error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error.

26
Determine Output:
void main()
{
      int i=0, j=0;
      if(i && j++)
            printf("%d..%d", i++, j);
      printf("%d..%d", i, j);
}
Discuss
Answer & Solution
Answer: Option C
Solution:

The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remains unchanged and get printed.

27
Determine Output:
void main()
{
      static int i=5;
      if(--i){
            main();
            printf("%d ", i);
      }
}
Discuss
Answer & Solution
Answer: Option B
Solution:
The variable "i" is declared as static, hence memory for I will be allocated for only once, as it encounters the statement. The function main() will be called recursively unless I becomes equal to 0, and since main() is recursively called, so the value of static I ie., 0 will be printed every time the control is returned.
28
Determine Output:
void main()
{
      int i=-1, j=-1, k=0, l=2, m;
      m = i++ && j++ && k++ || l++;
      printf("%d %d %d %d %d", i, j, k, l, m);
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression 'i++ && j++ && k++' is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for '0 || 0' combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.

29
Determine Output:
void main()
{
      int i = -1;
      +i;
      printf("i = %d, +i = %d", i, +i);
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator).

30
Determine Output:
void main()
{
      char *str1 = "abcd";
      char str2[] = "abcd";
      printf("%d %d %d", sizeof(str1), sizeof(str2), sizeof("abcd"));
}
Discuss
Answer & Solution
Answer: Option A
Solution:

In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character). The third sizeof is similar to the second one.