Explore topic-wise MCQs in Php.

This section includes 194 Mcqs, each offering curated multiple-choice questions to sharpen your Php knowledge and support exam preparation. Choose a topic below to get started.

1.

The practice of creating objects based on predefined classes is often referred to as.

A. lass creation
B. bject creation
C. bject instantiation
D. lass instantiation
Answer» E.
2.

What will be the output of the following PHP code?class Checkout {final function totalize(){// calculate bill} }class IllegalCheckout extends Checkout{final function totalize(){// change bill calculation}}

A. HP Fatal error: Class IllegalCheckout may not inherit from final class
B. alue of the bill calculated
C. HP Fatal error: Cannot find object
D. HP Fatal error: Cannot override final method
Answer» E.
3.

Which of the following is/are the right way to declare a method?1. function functionName() { function body }2. scope function functionName() { function body }3. method methodName() { method body }4. scope method methodName() { method body }

A. nly2
B. nly 4
C. and 2
D. and 4
Answer» D. and 4
4.

Which of the following statements is/are true about Constructors in PHP?1. PHP 4 introduced class constructors.2. Constructors can accept parameters.3. Constructors can call class methods or other functions.4. Class constructors can call on other constructors.

A. and 4
B. ll of the mentioned
C. one of the mentioned
D. , 3 and 4
Answer» C. one of the mentioned
5.

In the PHP code given below, what is/are the properties?

A. cho “This is an example”;
B. ublic $name;
C. lass Example
D. unction sample()
Answer» C. lass Example
6.

The class from which the child class inherits is called.1. Child class2. Parent class3. Super class4. Base class

A. nly 1
B. , 3 and 4
C. nly 3
D. and 4
Answer» E.
7.

Which of the following advanced OOP features is/are not supported by PHP?1. Method overloading2. Multiple Inheritance3. Namespaces4. Object Cloning

A. ll of the mentioned
B. one of the mentioned
C. and 2
D. and 4
Answer» D. and 4
8.

What will be the output of the following PHP code?title}: " .$shopProduct->getProducer() ." ({$shopProduct->price})n";print $str;}}$product1 = new ShopProduct( "My Antonia", "Willa", "Cather", 5.99 );$writer = new ShopProductWriter();$writer->write( $product1 );?>

A. rror
B. ather: Willa My Antonia (5.99)
C. illa: Cather My Antonia (5.99)
D. y Antonia: Willa Cather (5.99)
Answer» E.
9.

If you omit the visibility keyword in your method declaration, by default the method will be declared as.

A. ublic
B. rivate
C. rotected
D. riendly
Answer» B. rivate
10.

What will be the output of the following PHP code?

A. ool(true)
B. ool(false)
C. rror
D. one of the above
Answer» B. ool(false)
11.

A mutator method is also called as.

A. etter
B. ccessor
C. etter
D. estructor
Answer» B. ccessor
12.

What does PDO stand for?

A. HP Data Orientation
B. HP Database Object
C. HP Database Orientation
D. HP Data Object
Answer» E.
13.

What will be the output of the following PHP codeclass Person{function getName() { return "Bob"; }function getAge() { return 44; }function __toString() {$desc = $this->getName();$desc .= " (age ".$this->getAge().")";return $desc;}}$person = new Person();print $person;

A. bject Not Found
B. HP Catchable fatal error
C. OB (age 44)
D. OB
Answer» D. OB
14.

Which method is used to tweak an object’s cloning behavior?

A. lone()
B. _clone()
C. clone
D. bject_clone()
Answer» C. clone
15.

Which of the following is/are true for an abstract class?1. A class is declared abstract by prefacing the definition with the word abstract.2. A class is declare abstract by using the keyword implements.3. It is a class that really isn’t supposed to ever be instantiated but instead serves as a base class.4. Attempting to instantiate an abstract class results in an error.

A. nly 2
B. ll of the mentioned
C. and 4
D. , 3 and 4
Answer» B. ll of the mentioned
16.

You access an object’s properties and methods using the ....... operator ( a hyphen followed by a greater than symbol).

A. >
B. >
C. >
D. one of above
Answer» D. one of above
17.

All PHP classes come with a default constructor that takes .... arguments.

A. ne
B. wo
C. hree
D. o
Answer» E.
18.

The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited classes. Members declared as private are not accessible.

A. rue
B. alse
Answer» C.
19.

Once a class has been defined, objects can be created from the class with the ....... keyword.

A. ew object
B. onstruct
C. ew
D. oth A and C
Answer» D. oth A and C
20.

PHP makes it possible to automatically execute code when a new instance of a class is created, using a special class method called a ........

A. estructor
B. onstructor
C. riend
D. nitial
Answer» C. riend
21.

Heredoc's are a great alternative to quoted strings because of increased readability and maintainability.

A. rue
B. alse
Answer» B. alse
22.

The functions strtolower() and strtoupper() work on individual characters and not on entire strings.

A. rue
B. alse
Answer» C.
23.

What is the value of var?$var = 'HELLO WORLD!';$var= ucfirst($var);

A. ello World!
B. ello world!
C. ello world!
D. ELLO WORLD!
Answer» E.
24.

What is the output of below statement?Print substr ();

A. ree
B. ree’
C. ee
D. one of above
Answer» D. one of above
25.

PHP strings are binary-safe (i.e. they can contain null bytes) . Their size is limited only by the amount of memory that is available to PHP.

A. rue
B. alse
Answer» B. alse
26.

In . . . . . constructors, it is important to remember that you have to call the parent constructor explicitly.

A. ingleton
B. ecure
C. ublic
D. ubclass
Answer» E.
27.

PHP recognises constructors by the name . . . . .

A. construct
B. _construct
C. _constructor
D. constructor
Answer» C. _constructor
28.

What is the value of message?$name = “John”;$message = ‘Hello, $name’;

A. ello, John
B. t will print an error message
C. Hello, $name’
D. one of above
Answer» D. one of above
29.

Even if you don’t delete the object yourself using . . . . . , PHP still calls the destructor when it determines that the object is no longer used.

A. estructor
B. el()
C. em()
D. nset()
Answer» E.
30.

. . . . . . is a class which can only be instantiated once. You can effectively only have one object per . . . . . class in an application.

A. onstructor, Singleton
B. ingleton, Constructor
C. ingleton, Singleton
D. oader, Loader
Answer» D. oader, Loader
31.

You can invoke class constructors that don’t have any relation to the instantiated object by simply prefacing _constructor with the class name like.

A. lassname::__construct()
B. lassname:__construct()
C. lassname=>__construct()
D. lassname->__construct()
Answer» B. lassname:__construct()
32.

Unlike constructors, you cannot pass information to a destructor, because you are never sure when its going to be run.

A. RUE
B. ALSE
Answer» B. ALSE
33.

Constructor and destructor methods have no . . . . . . and are called automatically - they cannot be called explicitly and consequently their declarations need no access specifier.

A. arameters
B. estructor
C. eturn value
D. proval
Answer» D. proval
34.

Object-oriented code tries to minimize dependencies by moving responsibility for handling tasks away from _______ and toward the objects in the system.

A. erver code
B. lient code
C. achine code
D. rocedural code
Answer» C. achine code
35.

_________ are used in class diagrams to describe the way in which specific elements should be used.

A. ttributes
B. onstraints
C. onstants
D. lass Names
Answer» C. onstants
36.

A valid class name starts with a ......., followed by any number of letters, numbers, or underscores.1. Number2. Letter3. Period4. Underscore

A. ption 1 and 4
B. ption 2 and 3
C. ption 3 and 4
D. ption 1 and 3
Answer» C. ption 3 and 4
37.

PHP have not yet supported constructor overloading.

A. RUE
B. ALSE
Answer» B. ALSE
38.

You can overwrite a Singleton with the wrong kind of data.

A. RUE
B. ALSE
Answer» C.
39.

PHP have not yet supported constructor overloading.¬¨‚Ä~!

A. TRUE
B. FALSE
Answer» B. FALSE
40.

You can overwrite a Singleton with the wrong kind of data.¬¨‚Ä~!

A. TRUE
B. FALSE
Answer» C.
41.

Heredoc's are a great alternative to quoted strings because of increased readability and maintainability.%!

A. True
B. False
Answer» B. False
42.

The functions strtolower() and strtoupper() work on individual characters and not on entire strings.%!

A. True
B. False
Answer» C.
43.

What is the value of var?$var = 'HELLO WORLD!';$var = ucfirst($var);%!

A. Hello World!
B. hello world!
C. Hello world!
D. HELLO WORLD!
Answer» E.
44.

What is the output of below statement?Print substr (‘watch out for that tree’,20,5);%!

A. tree
B. tree’
C. ree
D. none of above
Answer» D. none of above
45.

PHP strings are binary-safe (i.e. they can contain null bytes) . Their size is limited only by the amount of memory that is available to PHP.%!

A. True
B. False
Answer» B. False
46.

Which characters is used to access property variables on an object-by-object basis?%!

A. ::
B. =
C. ->
D. .
Answer» D. .
47.

Which version of PHP introduced the visibility keywords i.e public, private, and protected?%!

A. PHP 4
B. PHP 5
C. PHP 5.1
D. PHP 5.3
Answer» C. PHP 5.1
48.

Fill in the blank with the best option. An Object is a/an ________ of a class.%!

A. type
B. prototype
C. instance
D. object
Answer» D. object
49.

Which one of the following is not a valid class name?%!

A. ShopProduct
B. Shopproduct
C. Shopproduct1
D. 1shopproduct
Answer» E.
50.

Even if you don’t delete the object yourself using . . . . . , PHP still calls the destructor when it determines that the object is no longer used.%!

A. destructor
B. del()
C. rem()
D. unset()
Answer» E.