OOPS interview questions and Answers

Questions

25

Last updated

Jan 4, 2023

OOPS, i.e. Object-Oriented Programming Language is one of the most important concepts which is widely used in the programming world. Every interview that you are attending today requires the knowledge of OOPS. The below article compiles the most frequently asked OOPS Interview Questions and Answers which help you to ace your interviews. If you don’t know object-oriented programming then you can’t think of developing mobile software. Its unique features like inheritance, data hiding which is also known as encapsulation, data binding, polymorphism make it superior to other programming languages.

Popular languages like Java, C++, and Python, etc. are using the concept of the oops model and making it more significant in the programming world. Through OOPs a user can break his program into small-sized problems and that can be solved easily like one object at one time. The system in which the concept of OOP is used can be easily upgraded and hence provides better quality of software, enhances programmer productivity, and lower maintenance cost.

Most Frequently Asked OOPS interview questions

Here in this article, we will be listing frequently asked OOPS interview questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. What is OOPS?
Answer

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.

Q2. What are the main features of OOPs?
Answer

You have to list these four features of OOPs while answering this question-

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Data Abstraction
Q3. What is Class and Objects in OOPS?
Answer

Class

In Object-Oriented Programming, a class is a blueprint that defines the functions and variables which are common to objects of a certain kind.

Object

In OOPS an object is a specimen of the class. It is nothing but a component that consists of methods and properties which make the data useful and help users to determine the behavior of the class.

lead interview questions
Let’s understand this with an example

class Person{

    public $name;

    function __construct($name){

        $this->name = $name;

    }

    function getUserDetails(){

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

    }

}

Q4. What is an object?
Answer

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

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

Q5. What is Encapsulation in oops with an example?
Answer

The process of binding up data and functions together that manipulates them is known as encapsulation in OOPS. It protects the data from outside interference and misuse.

Let’s understand the concept of encapsulation with both real-world examples and with the help of a program.

lead interview questions
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.

class Account {
    private int account_number;
    private int account_balance;

    public void show Data() {
        //code to show data
    }

    public void deposit(int a) {
        if (a < 0) {
            //show error
        } else
            account_balance = account_balance + a;
    }
}

Q6. What is Polymorphism and its types in OOPS?
Answer

Polymorphism is one of the core concepts which is used in object-oriented programming. Polymorphism generally shows the relationships between parent class objects and child class objects.

Types of polymorphism in OOPs

  • Compile Time Polymorphism- It is also known as Static Binding and allows the programmer to implement various methods. Names can be the same but their parameters should be different.
  • Runtime Polymorphism- It is also known as Dynamic Binding and allows the programmer to call a single overridden method during the runtime of the program.
Q7. What is Inheritance?
Answer

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.

Q8. What is constructor and destructor?
Answer

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

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;

 

Q9. What is different types of Visibility?
Answer

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.
Q10. What are the Final class and Final methods?
Answer

Final Class- A class that can’t be extended and inherited further is known as Final Class. This class is declared with the keyword final and should be declared.

Final Method- Methods in the final class are implicitly final and if a user uses the final keyword that means methods can’t be overridden by subclasses.

class childClassname extends parentClassname {
    protected $numPages;

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

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

Q11. What is static keyword ?
Answer
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.
Q12. What are the difference between overloading and overriding in oops?
Answer

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.

Q13. What is "this"?
Answer

It refers to the current object of a class.

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

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.

Q15. What is namespace in PHP?
Answer

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!';
}

 

Q16. What is traits? How it is used in php?
Answer

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.

trait HelloWorld

{

use Hello, World;

}

class MyWorld {

use HelloWorld;

}

$world = new MyWorld();

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

Q17. What are magic methods?
Answer

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
Q18. What are the advantages of OOPS in PHP?
Answer
  • Code Resusability
  • Flexibility
  • Maintainability
  • Security
  • Testability
Q19. What is member variable?
Answer

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.

Q20. What is member function?
Answer

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

Q21. What are the types of Class Variables?
Answer

We have three different types of Class Variables in OOPs.

1. Local Variables

These types of variables are declared locally in methods, blocks, and constructors. These variables are created when program control enters the methods, blocks, and constructor and are destroyed once program control leaves them.

2. Instance Variables

These variables are declared outside a block, constructor, or method. These are created once a class object is created and destroyed when the object is destroyed.

3. Static Variables

Static variables are also called class variables and are defined using the static keyword. These are declared within a class but outside a code block and a method. The creation of these variables starts when the program starts and is destroyed when the program ends.

Q22. Why is object-oriented programming so popular?
Answer

There are certain points due to which OOPs become so popular.

  • It provides a better programming style, so you don’t need to write code again and again which you want to run, just make a class of the object and call it.
  • As it supports the inheritance concept, an application created with OOPs can inherit another class property.
  • It provides a modularity model that means if you change any part of code that is in a separate module, that won’t impact any other module.
  • If you are stuck somewhere, this language allows your problems to break down into bite-sized pieces so that you can solve them.
  • Due to its polymorphism feature, a single class can be used to create different objects, and that too from the same piece of code.
Q23. What is the difference between Method overriding and Method overloading?
Answer
Method Overloading Method Overriding
It is the concept where we define two or more methods by the same name but with different signatures. In method overriding we define two or more identical methods which have the same name and signatures.
The binding of the method is done at compile time. The binding of the method is done at run time.
There are no class restrictions i.e. it can be achieved in the same or different classes. There are class restrictions in it i.e. it can only be achieved in different classes.
Q24. What is virtual and pure virtual function?
Answer
Virtual Function Pure Virtual Function
Virtual Function has its definition hidden in the Base class They have no definition in the Base class.
The derived class can override the virtual function if required. In the case of a pure virtual function, the derived class has to override it.
If the derived class fails to override then it has the option to use the virtual function of the base class It will throw a compilation error if it fails to override pure virtual function.
Q25. List out some of the differences between a class and an object?
Answer
CLASS OBJECT
It is a blueprint from which different instances (objects) are created. Objects are the instances of the class.
Class is a logical entity. Objects are physical entities.
Users can declare class only once. Objects can be declared multiple times depending upon the requirements.
When a class is created there is no memory allocation Objects allocated memory.
Class is a group of objects. Objects are real-world entities such as pen, copy, mouse, etc.
Class can be declared using class keyword e.g class Student {} Objects can be declared using the new keyword e.g. Student s1=new Student();