Monday, March 23

Php Objective Questions with Answers for written test exams


1. Which of the following functions allows you to store session data in a database?

A. session_start();

B. session_set_save_handler();

C. mysql_query();

D. You cannot store session data in a database.

   
Answer B is correct.You can use session_set_save_handler() to override

 

    PHP’s default session-handling functions and store session data any way you want.

 

    Answer A is incorrect because session_start() only activates PHP sessions for

 

    the current script. Answer C is incorrect because mysql_query() only executes a

 

    query with MySQL and does not affect the behavior of PHP’s session mechanism.

 

    Answer D is incorrect because this statement is false



2. Which of the following types can be used as an array key? (Select three.)

A. Integer

B. Floating-point

C. Array

D. Object

E. Boolean

   
Answers A, B, and E are correct. A Boolean value will be converted to either 0 if

    it is false or 1 if it is true, whereas a floating-point value will be truncated to its

integer equivalent.Arrays and objects, however, cannot be used under any circumstance.

3. Which of the following functions can be used to sort an array by its keys in

descending order?

A. sort

B. rsort

C. ksort

D. krsort

E. reverse_sort

   
Answer D is correct.The sort() and rsort() functions operate on values, whereas

  

    ksort() sorts in ascending order and reverse_sort() is not a PHP function.

4. What will the following script output?

<?php

$a = array (‘a’ => 20, 1 => 36, 40);

array_rand ($a);

echo $a[0];

?>

A. A random value from $a

B. ‘a’

C. 20

D. 36

E. Nothing

  
   
ANSWER E Only E is correct.The $a array doesn’t have any element with a numeric key of

  

    zero, and the array_rand() function does not change the keys of the array’s elements—

  

    only their order.


5. Given

$email = ‘bob@example.com’;

which code block will output example.com?

A. print substr($email, -1 * strrpos($email, ‘@’));

B. print substr($email, strrpos($email, ‘@’));

C. print substr($email, strpos($email, ‘@’) + 1);

D. print strstr($email, ‘@’);

   
Answer C is correct. strpos() identifies the position of the @ character in the

  

    string.To capture only the domain part of the address, you must advance one place

  

    to the first character after the @.

6. Which question will replace markup such as img=/smiley.png with <img

src=”/smiley.png”>?

A. print preg_replace(‘/img=(\w+)/’, ‘<img src=”\1”>’, $text);

B. print preg_replace(‘/img=(\S+)/’, ‘<img src=”\1”>’, $text);

C. print preg_replace(‘/img=(\s+)/’, ‘<img src=”\1”>’, $text);

D. print preg_replace(‘/img=(\w)+/’, ‘<img src=”\1”>’, $text);


   
Answer B is correct.The characters / and . are not matched by \w (which only

matches alphanumerics and underscores), or by \s (which only matches whitespace).

7. Which of the following functions is most efficient for substituting fixed patterns in
   strings?
       
        A. preg_replace()
       
        B. str_replace()
       
        C. str_ireplace()
       
        D. substr_replace()

       
   
    Answer B is correct.The PHP efficiency mantra is “do no more work than necessary.”
   
    Both str_ireplace() and preg_replace() have more expensive (and flexible)
   
    matching logic, so you should only use them when your problem requires it.
   
    substr_replace() requires you to know the offsets and lengths of the substrings
   
    you want to replace, and is not sufficient to handle the task at hand.
   
8. If

    $time = ‘Monday at 12:33 PM’;
   
    or
   
    $time = ‘Friday the 12th at 2:07 AM’;
   
    which code fragment outputs the hour (12 or 2, respectively)?
   
    A. preg_match(‘/\S(\d+):/’, $time, $matches);
   
    print $matches[1];
   
    B. preg_match(‘/(\w+)\Sat\S(\d+):\d+/’, $time, $matches);
   
    print $matches[2];
   
    C. preg_match(‘/\s([a-zA-Z]+)\s(\w+)\s(\d+):\d+/’, $time,
   
    $matches);
   
    print $matches[3];
   
    D. preg_match(‘/\s(\d+)/’, $time, $matches);
   
    print $matches[1];
   
    E. preg_match(‘/\w+\s(\d+):\d+/’, $time, $matches);
   
    print $matches[1];  

   
    Answer E is correct. Answer A and B both fail because \S matches nonwhitespace
   
    characters, which break the match. Answer C will correctly match the first $time
   
    correctly, but fail on the second because ‘12th’ will not match [a-zA-Z]. Answer D
   
    matches the first, but will fail on the second, capturing the date (12) instead of the
   
    hour.

