Priti's Technical Corner

October 3, 2009

PHP Interview Questions


Question 1: What is class?
Answer 1: Class is a collection of variables and method.

Question 2: What is data encapsulation and how to achieve it PHP?
Answer 2: Data encapsulation is hiding the member variables and method from the access of others.In PHP data encapsulation is implementated with concept called Class. Class is the collection of variable and method whoes access to other classes is defined in the class  itself.

Question 3: Can constructors can be inherited in PHP5?
Answer 3: Yes

Question 4: what is the difference between errors and exceptions?
Answer 4: An error is a fatal to the programs’s ability to do its job. Where as an exception is a stituation which is uncommon but recoverable.

Question 5:what is difference between class variables and class constants?
Answer 5: class vairables are declared with keyword static and can be changed.Class constants are declared with keyword ‘const’ and can’t be changed.

Question 6: Can we define value of constant inside methods in a class?
Answer 6: No.Values of constant can be set on;y when it’s deifned.

Question 7:what is concrete class?
Answer 7:concrete class is a term used for class which are not abstract.

Question 8:If a method is declared as abstract then, the class containing must also be declared as abstract?
Answer 8:Yes

Question 9: what is abstract methods?
Answer 9:abstract methods is just a declaration of method signature with keyword ‘abstract’.

Question 10: Can concrete class can have abstract methods?
Answer 10: No

March 25, 2009

PHP Interview Question

Filed under: PHP — Tags: , — pritisolanki @ 8:47 am

Iqbal Husain on PHP Questions Shared his valuable interview experience with us.I have simply formatted the content/experience provided by Iqbal. Enjoy !!

Question 1: Which of the following will NOT add john to the users array?
Answer 1:
$users[] = ‘john’; -> Successfully adds john to the array
array_add($users,’john’); -> Fails stating Undefined Function array_add()
array_push($users,‘john’); -> Successfully adds john to the array
$users ||= ‘john’; -> Fails stating Syntax Error

Question 2: What’s the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?
Answer 2:
sort(): Sorts an array in alphabetical order based on the value of each element. The index keys will also be renumbered 0 to length – 1.
This is used primarily on arrays where the indexes/keys do not matter.

assort(): The assort function does not exist, so I am going to assume it should have been typed asort().
ksort() :Sorts an array in alphabetical order by index/key. This is typically used for associate arrays where you want the keys/indexes to be in alphabetical order.

Question 3: What would the following code print to the browser? Why?
PHP:
$num = 10;
function multiply(){
$num = $num * 10;
}
multiply();
echo $num; // prints 10 to the screen

Answer 3: Since the function does not specify to use $num globally either by using global $num; or by $_GLOBALS['num'] instead of $num,
the value remains 10.

Question 4: What is the difference between a reference and a regular variable? How do you pass by reference and why would you want to?
Answer 4: Reference variables pass the address location of the variable instead of the value. So when the variable is changed in the function, it is also    changed in the whole application, as now the address points to the new value.Now a regular variable passes by value, so when the value is changed          in the function, it has no affect outside the function.

$myVariable = “its’ value”;
Myfunction(&$myVariable); // Pass by Reference Example

So why would you want to pass by reference? The simple reason, is you want the function to update the value of your variable so even after you are     done with the function, the value has been updated accordingly.

Question 5:What functions can you use to add library code to the currently running script?
Answer 5: This is another question where the interpretation could completely hit or miss the question. My first thought was class libraries written in PHP,       so include(), include_once(), require(), and require_once() came to mind. However, you can also include COM objects and .NET libraries. By       utilizing the com_load and the dotnet_load respectively you can incorporate COM objects and .NET libraries into your PHP code, thus anytime you see       “library code” in a question, make sure you remember these two functions.

Question 6: What is the difference between foo() & @foo()?
Answer 6: foo() executes the function and any parse/syntax/thrown errors will be displayed on the page.
@foo() will mask any parse/syntax/thrown errors as it executes.You will commonly find most applications use @mysql_connect() to hide mysql errors       or @mysql_query. However, I feel that approach is significantly flawed as you should never hide errors, rather you should manage them accordingly       and if you can, fix them.

Question 7: How do you debug a PHP application?
Answer 7: This isn’t something I do often, as it is a pain in the butt to setup in Linux and I have tried numerous debuggers. However, I will point out one      that has been getting quite a bit of attention lately.
PHP – Advanced PHP Debugger or PHP-APD. First you have to install it by running:

pear install apd
Once installed, start the trace by placing the following code at the beginning of your script:
apd_set_pprof_trace();
Then once you have executed your script, look at the log in apd.dumpdir.
You can even use the pprofp command to format the data as in:
pprofp -R /tmp/pprof.22141.0
For more information see http://us.php.net/manual/en/ref.apd.php

I welcome all readers to help each other my sharing their interview experience here.If you been ask some tuff question and searching answers then drop in here I will try to answer them.I again express my thanks to Iqbal for sharing his experience with us.

Older Posts »

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: