Search Any topic , section , query

Friday, 14 April 2017

php interview Question with answer





An    Interview , in   PHP . best he answered

Raj  is having  well hands-on experince in php , He  is getting into php technical interview round


 Interviewer started  by asking .

 "  What are different  areas , where  PHP  can be used ?"

Raj replied :

 
     There are three main areas where PHP scripts are used.

              1. Server-side scripting :  Php  run on  the web server, with a connected PHP installation. You can access the PHP program output with a web browser .

      2. Command Line Scripting : PHP Script will  also  run without any server or browser with help of  PHP parser .
       
            3 . Writing desktop applications : PHP can also  be used  to create a desktop application with a  graphical user interface .We can  use PHP-GTK  to write such programs.


Interviewer : 

 "Explain file handling in php  and all  their diffrent operation   like(reading , writing updating etc..)in detail"

Raj replies : 

PHP has several functions for     Creating,    Reading,    Open and Closing    and Editing Files.

PHP readfile() Function  , The readfile() function reads a file and  writes  it to the output buffer. Example is given below





 
  PHP Open File - fopen() . A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.Example is given below.
 
  PHP Read File - fread().  The fread() function reads from an open file.  The first parameter of fread() contains the name of  the file to read from and the second parameter specifies the maximum number of bytes to read. Example is given below


 
     PHP Close File - fclose(). The fclose() function is used to close an open file. Example  is given below.
 

   PHP Read Single Line - fgets() . The fgets() function is used to read a single line from a file. Example is given below

PHP Check End-Of-File - feof(). The feof() function checks if the "end-of-file" (EOF) has been reached.Example is given below


PHP Read Single Character - fgetc() .The fgetc() function is used to read a single character from a file. Example is given below.



PHP Write to File - fwrite() .The fwrite() function is used to write to a file.Example is given below


Interviewer next question :


"Whats the difference between mysql_connect  and  mysql_pconnect  ?"

Raj Replied : 

mysql_connect - Open a connection to a MySQL Server. Connection will be closed as soon as the execution  of the script ends, unless it's closed earlier by  explicitly calling mysql_close().Example  is given below.



While mysql_pconnect   Establishes a persistent connection to a MySQL server. When a persistent connection is requested,  PHP checks if there's already an identical persistent  connection (that remained open from earlier) - and if it exists, it uses it.
Example is given below.



Interviwer next question :


Explain PHP MySql functions :  mysql_fetch_array , mysql_fetch_assoc ,  mysql_fetch_row , mysql_fetch_object and their  differences.

Raj replied :


mysql_fetch_array    Fetch a result row as an associative array, a numeric array, or both

     mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] ) 
     
result_type is a constant and can take the following values: 
 
MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.

Example is given below.




mysql_fetch_assoc    Fetch a result row as an associative array 

mysql_fetch_assoc ( resource $result )

Example  is given below

mysql_fetch_row    Fetches one row of data from the  result associated with the specified result identifier. The row is returned as an array.  

mysql_fetch_row ( resource $result )

Example is given below


 mysql_fetch_object    Fetch a result row as an object 
mysql_fetch_object( resource $result )

Example is given below 



Interviewer next question :


 What is difference between COOKIES  and SESSION. Explain both.

Raj replied :


   A COOKIE is a small piece of text stored on a user's  computer by their browser. Each time the users' web browser interacts with a web server  it will pass the cookie information to the web server.



This means that cookies that relate to www.example.com will not be sent  to www.exampledomain.com.
   
   Common uses for COOKIES are authentication, storing of site preferences, shopping cart items,and server session identification.

 A SESSION can be defined as a server-side storage of  information that is desired to persist throughout the  user's interaction with the web site or web application. 


Instead of storing large and constantly changing  information via COOKIES in the user's browser, only a unique identifier is stored on the client side  (called a "session id(SSID)"). 

This session id is passed to the web server every time  the browser makes an HTTP request (ie a page link or AJAX request). 

The web application pairs this session id with it's internal database and retrieves  stored variables for use by the requested page.
  

Interviewer  next question :


 What is Sql injection and how  it can be prevented? 

Raj replied:


 SQL Injection is the placement of malicious code in SQL statements, via web page input.

             SQL Injection is one of the most common web hacking techniques.


  For Example The original purpose of the below code was to create  an SQL statement to select a user, with a given user id.



 If there is nothing to prevent a user from entering "wrong" input, the user can enter some "smart" input  like this: , UserId: 105 OR 1=1


Then, the SQL statement will look like this: , SELECT * FROM Users WHERE UserId = 105 OR 1=1; 

 The SQL above is valid and will return ALL rows from the "Users" table, since OR 1=1 is always TRUE.

 A hacker might get access to all the user detail,  that will be very dangerous.

-----------------------------------------------

 SQL Injection  can prevented by  using
       (1). mysqli_real_escape_string

     The mysqli_real_escape_string() function escapes  special characters in a string for use in an SQL statement.     


 Another way preventing SQL Injection  is by  using
  using prepared statment

     Example is given below 


Interviewer  next question :


What is   CrossSite  Scripting ?

Raj replied:


Cross-Site Scripting (XSS) attacks are a type of  injection, in which malicious scripts are injected into   trusted web sites. 
   
        Example is given below 



The end user’s browser has no way to know that the script should  not be trusted, and will execute the script , because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by the browser and passes  to attcker  . 
          These scripts can even rewrite the content of the HTML page. 
 
 Example is given below


Interviewer   Next Question :


Explain  Rest Services .

Raj replied :

