PHP ZCE mock test, interview preparation, daily lessons under chalk talk

Friday, April 27, 2012

value of $result?

What is the value of $result in the following PHP cdoe?

<?php
function timesTwo($int)
{
$int = $int*2;
}
$int = 2;
$result = timesTwo($int);
?>


a) 2
b) 0
c) 4
d) Null


You need catchy eyes in order to answer this question correctly...
Solution is d) Null  :P

Friday, April 20, 2012

PHP sitemap.xml generation via DOMDocument


A Sitemap is an XML file that lists the URLs for a site. It allows webmasters to include additional information about each URL: when it was last updated, how often it changes, and how important it is in relation to other URLs in the site. This allows search engines to crawl the site more intelligently. 


An example of generating sitemap.xml using DomDocument is mentioned below:

$doc = new DOMDocument();
$doc->formatOutput = true;


$r = $doc->createElementNS( "http://www.sitemaps.org/schemas/sitemap/0.9","urlset" );
$r->setAttributeNS(
                'http://www.w3.org/2001/XMLSchema-instance',
                'xsi:schemaLocation',
                'http://www.sitemaps.org/schemas/sitemap/0.9             http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'
                );


$doc->appendChild( $r );
/**
* Home Page
**/
$b = $doc->createElement( "url" );
$loc = $doc->createElement("loc");
$loc->appendChild($doc->createTextNode($SITE_URL));
$b->appendChild( $loc );
        $priority = $doc->createElement( "priority" );
        $priority->appendChild(
                        $doc->createTextNode('1.0')
                        );
        $b->appendChild( $priority );


        $r->appendChild( $b );


        $changefreq = $doc->createElement( "changefreq" );
        $changefreq->appendChild(
                        $doc->createTextNode('Daily')
                        );
       $b->appendChild( $changefreq );

        $lastmod = $doc->createElement( "lastmod" );
        $lastmod->appendChild(
                        $doc->createTextNode(date('Y-m-d'))
                        );
        $b->appendChild( $lastmod );

        $r->appendChild( $b );
/** repeat this for all pages on your site **/
$output = $doc->saveXML();
header("Content-type:text/xml");
echo $output;

Sunday, April 1, 2012

Value of $a?

What is the value of $a?


<?php
$a=in_array('01',array('1'))==var_dump('01'==1);
?>


a) True
b) False
c) Syntax Error
d) Null


At first, answer seems to False because 01 being a string is not equal to 1. ryt?
But 01 here is an octal number and it equals the 1 that is decimal...
therefore answer is
a) True
any confusion?


Friday, March 30, 2012

Output of the below code?

What will be output of the below code?

<?php
$a=array(1,2,3);
$b=array(4,5,6);
$c=1;
$a[$c++]=$b[$c++];
print_r($a);
?>

a) Array(1,4,3)
b) Array(1,6,3)
c) Array(4,2,6)
d) Array(1,5,3)
e) Array(1,2,5)



Solution: b) Array(1,6,3)
Now take some time and think how we got this solution!

At first we assume that right side of = is calculated first and is then assigned to left side.
So $b[$c++] should return 5, by then $c would have reached to 2 so $a[2]=5;
and output should be Array(1,2,5)

But what actually happens is that left side of = is calculated first, and then the right B-)

Thursday, March 15, 2012

Output of the below code?

What will be the output of the below code?


<?php
$a=0;
echo empty($a),isset($a);
?>

a) 10
b) 11
c) Syntax Error
d) 0


Solution: b) 11
Please tell me how?

Friday, February 24, 2012

Output of the following code?

What will be the output of the below code?

<?php
$qpt = 'Eat to live, but not live to eat';
echo preg_match("/^to/",$qpt);
?>


a)1
b)2
c)0
d)Null


Solution: c)0

Wednesday, February 1, 2012

Delete the content without resetting the auto increment?

Which of the following command will delete the entire contents of a table without resetting the auto increment value?

a) drop table <tablename>
b) delete from <tablename>
c) truncate table <tablename>
d) remove table <tablename>



I think the answer should be
b) delete from <tablename>

because truncate resets the auto increment id value to 1.
Please share if your answer differs...

Friday, January 20, 2012

PHP instanceOf and Type Hinting usage

instanceOf: It is one of the type operator present in php.


- It is used to determine whether a PHP variable is an instantiated object of a particular class.


- It can also be used to determine whether a variable is inheriting properly from another class. 


- Also used to determine whether a variable is an instantiated object of a class that implements an interface.




Example:


<?php
class ParentClass
{
}

class MyClass extends ParentClass
{
}

$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof ParentClass);
?>

Output:

bool(true)
bool(true)




Type Hinting: It is a feature that is added in PHP5


- It allows functions to force parameters to be objects by specifying the name of the class in the function prototype.


- It allows to use NULL as default parameter value.


