# Very simple, possibly trick question.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-23-2010
matthayzon89
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?
• 04-23-2010
laserlight
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?
• 04-23-2010
matthayzon89
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?
• 04-23-2010
matthayzon89
Quote:

Originally Posted by laserlight
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.
• 04-23-2010
erispre
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 :)
• 04-23-2010
laserlight
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.
• 04-23-2010
matthayzon89
Quote:

Originally Posted by laserlight
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..)
• 04-23-2010
tabstop
Quote:

Originally Posted by matthayzon89
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?
• 04-23-2010
matthayzon89
Quote:

Originally Posted by erispre
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.
• 04-23-2010
laserlight
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.
• 04-23-2010
EVOEx
Quote:

Originally Posted by matthayzon89
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?
• 04-23-2010
laserlight
Quote:

Originally Posted by matthayzon89

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.
• 04-23-2010
EVOEx
Quote:

Originally Posted by laserlight
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.
• 04-23-2010
claudiu
Quote:

Originally Posted by matthayzon89
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?.
• 04-23-2010
laserlight
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last