Wednesday 13 February 2019

PHP 7 interview Questions and Answers

PHP 7 interview Questions and Answers

Question: What is class Constants?
You can define constant as string OR expression within the class. but you can not define class as variable, or function.


Question: When will Autoloading function will be called once defined?
a) spl_autoload_register (autoload) will called when create object of class.
b) spl_autoload_register will called when extend by any class (using extends).
c) spl_autoload_register will called when implements an interface (using implements ).


Question: What is visibility? What are different type of visibility in class?
visibility means setting the access level of data member and member function.
Class methods must be declare as public, protected or private.
You can also declare with var which means public


Question: What is Static Keyword?
Declaring class property or methods as static makes them accessible without needing an instantiation of the class.
$this is not available inside the function.


Question: Tell me about Abstraction in PHP?
  1. Classes defined as abstract may not be instantiated.
  2. Class that contains at least one abstract method must also be abstract.
  3. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child.
  4. if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.



Question: Tell me about Interface in PHP?
  1. We use "interface" keyword to create class.
  2. All methods declared in an interface must be public.
  3. To implement an interface, the implements operator is used.
  4. We can include multiple interface while implementing



Question: Tell me about Traits in PHP?
Traits are a mechanism for code reuse in single inheritance languages such as PHP.
We can't create instance of a traits
We can create Traits as below
    
trait Hello {
        public function sayHello() {
            echo 'Hello ';
        }
    }
We can use multiple traits using comma like below
 use Hello, World;
When two traits have same function, it would conflict but we can fix using insteadof like below
A::bigTalk insteadof B;
We can set the visibility of function using "as" like below
use HelloWorld { sayHello as protected; }
We can use one trait from another using "use"
we can also define abstract,static, data members, method


Question: Tell me about Anonymous classes?
We can create a class without specifing the class-name. Following are example
$util->setLogger(new class {
    public function log($msg)
    {
        echo $msg;
    }
});


Question: What is Overloading in PHP?
Overloading in PHP provides means to dynamically create properties and methods.
These dynamic entities are processed via magic methods one can establish in a class for various action types.
example of magic functions __get(), __set(), __isset(), __unset(), __call(),__callStatic() etc


Tuesday 5 February 2019

Node Js Buffer module examples with script code

Node Js Buffer module examples with script code

Question: What is buffer module?
Buffer class provides instances to store binary data similar to an array of integers. Buffer class is a global class that can be accessed in an application.


Question: How to create an empty buffer object?
var bufferData=Buffer.alloc(1024); //1024 bytes



Question: How to write in buffer object?
var bufferData.write('this is string'); //write only string



Question: How to create an array of buffer?
var buf1 = Buffer.from('1');
var buf2 = Buffer.from('2');
var buf3 = Buffer.from('3');
var bufferData = [buf1, buf2, buf3];
console.log(bufferData);//

OR
arrayObject=[];
arrayObject.push(Buffer.from('1'));
arrayObject.push(Buffer.from('2'));
arrayObject.push(Buffer.from('3'));
console.log(arrayObject);



Question: What is concat? How to concate the buffer data?
Concatenates an array of Buffer objects into one Buffer object
var buf1 = Buffer.from('1');
var buf2 = Buffer.from('2');
var buf3 = Buffer.from('3');
var bufferData = [buf1, buf2, buf3];
bufferData=Buffer.concat(bufferData);

OR
arrayObject=[];
arrayObject.push(Buffer.from('1'));
arrayObject.push(Buffer.from('2'));
arrayObject.push(Buffer.from('3'));
bufferData=Buffer.alloc(1024);
bufferData = Buffer.concat(arrayObject);
console.log(bufferData);



Question: How to compare two buffer?
var buf1 = Buffer.from('abc');
var buf2 = Buffer.from('abc');
var compare1 = Buffer.compare(buf1, buf2);
console.log(compare1); //0

var buf1 = Buffer.from('a');
var buf2 = Buffer.from('b');
var compare2 = Buffer.compare(buf1, buf2);
console.log(compare2); //-1

var buf1 = Buffer.from('b');
var buf2 = Buffer.from('a');
var compare3 = Buffer.compare(buf1, buf2);
console.log(compare3); //1



Question: How to check if data is buffer?
Buffer.isBuffer(obj);//true|false