Example:


    //First parameter must be an object of type OtherClass
     public function test(OtherClass $otherclass)

    //First parameter must be an array    public function test_array(array $input_array) 


Question 1:



When checking to see if two variables contain the same instance of an object, which of the following comparisons should be used?
 1)    if($obj1->equals($obj2) && ($obj1 instanceof $obj2))
 2)    if($obj1->equals($obj2))
 3)    if($obj1 === $obj2)
 4)    if($obj1 instanceof $obj2)
 5)    if($obj1 == $obj2)




Question 2:



In PHP 5 you can use the ______ operator to ensure that an object is of a particular type. You can also use _______ in the function declaration.
1)     instanceof, is_a
2)     instanceof, type-hinting
3)     type, instanceof
4)     ===, type-hinting
5)     ===, is_a


Solution 1:
3)     if($obj1 === $obj2)


Solution 2:
2)     instanceof, type-hinting

Zend Certified Engineer PHP5.3 ZCE




I passed the ZCE exam on 24th dec, 2011. I would like to share my exam experience with you.
I went for exam at NIIT Ltd , Cannought Place, New Delhi. I already scheduled my exam via Pearson Vue.
At Exam Center, When I Informed that I have already scheduled my exam at 11AM, they were surprised!!! It seemed that usually people directly go to exam center without even scheduling.

For exam, I wasn't allowed to keep any blank paper or pen. They gave a writing board and a marking pen.
There were total 70 questions and time allotted was 90 minutes. Questions were of single choice, multiple-choice and open text questions. For multiple-choice questions , it was clearly mentioned on how many to choose. On choosing more answers it gave an alert. However, if you choose less, loss is all yours.

During the exam, Computer got switched off twice. As expected on restarting time counting started from where it was left.

I was done with the paper in about 50 minutes. Rest of the time I reviewed my answers. As per my review, out of 70 questions 48 were for sure correctly answered. Answer for 1 question was a blind guess. And for the rest of the question, I gave logical answers. In all I guess about 60 questions got correct. I was lucky enough to get the exam passed.

I have 2 years of experience of developing with PHP, and I believe that practical exposure is must in order to clear the exam. Exam wasn't about the theory part at all.

You must be clear about below types of questions before going to attemp

1)
function stringAppend($str)
{
$str = $str."append";
}
function stringPrepend($str)
{
$str = "prepend".$str;
}

$aString = "PHP";
echo stringPrepend(stringAppend($aString));

2)
function updateValue(&$number)
{
$number = $number +1;
}
$val = 9;
echo updateValue(&$val);


3)
$foo = 'charanjeet/working/with/php';
echo strrpos($foo, '/', -5);


4)
class MyException extends Exception { }
class Test {
        public function testing()
        {
                try
                {
                        try
                        {
                                throw new MyException('foo!');
                        }
                        catch (Exception $e)
                        {
                                echo 'cought1';
                                /* rethrow it */
                                throw $e;
                        }
                        catch (MyException $e)
                        {
                                echo 'cought2';
                                /* rethrow it */
                                throw $e;
                        }
                }
                catch (MyException $e)
                {
                        echo 'cought3';
                        echo $e->getMessage();
                }
        }
}

$foo = new Test;
$foo->testing();


5)
Which of the following are valid PHP variables?

1. @$foo
2. &$variable
3. ${0x0}
4. $variable
5. $0x0


6)What is the output of the following PHP code?
<?php
define('FOO', 10);
$array = array(10 => FOO,          
    "FOO" => 20);
print $array[$array[FOO]] * $array["FOO"];

7)

What is the output of the following PHP script?

<?php
$a = 1;
$b = 2.5;
$c = 0xFF;

$d = $b + $c;
$e = $d * $b;
$f = ($d + $e) % $a;

print ($f + $e);

?>
1. 643.75
2. 432
3. 643
4. 257
5. 432.75
Click here for answer

8)

What is the output of the following code?

<?php
$string = "111221";
for($i = 0; $i < strlen($string); $i++) {
 $current = $string[$i];
 $count = 1;
 while(isset($string[$i + $count]) && ($string[$i + $count] == $current)) $count++;
 $newstring .= "$count{$current}";
 $i += $count-1;
}

print $newstring;
?>
1. 312211
2. 3312212
3. 11221221
4. 221131
5. 3211122



9)

What is the best way to ensure that a user-defined function is always passed an object as its single parameter?


1. function myfunciton($a = stdClass)
2. Use is_object() within the function
3. There is no way to ensure the parameter will be an object
4. function myfunction(Object $a)


10)

function stringAppend(&$str)

{

return $str = $str."append";

}

function stringPrepend(&$str)

{

$str = "prepend".$str;

}


$aString = "PHP";

echo stringPrepend(stringAppend($aString));

Next parts would come soon!!!