none
Sunday special quiz. RRS feed

  • Question

  • Hello guyz,

     

         Today is Sunday. So, I guess everybody is free. I came up with the idea that why we shouldn't start a quick quiz and learn something from it?

     

         I will ask question here. As soon as somebody answers it, I will tell whether it is correct or not. So, be quick to answer.

     

         I am comparatively new to forum and i have seen this kind of other threads as well. So, I am starting this one. All the best.

    Sunday, April 29, 2007 12:03 PM

Answers

  • What will be the output of following program?

     

    Code Snippet

    main()
     {
     char *p;
     p=malloc(10);
     free(p);
     printf("%d",p);
     }

     

    Sunday, April 29, 2007 12:05 PM
  • IMO, it will give compilation error.
    Sunday, April 29, 2007 12:07 PM
  • Write value of x. 

    Code Snippet

    main()

    {

    int x=10,y=15,z=16;

    x=y==z;

    printf("%d",x);

    }

     

     

    Sunday, April 29, 2007 12:11 PM
  • Value of X is 0.
    Sunday, April 29, 2007 12:13 PM
  • Can I ask question too?
    Sunday, April 29, 2007 12:14 PM
  • Of course you can ask.
    Sunday, April 29, 2007 12:15 PM
  • The value of x will be 0.
    Sunday, April 29, 2007 12:17 PM
  • Code Snippet

    int func(int n)
    {

        int ans;

        if(n<=1)

        {

             return 1;

        }

        else 

        {

            ans=func(n-3)+func(n-1);

            return ans;

        }
    }

     

    What will be value of func(6)?

     

    Sunday, April 29, 2007 12:27 PM
  • Give output of following program.

    Code Snippet

    main()
    {
       printf("%c","abcdef"[4]);
    }


    Sunday, April 29, 2007 12:33 PM
  • Ans : 9

     

    because

    f(6)

    f(3)+f(5)

    f(0)+f(2)+f(2)+f(4)

    1+f(-1)+f(1)+f(-1)+f(1)+f(1)+f(3)

    1+1+1+1+1+1+f(0)+f(2)

    6+1+f(-1)+f(1)

    7+1+1

    9

     

     

    Sunday, April 29, 2007 12:34 PM
  • Answer is "e"
    Sunday, April 29, 2007 12:40 PM
  • Code Snippet

    main()

    {

        a[5]=={1,2,3,4,5}

        printf("%d",a[3]);

        printf("%d",3[a]);

        printf("%d",*(a+3);

    }

     

     What will be the output?

    Sunday, April 29, 2007 12:45 PM
  • Output of First and Third Statement will be same for both i.e. 4. I don't know about the middle one.
    Sunday, April 29, 2007 12:46 PM
  • @rakshit

    Output of 2nd is also "4"...

     

    so, output is 4 4 4

    Sunday, April 29, 2007 12:52 PM
  • Code Snippet

    for(i==0,j==0;i<10,j<10;i++,j+)

    {

        printf("%d",j);

    }

     

    What will be output?
    Sunday, April 29, 2007 12:54 PM
  • compilation error

     

    Sunday, April 29, 2007 12:58 PM
  • Well the answer in all the 3 cases will be 4 as the pointer in all the 3 cases will point to that specific location......
    Sunday, April 29, 2007 12:59 PM
  • Code Snippet

    int x = 10,y=15,z = 5;

    int i;

    i = x<y<z;

    printf("%d",i);

     

    What will be the output?

    Sunday, April 29, 2007 1:06 PM
  • Answer will be 1.
    Sunday, April 29, 2007 1:07 PM
  • What will be the output?


     

    Code Snippet
    int main()
    {
       printf("sizeof(short): %d\n", int(sizeof(short)));
       printf("sizeof(int): %d\n", int(sizeof(int)));
       printf("sizeof(long): %d\n", int(sizeof(long)));
       printf("sizeof(long long): %d\n", int(sizeof(long long)));

       return 0;
    }

     

    Sunday, April 29, 2007 1:11 PM
  • What will be output of following program?

     

    Code Snippet

    main()

    {

        printf(3+"Hello");

    }

    Sunday, April 29, 2007 1:11 PM
  • sizeof(short): 2
    sizeof(int): 4
    sizeof(long): 8
    sizeof(long long): 8
    Sunday, April 29, 2007 1:13 PM
  •  

    Output : "lo"

    Sunday, April 29, 2007 1:16 PM
  • The output will be
    lo

    the reason is, that the adress of the control string is passed in the function, but you are adding 3 to it before passing it,
    so the base address is not gone frm H but from L, so the string from the 4th position gets printed...
    Sunday, April 29, 2007 1:21 PM
  • What will be the output of

    int n = 0;

    printf("%d", n + ++n + ++n);


    Sunday, April 29, 2007 1:26 PM
  •  

    I think, the same question is there in another thread...

    and I think, Your attendence was also there...(mean u know that)...

    Sunday, April 29, 2007 1:30 PM
  • Output of code is 4.
    Sunday, April 29, 2007 1:32 PM
  •  

    Answer is 4....

    Sunday, April 29, 2007 1:33 PM
  • Output???

    Code Snippet

     

     

    main()
    {

    int i,j;
    int mat[3][3] ={1,2,3,4,5,6,7,8,9};
    for (i=2;i>=0;i--)
    for ( j=2;j>=0;j--)
    printf("%d" , *(*(mat+j)+i));
    }

     

     

    Sunday, April 29, 2007 1:34 PM
  • Output will be

    9 6 3 8 5 2 7 4 1

    Sunday, April 29, 2007 1:36 PM
  • yup, I had also eplained the logic their.. so if you understand the logic, you should be able to answer this one..
    Sunday, April 29, 2007 1:39 PM
  • Code Snippet

    int main()

    {    

     char value = '\125';

     printf("%C \n", value);

     return 0;

    }

     

    What will be the output?

    Sunday, April 29, 2007 1:41 PM
  • Output is "U"...
    Sunday, April 29, 2007 1:43 PM
  • output is
    O/oC
    Sunday, April 29, 2007 1:44 PM
  •  

    Hey Varun... what is this...??

    O/oC ?????

     

    Hey man.. it's "%c" and answer is "U"...

    Sunday, April 29, 2007 1:50 PM
  • What is the output of printf(”%d”)?
    Sunday, April 29, 2007 1:54 PM
  •  

    Any garbage value...

    Sunday, April 29, 2007 2:06 PM
  • Buddy, When i saw the question 1st, I thought it was O/oC, even i was confused, but then, this is what i thought it was and so i gave the answer on that basis...
    Sunday, April 29, 2007 4:16 PM
  • It will be compilation error.
    Sunday, April 29, 2007 5:16 PM
  • I have seen it on the other post. Results are different on different compilers.
    Sunday, April 29, 2007 5:18 PM
  • Code Snippet
    void main (void)
    {
    int i = 0;
    char ch = 'A';
    do
    putchar (ch);
    while(i++ < 5 || ++ch <= 'F');
    }

     

    What will be the output?

    Sunday, April 29, 2007 5:39 PM
  • It will keep printing A until i becomes 5 because if first expression is true in pair of two statements with OR, then second one will not be executed. After i becomes 5, it will start incrementing ch. So, output will be like

    AAAAAABCDEF
    Sunday, April 29, 2007 5:42 PM
  • What will be output of the following?

     

    Code Snippet

    char *rev(int val); 

    void main(void)

    {

    extern char dec[];

    printf ("%c", *rev);

    }

     

    char *rev (int val)

    {

    char dec[]="abcde";

    return dec;

    }

     

    Sunday, April 29, 2007 5:45 PM
  • It will print some garbage value.
    Sunday, April 29, 2007 5:46 PM
  • Next one is a bit tricky.

     

    Code Snippet

    void main (void)

    {

    char *p = "Bangalore";

    #if 0

    printf ("%s", p);

    #endif

    }

     

    What will be an output?

    Sunday, April 29, 2007 5:51 PM
  • It will print nothing because it will not go in #if statement.

    Sunday, April 29, 2007 5:52 PM
  • Code Snippet
    void main()
    {
    printf("%d",printf("ABC\\"));
    }

     

    What will be the output?

    Sunday, April 29, 2007 6:05 PM
  • Code Snippet
    void main()
    {
    int a[]={9,4,1,7,5};
    int *p;
    p=&a[3];
    printf("%d",p[-1]);
    }

     

    What will be the output?
    Sunday, April 29, 2007 6:06 PM
  • Code Snippet
    void main()
    {
    int a[]={10,20,30,40,50};
    int *p;
    p= (int*)((char *)a + sizeof(int));
    printf("%d",*p);
    }

     

    What will be the output?

    Sunday, April 29, 2007 6:08 PM
  • output will be
    ABC\\5

    now printf function returns the number of vharacters succeessfuly printed.. so it returns 5 to 1st printf....
    Sunday, April 29, 2007 6:10 PM
  • The output should be 1
    I have not tried it on any compiler, but telling you orally...

    as the 4th add is saved in the p,
    now when you say p[-1], it will get address of the previous element in the array..

    which is the 3rd element.. so it should be 1
    Sunday, April 29, 2007 6:14 PM
  • sagar you rock man.. where do u get such questions.. its fun solving them... keep them coming...

    well now the base pointer a is of type int,, but its converted to char type....

    sizeof(int) should return value 2, (can varry on computers and compilers)
    as the the data type is char, i.e. 1 byte so the address pointers moves 2 bytes address...
    which will be just equal to 1 int value..

    so finally the address will point to 2nd value of the array.. which is 20

    so the output is 20...
    Sunday, April 29, 2007 6:19 PM
  • Output : 20
    Sunday, April 29, 2007 6:21 PM
  • I would like to add more on this....

    #if is a preprocessor directive... now
    #if checks for a condition, if its true, it will include the rest of the statements untill #endif in the code before compiler...

    as 0 stands for false.. the code of printf will never be added in the code, so there is no quesstion of output
    Sunday, April 29, 2007 6:23 PM
  • Code Snippet
    void main()
    {
    int i;
    for(i=0;i<3;i++)
    {
    int i=100;
    i--;
    printf("%d..",i);
    }
    }

     

    Sunday, April 29, 2007 6:24 PM
  •  

    Output : 99..99..99..

    Sunday, April 29, 2007 6:28 PM
  • Output is
    99..99..99..

    Ask me if you need explanation on this
    Sunday, April 29, 2007 6:32 PM
  •  

    Code Snippet

    void main()

    {

    int a[]={9,4,1,7,5};

    int *p;

    p=&a[3];

    printf("%d",p[-1]);

    }

     

     

    Sunday, April 29, 2007 6:33 PM
  • Output :- 1
    Sunday, April 29, 2007 6:35 PM
  •  

    Output : 1

    Sunday, April 29, 2007 6:35 PM
  • hey Hiren, looks like its a race between us thatz going on.. Both of us are eager to answer first..
    keep up the spirit, but i will be going offline soon..
    Sunday, April 29, 2007 6:37 PM
  •  

    @ Hiren...

    Can u give explaination on this answer?

     

    @varun

    Be fast .. I will mark "Answer" only in FCFS manner...

    Sunday, April 29, 2007 6:38 PM
  • Sagar you repeated this question.. i have already given the explanation...
    Sunday, April 29, 2007 6:39 PM
  •  

    a[]={9,4,1,7,5}

    and p=&a[3]

    so p will point to 7 (means p[0]=7)

     

    and p[-1] will point to 1...

    so, 1 is the correct answer...

    Sunday, April 29, 2007 6:41 PM
  • Where are the questions?? Are their more questions coming.. i m waiting for them as i don have much time left... but don want to miss the questions...
    Sunday, April 29, 2007 6:44 PM
  •  

    Yes varun... I know that..

    But I want to ask this question to Hiren...

    Because he is giving answers very fast..

    So, I just want to test his knowledge and that's y I asked explaination also.

      

    and varun... U r right.. I think, we should stop now..

    I am inviting U, Hiren and Rakshit for tomorrow...

     

    We will continue tomorrow...

     

    Sunday, April 29, 2007 6:49 PM
  • Alright bro, tommorrow at 10 will be fine with me.. we can start at that time with common consent..
    Sunday, April 29, 2007 6:52 PM
  •  

    Thanks Sagar...

    Ok.. We will continue this thread tomorrow...

    I really enjoyed this thread...

    and I think, this thread is the best from all.

    I also like the spirit of Varun and Rakshit...

     

     

     

    Sunday, April 29, 2007 6:56 PM
  • output will be
    ABC\4

    @Varun
    printf function returns the number of characters succeessfuly printed. But it will return 4 because \\ is an escape sequence and it represents '\'. So, output of inner printf will be ABC\ and outer printf will print 4.
    Monday, April 30, 2007 1:41 AM
  • It will give 1 as output. Tried this kind of program on Turbo c++ once.
    Monday, April 30, 2007 1:49 AM
  • Yes, indeed output is 20.
    Monday, April 30, 2007 1:55 AM
  • Yes Rakshit you are right.. now y the hell did i forgot that...

    correction :- output will be ABC\4

    Even you need to do the correction Rakshit
    Monday, April 30, 2007 3:43 AM
  • Now, Lets continue.

     

    Code Snippet

    #define SUM(a,b) a+b

    main()

    {

    int a=2;

    int b=3;

    int x = SUM(a,b)*2;

    printf("x=%d\n",x);

    }

     

    What will be the output?

    Monday, April 30, 2007 6:56 AM
  • Hey, you are back. I was waiting for you.

    Answer is 10.
    Monday, April 30, 2007 6:59 AM
  • Check it again. Your answer is wrong.
    Monday, April 30, 2007 7:00 AM
  • I tried to run this code on Turbo C++. It gave the output 8!!! Now can you explain that?
    Monday, April 30, 2007 7:01 AM
  • Code Snippet
    #define max(a,b) (a>b)?a:b
    main()
    {
     int m,n;
     m=3+max(2,3);
     n=2*max(3,2);
     printf("%d,%d",m,n);
    }

     

    Here is the similar to the above. What will be the output?

    Monday, April 30, 2007 7:19 AM
  • It displays 2, 3 on Turbo C++. I am not able to understand it. Shouldn't it be 6, 6?
    Monday, April 30, 2007 7:20 AM
  • Oh, Thans for pointing that out. Output is ABC\4.
    Monday, April 30, 2007 7:29 AM
  • The Answer should be  

    6 , 6

    Monday, April 30, 2007 1:11 PM
  • well the answer is coming out to be 2 3
    let me analyze it, it will take time...
    Monday, April 30, 2007 1:39 PM
  • Hey... lets continue now....
    If u (Hiren, Varun, Rakshit) are online, then tell yes or no...
    Monday, April 30, 2007 7:21 PM
  •  

    Ok sagar.. I m ready.. lets start...

    Monday, April 30, 2007 7:37 PM
  • Code Snippet


    #include<stdio.h>
    main()
    {
    char *a="hello";
    char *b="bye";
    char *c="hai";
    int x=10,y=100;
    c=(x<y)?a:b;
    printf("%s",c);
    }



    Monday, April 30, 2007 7:41 PM
  •  

    Output is : hello

     

    because x is less then y

    so c=a...

    so c= hello

    Monday, April 30, 2007 7:44 PM

  • In C,  "X ? Y : Z "  is equal to
        a) if (X==0) Y ;else Z
        b) if (X!=0) Y ;else Z
        c) if (X==0) Y ; Z
    Monday, April 30, 2007 7:47 PM
  •  

    Answer is b...

     

    Monday, April 30, 2007 7:56 PM
  • Code Snippet


    #include <stdio.h>
    #include <string.h>
    int foo(char *);
    void main (void)
    {
    char arr[100] = {"Welcome to Mistral"};
    foo (arr);
    }
    foo (char *x)
    {
    printf ("%d\t",strlen (x));
    printf ("%d\t",sizeof(x));
    return 0;
    }


    What will be the output?
    Monday, April 30, 2007 8:02 PM
  •  

    Output will be    18    2    ...

     

    because total length of string is 18

    and it's a char pointer... sizeof(char) is 2...

    Monday, April 30, 2007 8:07 PM
  • Code Snippet


    void main(void)
    {
    int i;
    static int k;
    if(k=='0')
    printf("one");
    else if(k== 48)
    printf("two");
    else
    printf("three");
    }


    Output???
    Monday, April 30, 2007 8:11 PM
  •  

    Obviously "three"

     

    because static int will be initialize by 0..

    so, k=0

     

    k!='0'

    k!=48

    so output will be "three"

    Monday, April 30, 2007 8:17 PM
  • Code Snippet


    void fn(int *a, int *b)
    {
       int *t;
       t=a;
       a=b;
       b=t;
    }
    main()
    {
       int a=2;
       int b=3;
       fn(&a,&b);
       printf("%d,%d", a,b);
    }


    Output..??
    Monday, April 30, 2007 8:27 PM
  •  

    Hey... I am not sure... let me give some time...

    Monday, April 30, 2007 8:30 PM

  • Hey Hiren..
    I am also confused with right answer...
    Monday, April 30, 2007 8:33 PM
  •  

    Yes.. u r right..

    I compiled this code..

    output is  2   3

    but i don't know how...

    Monday, April 30, 2007 8:36 PM
  •  

    I think, we should stop now..

    we will continue tomorrow...

    Monday, April 30, 2007 8:44 PM
  • Hey, I missed yesterday... tell me the time you all start... will try to come online that time, as exams are coming, i dont stay online so offten....

    As for the magic answer 2 , 3 I will ask other people for this
    Tuesday, May 1, 2007 3:03 AM
  •  Sagar_Virani_d897b3 wrote:
    Code Snippet


    void fn(int *a, int *b)
    {
    int *t;
    t=a;
    a=b;
    b=t;
    }
    main()
    {
    int a=2;
    int b=3;
    fn(&a,&b);
    printf("%d,%d", a,b);
    }


    Output..??


    Ohh you guys didnt understand this???
    I will explain what is happening...
    See in the function you have declared the pointer variables, where you are transfering the address of the variables where 2 & 3 are stored, but inside the funtion you are just interchanging the address in the variables which are local in that function.. I wont change the value of at the address you are pointing.... for this you have to derefference the pointers, reach the address and change the values at the specific location... what you did was just changing the pointers address locally which does not effect globally....

    correct code would be
    Code Snippet

    void fn(int *a, int *b)
    {
    int t;
    t=*a;
    *a=*b;
    *b=t;
    }
    main()
    {
    int a=2;
    int b=3;
    fn(&a,&b);
    printf("%d,%d", a,b);
    }



    Now what i m asking for is the answer to that 3+max(2,3) Question of yours... I m not able to solve it...
    Tuesday, May 1, 2007 3:29 AM
  •  

    Thanks varun for explaination...

    And I will inform you before starting..

    Tuesday, May 1, 2007 4:44 AM

