Thread: Very simple, possibly trick question.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Very simple, possibly trick question.

    Question:

    What is the value of c before the program below terminates?



    Code:
    #include <stdio.h>
    int main() {
    
      int a,b,c;
      a = 4;
      b = a-2;
      if (a > b)
        c = b;
      else
        c = 8;
    
      return 0;
    }
    My options are:

    A) 2
    B) 3
    C) 7
    D) 8
    E)None of the Above

    and I said E) None of the above b/c C isn't initialized or defined before the program runs.
    If the declarations read: " int a,b, c=1; " then I suppose c would have a value before the program ran, is my logic correct?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The question asks about the value of c before the program terminates, not before the program is run. Put it another way: add a line of code to the program such that it prints the value of c that the question asks for. Where would you place this line?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Also,

    14) To what should the boolean expression in the following if statement be changed?

    if (12 < points < 25)
    printf("You pass the test.\n");

    A)12 < points || points < 25
    B)12 < points && points < 25
    C)points > 11 && points < 26
    D)!(12 < points < 25)
    E)None of the Above

    I said C) b/c i am assuming the range of the passing is from 12-25 so the minimum is 12 to pass the test thats why if the points are greater then 11 then its a passing grade, and the max is 25 so the grade must be lower then 26 in order to get a perfect score.


    Can someone please tell me if my logic is correct for these questions?
    Last edited by matthayzon89; 04-23-2010 at 10:12 AM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by laserlight View Post
    The question asks about the value of c before the program terminates, not before the program is run. Put it another way: add a line of code to the program such that it prints the value of c that the question asks for. Where would you place this line?
    I must not get what the question is asking... I don't really understand what you were trying to explain.

    NOTE: I am not suppose to change the code, I am suppose to pick from the multiple choice options.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    The Netherlands
    Posts
    6
    Well, for your second question, the range of passing the test is inbetween 12 and 25. The score should be higher than 12 and below 25. So if you have 12 (12 < 12 ==> false) of 25 (25 > 25 ==> false) points, then you would fail the test, so C is incorrect.

    In the code from your first question doesn't initialize c indeed, but it does declare the variable, and it also assigns a value to it, before the end of the program.. The correct value, is indeed one of the first four answers

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matthayzon89
    I must not get what the question is asking...
    Yes, that is what I think is the case

    Quote Originally Posted by matthayzon89
    NOTE: I am not suppose to change the code, I am suppose to pick from the multiple choice options.
    I know. I want you to change the code.

    Suppose your teacher tells you you are wrong. How are you going to prove to him/her that you are correct? Obviously, one way is to change the code such that the program prints precisely the value that the question asks for. You run the program in front of your teacher, and thus demonstrate that your answer is correct. I am asking you how exactly you would modify the code for this demonstration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by laserlight View Post
    You run the program in front of your teacher, and thus demonstrate that your answer is correct. I am asking you how exactly you would modify the code for this demonstration.
    I am preparing for the Final for intro to C programming and doing this won't help my grade on the multiple choice part, I will try to do, but it does not help my understanding of how to answer these questions in any way, b/c on the test all I have is a paper and pencil (i cant modify code and try to compile, and prove myself to the teacher lol, etc..)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matthayzon89 View Post
    I must not get what the question is asking... I don't really understand what you were trying to explain.

    NOTE: I am not suppose to change the code, I am suppose to pick from the multiple choice options.
    It might help to look it up?

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by erispre View Post
    Well, for your second question, the range of passing the test is inbetween 12 and 25. The score should be higher than 12 and below 25. So if you have 12 (12 < 12 ==> false) of 25 (25 > 25 ==> false) points, then you would fail the test, so C is incorrect.

    In the code from your first question doesn't initialize c indeed, but it does declare the variable, and it also assigns a value to it, before the end of the program.. The correct value, is indeed one of the first four answers

    Thank you! that clarifies. Now I get it. The answer is C=2.

    Terminates means after the code is interpreted and before the program starts running, right?
    So I actually do go through all the If statements....

    And for the test answer question the answer is B, in that case.

    Sometimes its hard to understand what these questions are asking, and if I miss interpret it in my head and I get the logic correct for my interpretation, i end up getting the question WRONG. grrrrrrrrrr.
    Last edited by matthayzon89; 04-23-2010 at 11:01 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matthayzon89
    I am preparing for the Final for intro to C programming and doing this won't help my grade on the multiple choice part, I will try to do, but it does not help my understanding of how to answer these questions in any way, b/c on the test all I have is a paper and pencil (i cant modify code and try to compile, and prove myself to the teacher lol, etc..)
    If you do not know how to modify the code in this way, then you can only guess what the answer should be, because you do not understand the question.

    So yes, modifying the code, and even running the program to check your answer, will help you understand how to answer these questions when you only do it on paper.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by matthayzon89 View Post
    Thank you! that clarifies. Now I get it.
    So, right now, what do you think the answers to the two questions are?
    Let me give you a hint for the second one. There is no construct in C like a < b < c. Not like there is with maths, though. a < b < c gets evaluated as (a < b) < c. And "x < y" is either false (0) or true (1). So what is a < b < c?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matthayzon89
    The answer is C=2.
    Yes, though be careful: names in C are case sensitive.

    Quote Originally Posted by matthayzon89
    Terminates means after the code is interpreted and before the program starts running, right?
    No. It means the point just before the main function returns (i.e., the program is already running). If you did what I told you to do, it would be:
    Code:
    #include <stdio.h>
    int main() {
    
      int a,b,c;
      a = 4;
      b = a-2;
      if (a > b)
        c = b;
      else
        c = 8;
    
      printf("%d\n", c); /* this point */
    
      return 0;
    }
    So on paper, you should mentally (or with a pen/pencil) mark that line.

    Quote Originally Posted by matthayzon89
    Sometimes its hard to understand what these questions are asking, and if I miss interpret it in my head and I get the logic correct for my interpretation, i end up getting the question WRONG. grrrrrrrrrr.
    To be honest, the second question is not a good one: it requires you to interpret 12 < points < 25 as mathematical notation, but strictly speaking the expression is a valid C expression.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    To be honest, the second question is not a good one: it requires you to interpret 12 < points < 25 as mathematical notation, but strictly speaking the expression is a valid C expression.
    I think they do intend it to be interpreted as a valid C expression. And of course, it is obvious that one of the answers is correct. If not, then E is valid, as that includes everything else.
    So it's a fairly good question, imho. Because many people will assume that it's equal to points > 12 && points < 25, even though that assumption is wrong.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by matthayzon89 View Post
    Also,

    14) To what should the boolean expression in the following if statement be changed?

    if (12 < points < 25)
    printf("You pass the test.\n");

    A)12 < points || points < 25
    B)12 < points && points < 25
    C)points > 11 && points < 26
    D)!(12 < points < 25)
    E)None of the Above

    I said C) b/c i am assuming the range of the passing is from 12-25 so the minimum is 12 to pass the test thats why if the points are greater then 11 then its a passing grade, and the max is 25 so the grade must be lower then 26 in order to get a perfect score.


    Can someone please tell me if my logic is correct for these questions?
    IMO this question is just phrased poorly. To what should the condition be changed to achieve what exactly?.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    I think they do intend it to be interpreted as a valid C expression. And of course, it is obvious that one of the answers is correct. If not, then E is valid, as that includes everything else.
    So it's a fairly good question, imho. Because many people will assume that it's equal to points > 12 && points < 25, even though that assumption is wrong.
    Look at what the question asks for: "To what should the boolean expression in the following if statement be changed?" It is not: Which of these boolean expressions is equivalent to the boolean expression in the following if statement?

    In fact, notice that I stated very clearly that I believe that the expression should be interpreted as being in mathematical notation. Therefore, I would have gotten the question wrong myself. Did the question test my knowledge of C? No, it tested my reading comprehension of a poorly phrased question.

    But suppose I am right. Then, your interpretation must be wrong. Since we cannot be both right, surely there must be something wrong with the question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM