Object Oriented Programming vs Procedural

Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)

Procedure Oriented Programming
In POP, program is divided into small parts called functions.
In POP,Importance is not given to data but to functions as well as sequence of actions to be done.
POP follows Top Down approach.
POP does not have any access specifier.
In POP, Data can move freely from function to function in the system.
In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system.
POP does not have any proper way for hiding data so it is less secure.

Object Oriented Programming

In OOP, program is divided into parts called objects.
In OOP, Importance is given to the data rather than procedures or functions because it works as a real world.
OOP follows Bottom Up approach.
OOP has access specifiers named Public, Private, Protected, etc.
In OOP, data can not move easily from function to function,it can be kept public or private so we can control the access of data.
OOP provides Data Hiding so provides more security.

PHP Sessions

A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn’t maintain state.

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.

Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.

PHP sessions can use cookies depending on how you configure them. Have a look at these settings:

session.use_cookies (boolean): specifies whether the module will use cookies to store the session id on the client side. Defaults to 1 (enabled).
session.use_only_cookies (boolean): specifies whether the module will only use cookies to store the session id on the client side. Enabling this setting prevents attacks involved passing session ids in URLs. This setting was added in PHP 4.3.0. Defaults to 1 (enabled) since PHP 5.3.0.

If you disable session cookies, a GET parameter is used instead.

Sessions can also be stored in the DB instead of the default storage.
Useful command : session_set_save_handler() to override.

http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/

session_start(); 

// store session data
$_SESSION['views']=1;

//retrieve session data
echo "Pageviews=". $_SESSION['views'];

// typical use 
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];

// unset data
if(isset($_SESSION['views']))
  unset($_SESSION['views']);

// destroy session 
session_destroy();

PHP Questions on Files

1) How to include remote file in PHP?
To allow inclusion of remote files, the directive allow_url_include must be set to On in php.ini

But it is bad, in a security-oriented point of view ; and, so, it is generally disabled (I’ve never seen it enabled, actually)

It is not the same as allow_url_fopen, which deals with opening (and not including) remote files — and this one is generally enabled, because it makes fetching of data through HTTP much easier (easier than using curl)

$url = “http://www.example.org/”;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);

As long as allow_url_fopen is enabled in php.ini, you can use HTTP and FTP URLs with most of the functions that take a filename as a parameter. In addition, URLs can be used with the include, include_once, require and require_once statements (since PHP 5.2.0, allow_url_include must be enabled for these)

2) What are the different ways of reading a file?

a) file — Reads entire file into an array
$lines = file('http://www.example.com/'); // $lines is an array

b) file_get_contents – Reads entire file into a string
$file = file_get_contents('./people.txt', true); // $file is a string

c) fread – Binary-safe file read
fread() reads up to length bytes from the file pointer referenced by handle. It stops if it encounters EOF earlier.

$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename)); // $contents is a string
fclose($handle);

d) fgets – Gets line from file pointer
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}

e) fscanf
fscanf() is similar to sscanf(), but it takes its input from a file associated with handle and interprets the input according to the specified format, which is described in the documentation for sprintf(). Each call to fscanf() reads one line from the file

$handle = fopen("users.txt", "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
list ($name, $profession, $countrycode) = $userinfo;
//... do something with the values
}
fclose($handle);

f) fgetc — Gets character from file pointer
$fp = fopen('somefile.txt', 'r');
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
echo "$char\n";
}

2) How to delete a file ?
unlink();

What are different ways to change url in javascript?

1) window.location.href = “http://www.yahoo.com/home.html”;
// document.URL is alternative to window.location.href

2) window.location.assign(“http://www.yahoo.com/home.html”);
// this will not alter the browser’s history.

3) window.location.replace(“http://www.yahoo.com/home.html”);
// this will alter the browser’s history

4) window.location.reload(true);
// force to get page from server

5) window.location.reload(false);
// get page from cache if available

Joins

INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
(Tip: Read it as : All rows from Left + The Join)
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table

CROSS JOIN: It gives us combinations of each row of first table with all records in second table
FULL JOIN:  a full outer join combines the effect of applying both left and right outer joins. Where rows in the FULL OUTER JOINed tables do not match, the result set will have NULL values for every column of the table that lacks a matching row. For those rows that do match, a single row will be produced in the result set (containing columns populated from both tables).

https://www.guru99.com/joins.html

What is your greatest weakness and strength?

Weakness:
1) Effective Delegation : Sometimes end up taking responsibility to complete task on myself which I could have easily delegated.

2) Learning to say NO: Often times we end up saying “Maybe I will try it out”. When it’s clear that saying NO would be the ideal.

3) Shyness :

4) Keeping up with technology

Strength:
1) Sincerity

2) Creative

3) Good Rapport with Team ,

4) Good observation skills (personal touch to dealings with team members)

5) Eagerness to learn new technologies

Javascript Set1 of 10 Interview Questions

1) What are the basic types used in JavaScript?

Ans:
Primitive: String, Number, Boolean, Null, Undefined .

undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value. Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

Complex: Object (Arrays are Objects, Functions are Objects)

2) What are the ways to create object in JS?

1) // object constructor

var mango =  new Object ();
mango.color = "yellow";
mango.shape= "round";
mango.sweetness = 8;

mango.howSweetAmI = function () {
console.log("Hmm Hmm Good");
}
      

2) // object literal

      // This is an object with 4 items, again using object literal
var mango = {
color: "yellow",
shape: "round",
sweetness: 8,

howSweetAmI: function () {
console.log("Hmm Hmm Good");
}
}
   

3) // constructor pattern

function Fruit (theColor, theSweetness, theFruitName, theNativeToLand) {

    this.color = theColor;
    this.sweetness = theSweetness;
    this.fruitName = theFruitName;
    this.nativeToLand = theNativeToLand;

    this.showName = function () {
        console.log("This is a " + this.fruitName);
    }

    this.nativeTo = function () {
    this.nativeToLand.forEach(function (eachCountry)  {
       console.log("Grown in:" + eachCountry);
        });
    }


}

var mangoFruit = new Fruit ("Yellow", 8, "Mango", ["South America", "Central America", "West Africa"]);
   

4) // using prototype pattern

function Fruit () {

}

Fruit.prototype.color = "Yellow";
Fruit.prototype.sweetness = 7;
Fruit.prototype.fruitName = "Generic Fruit";
Fruit.prototype.nativeToLand = "USA";

Fruit.prototype.showName = function () {
console.log("This is a " + this.fruitName);
}

Fruit.prototype.nativeTo = function () {
            console.log("Grown in:" + this.nativeToLand);
}

var mangoFruit = new Fruit ();
   

3) Creating Arrays in JS

var a = new Array();
a[0] = 1.2;
a[1] = “Javascript”;
a[2] = true;
a[3] = { x:1, y:3};

var a = new Array(1.2,”Javascript”, true)

4) What is the difference between using call and apply to invoke a function?

var func = function(){
alert(‘hello!’);
};
func.apply();
vs
func.call();

The main difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly.
[A for apply, A for array]
[C for call, C for column of args]

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, …)

5) What do you understand by this keyword in JavaScript?
Ans: In JavaScript the this is a context-pointer and not an object pointer. It gives you the top-most context that is placed on the stack. The following gives two different results (in the browser, where by-default the window object is the 0-level context):

var obj = { outerWidth : 20 };

function say() {
alert(this.outerWidth);
}

say();//will alert window.outerWidth
say.apply(obj);//will alert obj.outerWidth

6) What would be the output of the following statements?

var object1 = { same: ‘same’ };
var object2 = { same: ‘same’ };
console.log(object1 === object2);
Ans: // Logs false, JavaScript does not care that they are identical and of the same object type.
When comparing complex objects, they are equal only when they reference the same object (i.e., have the same address). Two variables containing identical objects are not equal to each other since they do not actually point at the same object.

What would be the output of the following statements?

Code

var object1 = { same: ‘same’ };
var object2 = object1;
console.log(object1 === object2);

7) Consider the following statements and tell what would be the output of the logs statements?

var price1 = 10;
var price2 = 10;
var price3 = new Number(’10’); // A complex numeric object because new was used.
console.log(price1 === price2);
console.log(price1 === price3);
Ans:

console.log(price1 === price2); // Logs true.
console.log(price1 === price3); /* Logs false because price3
contains a complex number object and price 1
is a primitive value. */

8 ) Javascript Timing Events
It’s very easy to time events in JavaScript. The two key methods that are used are:

setInterval() – executes a function, over and over again, at specified time intervals
setTimeout() – executes a function, once, after waiting a specified number of milliseconds

9) How do you add a css class to an existing html element?
document.getElementById(“p1″).className += ” big”;

This will add the class big to the list of existing classes of element p1. If the “+=” was replaced by “=” then the class “big” will REPLACE all the existing classes of p1.

10) If you forget to declare a variable using “var” keyword inside a function what happens?
That variable will be treated as a global variable.