Sunday, September 30, 2007

Interview questions for PHP and MySQL - Part 1


PHP

Q1.Can we write windows like applications in PHP.
Ans : Yes using PHP-GTK on linux and WinBinder on windows.


Q2.What difference does it make when I declare variables with $ and $ in prefix.
Ans: $x = "Lion";
$$x = "Zebra";
echo $Lion;
would display "Zebra"
Use : creating runtime variables


Q3.What is the difference between strpos and stripos function
Ans: strpos is case sensitive search, and stripos is case insensitive search


Q4.What are the ways by which we can find out if a variable has been declared?
Ans: isset or empty language constructs


Q5.What is "global" and how to use it?
Ans: variables declared outside the functions can be used inside the function using global keyword


Q6.What is the difference between echo and print
Ans: echo can take more than one parameter for displaying.
print cannot take more than one
e.g
echo 'This', 'That' //is valid
print 'This', 'That' //is invalid
print returns 1 always.
echo cannot be used to return anything
$ret = print "Abcd" //valid
$ret = echo "Abcd" //invalid


Q7.What are predefined variables in php, give some examples.
Ans: PHP provides an additional set of predefined arrays containing variables from the web server (if applicable), the environment, and user input. These new arrays are rather special in that they are automatically global

[ Resource Link : http://in.php.net/manual/en/language.variables.predefined.php ]

e.g., $_SERVER, $_REQUEST, $_POST, $_GET, $_ENV, $_COOKIE, $_FILES, $_SESSION, $GLOBALS, $php_errormsg, $http_response_header


Q8.Give examples of predefined classes in PHP, and specify the use of anyone of them.
Ans: stdClass, Exception,_PHP_Incomplete_Class, php_user_filter, Directory

Exception : for exception handling
Directory: dir class


Q9.Declaring a class.
Ans: public/private/protected [static] class <classname> extends <baseclassname>{
    public $x;
  …
    public function method_a() {
  …
  }
}


Q10.How can we instantiate a class with default values.
Ans: Write a function with the same name as class and assign values to the vars.
E.g.:
class myclass {
  private $var1, $var2;
  function myclass() {
    $this->var1 = "Demo";
    $this->var2 = "Disabled";
  }
  function __get($x) {
    return $this->$x;
  }
}

$m = new myclass();
echo $m->var1 . " Vars " . $m->var2;


Q11.Abstraction, interfaces explain the main difference.
Ans: a) Abstract classes cannot be instantiated,
b) They start with keyword abstract before the class name,
c) One can force the methods to be declared in the inheriting class by creating
abstract functions
d) only abstract class can have abstract methods
eg.
  abstract class a {
    abstract function b();
    public function c() {
      echo "Can be used as it is";
    }
  }


  class m extends a {
    public function b() {
      echo "Defined function b";
    }
  }

$tClass = new m();
$tClass->b();
$tClass->c();


Q12.Interface classes only provide the skeleton of the class that the inheriting class must implement.

Eg.
  interface Person{
    public function myFunc();
  }

  class Employee implements Person {
    public function myFunc() {
      echo "Implemented";
    }
  }

$tClass = new Employee();
$tClass->myFunc();


Q13.What are magic methods?
Ans: Functionalities like serializing, converting objects to string, getting setting values without explicit get and set methods for the variables etc, are magic functionality. PHP defines methods with names starting with __(double underscore).
Eg. __get, __set, __construct, __destruct, __set, __isset, __unset, __sleep, __wakeup, __toString, __clone, __autoload, __set_state

[ Resource Link : http://in.php.net/manual/en/language.oop5.magic.php ]

Q14.What does function `eval` do?
Ans: Evaluate a string as PHP code;
Eg. eval('echo "This would be printed"');


Q15.What is the method by which PHP converts datatype of a given variable.
Ans: settype()
$a = "10"; // $a is string
settype($a,"integer"); // $a is integer


Q.Can we change php.ini settings at the runtime, and how?
Ans : Yes, using ini_set();


Q16.What is the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?
Ans: Sorts an array, sorts an array and maintains index association, Sorts an array by key
Simple sort (sorts on values)
Simple sort after sorting the array (lets assume size of array is 10) rearranges the index, if we want to access element at index[5], using a simple sort an element value would have changed, but in assort the value is still held by index 5 though its position in the array may be 10th.
Ksort – sorts the array by key and maintains the key to data correlations


Q17.What is the difference between foo() & @foo()?
Ans: if an error occurs calling foo() would show up the error on the screen, whereas, @foo() would suppress the error because ‘@’ is a error control operator.


Q18.What is the difference between mysql_fetch_row() and mysql_fetch_array() and mysql_fetch_object?
Ans: mysql_fetch_row() – fetches a row as an enumerated array
mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_object - Fetch a result row as an object


Q19.What is the difference between include & include_once? include & require?
Ans: include_once includes the script only once if it is already included in the execution of the script
Include includes the script everytime the include is encountered in the code
Require is identical to include except that it results in fatal error when file is not present in the include_path.


Q20.What type of inheritance PHP supports?
Ans: An object can inherit only from one base class. Multiple inheritance is not supported in PHP.


Q21.How can we call an object's method by using the variable functions?
Ans: If MyClass contains function myFunction()


$foo = new MyClass();
$var = "myFunction";
$c->$var();

[ Resource Link : http://in.php.net/manual/en/functions.variable-functions.php ]

Ajax, Cross site scripting (JavaScript).

Q22.What is the use of AJAX?
Ans: If a page contains form that needs to be updated constantly based on runtime values, a javascript code constantly sends user input to the system and displays the results from the server, without refreshing the whole page.

Q23.What is cross site scripting, and how to avoid it?
Ans: Injecting a javascript code in the response of a form that is capable of calling and executing scripts from other site.

Occurs when variables holding form values are not cleaned with html code escaping functions.

Clean the variables before storing or before display. Recommendation is to display the code clean and store the value as it is, unless its specified otherwise.


MYSQL.

Q1.What is a join, what types of join are supported in MySQL.
Ans: A ‘join’ joins two table in such a way that all or partial records are selected from both the table based on join criteria.

Joins supported in mysql are :

[INNER | CROSS] JOIN

STRAIGHT_JOIN

LEFT [OUTER] JOIN

NATURAL [LEFT [OUTER]] JOIN

RIGHT [OUTER] JOIN

NATURAL [RIGHT [OUTER]] JOIN



Q2.In MySQL, what table type is required for foreign keys to work?
Ans: innoDB


Q3.How does full text search work.
Ans: A table must be a myISAM table
Table must have char varchar and text columns
FULLTEXT index must be created at the time of creation of table


Q4.What is the use of files .frm, .MYD, .MYI
Ans: frm files store the table definition
MYD files store the data
MYI files store the index


Q5.What is maximum size of a database in MySQL?
Ans: 65+GB / table / [ limited by the OS]


Q6.How many columns can exist in a mySql table?
Ans: 4096 colums


Q7.What is the maximum size of a row in a mysql table?
Ans: 65,535 not including blobs (as these are stored separately)


Q8. What would you use if you have a choice Natural [left] join or inner join or left join with using clause?
Ans: The NATURAL [LEFT] JOIN of two tables is defined to be semantically
equivalent to an INNER JOIN or a LEFT JOIN with a USING clause that names all columns that exist in both tables

[ Resource Link : http://dev.mysql.com/doc/refman/5.1/en/join.html ]