0% found this document useful (0 votes)
1 views7 pages

Software Testing-1 Chart

The document provides corrections for three pieces of PHP code, addressing syntax errors, logical issues, and formatting improvements. Each section outlines specific mistakes and offers corrected code snippets, including function definitions and print statements. Additionally, suggestions for further enhancements, such as adding comments and input validation, are included.

Uploaded by

Emmanuel Hayford
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Software Testing-1 Chart

The document provides corrections for three pieces of PHP code, addressing syntax errors, logical issues, and formatting improvements. Each section outlines specific mistakes and offers corrected code snippets, including function definitions and print statements. Additionally, suggestions for further enhancements, such as adding comments and input validation, are included.

Uploaded by

Emmanuel Hayford
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1

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:

for($i = 0; $i < 32; $i++)

3. There is a typo on line 5. The bitwise left shift operator is << not <<<=. It should be:

$result = $result << 1;

4. On line 6, the bitwise AND operation is missing the assignment operator. It should be:

$result |= ($n & 1);

5. The reverse_integer function needs to return the result:

return $result;

Here is the corrected code:

```
<?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));
?>
```

This will now print the expected output of 1260388352.


To further improve the code, adding some comments explaining the logic would be helpful. And
the function could validate that $n is a 32-bit unsigned integer before operating on it.

Q2
Here are a few things that need to be fixed in the code:

1. The function declaration is incomplete on line 1. It should be:

```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)
```

3. The string outputs are missing quotation marks around them.

4. The print_r statements are missing parentheses around the function call.

Here is the fixed code:

```
<?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);
```

And here is a flowchart:


Q3.

Here are the issues I see with the code:

1. On line 8, the return statement is missing a semicolon and has incorrect quotation marks:

```
return "Blank String";
```

2. On line 10, there is a typo - $words should be $words:

```
$words = explode(' ', $s);
```

3. The if/else logic is a bit unclear. I would simplify it to:

```
if(count($words) > 1) {
// get length of last word
} else {
echo "Single word";
}
```

4. The print statements need parentheses around the function call:

```
print_r(last_words("PHP Exercises"));
```

5. Add newline \n to the print statements for formatting.

Here is how I would rewrite the function:

```
function last_word_length($s) {

if (trim($s) === "") {


return "Blank String";
}

$words = explode(' ', $s);


if (count($words) > 1) {
return strlen(end($words));
} else {
return "Single word";
}

print_r(last_word_length("PHP Exercises") . "\n");


print_r(last_word_length("PHP") . "\n");
print_r(last_word_length("") . "\n");
print_r(last_word_length("") . "\n");
```

Flowchart

You might also like