banner
Sep 28, 2013
9 Views
0 0

Odesk PHP Programming Skills Test Answer 2013

Written by
banner

For the presence of your freelance accounts in Odesk, Elance with good quality you have to make a good a result in PHP Programming Skills Test . Here are the solutions, I try to combine them what I have found so far.

 

1.  Write a function declared as function ReformatPhoneNumber($number), whose argument will contain string data representing some phone number data (entered by the user).

A valid phone number may consist of between 7 and 12 digits (0..9). Assume that in between some adjacent digits there may optionally appear either a single space, or a single hyphen (-). Any other phone number should be considered invalid.If the phone number is valid, the return value of your function should contain a string containing between 7 and 12 digits, representing the same phone number after removing all hyphens and spaces. If the phone number is invalid, throw a standard PHP5 Exception initialized with the text “Invalid phone number”.The first and the last character of the string should be a number.

For example, after calling ReformatPhoneNumber(’012-345 69′) the return value should be ’01234569′. Calling the function with any of these values: ’012345′, ‘-012345 678′, ’01203- 34566′, ’123456678875432′, ’1234×567′ should result in an exception.

ANS:

function ReformatPhoneNumber($number)
{
if (preg_match('/^(\d[ -]?){7,12}$/', $number, $matches))
{
return preg_replace('/[ -]/', '', $number);
}

throw new Exception('Invalid phone number');
}

2. Write a program that outputs the numbers that are divisible by 8 and are between 200 and 600 (inclusive), separated by commas (without spaces or line breaks).

ANS:

for($i = 200; $i <= 592; $i+=8)
{
    echo $i.',';
}
echo $i;

3. You are given an array in PHP, which contains positive integers and/or recursively nested arrays of positive integers.
It may, for example, be initialized as:$arr = array(array(141,151,161), 2, 3, array(101, 202, array(303,404)));
Write a function “function MaxArray($arr)” which returns the maximum value contained in $arr or some array nested within $arr. In the example, the returned value should be 404.

ANS:

function MaxArray($arr)
{
    $GLOBALS['max'] = 0;
    array_walk_recursive($arr,create_function('$item,$key','if($item > $GLOBALS["max"]) $GLOBALS["max"] = $item;'));
    return $GLOBALS['max'];
}

4. Write a function ReadXml($xmlstr) which accepts an XML string as its only argument. Your function should write the names of the nodes and their values.

ANS:

function ReadXml($xmlstr)
{
    static $res = '';
    $xml = new SimpleXMLElement($xmlstr);

    if(count($xml->children()))
    {
        $res .= $xml->getName().PHP_EOL;
        foreach($xml->children() as $child)
        {
            ReadXml($child->asXML());
        }
    }
    else
    {
        $res .= $xml->getName().': '.(string)$xml.PHP_EOL;
    }

    return $res;
}

5. Write a function GeneratePassword which accepts two arguments, an integer and a character string consisting of letters (a-z) and digits (0-9).

ANS:

function GeneratePassword ($length,$chars)
{
    $res = '';
    $char_length = strlen($chars);
    for($i = 0; $i < $length; $i++)
    {
        $res .= $chars[rand(0,$char_length)];
    }

    return $res;
}

6. Write a function GetLongestString, whose arguments are character strings. It should return an integer representing the length of the longest string passed as its argument;
for example:

GetLongestString(“a”, “aaa”, “aa”) should return 3,GetLongestString(“a”, “bcd”, “efgh”, “ij”, “”) should return 4.

ANS:

function GetLongestString()
{
    $length = 0;
    foreach(func_get_args() as $arg)
    {

        $var = strlen($arg);
        if($var > $length)
        {
            $length = $var;
        }
    }
    return $length;
}

7. You are given an HTML form, which contains the following entries:

<input type=”checkbox” class=”form” name=”checkbox_2″/>
<input type=”checkbox” class=”form” name=”checkbox_1″/>
<input type=”checkbox” class=”form” name=”checkbox_3″/>

<input type=”checkbox” class=”form” name=”checkbox_10″/>

The form has been submitted using the “POST” method. Identify, which of the check-boxes and write (output) their numbers in increasing order. Separate all numbers by spaces (not new lines) and do not use any HTML formatting.
For example, if check-boxes 3, 5, and 10 are checked, the only valid output would be:3 5 10

ANS:

$res = array();
while(list($checkbox,) = each($_POST))
{
$res[] = intval(substr($checkbox,strpos($checkbox,'_') + 1));
}
sort($res);
echo implode(' ',$res);

8. Write a function GetUniqueOnes, which accepts a single argument. The argument is an array of integers, and the function should return the unique integers separated by commas.

ANS:

function GetUniqueOnes($arr)
{
    $res = implode(',',array_unique($arr));

    return $res;
}

9. Write a function declared as function SplitEmailAddress($address), whose argument will contain string data consisting of a valid e-mail address. This function will take the email address as the argument and return an array with two keys: user for the username part and domain for the domain part of the address. For example, after calling:$arr = SplitEmailAddress(‘myuser_1@mailserver.example.com’) $arr[‘user’] should contain the string myuser_1, and $arr[‘domain’] should contain the string mailserver.example.com.

ANS:

function SplitEmailAddress($address)
{
    list($user, $domain) = explode('@',$address);
    return array('user' => $user, 'domain' => $domain);
}

10. Consider a database with one table called “user” having two fields: “id” (type: INTEGER, PRIMARY KEY) “name” (type: VARCHAR(32)) Write a standard SQL query which retrieves the second highest value of “id” from the “user” table. The value returned should be represented using the column name “id”.

ANS:

 SELECT id FROM user ORDER BY id DESC LIMIT 1,1
Share this:
Article Categories:
Interview Question · Odesk
banner

Hi, to my all friends , this blog for you to just share my knowledge on different sections .

Comments to Odesk PHP Programming Skills Test Answer 2013

  • It is helpful to bidder and developer also.

    Bivash September 30, 2013 9:50 pm Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

Result of *

Social Widgets powered by AB-WebLog.com.