Php Objective Questions with Answers for written test exams 23

Question 23
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];
Answers 23


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.

  







Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---