locked
Technical Quiz on C / C++ - Part - II RRS feed

  • Question

  • Hi guys,

    this is a technical quiz thread on C and C++ language. for all the correct answers, i'll marks the replies as answers.

    questions are welcomed from the participants. but they should be of good quality.

    and the rules are,

    1. the next question will be available only when i've got the answer for previous one.

    2. anyone can post a question but only after the previous question owner has answered the correct one.
    3. a valid question will also be marked as an answer if it is of good quality.
     
    4. max time given for the answer is 1 day.
     
    5. at the end of 24 hrs the answer will be posted.
     
    6. the 1st one to answer correct gets the benefit, if supported with proper reasons

    so, let's start...

    Thursday, May 3, 2007 6:05 AM

Answers

  • hi sanket.....

    let me give this part-2 version a kick start, so heres the 30th Question

     

    Question: -

    1) Check this for possible errors: 

     

    Code Snippet

    void greeting(void);

    int main(void)

    {    greeting();    return 0; }

     

    void greeting(void)

    {   cout<<"Hello world";    return; }

     

    2) a) Is this code fragment okay? 

    b) What will it return?

     

    Code Snippet

    int getNumber()

    {   int x, y; cin >> x >> y; return(x,y); }

     

    Thursday, May 3, 2007 6:40 AM
  • Answer to Question 1 : The codeis perfectly alright, and it will run error free displaying out put on screen "Hello World":

    Answer to Question 2 : it will take input the values of x and y from the console, and then retyrn the value of y.
    Reason : When , (comma) opeator is used, the value of the expression at the right most end will be returned.
    Thursday, May 3, 2007 8:34 AM