REST defines a set of architectural principles by which you can design Web services that focus on a system's resources, including how resource states  are addressed and transferred over HTTP by a wide range of clients written in different languages.

REST Web service follows four basic design principles:
    * Use HTTP methods explicitly.
    * Be stateless.
    * Expose directory structure-like URIs.
    * Transfer XML, JavaScript Object Notation (JSON), 
           or both.



Interviewer next question :


Explain  SOAP.

Raj replied :


 SOAP  stands for Simple Object Access Protocol.

SOAP provides the envelope for sending  Web Services messages over the Internet/Internet.  It is part of the set of standards specified by      the W3C


The SOAP envelope contains two parts:

*  An optional header providing information on authentication,   encoding of data, or how a recipient of a     SOAP message should process the    message.

*  The body that contains the message. These messages can be defined using the WSDL specification.

Interviewer  next question


 What is difference  between include  and require ?

Raj replied:


With the include or require statement. We used to insert the content of one PHP/html file into  another PHP file (before the server executes it)

    In  include - (if source file missing it'll give warning    and continue execution),    while in require - (if source file missing it'll give         error  and halt execution),)

      With the include or require statement. We used to insert the content of one PHP/html file into  another PHP file (before the server executes it)

      In  include - (if source file missing it'll give warning    and continue execution),    while in require - (if source file missing it'll give  error  and halt execution),)


Interviewer Next Question :


Explain session_set_save_handler   ?

Raj replied :


To maintain the session data, we can use session_set_save_handler function.
  


This function provide 6 callback functions which call automatically.

   openFunction will be called automatically when session start.
   closeFunction will be called automatically when session end.
    readFunction will be called automatically when you read the session.
  writeFunction will be called automatically when you write in the session.
  destroyFunction will be called automatically when you destroy in the session.
   gcFunction will be called automatically when session inactive for long time.


Interviewer Next Question :

 How to convert string to array   and array to string in php?

Raj replied:


 Explode convert string to array ,   example given below


Implode convert array to string   ,    example given below

  

Interviewer  Next Question :


  How to scrape the data from website using CURL?

Raj replied :


 To scrap the data from website, Website must be  public and open for scrapable. In the blow code, Just update the CURLOPT_URL  to which websites data you want to scrap.


Interviewer Next Question :


How to explode a string using multiple delimiters("," and "|" )?

Raj replied:

  To explode a string with multiple delimeters , we use preg_split

 $string='php, interview | questions , and | answers ';
$output = preg_split( "/(\,|\|)/", $string );
print_r($output );
 
-----------------------------------------------------------------------------------

  Raj replied to  all question    very well



  
   





























Thursday, 6 April 2017

Strengths and weaknesses asked in interview






Strengths and Weakness  Asked in Interview

This is another most common question asked by Interviewer.This question is asked to know whether :

* Candidate does have those qualities which are required by the company and for particular job.

Similarly weakness to know in which area you are lacking.
*Aren’t your weakness harm the job.

Let’s see how to answer this question.

Lalit is one interviewee who has gone for interview 


. Interviewer asked him
    "what are your strengths and weaknesses?"

Lalit:  

"My strengths are:
·       I am having very good communication skills.
·       I have excellent presentation skills.
·       I can work with diverse group of people.
·       I am a very good team player.   "


----------------------------------------------------------------------------
Hmm…is this the right way to answer this question  ?

No


Lalit has clearly mentioned about his strengths, that’s good.

That’s good but how recruiter would come to know, lalit actually practiced these strengths.


Then what should he do?

He should substantiate at least 2 strengths briefly to interviewer, not all as it will stretch the interview or explain that one which interviewer wants to know. 


Let’s see again how Lalit answer the question for strengths.

Interviewer:  "what are your strengths and weakness?"


Lalit :  My strengths are: 

" I am having very good communication skills (as I headed student’s editing team of my college magazine plus hosted various college programme), with excellent presentation skills, I can also work with diverse group of people, (during my internship in Master’s which I did in retail; I worked closely from Area sales manager to store security Guard. I need to train whole staff of retail store which also includes helpers and security Guards. I maintain very healthy relationship with all). I am a very good team player plus hard worker.  "


Now we have seen how Lalit described only two strengths to interviewer with mixing other strengths.


Try to highlight those strengths which are directly related to your job or suit the requirement of job. Example:

Sales profile job’s require:
·       good communication
·       Person who likes to meet new people
·       Like to travel etc.




But it doesn’t mean you start lying plus also sounds like a scripted robot. Be natural answer as a conversation, because an interview is also a conversation



Now we see how Lalit told his weakness.

Interviewer: " good, What about weaknesses?"

Lalit: " Mam! Rather calling these are my weaknesses, I would say these are my areas of improvements. Like I am slightly impatience regarding my work, as I want my all work perfect so I am working on it to be clam, I also want to learn to say no, by saying yes to everyone I burden myself with work which leads to stress at last I am not able delegate the work which again piled up work on me which hampers my work style. So these are the areas of improvement, I require."


Interviewer: 
"Well! Lalit these are not seems as weaknesses while showing you as a workaholic, it’s good for companies but you should balance it. It’s nice talking to you.  Wait for some time for result. "


What do you think? 

 Lalit has answered very well about weaknesses.  

Firstly he stated weaknesses as a area of improvement which every interview should say while answering this question. 

Secondly he stated all his work positive weaknesses which actually are requirement of any company. That’s why interviewer said you are workaholic.

 What did this mean?

 Telling weaknesses are not being completely open yourself in front of interviewer while smartly module your answer in a positive way, so that it shows you positive in front of interviewer.