Some PHP Questions

0. Name the Super Global that holds the cookies.

1. Which was not available before php5 -
a) DOM b) SOAP c) SAX d) XSL

2. Is the use of type hinting available in methods only ?

3. What's the output of the following:


function foo(IAMACLASS &$arg0 = NULL)
{
    if (is_null($arg0))
    {
         echo 42;
    }
}

4. What's the output of the following:


function foo(IAMACLASS &$x = NULL)
{
    $x = 42;
}

$obj = new stdClass;
foo($obj);
echo $obj;

5. Which DomElement function isused to check the presence of an attribue in non-namespace way?
a) getAttributeNS()
b) getAttribute()
c) hasAttribute()
d) hasAttributeNS()

6. Use of quotemeta, addslashes()

7. What should be used as the second parameter of htmlentities() to escape single quotes?
a) ENT_QUOTES b) ENT_COMPAT

8. What's the best way to copy from one file to another?
a) copy() b) stream_to_copy_stream()

9. Where is business rules implemented in MVC (Model-View-Controller) ?
a) Model b) View c) Controller

10. Which pattern do you use if you want to initialize a class only once?
a) Singleton b) Adaptor

11. Which interface should be implemented to use array_values() on objects?
a) Traversable b) ArrayAccess c) ArrayObect

12. mail(...., "From: {$_COOKIE['email']}");
- what kind of vulnerability does this line of code expose?
a) XSS b) XSRF c) Email injection d) None of the above

13. Questions on substr_compare(), strrpos(), and strstr()

14. Question on preg_match() and a weird regex.

15. What's the best way to check for a key in an arry when none of the value is NULL?
a) isset() b) array_key_exists()

16. Name the function that's used to load a class on the fly?

17. Which of the following function is used to access virtual class properties?
a) __get() b) __fetch

18. Question on __set(), __get(), __isset(), __unset()

19. Which of the following doesn't contain any direct client input?
a) $_GET b) $_POST c) $_SESSION d) $_SERVER

20. Which function is used to import a SimpleXML document into DOM?
a) dom_import_simplexml()
b) simplexml_import_dom()
c) dom2simplexml()
d) simplexml2dom()

21. What's the output of the following?

book.xml:
---------
    <books>
      <book id="1">book0</book>
      <book id="2">book1</book>
    </books>

-----
<?php
    $xml = simplexml_load_file('book.xml');
    $c = $xml->children();
    echo $c[1];

22. Which of the following two statements about PHP classses is true?
a) A class can extend multiple classes
b) A class can implement multiple classes
c) A class can implement multiple interfaces
d) A class can extend multiple interfaces
e) An interface can extend multiple interfaces
f) An interface can implement multiple interfaces

23. What happens when a child class tries to use a private variable of the parent class?
a) E_ERROR
b) E_WARNING
c) E_NOTICE
d) None of the above

24. What's the output of the following?

  
<?php
    define('PI', 3.14);

    class Math
    {
        const PI = PI;
    }

    class A extends Math
    {
         function __construct()
         {
              echo Math::PI;
         }
    }

    $obj = new A;
  

25. Which of the following function declaration is invalid?
a) function foo($a = array())
b) function foo(&$a = NULL)
c) function foo($x = 1)
d) function foo($x = $x2)

26. What's the output of the following?

  
<?php
    $a = 'three';

    switch($a)
    {
        case 'one': echo 'one'; break;
        case 'two': echo 'two'; break;

        default: echo 'four'; break;

        case 'three': echo 'three'; break;
    }
  

27. Which of the following is a valid CASE value in SWITCH statements?
a) scaler b) bool c) expressions

28. $a = array(true, '0' => false, false => false);
$a[0] = ?

29. Which of the following should be used to prevent session fixation?
a) session_regenerate_id()
b) session_start()

30. 1 ^ 2 = ?

31. How to parse a url?
a) parse_url() b) parse_str()

32. What is true database connections? (Choose 2)
a) Database connections are closed after every call.
b) Database connections are closed after every request.
c) Database connections are closed when web server is shutdown.
d) Multiple PHP web apps can use the same database connection.

33. What's the benefit of prepared statements over programmatically built SQL?
a) secure b) faster

34. What's the use of SQL 'LIMIT' ?
a) DELETE certain rows.
b) SELECT certain rows.

35. What can be done to speed up a join query?
a) Add 'WHERE' clause.
b) INNER JOIN
c) Create INDEX over the joined columns
d) None of the above

36. Which functions are used to secure output in the context of HTML? (Choose 2)
a) htmlentities()
b) addslashes()
c) strip_tags()
d) htmlspecialchars()

37. What can be done to prevent XSS attack?
a) Strip all <script> tags.
b) Strip all javascript.
c) None of the above.

38. What was allowed in PHP4 but not in PHP5?
a) Assign value to $this inside the constructor function.

39. What's the output of the following?

  class A
  {
    function A() { echo 1; }

    function __construct() { echo 2; }
  }

  $obj = new A;

40. Question on SimpleXMLElement->xpath()