Monday, January 9, 2012

PHP Evidence Questions


PHP Exam 1
 
Q1. Create an associative array of 5 elements where index will be country name and value will be the corresponding capital name. Sort the array by capital name and print.

Q2. Write code to create a new text file named book.txt and write the following data into the file,
            674-5128884       A byte of Python    Swaroop C H 
      Create a class named BookList which contain isbn, bookName, authorName as properties
      and a method named display which will print the isbn no, book name and author name. Use
      constructor to initialize the properties using the data of book.txt file.
     

Q3. Calculate the area of circle using function.
Q4. Calculate the factorial of a number.
Q5.  Validate the email address using regular expression.


Saturday, January 7, 2012

PHP Descriptive Questions and Solution



1.       What are the general language features of php?
Practicality: It requires minimum of knowledge of programming.
Power: It has the ability to interface with databases, form and create pages dynamically.
Possibility: It rarely bound to any single implementation solution.
Price: It is free of charge.

2.       Between echo () and print () functions which one is the faster and why?
The echo() function is a tad faster than print() function; because it returns nothing, whereas print () will return 1 if the statement is successfully output.

3.       What are type casting and juggling?
Ans: Type casting:  converting values from one data type to another is known as type casting.
Juggling: Automatic conversion is known as type juggling.

4.       What is constant? How can you declare a constant?
Ans: A constant is a value that cannot be modified throughout the execution of a program.
Constant are declare by using the define() function.

5.       Which functions are used to add file in a script?
Ans: The include() and require() functions are used to add a file to a script.

6.       What are passing arguments by reference and passing arguments by value?
passing arguments by value: Means any changes made to those values within the scope of the function are ignored outside of the function.
passing arguments by reference: Means any changes made to those values within the scope of the function are reflected outside of the function.

7.       What is recursive function?
Ans: When a function is called by itself then that function is called recursive function.

8.       What are the types of key of array?
Ans: There are two types of key of array. It can be numerical And associative.
Numerical : It stores each array element with numeric index name.
associative : It contains characteristic element or string index name.

9.       What are class and object?
 Class: Classes are intended to represent those real-life items that coder like to manipulate within an application.
object: An instance of class is called object.

10.   What is property overloading?
Ans: Property overloading continues to protect properties by forcing access and manipulation through public methods, yet allowing the data to be accessed as if it were a public property.

11.   What are constructor and destructor?
Constructor: It is automatic object creation process.
Destructor: It is automatic object destruction process.

12.   What is object cloning?
To remedy the problems with copying, PHP offers an explicit means for cloning an object is known as object cloning.

13.   What type of inheritance that PHP supports?
Ans:  PHP generally supports single type of inheritance.

14.   What are the abstract class and interface?
Abstract class: An Abstract. Class is a class that cannot be instantiated.
Interface: An Interfaces is a collection of unimplemented method definitions and constants

15.   Define exceptions?
Ans: An exception is an abnormal condition that arises in a code sequence at run time. Basically there are four important keywords which form the main pillars of exception handling: try, catch, throw and finally.

16.   What is regular expression?
Regular expressions provide the foundation for describing or matching data according to defined  syntax rules.

17.   How many ways you can read a file?
We can read a file using the following mode:  
  R, r+, w+, a+, x+

18.   How many ways you can write into a file?
We can write into a file using the following mode:   
r+,  W,  w+, A, a+, X, x+

19.   What is the functionality of the function strstr and stristr?
strstr: It returns the remainder of a string beginning with the first occurrence of a predefined string.
stristr: same as strstr except the search of for the pattern is case insensitive. 

20.   What is the difference between ereg_replace() and eregi_replace()?
ereg_replace(): it is used to find and replace a pattern with a replacement string.
eregi_replace(): same as ereg_replace() except the search of for the pattern is case insensitive. 

21.   What are the different functions in sorting an array?Discuss them?
sort(): sort array in ascending order.
rsort():sort array in descending order.
asort() : Sort array  in ascending order with key/value.
arsort(): reverse of assort.
natsort (): sorting the in order we expect.
natcasesort(): case insensitive natsort.
ksort(): sorts an array by its keys.
krsort(): reverse of ksort
Usort(): sorting an array by using user defined comparison algorithm

22.   Difference between get and post method?
GET Method: get method is used for submitting small amount of data. Data shows in the URL, so it is not secure.
POST Method: post method is used for submitting small amount of data. Data does not show URL, so it is  secure.

23.   What is super global variable? Write name of some superglobal variable?
Ans: The super global variables are predefined variable in PHP.  These are accessible from anywhere within a substantial amount of environment specified information.
Name of some super global variable:-
HTTP_HOST, HTTP_ACCEPT , _GET, _POST.

24.   What is the difference between print and printf?
The difference of print and printf are as the print statement is used output data passed to it, but the printf statement is used to output a blend of static text and dynamic information stored within one or several variable.

25.   Discuss five predefined subscript of of $_FILES super global variable?
a.       $_FILES[‘upload-name’][‘’name]: The name of the file as uploaded from the client to the     server.
b.       $_FILES[‘upload-name’][‘type’]: The MIME type of the uploaded file.
c.       $_FILES[‘upload-name’][‘size’] : The byte size of the uploaded file.
d.       $_FILES[‘upload-name’][‘tmp_name’] : Once uploaded, the file will be assigned a temporary name before it is moved to its final location.
e.      $_FILES[‘upload-name’][‘error’]: An upload status code. Despite the name, this variable will be populated even in the case of success.

26.   Difference between w, r, a and a+ mode of file?
W   :  write only. Write on a new file or write on an existing file after deleting content.
R     : Read only. Read content of an existing file.
A     :  Write only.  Write on a new file or append on an existing file.
a+   : Read and write only. Write on a new file or append on an existing file and read from the file.

27.   Why substr() is used?
Ans: The substr () function is used to return the part of a string located between a predefined starting offset and length position.

28.   Why explode() is used?
Ans: The explode () function is used divides the string into an array of substrings.

29.   Difference between strpos() and strrpos()?
The strpos() function returns the position of the first occurrence of a string inside another string. If the string is not found, this function returns FALSE. 
Syntax:  is  strpos(string,find,start)

The strrpos() function finds the position of the last occurrence of a string inside another string. This function returns the position on success, otherwise it returns FALSE.    
 Syntax;  is  strrpos(string,find,start)

30.   Difference between array_merge() and array_slice()?
The array_merge() function merges one ore more arrays into one array. This function returns one array with the elements of all the parameter arrays.

Syntax is array_merge(array1,array2,array3...)
The array_slice () function returns a section of an array based on a starting and ending offset value.
Syntax is array_slice(array,start,length,preserve)