Zend认证试题第四章(Working with Arrays)
ZendChina官方:数组可能是PHP中最强大的部分。这门语言允许开发者在任何时候创建和操作数组,这个自由度很强大。你不仅可以混合和匹配关键字和值的不同类型,而且可以在它们上执行各种操作,从分类到分离再到聚合。但是,强大的能力带来了很大的责任。一个可能存在的巨大数组的另一面(不是有意的双关语)是,了解操作这个数组的最好方式不是一项简单的任务。这部分考试不仅仅从理论的角度,也同样从实践的角度集中考察你对数组如何工作的理解能力。因此,预计你面对一个主要脚本时将很多问题,让自己理解它哪里错了,或者它的最终结果将会是什么。
1. Array values are keyed by ______ values (called indexed arrays) or using ______ values (called associative arrays). Of course, these key methods can be combined as well.
A. Float, string
B. Positive number, negative number
C. Even number, string
D. String, Boolean
E. Integer, string
2. Consider the following array, called $multi_array. How would the value cat be referenced
within the $multi_array array?
[php]<?php
$multi_array = array("red",
"green",
42 => "blue",
"yellow" => array("apple",
9 => "pear",
"banana",
"orange" => array("dog",
"cat",
"iguana")
)
);
?>[/php]
A. $multi_array['yellow']['apple'][0]
B. $multi_array['blue'][0]['orange'][1]
C. $multi_array[3][3][2]
D. $multi_array['yellow']['orange']['cat']
E. $multi_array['yellow']['orange'][1]
3. What will the $array array contain at the end of the execution of the following script?
[php]<?php
$array = array ('1', '1');
foreach ($array as $k => $v) {
$v = 2;
}
?>[php]
A. array ('2', '2')
B. array ('1', '1')
C. array (2, 2)
D. array (Null, Null)
E. array (1, 1)
4. Assume you would like to sort an array in ascending order by value while preserving key associations. Which of the following PHP sorting functions would you use?
A. ksort()
B. asort()
C. krsort()
D. sort()
E. usort()
5. What is the name of a function used to convert an array into a string?
Your Answer: ____________________________
6. In what order will the following script output the contents of the $array array?
[php]<?php
$array = array ('a1', 'a3', 'a5', 'a10', 'a20');
natsort ($array);
var_dump ($array);
?>[/php]
A. a1, a3, a5, a10, a20
B. a1, a20, a3, a5, a10
C. a10, a1, a20, a3, a5
D. a1, a10, a5, a20, a3
E. a1, a10, a20, a3, a5
7. Which function would you use to rearrange the contents of the following array so that they
are reversed (i.e.: array ('d', 'c', 'b', 'a') as the final result)? (Choose 2)
[php]<?php
$array = array ('a', 'b', 'c', 'd');
?>[/php]
A. array_flip()
B. array_reverse()
C. sort()
D. rsort()
E. None of the above
8. What will the following script output?
[php]<?php
$array = array ('3' => 'a', '1b' => 'b', 'c', 'd');
echo ($array[1]);
?>[/php]
A. 1
B. b
C. c
D. A warning.
E. a
9. What is the simplest method of computing the sum of all the elements of an array?
A. By traversing the array with a for loop
B. By traversing the array with a foreach loop
C. By using the array_intersect function
D. By using the array_sum function
E. By using array_count_values()
10. What will the following script output?
[php]<?php
$array = array (0.1 => 'a', 0.2 => 'b');
echo count ($array);
?>[/php]
A. 1
B. 2
C. 0
D. Nothing
E. 0.3
11. What elements will the following script output?
[php]<?php
$array = array (true => 'a', 1 => 'b');
var_dump ($aray);
?>[/php]
A. 1 => 'b'
B. True => 'a', 1 => 'b'
C. 0 => 'a', 1 => 'b'
D. None
E. It will output NULL
12. Absent any actual need for choosing one method over the other, does passing arrays by value to a read-only function reduce performance compared to passing them by reference?
A. Yes, because the interpreter must always create a copy of the array before passing it
to the function.
B. Yes, but only if the function modifies the contents of the array.
C. Yes, but only if the array is large.
D. Yes, because PHP must monitor the execution of the function to determine if
changes are made to the array.
E. No.
13. What will the following script output?
[php]<?php
function sort_my_array ($array)
{
return sort ($array);
}
$a1 = array (3, 2, 1);
var_dump (sort_my_array (&$a1));
?>[/php]
A. NULL
B. 0 => 1, 1 => 2, 2 => 3
C. An invalid reference error
D. 2 => 1, 1 => 2, 0 => 3
E. bool(true)
14. What will be the output of the following script?
[php]<?php
$result = '';
function glue ($val)
{
global $result;
$result .= $val;
}
$array = array ('a', 'b', 'c', 'd');
array_walk ($array, 'glue');
echo $result;
?>[/php]
Your Answer: ____________________________
15. What will the following script output?
[php]<?php
$array = array (1, 2, 3, 5, 8, 13, 21, 34, 55);
$sum = 0;
for ($i = 0; $i < 5; $i++) {
$sum += $array[$array[$i]];
}
echo $sum;
?>[/php]
A. 78
B. 19
C. NULL
D. 5
E. 0
答案附在下面,方便大家参考。
[attach]149[/attach]
[color=#ff0000]ZendChina官方资讯,转载请以链接形式注名来源:[/color][url=http://www.zendchina.net/][color=#ff0000]ZendChina[/color][/url][color=#ff0000] - [/color][url=http://bbs.zendchina.net/thread-137-1-1.html][color=#ff0000]Zend认证试题第四章[/color][/url] [b]答案详解:[/b]
1.键名是整型数字(integer)的数组叫索引数组,键名是字符串的数组叫关联数组。正确答案是E。
2.cat被包含在另外两个数组中。顺藤摸瓜,首先,键yellow必须要用到,它跟在orange后面。最内部的数组是个索引数组,字符串cat是第二个值,它的索引是1。正确答案是E。
3.答案是B。foreach操作的是$array的副本,因此对原来的值没有影响。
4.只有asort函数能在保留原有索引关系的前提下进行排序。答案是B。
5.serialize函数能把复杂的数据结构转换成字符串,并可以用unserialize函数再转换回原先的结构。还有implode函数,它可以把数组中的所有元素组成一个字符串。
6.函数natsort()使用“自然排序”法对数组进行排序。在本题中,数组元素已经“自然”排列了,因此函数根本没有改变数组。答案是A。
7.array_flip()只能把数组中每个元素的键和值进行交换。rsort()和array_reverse()则能把题目中的数组逆向排序为需要的形式(’d’,’c’,’b’,’a’)。答案是B和D。
8.给数组中的元素设置数字键时,PHP从可用的最小的数字开始,递增设置。如果没有指定从哪个数字开始,PHP将从0开始。本题中,3是第一个元素的键,因此,第三个元素的键将被设置为4,最后一个元素是5。注意,1b不是数字。因此,键是1的值不存在,答案是D。
9.array_sum函数计算数组中所有元素的总和。答案是D。
10.脚本输出1(答案是A)。因为只有整型数字和字符串可以被用来做数组的键——浮点数字会被转换成整型数字。所以0.1和0.2会被转换成0,$array中只有0=>’b’这个元素。
11.这题试图把你的注意力从真正的问题上转移开。true等同于数字1,因此$array数组将只包含一个元素。然而在var_dump()函数里出现了一个错误——$array被错拼成了$aray,少了一个“r”。因此var_dump将输出NULL(也可能是一个提示 ,这取决于你的设置)。答案是E。
12.这题有些绕人。首先,注意两点:第一,你并非一定要使用这两种方式来传递数组。如果需要用一个函数来修改数组的内容,通过引用传递将是唯一的方法——但题中不是这种情况;第二,题目强调把数组传递个一个只读函数。如果不是这样,由于对数组进行改变将产生一个该数组的副本,答案会是B。然而常规情况下,PHP需要创建一套结构来维持一个引用,另一方面,由于PHP采用懒拷贝(lazy-copy)——又叫写时拷贝(copy-on-write)——机制,变量在被改变之前不会产生副本,所以通过引用将数组传递给一个不修改数组内容的函数比通过值传递要慢,而通过值传递是一种快速、安全的在函数间共用数组的方式。答案是E。
13.答案是E。sort函数不产生或返回数组副本,而是直接对传递给它的数组本体进行操作。该函数只返回布尔值true,代表排序成功(或者false,代表出错)。注意,这里将数组$a1引用传递给了sort_my_array(),我们不赞成这样做,应该在函数中重新声明引用。
14.array_walk函数将一个指定函数应用在数组中的每个元素上。因此脚本glue函数将把数组中的所有元素连在一起,输出abcd。
15.本题主要考验你分析脚本的能力。你也许觉得这题很费解——但我们在调试别人写的代码时却不得不经常面对此类令人不悦的问题。相对于我们所见过的一些代码,这已经算相当简单了。脚本中的for循环了5次,每次都把键是数组$array中键为$i的值的值加进$sum。这听起来有点像绕口令,但如果你一步一步想,你将发现, 当$i等于零时,$array[$array[$i]]等同于$array[$array[0]],也就是$array[1],也就是2。顺着这个思路,最终的答案是78。
页:
[1]
