Some C test questions: challenge

This is a discussion on Some C test questions: challenge within the C Programming forums, part of the General Programming Boards category; Originally posted by Mister C Thanks for all your help in giving real good test questions. Also, thanks for all ...

  1. #16
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Mister C
    Thanks for all your help in giving real good test questions. Also, thanks for all of the suggestions to make my testing and course better. I forgot to post the answer to my test question. the answer is D. radius = 48.

    Continue to post questions as I will check back when I have time.

    Mr. C.
    do you speak english? did you read the 2 posts that explained to you (i agree salem did it more gracefully than me) that the behavior of the said example is undefined? i already knew it was defined, but i compiled it on the three compilers i have available to me, just so i could illustrate more clearly to you your error. gcc 2.7.2.1 of djgpp output radius=48. pacific for dos v7.51 output radius=48. lcc for windoze v2.5 output radius=47. now a piece of code that produces 2 different outputs on 3 perfectly functional c compilers is crap. the compilers say its crap. the ansi standard says its crap. he says its crap. we say its crap. you teach it to your students, they WONT say its crap. so dont teach crap.
    hello, internet!

  2. #17
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Well looks like we have a problem. I can read English by the way.
    I am sorry I cannot post this board every time someone post back. The problem I gave has been field tested by two other C instructors who has been teaching longer than me. Anyway, I teach my students to rewrite the problem. So in this case rewrite:

    //radius = (++radius) + (z++)

    radius = ++radius;
    radius = radius + z;
    z = z++;


    //radius = (radius++) + (z++)

    radius = radius + z;
    radius = radius++;
    z = z++;


    Remember what prefix/postfix mean!!
    If you trace this by hand and rewrite it you do get 48.

    I did test this on VC++ 6.0 and Borland C again and got 48 again.

    Once again I can't get the CODE formatting correct. So I guess I will just post and not write code.

    Mr. C.
    Last edited by Mister C; 09-07-2002 at 07:45 PM.

  3. #18
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    It doesnt matter what result you got on any compiler. It is not really valid c.Its undefined behaviour. This is what you should be teaching your students to eradicate from their programs. Read the link that salem gave you.
    btw what is radius=++radius
    dont you just mean .....
    ++radius

    if u really must use ++ in a question how about this one....

    which is faster theoretically and why?
    ++c;
    c++;
    why only theoretically?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #19
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use square brackets..... [ c o d e ] code here [ / c o d e ]
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #20
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Stoned_Coder



    ++c;
    c++;
    this is also covered in the same faq salem posted.

    mister c: no. no. no. read salem's link. this is undefined behavior.
    hello, internet!

  6. #21
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    I read Salem's link. It is correct in what it is saying about using prefix/postfix expression being undefined in certain cases. But this not what that is.

    Thanks for the help on the coding

    Sorry I made a typo in the code I posted. It should be:

    Code:
    //radius = (++radius) + (z++)
    
    radius = radius + 1;
    radius = radius + z;
    z = z++;
    
    
    //radius = (radius++) + (z++)
    
    radius = radius + z;
    radius = radius + 1;
    z = z++;
    Here is the test program I used for it

    Code:
    #include <stdio.h>
    int main()
    {
    
     int radius = 21;
     int z = 12;
    
    
    //radius = (++radius) + (z++)
    
    radius = radius + 1;
    radius = radius + z;
    z = z++;
    
    
    //radius = (radius++) + (z++)
    
    radius = radius + z;
    radius = radius + 1;
    z = z++;
    
    printf ("Radius = %d", radius);
    return 0;
    }
    Anyway, I forgot to metion I use this as in-class exercises after I have talked about prefix/postfix. I also give one example about prefix/postfix being undefined as well.



    Any more questions that I can use.



    Mr. C.

  7. #22
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well in that case u should have no trouble answerring it then. but the information the answer to this requires is not in the link that salem gave. that only says they r equivalent which may or may not be strictly true. Depends on compiler.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #23
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Did you try to solve the problem by hand? Not using the complier as your guide.

    Mr. C.

  9. #24
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Mister C
    Did you try to solve the problem by hand? Not using the complier as your guide.

    Mr. C.

    Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

    I solved it by hand using the rules of C as my guide. Your code exhibits dangerous undefined behavior (ALL undefined behavior is dangerous because of the potential to do anything to your system), and my compiler is not broken by outputting

    Radius = 47;

    to your so-called program. In fact, it's actually being nice to me by not formatting my harddrive, which would fall under the ansi C specification for the statement "radius = radius++ + z++;"
    hello, internet!

  10. #25
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You can lead a horse to water......

    any attempt to modify a variable more than once in a single expression is undefined behaviour.

    radius = (radius++) + (z++)

    here you modify radius with ++ and modify again with assignment. hence undefined behaviour.

    Go back to school teach. oh and btw. try to answer my question. im interested in your answer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #26
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Mr. C, how about this one?

    Code:
    #include <stdio.h>
    int f (void)
    {
      return printf ("Hello, world!\n");
    }
    int g (void)
    {
      return printf ("Goodbye, world!\n");
    }
    int h (void)
    {
      return printf ("Buy me a beer, world!\n");
    }
    int main (void)
    {
      int a = g () + f () * h ()
      return 0;
    }
    what does this program output? (hint: the answer is not what you think. but, this does not exhibit any undefined behavior. if you read throughly the FAQ link that Salem posted, you would know this one. Maybe a good one to ask your students?
    hello, internet!

  12. #27
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    I think the main thing here is not to use prefix/postfix in test questions. Someone mentioned that earlier. I like to give examples as well as other textbooks on it though.

    Did anyone try to trace the program by hand without the complier? What answer do you get?

    I guess I better not show the pointer problem... we will argue about the correct answer as well.

    Here is another problem:
    Code:
    What is the output of the following code fragment if the input value is 4? (Be careful here.)
    	
      int num;
    	int alpha = 10;
    
    	scanf(%d\n, &num);
    	switch (num)
    	{
    	    case 3  : alpha++;
    	    case 4  : alpha = alpha + 2;
    	    case 8  : alpha = alpha + 3;
    	    default : alpha = alpha + 4;
    	}
      printf(%d\n, alpha);
    
    A.	10							C.  12
    B.	14							D.  19
    Mr. C

  13. #28
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    d

    now answer mine.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #29
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Mister C

    Did anyone try to trace the program by hand without the complier? What answer do you get?

    Mr. C
    I got AN UNDEFINED ANSWER because the CODE does not to conform to STANDARD SPECIFICATIONS for C CODE and makes NO SENSE. There's nothing wrong with questions with ++ operators on tests, but make sure that YOU have the RIGHT answers before you ask the question. It's like asking "what does this program print?"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
      int *temp = malloc (sizeof (int));
      printf ("result is %i\n", *temp);
      return 0;
    }
    And it makes just about as much sense.
    hello, internet!

  15. #30
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Yes I could go back to school. As well as the other Ph. D's and career programmers that solved this problem as well.

    I saw Moi's question 3.4 on the web site. I am not getting into that mess. As well as your question.

    You are correct in D.

    Here is another. My last one. I have to finish a Java test. Maybe I will give them for bonus a SCJP exam question.

    Code:
    29. Which statement is true?
    a) || is the bitwise or operator,
    b) | is the logical or operator
    c) Operators || are interchangeable.
    d) || and | are each binary operators.
    Code:
    21. The bitwise operators can be used to manipulate the bits of variables of type __________.
    a) float
    b) double
    c) long
    d) long double

    Mr. C

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. My C++ test is COMING!!...
    By [Z-D] in forum C++ Programming
    Replies: 52
    Last Post: 12-01-2006, 07:02 PM
  4. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  5. Bitwise Test Questions
    By Mister C in forum C Programming
    Replies: 9
    Last Post: 11-27-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21