Software Testing-1 Chart
Software Testing-1 Chart
Here are a few suggestions to correct the errors in the code and get the expected output:
1. There is a syntax error on line 8. The print_r statement is missing a closing parenthesis
before the semicolon. It should be:
print_r(reverse_integer(1234));
2. The for loop condition is incorrect. It should check against i less than 32 rather than just i:
3. There is a typo on line 5. The bitwise left shift operator is << not <<<=. It should be:
4. On line 6, the bitwise AND operation is missing the assignment operator. It should be:
return $result;
```
<?php
function reverse_integer($n) {
$result = 0;
for($i = 0; $i < 32; $i++) {
$result = $result << 1;
$result |= ($n & 1);
$n >>= 1;
}
return $result;
}
print_r(reverse_integer(1234));
?>
```
Q2
Here are a few things that need to be fixed in the code:
```php
function isPowerOfTwo($n) {
}
```
2. There is a syntax error on line 3 with the & operator. This looks like it was supposed to be a
bitwise AND operator but is missing the second operand. It should check if $n AND ($n - 1) is 0:
```php
if (($n & ($n - 1)) == 0)
```
4. The print_r statements are missing parentheses around the function call.
```
<?php
function isPowerOfTwo($n) {
if (($n & ($n - 1)) == 0) {
echo "$n is power of 2.\n";
} else {
echo "$n is not power of 2.\n";
}
}
isPowerOfTwo(4);
isPowerOfTwo(3);
isPowerOfTwo(16);
```
1. On line 8, the return statement is missing a semicolon and has incorrect quotation marks:
```
return "Blank String";
```
```
$words = explode(' ', $s);
```
```
if(count($words) > 1) {
// get length of last word
} else {
echo "Single word";
}
```
```
print_r(last_words("PHP Exercises"));
```
```
function last_word_length($s) {
Flowchart