All replies

  • What will be the output of following program?

     

    Code Snippet

    main()
     {
     char *p;
     p=malloc(10);
     free(p);
     printf("%d",p);
     }

     

    Sunday, April 29, 2007 12:05 PM
  • IMO, it will give compilation error.
    Sunday, April 29, 2007 12:07 PM
  • Write value of x. 

    Code Snippet

    main()

    {

    int x=10,y=15,z=16;

    x=y==z;

    printf("%d",x);

    }

     

     

    Sunday, April 29, 2007 12:11 PM
  • Value of X is 0.
    Sunday, April 29, 2007 12:13 PM
  • Can I ask question too?
    Sunday, April 29, 2007 12:14 PM
  • Of course you can ask.
    Sunday, April 29, 2007 12:15 PM
  • The value of x will be 0.
    Sunday, April 29, 2007 12:17 PM
  • Code Snippet

    int func(int n)
    {

        int ans;

        if(n<=1)

        {

             return 1;

        }

        else 

        {

            ans=func(n-3)+func(n-1);

            return ans;

        }
    }

     

    What will be value of func(6)?

     

    Sunday, April 29, 2007 12:27 PM
  • Give output of following program.

    Code Snippet

    main()
    {
       printf("%c","abcdef"[4]);
    }


    Sunday, April 29, 2007 12:33 PM
  • Ans : 9

     

    because

    f(6)

    f(3)+f(5)

    f(0)+f(2)+f(2)+f(4)

    1+f(-1)+f(1)+f(-1)+f(1)+f(1)+f(3)

    1+1+1+1+1+1+f(0)+f(2)

    6+1+f(-1)+f(1)

    7+1+1

    9

     

     

    Sunday, April 29, 2007 12:34 PM
  • Answer is "e"
    Sunday, April 29, 2007 12:40 PM
  • Code Snippet

    main()

    {

        a[5]=={1,2,3,4,5}

        printf("%d",a[3]);

        printf("%d",3[a]);

        printf("%d",*(a+3);

    }

     

     What will be the output?

    Sunday, April 29, 2007 12:45 PM
  • Output of First and Third Statement will be same for both i.e. 4. I don't know about the middle one.
    Sunday, April 29, 2007 12:46 PM
  • @rakshit

    Output of 2nd is also "4"...

     

    so, output is 4 4 4

    Sunday, April 29, 2007 12:52 PM
  • Code Snippet

    for(i==0,j==0;i<10,j<10;i++,j+)

    {

        printf("%d",j);

    }

     

    What will be output?
    Sunday, April 29, 2007 12:54 PM
  • compilation error

     

    Sunday, April 29, 2007 12:58 PM
  • Well the answer in all the 3 cases will be 4 as the pointer in all the 3 cases will point to that specific location......
    Sunday, April 29, 2007 12:59 PM
  • Code Snippet

    int x = 10,y=15,z = 5;

    int i;

    i = x<y<z;

    printf("%d",i);

     

    What will be the output?

    Sunday, April 29, 2007 1:06 PM
  • Answer will be 1.
    Sunday, April 29, 2007 1:07 PM
  • What will be the output?


     

    Code Snippet
    int main()
    {
       printf("sizeof(short): %d\n", int(sizeof(short)));
       printf("sizeof(int): %d\n", int(sizeof(int)));
       printf("sizeof(long): %d\n", int(sizeof(long)));
       printf("sizeof(long long): %d\n", int(sizeof(long long)));

       return 0;
    }

     

    Sunday, April 29, 2007 1:11 PM
  • What will be output of following program?

     

    Code Snippet

    main()

    {

        printf(3+"Hello");

    }

    Sunday, April 29, 2007 1:11 PM
  • sizeof(short): 2
    sizeof(int): 4
    sizeof(long): 8
    sizeof(long long): 8
    Sunday, April 29, 2007 1:13 PM
  •  

    Output : "lo"

    Sunday, April 29, 2007 1:16 PM
  • The output will be
    lo

    the reason is, that the adress of the control string is passed in the function, but you are adding 3 to it before passing it,
    so the base address is not gone frm H but from L, so the string from the 4th position gets printed...
    Sunday, April 29, 2007 1:21 PM
  • What will be the output of

    int n = 0;

    printf("%d", n + ++n + ++n);


    Sunday, April 29, 2007 1:26 PM
  •  

    I think, the same question is there in another thread...

    and I think, Your attendence was also there...(mean u know that)...

    Sunday, April 29, 2007 1:30 PM
  • Output of code is 4.
    Sunday, April 29, 2007 1:32 PM
  •  

    Answer is 4....

    Sunday, April 29, 2007 1:33 PM
  • Output???

    Code Snippet

     

     

    main()
    {

    int i,j;
    int mat[3][3] ={1,2,3,4,5,6,7,8,9};
    for (i=2;i>=0;i--)
    for ( j=2;j>=0;j--)
    printf("%d" , *(*(mat+j)+i));
    }

     

     

    Sunday, April 29, 2007 1:34 PM
  • Output will be

    9 6 3 8 5 2 7 4 1

    Sunday, April 29, 2007 1:36 PM
  • yup, I had also eplained the logic their.. so if you understand the logic, you should be able to answer this one..
    Sunday, April 29, 2007 1:39 PM
  • Code Snippet

    int main()

    {    

     char value = '\125';

     printf("%C \n", value);

     return 0;

    }

     

    What will be the output?

    Sunday, April 29, 2007 1:41 PM
  • Output is "U"...
    Sunday, April 29, 2007 1:43 PM
  • output is
    O/oC
    Sunday, April 29, 2007 1:44 PM
  •  

    Hey Varun... what is this...??

    O/oC ?????

     

    Hey man.. it's "%c" and answer is "U"...

    Sunday, April 29, 2007 1:50 PM
  • What is the output of printf(”%d”)?
    Sunday, April 29, 2007 1:54 PM
  •  

    Any garbage value...

    Sunday, April 29, 2007 2:06 PM
  • Buddy, When i saw the question 1st, I thought it was O/oC, even i was confused, but then, this is what i thought it was and so i gave the answer on that basis...
    Sunday, April 29, 2007 4:16 PM
  • It will be compilation error.
    Sunday, April 29, 2007 5:16 PM
  • I have seen it on the other post. Results are different on different compilers.
    Sunday, April 29, 2007 5:18 PM
  • So, you guyz still holding up hmm?

     

    Got another question for you.

     

    Code Snippet

    int f1(int *x,int flag)
      {

              int *y;
                *y=*x+3;
                switch(flag)
                {
                        case 0: 
                        *x=*y+1;
                        break;
                        case 1:
                        *x=*y;
                        break;
                        case 2:
                         *x=*y-1;   
                        break;
                } 
               return(*y);
     }   

    main()

    {

         int *x,i,j;

         *x=5;

         i=f1(x,0); j=f1(x,1);

         printf("%d %d %d ",i,j,*x);

    }

     

    What wiil be the output?

     

    Sunday, April 29, 2007 5:27 PM
  • Answer will be 9, 12, 12.
    Sunday, April 29, 2007 5:30 PM
  • Code Snippet
    void main()
    {
       int i;
       for(i=2;i<=7;i++)
          printf("%5d",fno());
    }
    fno()
    {
       static int f1=1,f2=1,f3;
       return(f3=f1+f2,f1=f2,f2=f3);
    }

     

    What will be the output?

    Sunday, April 29, 2007 5:32 PM
  • Output will be

    2 3 5 8 13 21
    Sunday, April 29, 2007 5:34 PM
  • Code Snippet
    void main (void)
    {
    int i = 0;
    char ch = 'A';
    do
    putchar (ch);
    while(i++ < 5 || ++ch <= 'F');
    }

     

    What will be the output?

    Sunday, April 29, 2007 5:39 PM
  • It will keep printing A until i becomes 5 because if first expression is true in pair of two statements with OR, then second one will not be executed. After i becomes 5, it will start incrementing ch. So, output will be like

    AAAAAABCDEF
    Sunday, April 29, 2007 5:42 PM
  • What will be output of the following?

     

    Code Snippet

    char *rev(int val); 

    void main(void)

    {

    extern char dec[];

    printf ("%c", *rev);

    }

     

    char *rev (int val)

    {

    char dec[]="abcde";

    return dec;

    }

     

    Sunday, April 29, 2007 5:45 PM
  • It will print some garbage value.
    Sunday, April 29, 2007 5:46 PM
  • Next one is a bit tricky.

     

    Code Snippet

    void main (void)

    {

    char *p = "Bangalore";

    #if 0

    printf ("%s", p);

    #endif

    }

     

    What will be an output?

    Sunday, April 29, 2007 5:51 PM
  • It will print nothing because it will not go in #if statement.

    Sunday, April 29, 2007 5:52 PM
  • Code Snippet
    void main()
    {
    printf("%d",printf("ABC\\"));
    }

     

    What will be the output?

    Sunday, April 29, 2007 6:05 PM
  • Code Snippet
    void main()
    {
    int a[]={9,4,1,7,5};
    int *p;
    p=&a[3];
    printf("%d",p[-1]);
    }

     

    What will be the output?
    Sunday, April 29, 2007 6:06 PM
  • Code Snippet
    void main()
    {
    int a[]={10,20,30,40,50};
    int *p;
    p= (int*)((char *)a + sizeof(int));
    printf("%d",*p);
    }

     

    What will be the output?

    Sunday, April 29, 2007 6:08 PM
  • output will be
    ABC\\5

    now printf function returns the number of vharacters succeessfuly printed.. so it returns 5 to 1st printf....
    Sunday, April 29, 2007 6:10 PM
  • The output should be 1
    I have not tried it on any compiler, but telling you orally...

    as the 4th add is saved in the p,
    now when you say p[-1], it will get address of the previous element in the array..

    which is the 3rd element.. so it should be 1
    Sunday, April 29, 2007 6:14 PM
  • sagar you rock man.. where do u get such questions.. its fun solving them... keep them coming...

    well now the base pointer a is of type int,, but its converted to char type....

    sizeof(int) should return value 2, (can varry on computers and compilers)
    as the the data type is char, i.e. 1 byte so the address pointers moves 2 bytes address...
    which will be just equal to 1 int value..

    so finally the address will point to 2nd value of the array.. which is 20

    so the output is 20...
    Sunday, April 29, 2007 6:19 PM
  • Output : 20
    Sunday, April 29, 2007 6:21 PM
  • I would like to add more on this....

    #if is a preprocessor directive... now
    #if checks for a condition, if its true, it will include the rest of the statements untill #endif in the code before compiler...

    as 0 stands for false.. the code of printf will never be added in the code, so there is no quesstion of output
    Sunday, April 29, 2007 6:23 PM
  • Code Snippet
    void main()
    {
    int i;
    for(i=0;i<3;i++)
    {
    int i=100;
    i--;
    printf("%d..",i);
    }
    }

     

    Sunday, April 29, 2007 6:24 PM
  •  

    Output : 99..99..99..

    Sunday, April 29, 2007 6:28 PM
  • Output is
    99..99..99..

    Ask me if you need explanation on this
    Sunday, April 29, 2007 6:32 PM
  •  

    Code Snippet

    void main()

    {

    int a[]={9,4,1,7,5};

    int *p;

    p=&a[3];

    printf("%d",p[-1]);

    }

     

     

    Sunday, April 29, 2007 6:33 PM
  • Output :- 1
    Sunday, April 29, 2007 6:35 PM
  •  

    Output : 1

    Sunday, April 29, 2007 6:35 PM
  • hey Hiren, looks like its a race between us thatz going on.. Both of us are eager to answer first..
    keep up the spirit, but i will be going offline soon..
    Sunday, April 29, 2007 6:37 PM
  •  

    @ Hiren...

    Can u give explaination on this answer?

     

    @varun

    Be fast .. I will mark "Answer" only in FCFS manner...

    Sunday, April 29, 2007 6:38 PM
  • Sagar you repeated this question.. i have already given the explanation...
    Sunday, April 29, 2007 6:39 PM
  •  

    a[]={9,4,1,7,5}

    and p=&a[3]

    so p will point to 7 (means p[0]=7)

     

    and p[-1] will point to 1...

    so, 1 is the correct answer...

    Sunday, April 29, 2007 6:41 PM
  • Where are the questions?? Are their more questions coming.. i m waiting for them as i don have much time left... but don want to miss the questions...
    Sunday, April 29, 2007 6:44 PM
  •  

    Yes varun... I know that..

    But I want to ask this question to Hiren...

    Because he is giving answers very fast..

    So, I just want to test his knowledge and that's y I asked explaination also.

      

    and varun... U r right.. I think, we should stop now..

    I am inviting U, Hiren and Rakshit for tomorrow...

     

    We will continue tomorrow...

     

    Sunday, April 29, 2007 6:49 PM
  • Alright bro, tommorrow at 10 will be fine with me.. we can start at that time with common consent..
    Sunday, April 29, 2007 6:52 PM
  •  

    Thanks Sagar...

    Ok.. We will continue this thread tomorrow...

    I really enjoyed this thread...

    and I think, this thread is the best from all.

    I also like the spirit of Varun and Rakshit...

     

     

     

    Sunday, April 29, 2007 6:56 PM
  • output will be
    ABC\4

    @Varun
    printf function returns the number of characters succeessfuly printed. But it will return 4 because \\ is an escape sequence and it represents '\'. So, output of inner printf will be ABC\ and outer printf will print 4.
    Monday, April 30, 2007 1:41 AM
  • It will give 1 as output. Tried this kind of program on Turbo c++ once.
    Monday, April 30, 2007 1:49 AM
  • Yes, indeed output is 20.
    Monday, April 30, 2007 1:55 AM
  • Yes Rakshit you are right.. now y the hell did i forgot that...

    correction :- output will be ABC\4

    Even you need to do the correction Rakshit
    Monday, April 30, 2007 3:43 AM
  • Now, Lets continue.

     

    Code Snippet

    #define SUM(a,b) a+b

    main()

    {

    int a=2;

    int b=3;

    int x = SUM(a,b)*2;

    printf("x=%d\n",x);

    }

     

    What will be the output?

    Monday, April 30, 2007 6:56 AM
  • Hey, you are back. I was waiting for you.

    Answer is 10.
    Monday, April 30, 2007 6:59 AM
  • Check it again. Your answer is wrong.
    Monday, April 30, 2007 7:00 AM
  • I tried to run this code on Turbo C++. It gave the output 8!!! Now can you explain that?
    Monday, April 30, 2007 7:01 AM
  • Code Snippet
    #define max(a,b) (a>b)?a:b
    main()
    {
     int m,n;
     m=3+max(2,3);
     n=2*max(3,2);
     printf("%d,%d",m,n);
    }

     

    Here is the similar to the above. What will be the output?

    Monday, April 30, 2007 7:19 AM
  • It displays 2, 3 on Turbo C++. I am not able to understand it. Shouldn't it be 6, 6?
    Monday, April 30, 2007 7:20 AM
  • Oh, Thans for pointing that out. Output is ABC\4.
    Monday, April 30, 2007 7:29 AM
  • The Answer should be  

    6 , 6

    Monday, April 30, 2007 1:11 PM
  • well the answer is coming out to be 2 3
    let me analyze it, it will take time...
    Monday, April 30, 2007 1:39 PM
  • Hey... lets continue now....
    If u (Hiren, Varun, Rakshit) are online, then tell yes or no...
    Monday, April 30, 2007 7:21 PM
  •  

    Ok sagar.. I m ready.. lets start...

    Monday, April 30, 2007 7:37 PM
  • Code Snippet


    #include<stdio.h>
    main()
    {
    char *a="hello";
    char *b="bye";
    char *c="hai";
    int x=10,y=100;
    c=(x<y)?a:b;
    printf("%s",c);
    }



    Monday, April 30, 2007 7:41 PM
  •  

    Output is : hello

     

    because x is less then y

    so c=a...

    so c= hello

    Monday, April 30, 2007 7:44 PM

  • In C,  "X ? Y : Z "  is equal to
        a) if (X==0) Y ;else Z
        b) if (X!=0) Y ;else Z
        c) if (X==0) Y ; Z
    Monday, April 30, 2007 7:47 PM
  •  

    Answer is b...

     

    Monday, April 30, 2007 7:56 PM
  • Code Snippet


    #include <stdio.h>
    #include <string.h>
    int foo(char *);
    void main (void)
    {
    char arr[100] = {"Welcome to Mistral"};
    foo (arr);
    }
    foo (char *x)
    {
    printf ("%d\t",strlen (x));
    printf ("%d\t",sizeof(x));
    return 0;
    }


    What will be the output?
    Monday, April 30, 2007 8:02 PM
  •  

    Output will be    18    2    ...

     

    because total length of string is 18

    and it's a char pointer... sizeof(char) is 2...

    Monday, April 30, 2007 8:07 PM
  • Code Snippet


    void main(void)
    {
    int i;
    static int k;
    if(k=='0')
    printf("one");
    else if(k== 48)
    printf("two");
    else
    printf("three");
    }


    Output???
    Monday, April 30, 2007 8:11 PM
  •  

    Obviously "three"

     

    because static int will be initialize by 0..

    so, k=0

     

    k!='0'

    k!=48

    so output will be "three"

    Monday, April 30, 2007 8:17 PM
  • Code Snippet


    void fn(int *a, int *b)
    {
       int *t;
       t=a;
       a=b;
       b=t;
    }
    main()
    {
       int a=2;
       int b=3;
       fn(&a,&b);
       printf("%d,%d", a,b);
    }


    Output..??
    Monday, April 30, 2007 8:27 PM
  •  

    Hey... I am not sure... let me give some time...

    Monday, April 30, 2007 8:30 PM

  • Hey Hiren..
    I am also confused with right answer...
    Monday, April 30, 2007 8:33 PM
  •  

    Yes.. u r right..

    I compiled this code..

    output is  2   3

    but i don't know how...

    Monday, April 30, 2007 8:36 PM
  •  

    I think, we should stop now..

    we will continue tomorrow...

    Monday, April 30, 2007 8:44 PM
  • Hey, I missed yesterday... tell me the time you all start... will try to come online that time, as exams are coming, i dont stay online so offten....

    As for the magic answer 2 , 3 I will ask other people for this
    Tuesday, May 1, 2007 3:03 AM
  •  Sagar_Virani_d897b3 wrote:
    Code Snippet


    void fn(int *a, int *b)
    {
    int *t;
    t=a;
    a=b;
    b=t;
    }
    main()
    {
    int a=2;
    int b=3;
    fn(&a,&b);
    printf("%d,%d", a,b);
    }


    Output..??


    Ohh you guys didnt understand this???
    I will explain what is happening...
    See in the function you have declared the pointer variables, where you are transfering the address of the variables where 2 & 3 are stored, but inside the funtion you are just interchanging the address in the variables which are local in that function.. I wont change the value of at the address you are pointing.... for this you have to derefference the pointers, reach the address and change the values at the specific location... what you did was just changing the pointers address locally which does not effect globally....

    correct code would be
    Code Snippet

    void fn(int *a, int *b)
    {
    int t;
    t=*a;
    *a=*b;
    *b=t;
    }
    main()
    {
    int a=2;
    int b=3;
    fn(&a,&b);
    printf("%d,%d", a,b);
    }



    Now what i m asking for is the answer to that 3+max(2,3) Question of yours... I m not able to solve it...
    Tuesday, May 1, 2007 3:29 AM
  •  

    Thanks varun for explaination...

    And I will inform you before starting..

    Tuesday, May 1, 2007 4:44 AM
  • x will be 0
    Friday, May 25, 2007 4:45 PM
  • 9
    Friday, May 25, 2007 4:48 PM
  • answer is 'e'
    Friday, May 25, 2007 4:48 PM
  • 4 4 4
    Friday, May 25, 2007 4:49 PM
  • hey, output should be 18 & 100
    Friday, May 25, 2007 4:58 PM
  • I think ill also jump in this Quiz now Smile Hope you guys dont mind.
    Friday, May 25, 2007 7:39 PM
  •  

    Hey... we don't mind.

     

    but My exam is comming.

    So, I will not take part in this thread now onwards.

     

    but you all guys continue this.

     

    Saturday, May 26, 2007 9:00 AM
  • hey when all oy you are going to star this sunday quiz today?
    do send me the message.
    & please start  a new thread & send me the link/
    Sunday, May 27, 2007 3:07 AM

  • link to new thread from where this sunday quiz will be continued.
    please come here.
    browsing through this thread is tedious

    http://forums.microsoft.com/SamVaad/ShowPost.aspx?PostID=1653556&SiteID=43&mode=1
    Sunday, May 27, 2007 3:11 AM