0

In the university I had a quiz today. The quiz is over but I can't understand some of its questions are their correct answers.

Note: I am not asking this to solve my quiz. Quiz is over I am just confused in some questions.

Question 1:

Consider the following variable declarations:

int catHeight = 6;
int dogHeight = 7; 
string dogName = "Rover"; 
string catName = "Sylvester"; 
float catWeight = 15.0; 
float dogWeight = 20.0; 
bool dogRabies = true; 
bool catRabies = false; 

Choose Boolean expressions equivalent to following statements.

the cat has rabies and does not weigh 20 pounds or less

  • catRabies && catWeight > 20
  • !( catRabies && catWeight <=20)
  • ! catRabies && catWeight >=20(This was marked as correct. I think the first option is correct)

the cat height and the dog height are not 10 (Hint: more than 1 answer)

  • catHeight > 10 && dogHeight >10
  • (catHeight && dogHeight) != 10
  • catHeight !=10 && dogHeight != 10

2nd and third are were marked as correct in result. But I think that only third one is correct. Please explain if I am wrong.

Question 2:

if numNeighbors >= 3 || numNeighbors = 4
   ++numNeighbors;
   cout << "You are dead" << endl;
else
   --numNeighbors;

What is wrong with the following if statement (there are at least 3 errors). The indentation indicates the desired behavior

  • syntax error; else without previous if(marked as correct)
  • syntax error; value required of left operand (marked as correct)
  • syntax error; Parenthesis missing (marked as corrent)
  • syntax error; statement missing

I understand why 1 and 3 are correct but can't get the meaning of second one. Kindly explain it.

Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • 9
    Well I only looked at the first question, but I would say you are correct and the given answers are wrong. I'd ask for your money back. – john Jan 06 '21 at 09:08
  • @Scheff So are options marked by teacher as correct in second question are actually correct? – Maheer Ali Jan 06 '21 at 09:13
  • @Scheff I agree that the quiz is bad, but finding those two problems is a part of a quiz. They were added intentionally. – HolyBlackCat Jan 06 '21 at 09:16
  • It seems so. Though, what is a _value_? ;-) – Scheff's Cat Jan 06 '21 at 09:16
  • @Scheff No value mentioned. It was just an image of code :) – Maheer Ali Jan 06 '21 at 09:17
  • @HolyBlackCat Maybe, I misunderstood the question... ;-) I thought it's something like a Webinar. I still struggle with Question 1... – Scheff's Cat Jan 06 '21 at 09:17
  • Concerning question 1: _cat has rabies and does not weigh 20 pounds or less_ -> `catRabies && catWeight > 20` Though, the `catWeight = 15.0;`. And, btw. assigning a `double` constant (`15.0`) to a `float` variable... Hmm. I would stumble with my eyes if it were in my code... ;-) – Scheff's Cat Jan 06 '21 at 09:21
  • 3
    Your teacher is quizzing in an incredibly confusing way. Writing `!catRabies` as a check that the cat **does** have rabies is non-descriptive and incredibly unidiomatic. – StoryTeller - Unslander Monica Jan 06 '21 at 09:22
  • One last question. Should I continue studying in this university or just leave it? :) – Maheer Ali Jan 06 '21 at 09:24
  • 1
    This is a hard decision and I wouldn't dare to recommend something - after a short conversation. However, you still have the option with a [good book](https://stackoverflow.com/q/388242/7478597) (for accompanying studies). ;-) – Scheff's Cat Jan 06 '21 at 09:26

3 Answers3

2

3 errors in question 2:

  1. missing ( ) around the if condition
  2. in the second part of the if condition there must be double ==
  3. { } are missing

To be a valid code it must be set like this:

if (numNeighbors >= 3 || numNeighbors == 4)
{
   ++numNeighbors;
   cout << "You are dead" << endl;
}
else
   --numNeighbors;
Lies
  • 516
  • 6
  • 18
  • But double equal is not a syntax error I guess. Single equal will not work as expected but its not syntax error – Maheer Ali Jan 06 '21 at 09:12
  • 2
    @MaheerAli You are not complete right. The assignment operator has a less precedence than nearly all other operators. Hence, `numNeighbors >= 3 || numNeighbors = 4` is processed as `(numNeighbors >= 3 || numNeighbors) = 4` and `numNeighbors >= 3 || numNeighbors` is not an LValue (i.e. not something you can assign to). – Scheff's Cat Jan 06 '21 at 09:15
2

This was marked as correct. I think the first option is correct

Yes, you're right.

But I think that only third one is correct.

You're also right here.

Question 2

This one does not make sense unless you are trying to parse like a compiler. For instance, "else without previous if" only makes sense if you consider the current state of the code and not what you are trying to achieve. But the question tells you what you are trying to achieve.

syntax error; value required of left operand (marked as correct)

This means the condition is being parsed as (numNeighbors >= 3 || numNeighbors) = 4; which makes clear that the left side is not something you can assign to.

Acorn
  • 24,970
  • 5
  • 40
  • 69
1

Your understanding of (1.1) and (1.2) seems to be correct.

In (2), if you fix the other errors,

if (numNeighbors >= 3 || numNeighbors = 4)

will be parsed as

if ((numNeighbors >= 3 || numNeighbors) = 4)

For this GCC outputs error: lvalue required as left operand of assignment, which reads similar to "value required of left operand".

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207