Which of the following assignment statements are syntactically correct?

PHP

i.   $a = -10;
ii.  10 = $b;
iii. $a_b = $a_b + 1;    
iv.  $a = "COWS";
v.    $a = COWS;
vi.   $a + $b = 40;    
vii.  $a = 3 $b;    
viii. $a = "true";
ix.  $a = true;
x.   $a %= 2;
xi.  $a += 1;
xii. $a =* 2;

Java, C++, C#

i.   a = -10;
ii.  10 = b;
iii. a_b = a_b + 1;    
iv.  a = "COWS";
v.    a = COWS;
vi.   a + b = 40;    
vii.  a = 3 b;    
viii. a = "true";
ix.  a = true;
x.   a %= 2;
xi.  a += 1;
xii. a =* 2;

Visual Basic, Python

i.   a = -10
ii.  10 = b
iii. a_b = a_b + 1    
iv.  a = "COWS"
v.    a = COWS
vi.   a + b = 40    
vii.  a = 3 b    
viii. a = "true"
ix.  a = true
x.   a -= b
xi.  a += 1
xii. a =* 2
Solution

  1. Correct. It assigns the integer value -10 to variable a.
  2. Wrong. On the left side of the assignment operator, only one variable can exist.
  3. Correct. It increases variable a_b by one.
  4. Correct. It assigns the string (the text) COWS to variable a.
  5. Correct. It assigns the content of constant COWS to variable a.
  6. Wrong. On the left side of the assignment operator, only one variable can exist.
  7. Wrong. It should have been written as a = 3 * b (as $a = 3 * $b in PHP).
  8. Correct. It assigns the string true to variable a.
  9. Correct. It assigns the value true to variable a.
  10. Correct. This is equivalent to a = a - b (to $a = $a - $b in PHP).
  11. Correct. This is equivalent to a = a + 1 (to $a = $a + 1 in PHP).
  12. Wrong. It should have been written as a *= 2 (as $a *= 2 in PHP)

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!