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()
{
      struct xx
      {
            int x=3;
            char name[] = "hello";
      };
      struct xx *s = malloc(sizeof(struct xx));
      printf("%d", s->x);
      printf("%s", s->name); 
}

Answer: Option B

Explanation:

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

2.

Determine Output:

void main()
{
      int a[] = {10,20,30,40,50}, j, *p;
      for(j=0; j<5; j++){
            printf("%d" ,*a);
            a++;
      }
      p = a;
      for(j=0; j<5; j++){
            printf("%d" ,*p); 
            p++;
      }
}

}

Answer: Option C

Explanation:

Compiler error: lvalue required
Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue.

3.

Determine Output:

void main()
{
      int c = - -2;
      printf("c=%d", c);
}

Answer: Option C

Explanation:

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.

4.

Determine Output:

#include<stdio.h>
void main()
{
      char s[]={'a','b','c','n','c','\0'}; 
      char *p, *str, *str1; 
      p=&s[3]; 
      str=p;
      str1=s;
      printf("%c", ++*p + ++*str1-32);
}

}

Answer: Option C

Explanation:

p is pointing to character '\n'. str1 is pointing to character 'a'.
++*p: "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it is incremented to 11. the value of ++*p is 11.
++*str1: "str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98.
Both 11 and 98 is added and result is subtracted f

5.

Determine Output:

void main()
{
      int i=1, j=2;
      switch(i)
      {
            case 1: printf("GOOD"); break;
            case j: printf("BAD"); break;
      }
}

Answer: Option C

Explanation:

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.

6.

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.

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()
{
      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-

9.

Determine Output:

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

Answer: Option A

Explanation:

10.

Determine Output:

void main()
{
      extern int i;
      i=20;
      printf("%d", sizeof(i));
}

Answer: Option D

Explanation:

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.

11.

Determine Output:

void main()
{
      char far *farther, *farthest;
      printf("%d..%d", sizeof(farther), sizeof(farthest));
}

Answer: Option A

Explanation:

The second pointer is of char type and is not a far pointer.

12.

Determine Output:

#define int char
void main()
{
      int i = 65;
      printf("sizeof(i)=%d", sizeof(i));
}

Answer: Option B

Explanation:

Since the #define replaces the string int by the macro char.

13.

Determine Output:

void main()
{
      char *p;
      p="Hello";
      printf("%c", *&*p);
}

Answer: Option B

Explanation:

* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H.

14.

Determine Output:

void main()
{
      int const *p=5;
      printf("%d", ++(*p));
}

Answer: Option D

Explanation:

p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

15.

Determine Output:

void main()
{
      char *str1 = "abcd";
      char str2[] = "abcd";
      printf("%d %d %d", sizeof(str1), sizeof(str2), sizeof("abcd"));
}

}

Answer: Option A

Explanation:

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.