Thursday, October 28, 2010

C Puzzles Part 4

Here is the next set of C Riddles.  It contains questions like Find The OutputSingle Word Answer, and some Write A Program type.  I have not include the answer or the solutions for any of the questions as it does not make any sense.   If you could not arive at the  Solution, then you can mail me the question.   Also, your contributions to build this site with more Riddles will be awaited.   Happy Riddling!!!!


1. What Would be the output of the following program?


void main()
{
    const int x=get();
    printf("%d",x);
}
get(){ return(20);}
A. 20 B. Garbage Value
C. Error D. 0
2. Is the following program valid in C?


void main()
{
    printf("%d",main);
}
A. Yes B. No
3. If i and k are two integer variables with i initialized to 2,
        what will be the value of k and i after the assignment statement


k = ++i + i++ * ++i
A. k=13, i=5 B. k=20, i=5
C. k=16, i=5 D. None of the above
4. What would be the pointer expression for referring the same statement as
        a[i][j][k][l]


A. ****( a+i + j + k + l )
B. *( *( *( (*a+i) + j ) + k ) + l )
C. *( *( *( *(a+i) + j ) + k ) + l )
D. None of the above
5. What is the value returned by sizeof(a) if a is defined as
        int a[' ']


A. 32 B. 0
C. 64 D. Garbage value
6. What would be the output of the following program?


void main()
{
    float a=0.7;
    if( a < 0.7f )
        printf("This is From C");
    else
        printf("This is From C++");
}
A. This is From C B. This is From C++
C. Error D. None of the above
7. What is the output of the following program?


void main()
{
    printf("%f",sqrt(36.0));
}
A. 6.0 B. 6
C. 6.000000 D. Garbage value
Answer the following Questions ( Quest #8 to Quest #12 ):
8. Which is the only keyword in C which is also an Operator?
9. Write the smallest possible recursive program in C.
10. In which Header file is the NULL MACRO defined?
11. What does the following declaration signify?


void ( *f ) ( int, void ( * )() );
12. What is the difference between the declarations


char *const ptr
and char const *ptr
Find the Output in the following programs ( Quest #13 to Quest #25 ). If any of the following programs does not get compiled or generate error, just point out the errors.
13. #define CONDITION if ( c == 8 ) printf(" C IS EQUAL TO 8")


void main()
{
     int a=2, b=3, c=4;
     if ( a > b )
         CONDITION;
     else
         printf("A IS NOT EQUAL TO B");
}
14.        void main()


{
    int x=1, y=2;
    switch ( x++, --y, x+=y, y+=x )
    {
    case 2:
        printf("This is Case #2");
        break;
    case 3:
        printf("This is Case #3");
        break;
    case 4:
        printf("This is Case #4");
        break;
    default:
        printf("This is Default Case");
  }
}
15.  void main()
          {
              printf("\n %c", "ABCDEFGH"[4]);
              printf("\n %d", printf(5+"SIR MVIT")+10);
              printf("\n %d", printf("\n\n\n%s","Test"));
          }
16.       void main()
          {
              int a, b = 5;
              a=b+NULL;
              printf("%d",a);
              printf("\n%d %d",sizeof(NULL),sizeof(""));
          }
17.       #define CUBE(X) (X * X * X)
          void main()
          {
              int i=2,j;
              j= CUBE( CUBE( i++ ) );
              printf(" i= %d j= %d ",i,j);
          }
18.  void test();
          #pragma exit test 100
          void main()
          {
              int i=10;
              i= ++i;
              printf("i = %d",i++);
          }
          void test()
          {    
              clrscr();
              printf("Kalanjali - 99");
          }
19.  void main()
          {
              int i=20;
              printf("i = %d",i++);
          }
          auto int i=10;
20.       void main()
          {
              int i=10, j=20;
              float x=10, y=20;
              printf("\nSize of ( i + j ) = %d", sizeof(i+j));
              printf("\nSize Of ( i + x ) = %d", sizeof(i+x));
              printf("\nSize Of ( i + y ) = %d", sizeof(x=i+y));
          }
21.  void main()
          {
              char far *near *ptr1;
              char near *far *huge *ptr2;
              char far *huge *near *ptr3;
              printf(" %d %d %d ",sizeof(ptr1),sizeof(*ptr2),sizeof(**ptr3));
          }
22.       void main()
          {
              joke(1,2,3,4);
          }
          joke(int n,...)
          {
              va_list ptr;
              int num;
              num=va_arg(ptr,int);
              printf("%d",num);
          }
23.  #define MAX 100
          void main()
          {
              const int max = 200;
              char string[MAX];
              char array[max];
              array[0] = string[0] = 'A';
              printf("%s %c",string,array[0]);
          }
24.  #define Print(int) printf(" int = %d ", int)
          void main()
          {
              int x=1, y=1, z=1;
              x+=y+=z;
              ++z || ++y && ++z;
              Print(x
              printf(" X= %d Y= %d Z= %d ",x,y,z);
          }
25.       void main()
          {
              int a = 10;
              printf(" Stranger Ahead... \n");
              switch(a){};
              printf("Infinite Loop..?!!");
          }
  
26.    If the following program(test) is run from the command line as
       test one two three what would be the output?
       void main(int argc, char *argv[])
       {
          clrscr();
          printf("\n%s", *++argv);
          printf("\n%s",++*++argv);
          printf("\n%c",**++argv);
       }
27.    If the following program
       void main()
       {
           int x=4;
           float a=3.14;
           char c='A';
           TEST(x,%d);
           TEST(a,%f);
           TEST(c,%c);
       }
       generates the output
       TEST : x = 4
       TEST : a = 3.140000
       TEST : c = A
       then define the MACRO TEST.
28.   Modify the statement
       ( a > b ) ? b= 3 : c= 3;
       and generate the same effect by using the digit 3 only once.
29.   Write a statement using scanf() to accept a string including Blank Spaces.
30.   Write a function int Sum ( int count, int num,... ) which can take any number of integers as arguements and return the sum of them.
31.     Write a program to simulate the Printf function.

No comments:

Post a Comment