Log In Register

Units


C Programming Tricky Questions - Questions and Answers

 

Practice these C Programming Tricky Questions under C Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on C Programming Tricky Questions for following purpose.


– Anyone who is wishing to increase their perfect knowledge of C Programming Tricky Questions of C Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge C Programming specifically C Programming Tricky Questions.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on C Programming Tricky Questions under C Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  C Programming Tricky Questions topic of C Programming:

 

1.

Determine Output:

void main()
{
      int i=0;
      for(;i++;printf("%d", i));
      printf("%d", i);
}

Answer: Option A

Explanation:

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).

2.

Determine Output:

#define prod(a,b)  a*b
void main()
{
      int x=3, y=4;
      printf("%d", prod(x+2, y-1));
}

}

Answer: Option B

Explanation:

The macro expands and evaluates to as:
x+2*y-1 => x+(2*y)-1 => 10

3.

Determine Output:

void main()
{
      static char *s[] = {"black", "white", "yellow", "violet"};
      char **ptr[] = {s+3, s+2, s+1, s}, ***p;
      p = ptr;
      ++p; 
      printf("%s",*--*++p + 3); 
}

}

Answer: Option D

Explanation:

In this problem we have an array of char pointers "s" pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p holds the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated as follows: *++p causes gets value s+1 then the pre decrement is executed and we get s+1-

4.

Determine Output:

void main()
{
      int c[] = {2.8,3.4,4,6.7,5};
      int j, *p=c, *q=c;
      for(j=0;j<5;j++){
            printf(" %d ", *c);
            ++q;
      }
      for(j=0;j<5;j++){
            printf(" %d ", *p);
            ++p;
      }
}

}

Answer: Option D

Explanation:

Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

5.

Determine Output:

#define clrscr() 100
void main()
{
      clrscr();
      printf("%d", clrscr());
}

Answer: Option C

Explanation:

Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs. The input program to compiler looks like this :

void main()
{
      100;
      printf("%d", 100);
}

Note: 100; is an executable statement but with no action. So it doesn't give any problem.

6.

Determine Output:

void main()
{
      int i=5;
      printf("%d%d%d%d%d", i++, i--, ++i, --i, i);
}

Answer: Option A

Explanation:

7.

Determine Output:

#define square(x) x*x
void main()
{
      int i;
      i = 64/square(4);
      printf("%d", i);
}

Answer: Option B

Explanation:

The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64

 

8.

Determine Output:

void main()
{
      float me = 1.1;
      double you = 1.1;
      if(me==you)
            printf("I hate TestMirror");
      else
            printf("I love TestMirror");
}

Answer: Option B

Explanation:

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 (== , >, <, <=, >=,!= ) .

9.

Determine Output:

void main()
{
      int i = -1;
      +i;
      printf("i = %d, +i = %d", i, +i);
}

Answer: Option C

Explanation:

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).

10.

Determine Output:

void main()
{
      char *p="hi friends", *p1;
      p1=p;
      while(*p!='\0') ++*p++;
      printf("%s", p1);
}

Answer: Option B

Explanation:

++*p++ will be parse in the given order :
1. *p that is value at the location currently pointed by p will be taken
2. ++*p the retrieved value will be incremented
3. when ; is encountered the location will be incremented that is p++ will be executed.

 

11.

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

Answer: Option B

Explanation:

The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

12.

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

Answer: Option B

Explanation:

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].

13.

Determine Output:

void main()
{
      int i=i++, j=j++, k=k++;
      printf("%d %d %d", i, j, k);
}

}

Answer: Option C

Explanation:

An identifier is available to use in program code from the point of its declaration. So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage value.

14.

Determine the Final Output:

void main()
{
      printf("\nab");
      printf("\bsi");
      printf("\rha");
}

Answer: Option D

Explanation:

\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

15.

Determine Output:

void main()
{
      int i=5, j=6, z;
      printf("%d", i+++j);
}

}

Answer: Option C

Explanation:

The expression i+++j is treated as ((i++) + j)