9. Which of the following output ‘True’?

    A. if(“true”) { print “True”; }
   
    B. $string = “true”;
   
    if($string == 0) { print “True”; }
   
    C. $string = “true”;
   
    if(strncasecmp($string, “Trudeau”, 4)) { print “True”; }
   
    D. if(strpos(“truelove”, “true”)) { print “True”; }
   
    E. if(strstr(“truelove”, “true”)) { print “True”; }

   
    Answers A, B, C, and E are correct. Answer A is correct because a non-empty
   
    string will evaluate to true inside an if() block. Answer B is covered in the chapter—
   
    when comparing a string and an integer with ==, PHP will convert the string
   
    into an integer. ‘true’ converts to 0, as it has no numeric parts. In answer C,
   
    strncasecmp() returns 1 because the first four characters of ‘Trud’ come before
   
    the first four characters of true when sorted not case sensitively. Answer D is
   
    incorrect because strpos() returns 0 here (true matches truelove at offset 0).
   
    We could make this return True by requiring strpos() to be !== false. Answer
   
    E is correct because strstr() will return the entire string, which will evaluate to
   
    true in the if() block.
   
10. What are the contents of output.txt after the following code snippet is run?

    <?php
   
    $str = ‘abcdefghijklmnop’;
   
    $fp = fopen(“output.txt”, ‘w’);
   
    for($i=0; $i< 4; $i++) {
   
    fwrite($fp, $str, $i);
   
    }
   
    ?>
   
    A. abcd
   
    B. aababcabcd
   
    C. aababc
   
    D. aaaa

   
    The correct answer is C. On the first iteration, $i is 0, so no data is written. On
   
    the second iteration $i is 1, so a is written. On the third, ab is written, and on the
   
    fourth abc is written.Taken together, these are aababc.

11. Which of the following can be used to determine if a file is readable?

    A. stat()
    B. is_readable()
    C. filetype()
    D. fileowner()
    E. finfo()

   
    The correct answers are A and B. stat() returns an array of information about a
   
    file, including who owns it and what its permission mode is.Together these are
   
    sufficient to tell if a file is readable. is_readable(), as the name implies, returns
   
    true if a file is readable.
   
12. Specifying the LOCK_NB flag to flock() instructs PHP to

    A. Return immediately if someone else is holding the lock.
    B. Block indefinitely until the lock is available.
    C. Block for a number of seconds dictated by the php.ini setting
       flock.max_wait or until the lock is available.
    D. Immediately take control of the lock from its current holder.

   
    The correct answer is A.The LOCK_NB flag instructs PHP to take a nonblocking
   
    lock, which immediately fails if another process holds the lock.

13. If you have an open file resource, you can read data from it one line at a time with
    the _____ function.

   
       
    The correct answer is fgets().
   
14. Which of the following functions require an open file resource?

    A. fgets()
    B. fopen()
    C. filemtime()
    D. rewind()
    E. reset()

           
    The correct answers are A and D. fgets() and rewind() both act on an open file
   
    resource. fopen() opens files to create resources, whereas filemtime() takes a filename
   
    and reset() acts on arrays.
   
15. Which of the following sentences are incorrect?

    A. date() returns the current UNIX datestamp.
    B. date() returns a formatted date string.
    C. date() requires a time stamp to be passed to it.
    D. date() returns a date array.


    The correct answers are A, C, and D. date() takes a format string and an optional
   
    time stamp and produces a formatted date string. If a UNIX time stamp is not
   
    passed into date(), it will use the current time.
   
16. The ________ function will return the current UNIX time stamp.
   
       
    The correct answer is time().
   
17. Which of the following functions will output the current time as 11:26 pm?

    A. print date(‘H:m a’);
    B. print date(‘G:M a’);
    C. print date(‘G:i a’);
    D. print strftime(‘%I:%M %p’);

   
    The correct answers are C and D.
   
18. Which of the following is not an aggregate function?

    A. AVG
    B. SUM
    C. COUNT
    D. GROUP BY
    E. MIN

       
    The correct answer is D. Group by is a grouping clause, not an aggregate function.
   
19. How is a transaction terminated so that the changes made during its course are discarded?

    A. ROLLBACK TRANSACTION
    B. COMMIT TRANSACTION
    C. By terminating the connection without completing the transaction
    D. UNDO TRANSACTION
    E. DISCARD CHANGES

   
    A and C are both valid answers. A transaction is not completed when the connection

    between your script and the database server is discarded, as if a ROLLBACK
   
    TRANSACTION command has been issued.

No comments:

Post a Comment