All replies

  • hi sanket.....

    let me give this part-2 version a kick start, so heres the 30th Question

     

    Question: -

    1) Check this for possible errors: 

     

    Code Snippet

    void greeting(void);

    int main(void)

    {    greeting();    return 0; }

     

    void greeting(void)

    {   cout<<"Hello world";    return; }

     

    2) a) Is this code fragment okay? 

    b) What will it return?

     

    Code Snippet

    int getNumber()

    {   int x, y; cin >> x >> y; return(x,y); }

     

    Thursday, May 3, 2007 6:40 AM
  • Answer to Question 1 : The codeis perfectly alright, and it will run error free displaying out put on screen "Hello World":

    Answer to Question 2 : it will take input the values of x and y from the console, and then retyrn the value of y.
    Reason : When , (comma) opeator is used, the value of the expression at the right most end will be returned.
    Thursday, May 3, 2007 8:34 AM
  • Yes, harshil's answer is perfect.. no more explanation required..

    also you can try out this different stuff like using ,

    lets say you have
    int a[10]={0};

    main()
    {
        a[4] = 8;
        cout<<a[50,4];
    }
    Thursday, May 3, 2007 10:27 AM
  • Thanks Varun for further explaining what i have said in my earlier post. An example speaks more than words Smile

    ALso good example Wink
    Thursday, May 3, 2007 1:50 PM
  • 1) It will display "Hello world"

     

    2)

    a) yes

    b) It will get the numbers 'x' and 'y' and return the value of x

    Thursday, May 3, 2007 7:10 PM
  • Nope! Don’t try to return more than one value from a function.

     

    y will return here because of the comma operator.

     

    actually, for the second part of the question,

    two things here

    1. multiple return values are not allowed

    2. it will return the value of y and not x, it will return y because of comma operator. you can check it out in your compiler.

     

    Thursday, May 3, 2007 9:41 PM
  • Guys if you have post another question.
    Friday, May 4, 2007 5:35 AM
  • The value will be 2* i as this is the post fix increment operator, the value of i will be incremented 2 times but only after the current operation...

    so if i = 4 initially,
    then the output of this statement will be 8 and value of i after it will be 6...

    Saturday, May 5, 2007 2:17 AM
  • Alright now for my next question...

    Do you remember my question why we declare a global variable as Static....
    Now here is the related question... How can we achieve the similar effect without using the Static key word ????
    Saturday, May 5, 2007 2:23 AM
  • Not much idea regarding the static global variable. I guess ill wait till the end of the day, and if no one answers, then you can post the answer Smile
    Saturday, May 5, 2007 4:18 AM
  •  Varun_Modi_a59ed9 wrote:
    The value will be 2* i as this is the post fix increment operator, the value of i will be incremented 2 times but only after the current operation...

    so if i = 4 initially,
    then the output of this statement will be 8 and value of i after it will be 6...

    No its wrong, surprising right, and tricky too....

    now this is what Bjarne Stroustrup answers, in his book titled "the c++ programming language."

     

     

    Code Snippet

    What's the value of i++ + i++?

     

    It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:

     v[i] = i++;

    Related example:

     f(v[i],i++);

    Here, the result is undefined because the order of evaluation of function arguments are undefined.

    Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools.

     

    Saturday, May 5, 2007 8:28 PM
  •  Varun_Modi_a59ed9 wrote:
    Alright now for my next question...

    Do you remember my question why we declare a global variable as Static....
    Now here is the related question... How can we achieve the similar effect without using the Static key word ????

     

    using the "auto" keyword?

    Saturday, May 5, 2007 8:31 PM
  • I dont know about the "auto", I think it wont work,
    Now using the Static key word with global variable, we were restricting the use of the variable in the current file onle.. i.e. if we have whole program in multiple files in pieces, than no other file will be able to use this variable using even an Extern keyword.....

    Now the same effect can be achieved by using the Namespace...
    I have given a hint, now tell me how to use Namespace to restrict it ???
    Sunday, May 6, 2007 4:30 AM
  •  Anoop_Mehta_056d33 wrote:

    Varun_Modi_a59ed9 wrote:
    The value will be 2* i as this is the post fix increment operator, the value of i will be incremented 2 times but only after the current operation...

    so if i = 4 initially,
    then the output of this statement will be 8 and value of i after it will be 6...

    No its wrong, surprising right, and tricky too....

    now this is what Bjarne Stroustrup answers, in his book titled "the c++ programming language."

    Code Snippet

    What's the value of i++ + i++?

    It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:

     v[i] = i++;

    Related example:

     f(v[i],i++);

    Here, the result is undefined because the order of evaluation of function arguments are undefined.

    Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools.



    Now I dont know why this book has written this.. but rest assure buddy, we had similar questions before and i had spent 3 days trying to track this stuff,... I had my seniors and Professors all into this.. and finally we were able to track it...

    In computer there is nothing like unexpected.... its just that if we dont know something we say it is unexpected....

    You give me any expression with the post or pre increment operator.. i will show you the way to track it....

    In post increment operator a stack is maintain to perform this operation....
    Pre increment has a diferent way... So i suggest you dont totally rely on this book
    Sunday, May 6, 2007 4:36 AM
  • LOL its strange that a computers dont know how to compute the values.

    But i think i know what Bjarne Stroustrup was trying to say, he was trying to say that the value is undefined in THEORY, but it is up to the compiler implementer to take care of this issue and define a standard way to evaluate it.

    Correct me if i have any misunderstand about it
    Sunday, May 6, 2007 6:51 AM
  • thanks for that "auto" thingy man. I dont now why i forgot about it. its danm simple, but we dont think the things that are easy Stick out tongue

    but good one it was Smile
    Sunday, May 6, 2007 6:54 AM
  • @varun - i agree with you, but the thing is that, Bjarne Strautrop[pardon the spelling mistake], cant be wrong, you see, he definitely has a point, he is the founder of c++....now this is what we can do to clear out doubts

    1. i think, what harshil has said, may be correct, it might depend on the compiler, i am not sure, but this statement of harshil has made me think about it.....

    2. let us create a simple program and assign the values of "i++ + ++i" to z or self....then check the output, whether it works fine or not, or gives any errors?...

     

     

    @harshil - oh, ur welcome for that auto thing.......anyways , ya its amazing, that computers do get self confused....remember the lock out condition and the infinite loop program?

    Sunday, May 6, 2007 9:53 PM
  • Yes buddy, you can do it...
    Infact I have tested this things on VC++, Bloodshed and even unix C++ compilers... all give the same output, so i believe it to be a standard...

    lets say, you want to find the value of
    i = 0;
    j = ++i + ++i + ++i;

    what do you think the value of j is ???

    Its 7,


    sameway for i = 1, it will be 10
                   for i = 2, it will be 13

    Now if you think that the compiler would react unexpectedly, than we could not state the results... also the result would change everytime, but it dosen...
    so now try to trace it and if you are not able to than ask me i will explain how it works...
    Monday, May 7, 2007 3:56 AM
  • i = 0;
    j = ++i + ++i + ++i;

    Shouldn;t the answer to that be 6 ?
    cause

    j = ++i + ++i + ++i;
    j = 1 + 2 + 3
    j = 6

    same way for i=1, it should be 9, and for i=2 it should be 12. Could you explain the steps involved in the evaluation of this expression please ?
    i still get confused with these kind of things sometimes Stick out tongue
    Monday, May 7, 2007 9:28 AM
  • @varun - where you have tried are visual compilers, may be the language changes, from c++ to visual c++..so this difference may be overcomed....have you tried it on turbo or borland compilers?

     

    @harshil - the statement which u r trying out right now?, which compiler r u using?

     

     

    note: - if this thing works, i m sure to mailup Bjaurne staru.... , regarding this statement, it would be good if he replies ...

    Thursday, May 10, 2007 8:02 PM
  • @Anoop,

    Regarding the above problem....
    int i=0,j;
    j= ++i + ++i + ++i;
    printf("%d %d\n",i,j);

    when tried in VC++ and TC, in both i get i=3 and j=9

    Now anyone has any idea how j=9 came as output ? Shouldn't it be 6 ??

    I think we need to clear out this problem.


    j= ++i ; gives output as 1 as expected.


    j= ++i + ++i; gives output as 4 instead of 3 (1 + 2)
    Why is it 1 more than expected

    j= ++i + ++i + ++i; gives output as 9 instead of 6 (1+2+3)
    why is it 3 more than expected

    j= ++i + ++i + ++i + ++i; gives output as 16 instead of 10 (1+2+3+4)
    why is it 6 more than expected

    j= ++i + ++i + ++i + ++i; gives output as 25 instead of 15 (1+2+3+4+5)
    why is it 10 more than expected

    How can we calculate the value of it ? I think thats why Bjarne Stroustrup said that the answer cannot be predicted. ????


    Friday, May 11, 2007 6:03 AM
  • Where did you tried it man???
    I tried it in VC++ and the answer comes as
    3 7
    as i had expected...

    now it get evaluated as
    j = 2 + 2 + 3
    this is something i can prove, try to get why this happens, if still unsuccessful, i will explain why this happens...

    also when you extend it will become

    j = 2 + 2 + 3 + 4
    j = 2 + 2 + 3 + 4 + 5
    j = 2 + 2 + 3 + 4 + 5 + 6

    and so on... and now you can also check the answer, it will be like this all the time.. so no unexpected behaviour...

    @anoop
    if you want to send a mail, do send it, i can bet 100% on this...
    Friday, May 11, 2007 11:19 AM
  • @Varun , i think you are right for the VC++ of version 6.0
    I get the same output as you suggested in VC++ 6.0

    But have you tried VC++ in Visual Studio 2003 ? or Visual studio 2005 ?

    The previous post that discussed the outputs, were same for both TC and also for VC++ in VisualStudio 2003.

    If you ahve VS2003, then i suggest you to try it and compare the results of it with TC, it will match the same, but it will not match with the VC++ in 6.0 version.

    Please clarify,.....
    Friday, May 11, 2007 4:19 PM
  • Well I m using VC++ of VB6, so i suppose its VC++6, and i also tried this on tc, it gives same output...
    But Tc has lots of bugs, so i dont rely on it for C++, like lets say
    if you do this
    i=0;
    j = ++i + ++i + ++i;
    i=0;

    now printf(%d..%d.", j, ++i + ++i + ++i);

    logically and practically the output should be same, but they wont.. because of the bug in TC...
    Friday, May 11, 2007 5:51 PM
  • Ok if you think that its a bug in TC, then can you try it in VC++ for VisualStudio 2003 ?

    The results shown in VisualStudio2003 are same as the ones i got in tc.

    So there is a bug introduced after upgrading from VC++6 to VC++ for VS2003 ?

    We need to work on this thing more.

    I guess we should mail Bjarne Stroustrup.... hehe
    Friday, May 11, 2007 6:12 PM
  • after seeing the reply from you guys, the possibility that i see is,

    bjaurne, must definately be knowing of this variation and non-uniformity, and that must be the very reason why he termed it as UNDEFINED...

     

    the current and latest upadted versions have resolved this issue, whereas the older versions have not...and i think even the books edition is not updated...so...i have already emailed Bjaurne star....

    about this bug, and have asked him to modify it in the next issue of his textbook's edition....

    Sunday, May 13, 2007 3:54 PM
  • Thanks Anoop for emailing Bjarne Stroustrup about this issue. Hope its resolved.
    Sunday, May 13, 2007 7:41 PM
  • would let you know guys, if i recieve a reply from him....[which i dont think he would]...but even if the suggestion reaches the place, i will be happy...
    Sunday, May 13, 2007 7:51 PM
  • thnks for info
    Monday, May 14, 2007 5:45 AM
  •  Harshil_Patel_03b5f2 wrote:
    @Anoop,

    Regarding the above problem....
    int i=0,j;
    j= ++i + ++i + ++i;
    printf("%d %d\n",i,j);

    when tried in VC++ and TC, in both i get i=3 and j=9

    Now anyone has any idea how j=9 came as output ? Shouldn't it be 6 ??

    I think we need to clear out this problem.


    j= ++i ; gives output as 1 as expected.


    j= ++i + ++i; gives output as 4 instead of 3 (1 + 2)
    Why is it 1 more than expected

    j= ++i + ++i + ++i; gives output as 9 instead of 6 (1+2+3)
    why is it 3 more than expected

    j= ++i + ++i + ++i + ++i; gives output as 16 instead of 10 (1+2+3+4)
    why is it 6 more than expected

    j= ++i + ++i + ++i + ++i; gives output as 25 instead of 15 (1+2+3+4+5)
    why is it 10 more than expected

    How can we calculate the value of it ? I think thats why Bjarne Stroustrup said that the answer cannot be predicted. ????


    j= ++i + ++i + ++i;

     

    since pre-increment is used, increments are done before the addition operation..

    i=0 initially... then after the first ++i it is 1, then after another ++i it is 2 and finally 3

    then the addition comes into picture... i+i+i =3+3+3 =9

     

     

    Hope u got it

     

     

    Thursday, May 17, 2007 1:05 AM
  • a very nice point made by raghuram...good insight, didnt came to my mind...
    Friday, May 18, 2007 12:58 PM
  • Cery good point Raghram. You cleared my doubt. I didn;t answer since few days, cause i wanted to first try it out throughly,and then let you know about the answer. But now given any ++i + ++i sequence etc etc, its damn easy to solve it now Smile
    Saturday, May 19, 2007 4:11 PM
  • would like to correct you.
    the thing is first time it is accessed, the original value will be returned but when it is accessed 2nd time, i+1 will be returned.
    therefore, 2*i+1
    Monday, June 4, 2007 11:33 AM
  • Thanks sunil, but i cann't what you are trying to say by 2*i+1,  I dont know what example you are trying to refer to, can you give example and then tell us ? now i know how to evaluate ++i + ++i ...... any number of times, so i can comment upon it very well.
    Tuesday, June 5, 2007 4:11 AM
  • i mean to say that
    whatever will be the value of i
    the statement <printf("%d",i+++i++);> is always going to print 2*i+1;
    Tuesday, June 5, 2007 1:33 PM
  • ohh,ok thanks man.
    Tuesday, June 5, 2007 2:21 PM
  • nice work......
    Wednesday, June 6, 2007 2:37 PM