Loading…

OOPS interview questions and Answers

By Admin
Last update: 16 May 2020, 20 Questions
OOPS interview questions

OOPS Stands for Object-Oriented Programming System. It refers to the languages which are using objects in programming. The objective of OOPS is to carry out practical entities such as hiding, abstraction, polymorphism, and more into coding.

If you are new to OOPS or want to improve your skills. Here is a list of the most frequently asked OOPS Interview Questions which can help you crack that dream job as an OOPS Developer. You can also download the OOPS Interview Questions and Answers in the form of a PDF format to access it anytime, on any device, offline.

While implementing these entities, OOPS aims to increase the efficiency of code by binding the data and its functions needed for operation in such a way that accessibility to data remains limited to the functions which need them, thus, keeping your code extremely organized.

Most Frequently Asked OOPS interview questions

1. What is OOPS?

OOPS stands for Object Oriented Programming system. It is a programming technique which is used to design your application. In this programs are considered as a collection of objects and each object is an instance of a class.

2. Write basic concepts of OOPS?
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction etc
3. What is class?

Class is a user-defined datatype that contains variables, properties, and methods. It defines properties of an Object

Example

                                                    

class Person{

    public $name;

    function __construct($name){

        $this->name = $name;

    }

    function getUserDetails(){

        return "My name is ".$this->name;

    }

}

4. What is an object?

An object is an instance of a class. It has its own behavior and identity.

Example

                                                    

//Create an object of MyClass
$object = new MyCustomClass();
OR
$object = new MyCustomClass;

5. What is Encapsulation?

It is an attribute of an object, and it contains all data which is hidden. That hidden data is restricted to the members of that class.

6. What is Polymorphism?

It takes more than one form. It is nothing but assigning behavior or value in a subclass to something that was already declared in the main class.

7. What is Inheritance?

It is a technique in which one class acquires the property of another class. With the help of inheritance, we can reuse the fields and methods of the existing class.

Inheritance has three type, are given below.

  • Single inheritance
  • Multiple inheritance
  • Multi level inheritance

But PHP supports only single inheritance, where only one class can be derived from single parent class. We can also use multiple inheritance by using interfaces.

8. What is constructor and destructor?

Constructor and Destructor both are special functions which are automatically called when an object is created and destroyed.

Example

                                                    

Constructor

class Animal

{

     public $name = "No-name animal";

     public function __construct() {

     echo "I'm alive!";

}

}

Destructor

class Animal {

public $name = "No-name animal";

public function __construct($name) {

echo "I'm alive!";

$this->name = $name;

}

public function __destruct() {

echo "I'm dead now :(";

}

}

$animal = new Animal("Bob");

echo "Name of the animal: " . $animal->name;

 

9. What is different types of Visibility?

PHP have three access modifiers such as public, private and protected.

  • public scope of this variable or function is available from anywhere, other classes and instances of the object.
  • private scope of this variable or function is available in its own class only.
  • protected scope of this variable or function is available in all classes that extend current class including the parent class.
10. What is final class and final method?

It's properties can not be declared final, only classes and methods may be declared as final. If the class or method defined as final then it cannot be extended.

Example

                                                    

class childClassname extends parentClassname {
    protected $numPages;

    public function __construct($author, $pages) {
        $this->_author = $author;
        $this->numPages = $pages;
    }

    final public function PageCount() {
        return $this->numPages;
    }
}

11. What is static keyword ? When a variable or function declared as static then it cannot be accessed with an instantiated class object. It is treats as public if no visibility is defined. It can also be used to define static variables and for late static bindings.
12. What are the difference between overloading and overriding in oops?

Overloading : It occurs when two or more methods in one class have the same method name but different parameters.

Overriding : It means having two methods with the same method name and parameters. One of the methods is in the parent class and the other is in the child class.

13. What is "this"?

It refers to the current object of a class.

14. What are the difference between abstract class and interface in OOPS?

Thera are many differences between abstract class and interface in php.

1. Abstract methods can declare with public, private and protected. But in case of Interface methods declared with public.

2. Abstract classes can have constants, members, method stubs and defined methods, but interfaces can only have constants and methods stubs.

3. Abstract classes doest not support multiple inheritance but interface support this.

4. Abstract classes can contain constructors but interface doest not support constructors.

15. What is namespace in PHP?

It allows us to use the same function or class name in different parts of the same program without causing a name collision.

namespace MyAPP;
function output() {
echo 'IOS!';
}
namespace MyNeWAPP;
function output(){
echo 'RSS!';
}

 

16. What is traits? How it is used in php?

Traits is a group of methods that reuse in single inheritance. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods.

Example

                                                    

trait HelloWorld

{

use Hello, World;

}

class MyWorld {

use HelloWorld;

}

$world = new MyWorld();

echo $world->sayHello() . " " . $world->sayWorld(); //Hello World

17. What are magic methods?

Magic methods are special names, starts with two underscores, which denote methods which will be triggered in response to particular PHP events.

The magic methods available in PHP are given below:-

  • __construct()
  • __destruct()
  • __call()
  • __get()
  • __set()
  • __isset()
  • __unset()
  • __sleep()
  • __clone()
  • __autoload() etc
18. What are the advantages of OOPS in PHP?
  • Code Resusability
  • Flexibility
  • Maintainability
  • Security
  • Testability
19. What is member variable?

Member variables defined inside a class. Data of member variables will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.

20. What is member function?

Member function defined inside a class and are used to access object data.