Zend认证试题第二章(Object-oriented Programming with PHP 4)
ZendChina官方:尽管PHP 4不是一个成功的面向对象技术的典范,但是它适用于建立一个可行的面向对象的架构——你只需要了解一个有缺陷的对象模型的缺陷在哪,并围绕着它小心处理。即使PHP 5在PHP处理对象的方式上已经带来了许多的变化,从而你可能受到吸引而忽视了PHP 4的能力。事实是,这个面向对象的程序设计是非常受大多数程序员欢迎的,这些程序员在开始开发自己的应用软件时都是使用这个"老"版本的PHP。这导致了有大量的OOP代码,甚至在你转向PHP 5之前,你也可能会经常使用到这些代码。
考试的 OOP 部分不仅仅测试常用的面向对象开发,同时也测试PHP 4实现OOP的独特方式。
1. What is the construct used to define the blueprint of an object called?
Your Answer: ____________________________
2. At the end of the execution of the following script, which values will be stored in the $a->my_value array? (Choose 3)
[php]
<?php
class my_class
{
var $my_value = array();
function my_class ($value)
{
$this->my_value[] = $value;
}
function set_value ($value)
{
$this->$my_value = $value;
}
}
$a = new my_class ('a');
$a->my_value[] = 'b';
$a->set_value ('c');
$a->my_class('d');
?>
[/php]
A. c
B. b
C. a
D. d
E. e
3. How can you write a class so that some of its properties cannot be accessed from outside its methods?
A. By declaring the class as private
B. By declaring the methods as private
C. It cannot be done
D. By writing a property overloading method
4. Which object-oriented pattern would you use to implement a class that must be instantiated only once for the entire lifespan of a script?
A. Model-view-controller
B. Abstract factory
C. Singleton
D. Proxy
E. State
5. A class can be built as an extension of other classes using a process known as inheritance. In PHP, how many parents can a child class inherit from?
A. One
B. Two
C. Depends on system resources
D. Three
E. As many as needed
6. What OOP construct unavailable in PHP 4 does the following script approximate?
[PHP]
<?php
class my_class
{
function my_funct ($my_param)
{
user_error ("Please define me", E_ERROR);
}
function b()
{
return 10;
}
}
?>
[/php]
A. Multiple inheritance
B. Interfaces
C. Abstract methods
D. Private methods
E. Function overloading
7. Assume that a class called testclass is defined. What must the name of its constructor method be?
A. __construct
B. initialize
C. testclass
D. __testclass
E. Only PHP 5 supports constructors
8. How can a class override the default serialization mechanism for its objects?
A. By implementing the __shutdown and __startup methods
B. By calling register_shutdown_function()
C. By implementing __sleep() and __wakeup()
D. The default serialization mechanism cannot be overridden
E. By adding the class to the output buffering mechanism using ob_start()
9. In PHP 4, which object-oriented constructs from the following list are not available?
• Abstract classes
• Final classes
• Public, private, protected (PPP) methods
• Interfaces
A. Abstract classes
B. PPP methods
C. Neither PPP methods nor interfaces
D. None of the above are available
E. All of the above are available
10. How would you call the mymethod method of a class within the class itself?
A. $self=>mymethod();
B. $this->mymethod();
C. $current->mymethod();
D. $this::mymethod();
E. None of the above are correct
11. What will the following script output?
[php]
<?php
class my_class
{
var $my_var;
function _my_class ($value)
{
$this->my_var = $value;
}
}
$a = new my_class (10);
echo $a->my_var;
?>
[/php]
A. 10
B. Null
C. Empty
D. Nothing
E. An error
12. What will the following script output?
[php]
<?php
class my_class
{
var $value;
}
$a = new my_class;
$a->my_value = 5;
$b = $a;
$b->my_value = 10;
echo $a->my_value;
?>
[/php]
A. 10
B. 5
C. 2
D. Null
E. Nothing
13. Consider the following script. What will it output?
[php]
<?php
$global_obj = null;
class my_class
{
var $value;
function my_class()
{
global $global_obj;
$global_obj = &$this;
}
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
?>
[/php]
A. 5
B. 10
C. Nothing
D. The constructor will throw an error
E. 510
14. Consider the following segment of PHP code. When it is executed, the string returned by the $eight_tenths->to_string method is 8 / 10 instead of the expected 4 / 5. Why?
[php]
<?php
class fraction {
var $numerator;
var $denominator;
function fraction($n, $d) {
$this->set_numerator($n);
$this->set_denominator($d);
}
function set_numerator($num) {
$this->numerator = (int)$num;
}
function set_denominator($num) {
$this->denominator = (int)$num;
}
function to_string() {
return "{$this->numerator}
/ {$this->denominator}";
}
}
function gcd($a, $b) {
return ($b > 0) ? gcd($b, $a % $b) : $a;
}
function reduce_fraction($fraction) {
$gcd = gcd($fraction->numerator,
$fraction->denominator);
$fraction->numerator /= $gcd;
$fraction->denominator /= $gcd;
}
$eight_tenths = new fraction(8,10);
/* Reduce the fraction */
reduce_fraction($eight_tenths);
var_dump($eight_tenths->to_string());
?>
[/php]
A. The reduce_fraction function must return a value
B. The reduce_fraction function should accept integer values
C. The gcd function is flawed
D. You must pass the $eight_tenths object by-reference
E. You cannot pass instances of objects to anything but methods
15. What does the following PHP code segment do?
<?php
require_once("myclass.php");
myclass::mymethod();
?>
A. Calls the mymethod method in the class statically.
B. Creates and instance of myclass and calls the mymethod method.
C. Generates a syntax error
D. Defaults to the last-created instance of myclass and calls mymethod()
E. Calls the function named myclass::mymethod()
16. Do static class variables exist in PHP?
A. Yes
B. No
17. What will the following script output?
[php]
<?php
class a
{
function a ($x = 1)
{
$this->myvar = $x;
}
}
class b extends a
{
var $myvar;
function b ($x = 2)
{
$this->myvar = $x;
parent::a();
}
}
$obj = new b;
echo $obj->myvar;
?>
[/php]
A. 1
B. 2
C. An error, because a::$myvar is not defined
D. A warning, because a::$myvar is not defined
E. Nothing
18. How can you load classes on demand as they are required by the interpreter?
A. By using the __autoload magic function
B. By defining them as forward classes
C. By implementing a special error handler
D. It is not possible to load classes on demand
E. By including them in conditional include statements
19. _____________________ are used to provide high-quality solutions to a recurrent design
problem using object-oriented programming.Your Answer: ____________________________
20. What will the following script output?
[php]
<?php
class a
{
function a()
{
echo 'Parent called';
}
}
class b
{
function b()
{
}
}
$c = new b();
?>
[/php]
A. Parent called
B. An error
C. A warning
D. Nothing
答案附在下面,方便大家参考。
[attach]172[/attach]
[color=#ff0000]ZendChina官方资讯,转载请以链接形式注名来源:[/color][url=http://www.zendchina.net/][color=#ff0000]ZendChina[/color][/url][color=#ff0000] - [/color][url=http://bbs.zendchina.net/thread-89-1-1.html][color=#ff0000]Zend认证试题第二章[/color][/url] 这本书做了一大半了,不过正确率比较低,都不好意思哈。
本来还打算翻译来着,但是工作紧的要死,没时间搞了。
应该还没有中文的吧,不管是这个test还是studyguide。 因为考试都是英文试题,估计永远也不会有中文的试题和学习手册。
ZEND认证毕竟是全球的PHP工程师认证,所以要求统一使用英语进行答题,也是可以理解的。 国内现在过ZCE认证的有多少?看国内的考点蛮多的,不过一次125美金就不是吃个冰棍那么简单了,慎重哦,跟以前考雅思差不多。 目前国内有代理在做考试这块,但是我们没有认真调查过。不知道是真是假。而且ZEND国际认证在国内反映不是很强,也和他们宣传力度不够有关系。 [b]答案详解:[/b]
1.类是对象的蓝图(对象是类的实例)。
2.正确答案是B、C和D。set_value方法使用了错误的表达式$this->$my_value,因此该方法实际上是空的(这在PHP5里会导致一个错误,但在PHP4中不会。——译者注)。
3.答案是C。PHP4中无法限制对类成员的访问,而在PHP5中则可以通过private关键字实现。
4.单件模式可以限制一个类被实例化的次数。
5.尽管其他编程语言允许多重继承,但在PHP的对象模型中却不可以。因此答案是A。
6.方框中的代码表现的是抽象方法(abstrace method)的实现。如果这个类继承自其他类,而my_funct方法在子类中被调用时没有覆盖,代码将抛出一个错误。虽然只是近似的实现了抽象方法,但在PHP4有限的对象模型中,这已经做得很好了。
7.PHP5有统一的构造函数(__construct()),但在PHP4中,构造函数就是和类有相同名称的方法。对于名为testclass的类,它的构造函数就是testclass()。答案是C。
8.__sleep()和__wakeup()能被用来自定义对象的序列化过程。正确答案是C。
9.PHP4中没有题目选项里所列的任何一个概念。答案是D。
10.PHP中,在类的内部访问其成员和方法,要用$this这个特殊变量。因此答案是B。
11.正确答案是D。my_class::_my_class()不是合法的构造函数(方法名的开头多了个_),因此脚本不会输出任何东西。你可能觉得这题是在考眼力而不是知识,是的,我们就是这么打算。仔细想想你就会同意——绝大多数的bug都是由错误的拼写造成的。这题并不是在戏弄你,而是考验你的排错能力。
12.PHP4把对象视作标量进行处理,当$a赋给$b时,解释器创建对象的副本,因此对后一个对象的赋值不会影响到原先的对象。答案是B。但是要注意,PHP5里就不是这样处理的了(将会输出10)。
13.一上来,构造函数my_class通过引用,将自身存储在了变量$global_obj中。你可能会因此觉得,当我们后来吧$global_obj->my_value的值变为10时,$a也会相应改变。不幸的是,new操作符返回的不是引用,而是对象的副本。脚本输出5,答案是A。
14.PHP中,把对象传递给函数或者方法时,默认传递的是值。这意味着通过参数传递给函数的对象,其实是对象的副本。这点导致了在函数或方法里对对象进行改动时,不会影响函数外的原先的那个对象。
回到第14题中,这就说明对象$eight_tenths从来没有被reduce_fraction函数改动过,而$fraction对象(参数)则被改动了。如果要在函数内部改动对象,就必须以引用的方式传递参数:
function reduce_fraction(&$fraction)
答案是D。
15.题中所示的语法是用来进行静态调用的。当方法被静态调用时,它们就像一个独立的函数,与任何类的实例无关。答案是A。
16.没有。PHP4只允许声明静态函数变量,没有静态类变量。
17.答案是A。类b的属性$myvar将在b的父类——类a调用构造函数时被定义,此外,像PHP4中的普通变量一样,定义类变量时也不需要给它赋值。类b在其父类调用构造函数之前就给$myvar赋了值,所以不管之后如何赋值,输出都是1。
18.PHP4中无法即时装载类——它们必须在使用前就仔细声明好。PHP5中,可以使用__autoload魔术函数提醒解释器在找不到需要的类时尝试自动调用。因此答案是D。
19.为软件设计和编程中的常规问题提供良好的解决方案,这显然是在说设计模式。
20.脚本什么都不输出(答案是D)。因为子类的构造函数不会自动调用父类的构造函数。
页:
[1]
