RSS

PHP - 3


Question 1: What will this output
      <?php
      $a = false;
      $b = true;
      $c = false;
 
      if ($a ? $b : $c) {
      echo "false";
      } else {
      echo "true";
      }
      ?>
    
Correct answer: true
Explanation: If $a is true, then $b will be evaluated; otherwise (as is the case here) $c will be evaluated. As $c is false, the conditional evaluates as false, and the script (confusingly) prints true.

Question 2: What can you use to replace like with hate in I like Regular Expressions?
Correct answer: preg_replace("/like/", "hate", "I like Regular Expressions")
Explanation: The search is a regular expression and it has slashes around it, the replace isn't, so it doesn't have any slashes.

Question 3: What library do you need in order to process images?
Correct answer: GD library
Explanation: http://www.boutell.com/gd/

Question 4: What function sends mail:
Correct answer: Both of them
Explanation: imap_mail, mail

Question 5: What is the problem with <?=$expression ?> ?
Correct answer: It requires short tags and this is not compatible with XML
Explanation: If you have short_open_tag On then this XML <?xml version="1.0" encoding="utf-8"?> (for example) will be parsed by the PHP engine, causing both a PHP and a XML parsing error.

Question 6: Put this line php display_errors=false in a .htaccess file when you deploy the application?
Correct answer: That won't hide any error 'coz it's not the correct code
Explanation: I would have said 'Good idea, increases security', but wait a minute... the correct syntax is php_flag display_errors Off ...

Question 7: Which of the following regular expressions will match the string go.go.go?
Correct answer: ..\...\...
Explanation: Any character (twice) followed by a period followed by any character (twice) followed by ...

Question 8: A constructor is a special kind of ...
Correct answer: Method
Explanation: It is a function, but since it's in a class we call it a method.

Question 9: What does break; do?
Correct answer: Ends execution of the current for, foreach, while, do-while or switch structure
Explanation: Yup. If you were tempted to say 'moves on to the next iteration', that's the continue statement.

Question 10: Can this PHP code be valid:
$4bears = $bears->getFirst4();
Correct answer: No
Explanation: A variable name can't start with a number. Don't ask me why not, I don't see any reason why it shouldn't; it's probably some carry over from C.

Question 11: Who created PHP as we know it today?
Correct answer: Rasmus Lerdorf
Explanation: The history is a bit more complicated than that, so here it is if you want to read it.

Question 12: What will this output:
<?php
$dog = "Dogzilla";
$dragon = &$dog;
$dragon = "Dragonzilla";
 
echo $dog." ";
echo $dragon;
?>
Correct answer: Dragonzilla Dragonzilla
Explanation: $dragon is really $dog, it just has a different name.

Question 13: How can you tell a page (like this one) has been created by PHP?
Correct answer: You can never be 100% sure if a page was created by PHP
Explanation: A PHP page might be missing the X-Powered-By: PHP header and have an extesion different from .php. In addition, you can output that header and use the extension .php when programming with any other language, just to confuse potential hackers.

Question 14: Assuming all the variables a, b, c have already been defined, could this be a variable name: ${$a}[$b][$c] ?
Correct answer: Yes
Explanation: Yup, it's a multidimensional array.

Question 15: In MySQL, if you want to find out about the structure of a table tblQuiz, what will you use?
Correct answer: DESCRIBE tblQuiz
Explanation: The others will just produce an error.

Question 16: How can you count all the rows in a table tblQuiz?
Correct answer: SELECT COUNT(*) FROM tblQuiz
Explanation: This is some very basic SQL syntax, I hope you got it right.

Question 17: In which of the following scenarios might you use the function md5?
Correct answer: perform authentication without unnecessarily exposing the access credentials
Explanation: md5 returns an unique hash string for the string you pass it; if you have only the hash it's practically impossible to find the string that produces it.

Question 18: Back to MySQL, can you select data from more than one table in a single query?
Correct answer: Yes
Explanation: Duuh! Ever heard of joins?!

Question 19: What is the logo of MySQL?
Correct answer: A dolphin
Explanation: You'll see the dolphin on the My SQL site.

Question 20: What does this function do:
<?php
function my_func($variable)
{
        return (is_numeric($variable) && $variable % 2 == 0);
}
?>
Correct answer: tests whether $variable is an even number
Explanation: It returns true if $variable is divisible by 2.

Question 21: How do you find the square root of a number?
Correct answer: In both PHP and MySQL you have a function called sqrt
Explanation: The Manual.

Question 22: Assuming results of a bunch of quizzes are kept in the following table, how do you print all the quizzes' names and their average score (quizName is 'Php Mysql', 'Css Html', 'Web Design' and so on for other quizzes)?
+--------------+--------------+
| Field        | Type         |
+--------------+--------------+
| userName     | varchar(20)  |
| userScore    | tinyint(3)   |
| userComments | varchar(255) |
| quizName     | varchar(20)  |
+--------------+--------------+
Correct answer: select quizName, avg(userScore) from tblQuiz group by quizName;
Explanation: The third option would have been correct if instead of userScore / count(userScore) it had sum(userScore) / count(userScore).

Question 23: Is this quiz table normalized and is that OK?
+--------------+--------------+
| Field        | Type         |
+--------------+--------------+
| userName     | varchar(20)  |
| userEmail    | varchar(20)  |
| userScore    | tinyint(3)   |
| userComments | varchar(255) |
| quizName     | varchar(20)  |
| quizType     | varchar(20)  |
| quizUrl      | varchar(20)  |
+--------------+--------------+
Correct answer: it's not normalized, and that's NOT OK
Explanation: The quiz data (name, type, url) should be moved in a new table because it repeats itself on many rows. The table that stores results should be linked to this new table by a foreign key.

Question 24: Which is the correct timeline (OO stands for object oriented)?
Correct answer: OO Analysis, OO Design, OO Programming
Explanation: First Analysis, then Design, then Programming.

Question 25: What does this output:
class  T
{
        public $m = "a";
        
        function T()
        {
               $this->m = "b";
        }
}
 
$t = new T();
$p = $t->m;
$p = "c";
 
echo $t->m;
 
Correct answer: b
Explanation: $p = "c"; does not modify the object member.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Post a Comment