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:

#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

2.

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.

3.

Determine Output:

void main()
{
      char string[]="Hello World";
      display(string);
}
void display(char *string)
{
      printf("%s", string);
}

Answer: Option B

Explanation:

Type mismatch in redeclaration of function display.
In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.

4.

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.

5.

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-

6.

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

 

7.

Determine Output:

void main()
{
      char not; 
      not = !2;
      printf("%d", not);
}

}

Answer: Option C

Explanation:

! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0.

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

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.

9.

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

10.

Determine Output:

void main()
{
      int i;
      char a[]="�"; 
      if(printf("%sn", a))
            printf("Ok here n");
      else
            printf("Forget itn");
}

}

Answer: Option A

Explanation:

Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed.

11.

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

12.

Determine Output:

void main()
{
      char *p;
      printf("%d %d", sizeof(*p), sizeof(p));
}

Answer: Option B

Explanation:

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

13.

Determine Output:

void main()
{
      printf("%p", main);
}

Answer: Option C

Explanation:

Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf() specifies that the argument is an address. They are printed as hexadecimal numbers.

14.

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.

15.

Determine Output:

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

}

Answer: Option B

Explanation:

"return(i++)", it will first return i and then increment it. i.e. 10 will be returned.