C Miscellaneous(C Program ) Questions and Answers

Question 1. Determine output:
void main()
{
int const *p=5;
printf("%d", ++(*p));
}
  1.    6
  2.    5
  3.    Garbage Value
  4.    Compiler Error
Explanation:-
Answer: Option D. -> Compiler Error
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

Question 2. Determine Output:
void main()
{
char s[]="man";
int i;
for(i=0; s[i]; i++)
printf("%c%c%c%c ", s[i], *(s+i), *(i+s), i[s]);
}
  1.    mmm nnn aaa
  2.    mmmm nnnn aaaa
  3.    Compiler Error
  4.    None of These
Explanation:-
Answer: Option D. -> None of These
Correct Output : mmmm aaaa nnnn
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

Question 3. Determine Output:
void main()
{
char *p;
printf("%d %d", sizeof(*p), sizeof(p));
}
  1.    1 1
  2.    1 2
  3.    2 1
  4.    2 2
Explanation:-
Answer: Option B. -> 1 2
The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

Question 4. Determine Output:
void main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I hate Examveda");
else
printf("I love Examveda");
}
  1.    I hate Examveda
  2.    I love Examveda
  3.    Error
  4.    None of These
Explanation:-
Answer: Option B. -> I love Examveda
For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== , >,

Question 5. Determine Output:
void main()
{
static int var = 5;
printf("%d ", var--);
if(var)
main();
}
  1.    5 5 5 5 5
  2.    5 4 3 2 1
  3.    Infinite Loop
  4.    None of These
Explanation:-
Answer: Option B. -> 5 4 3 2 1
When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

Question 6. Determine the Final Output:
void main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
  1.    absiha
  2.    asiha
  3.    haasi
  4.    hai
Explanation:-
Answer: Option D. -> hai
\n - newline - printf("\nab"); - Prints ab
\b - backspace - printf("\bsi"); - firstly '\b' removes 'b' from 'ab ' and then prints 'si'. So after execution of printf

Question 7. Determine Output:
void main()
{
int i=10;
i=!i>14;
printf("i=%d", i);
}
  1.    10
  2.    14
  3.    0
  4.    1
Explanation:-
Answer: Option C. -> 0
In the expression !i>14 , NOT (!) operator has more precedence than ">" symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

Question 8. Determine Output:
void main()
{
int i=3;
switch(i)
{
default: printf("zero");
case 1: printf("one"); break;
case 2: printf("two"); break;
case 3: printf("three"); break;
}
}
  1.    zero
  2.    three
  3.    Error
  4.    None of These
Explanation:-
Answer: Option B. -> three
The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

Question 9. Determine Output:
#define int char
void main()
{
int i = 65;
printf("sizeof(i)=%d", sizeof(i));
}
  1.    sizeof(i)=2
  2.    sizeof(i)=1
  3.    Compiler Error
  4.    None of These
Explanation:-
Answer: Option B. -> sizeof(i)=1
Since the #define replaces the string int by the macro char.

Question 10. Determine Output:
void main()
{
int c = - -2;
printf("c=%d", c);
}
  1.    1
  2.    -2
  3.    2
  4.    Error
Explanation:-
Answer: Option C. -> 2
Here unary minus (or negation) operator is used twice. Same maths rule applies, ie. minus * minus= plus.
Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.