Tag Archives: OOPS

Object-Oriented Programming (OOP) Interview Questions

Following are the Object-Oriented Programming (OOP) Interview Questions related to Java/Python/PHP/c++. These questions and their multiple options will be helpful for you to clear the doubt in Object Oriented Programming concepts. These are equally helpful for freshers and experienced aspirants. These questions are very useful when you are doing OOP LinkedIn Skill Assessment Quiz and any online Multiple Choice Based quiz for any company.

Q1. What is an example of dynamic binding?

  1. any method
  2. method overloading
  3. method overriding
  4. compiling

Q2. For which case would the use of a static attribute be appropriate?

  1. the number of people in each house in a small neighborhood
  2. the lot size for each house in a small neighborhood
  3. the color of each house in a small neighborhood
  4. the weather conditions for each house in a small neighborhood

Q3.1 Why would you create an abstract class, if it can have no real instances?

  1. to avoid redundant coding in children
  2. to explore a hypothetical class
  3. to prevent unwanted method implementation
  4. to reserve memory for an unspecified class type

Q3.2 Why would you create an abstract class, if it can have no real instances?

  1. to have common behavior in derived classes
  2. to explore a hypothetical class
  3. to prevent unwanted method implementation
  4. to reserve memory for an unspecified class type

Q4. When does static binding happen?

  1. only when you export
  2. both at compile time and runtime
  3. at compile time
  4. at runtime

Q5. What is the best reason to use a design pattern?

  1. It will result in code that is more extensible and maintainable
  2. It will result in a more compact product.
  3. It will speed initial development.
  4. It will allow you to add that design pattern to your resume.

Q6. What is encapsulation?

  1. defining classes by focusing on what is important for a purpose
  2. hiding the data and implementation details within a class
  3. making all methods private
  4. using words to define classes

Q7. What is an IS-A relationship?

  1. It implies encapsulation.
  2. A superclass object has an IS-A relationship with its subclass.
  3. It implies a virtual method.
  4. A subclass object has an IS-A relationship with its superclass or interface

Q8. You want a method with behavior similar to a virtual method–it is meant to be overridden –expect that it does not have a method body. It just has a method signature. What kind of method should you use?

  1. an abstract method
  2. a public internal method
  3. an internal method
  4. a protected internal method

Q9. Which code creates a new object from the Employee class?

  1. Employee current Employee = Employee.Create();
  2. Employee current Employee = new Employee();
  3. Employee currentEmployee;
  4. Employee currentEmployee = Employee.New();

Q10. Which type of constructor cannot have a return type?

  1. default
  2. copy
  3. parameterized
  4. Constructors do not have a return type

Q11.1 When is a constructor executed?

  1. when an object is created from a class using the new keyword
  2. when an class is defined using the class keyword
  3. every time an object is referenced
  4. when an object is created from a class using the create keyword

Q11.2 When is a constructor executed?

  1. when an object is created from a class
  2. when an class is defined using the class keyword
  3. every time an object is referenced
  4. when an object is created from a class using the create keyword

Q12. If a local class is defined in a function, what is true for an object of that class?

  1. The object can be accessed, declared, and used locally in that function.
  2. The object must be declared inside any other function.
  3. The object is temporarily accessible outside the function.
  4. The object can call all the other class members anywhere in the program.

Q13. Which two blocks are used to handle and check errors?

  1. do and check
  2. catching and trying
  3. try and catch
  4. do and while

Q14. Why would you implement composition using an id instead of a reference?

  1. It makes it easier to save the entity.
  2. all of these answers
  3. It can make the entity retrieval more efficient
  4. It minimizes coupling.

Q15. Which statement best describes the method of inheritance in OOP?

  1. Inheritance describes the ability to create new classes based on an existing class.
  2. Inheritance means that a group of related properties, methods, and other members are treated as a single unit or object.
  3. Inheritance forces a class to have a single responsibility from only one parent.
  4. Inheritance means that you will never have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.

Q16 Which type of inheritance ,when done continuously, is similar to a tree structure?

  1. multilevel
  2. hierarchical and multiple
  3. hierarchical
  4. multiple

Q17. Which statement is true?

  1. A default parameter’s constructor is not equivalent to the default constructor
  2. A default constructor is inherited from a parent class
  3. A default constructor can be called explicitly
  4. A default constructor cannot be defined by the coder

Q18. Which of the following is NOT an advantage of using getters and setters?

  1. Getters and setters can speed up compilation.
  2. Getters and setters provide encapsulation of behavior.
  3. Getters and setters provide a debugging point for when a property changes at runtime.
  4. Getters and setters permit different access levels.

Q19. In the context of OOP, what is association?

  1. Association is a relationship where all objects have their own life cycle and there is no owner.
  2. Association is the process where model elements cooperate to provide higher-level behavior.
  3. Association is a whole/part relationship where one object is composed of one or more other objects, each of which is considered a part of the whole.
  4. Association is where all objects have their own life cycle, but there is ownership, and child objects can not belong to another parent object.

Q20. How are user stories different from use cases?

  1. User Stories are shorter and less detailed.
  2. User stories are more accurate.
  3. User stories are more detailed and structured.
  4. User stories are more anecdotal and personal.

Q21. Which type of inheritance must be used so that the resultant is hybrid?

  1. multiple
  2. any type of inheritance
  3. multilevel
  4. hierarchical
Hybrid inheritance is a composition of multiple and hierarchical inheritances

Q22. A language that does not support polymorphism but supports classes is considered what?

  1. an object-based language
  2. a class-based language
  3. a procedure-oriented language
  4. if classes are supported, polymorphism will be supported

Q23. If two classes combine some private data members and provide public member functions to access and manipulate those data members. Where is abstraction used?

  1. Abstraction is using a private access specifier for data members
  2. Abstraction is using public member functions to access and manipulate the data members
  3. Abstraction is using the class concept with both data members and member functions
  4. There is insufficient information to decide where abstraction is being used.

Q24. What are the five Creational Design patterns by the Gang of Four ?

  1. Observer, State, Strategy, Template Method, and Visitor.
  2. Composite, Visitor, State, Prototype, and Singleton.
  3. Composite, Builder, Factory Method, Prototype, and Singleton.
  4. Abstract Factory, Builder, Factory Method, Prototype, and Singleton.

Q25. In multilevel inheritance, one class inherits how many classes?

  1. one class only
  2. two classes
  3. as many classes as required
  4. at least two classes

Q26. if an object is passed by reference, the changes made in the function are reflected **\_\_\_**.

  1. to the main object of the caller function, too
  2. on the caller function object and also the called function object
  3. on the copy of the object that is made during the pass
  4. only in the local scope of the called function

Q27. What is a method?

  1. a set of instructions designed to perform a frequently used operation within a program and return no values
  2. the exact same thing as a function and subroutine
  3. a set of variables that can change over time
  4. a procedure associated with data and behaviour

Q28. A mobile phone is made up of components such as a motherboard, camera, and sensors. The motherboard represents all the functions of a phone, the display shows the display only, and the phone is represented as a whole. Which of the following has the highest level of abstraction?

  1. camera
  2. display
  3. motherboard
  4. mobile phone

Q29. Which class has the highest degree of abstraction in a multilevel inheritance relationship of five levels?

  1. the class at the third level
  2. the class at the first level
  3. All have the same degree of abstraction.
  4. the class at the second level

Q30. In the context of OOP, what is association?

  1. Association is a whole/part relationship where one object is composed of one or more other objects, each of which is considered a part of the whole.
  2. Association is where all objects have their own life cycle, but there is ownership, and child objects can not belong to another parent object.
  3. Association is the process where model elements cooperate to provide higher-level behavior.
  4. Association is a relationship where all objects have their own life cycles and there is no owner.

Q31. Which is NOT one of the basic types of inheritance?

  1. multilevel inheritance
  2. double inheritance
  3. single inheritance
  4. hierarchical inheritance

Q32. Why is code duplication so insidious?

  1. The duplication uses unnecessary space.
  2. One has to maintain all the duplicates.
  3. Duplication can cause intellectual property concerns.
  4. Duplication is easy to hide.

Q33. When and how often is a static constructor called?

  1. It is called initially when an object is created and called with every new object instance.
  2. It is called when an object is destroyed and only one time.
  3. It is called initially when an object is created and only one time.
  4. It is created at a time when the object is discarded.

Q34. What does the code shown below demonstrate, and why?

  static void Multiply(int num1, int num2) {};

   static void Multiply(double num1, double num2, double num3) {};

   static void Multiply(float num1, float num2) {};

 

  1. polymorphism, because each method can perform different task
  2. method overriding, because it display the same method name, different or same parameters, and same return type
  3. method overloading, because it allows the creation of several methods with the same name, which differ by the type of input via parameter
  4. method overriding, because it display the same method name, different parameters, and same return type

Q35. What is the purpose of a static constructor?

  1. to initialize all the members with static value
  2. to delete the static members when not required
  3. to initialize the static members of class
  4. to clear all the static members’ initialized values

Q36. What are CRC Cards?

  1. Code Responsibility Collection cards are a brainstorming tool used in the design of procedural software
  2. Class responsible collaboration cards are a brainstorming tool used in the design of oop software
  3. Code Responsibility Correction cards are tools used for debugging
  4. Code Responsibility Correction cards are tools for modeling

Q37.1 How are contents of a composition different from those of aggregation?

  1. if one element of an aggregation is dereferenced, all its elements are eligible for garbage collection
  2. if a composition dies, the contents die
  3. the contents of a composition are all siblings
  4. an aggregation contains only abstract classes

Q37.2 Which statement about compositions and aggregations is true?

  1. if one element of an aggregation is dereferenced, all its elements are eligible for garbage collection
  2. if a composition dies, the contents die
  3. the contents of a composition are all siblings
  4. an aggregation contains only abstract classes

Q38. What is the result of using more abstraction?

  1. it can increase code vulnerability
  2. it can make code unsafe
  3. it can limit code readability
  4. it can be safer for coding

Q39. Which is false for a member function of a class?

  1. they can be defined only inside or outside the class body
  2. the can be made to be friends of another class
  3. they do not need to be declared inside the class definiton
  4. they need to be defined

Q40. Why is inheritance used when creating a new class?

  1. to protect attributes from unwanted changes
  2. to delegate coding responsibility more efficiently
  3. to conserve memory
  4. to avoid writing duplicate code

Q41. In addition to attributes and behaviours, what quality must a class possess?

  1. a name
  2. a state
  3. a color
  4. an object

Q42. Which type of function among the following shows polymorphism?

  1. inline function
  2. undefined function
  3. virtual function
  4. class member function

Q43. Which words in the following list are candidates for objects: trumpet, clean, enrage, leaf, tree, collapse, active, and lively?

  1. leaf and tree
  2. clean, enrage, and collapse
  3. clean, active, and lively
  4. leaf, tree, and trumpet

Q44. What best describes what object-oriented programming does?

  1. It focuses on objects that interact cleanly with one another.
  2. It programs exclusively to interfaces.
  3. It programs exclusively to classes.
  4. It creates one class for all business logic.

Q45. Can abstract classes be used in multilevel inheritance?

  1. No, abstract classes can be used only in single-level inheritance since they must be immediately implemented.
  2. yes, always
  3. yes, but with only one abstract class
  4. No, abstract classes do not have constructors.

Q46. What type of inheritance may lead to the diamond problem?

  1. single level
  2. multilevel
  3. hierarchical
  4. multiple

Q47. How are the contents of a composition different from those of an aggregation?

  1. The contents of a composition are all siblings.
  2. An aggregation contains only abstract classes.
  3. If a composition dies, the contents die.
  4. If one element of an aggregation is dereferenced, all its elements are eligible for garbage collection.

Q48. What is the relationship between abstraction and encapsulation?

  1. Abstraction is about making relevant information visible, while encapsulation enables a programmer to implement the desired level of abstraction.
  2. Abstraction and encapsulation are essentially the same.
  3. Abstraction and encapsulation are unrelated.
  4. Encapsulation is about making relevant information visible, while abstraction enables a programmer to implement the desired level of encapsulation.

Q49. Which of these keywords are access specifiers?

  1. abstract and public
  2. public and private
  3. this and final
  4. final and abstract

Q50. Why is inheritance used when creating a new class?

  1. to conserve memory
  2. to protect attributes from unwanted changes
  3. to separate class behaviour from the more general to the more specific
  4. to delegate coding responsibility more efficiently

Q51. What is a reference to an object?

  1. It is the address of the variable only — not the method of an object.
  2. It is a shallow pointer that contains the address of an object.
  3. It is the physical address of an object.
  4. It is the address where the variables and methods of an object are stored.

Q52. Why is unit testing harder in OOP than functional programming?

  1. Objects may maintain internal state, which is not easily accessible by the tests.
  2. The quality of unit testing frameworks for functional languages is better.
  3. OOP promotes code reuse, which means that your tests have to consider more use cases.
  4. Object-oriented languages tend to rely on frameworks such as Spring or Hibernate, which make them difficult to test.

Q53. What is the function of a user diagram?

  1. It connects actors to use cases.
  2. It links actors to roles played in all use cases.
  3. It lists all actors for each use case.
  4. It minimizes the number of actors required.

 

Spread the love

OOPS interview questions and answers

What is OOPS(Object Oriented Programming System)?
OOPS, that is Object Oriented Programming System is a modularized system of programming. Chunks of programs are designed in such a manner that we can reuse them, modify them and also can rework on them.


What is a class?
A class is a collection or a group to represent similar type entities having identical properties and behavior.A class does not exist in physical but a concept to visualize real-world entities.


What is an object?
An object is a blueprint of a class. It is a prototype what class defines. When we have a working object we can say it is an instance.

How can we differentiate between Procedural Programming System and OOPS?
An object-oriented language is based on modules that are Object but procedural programming has no such concepts.
OOPS is able to encapsulate data and member functions in a single block but procedural programming language represents a discrete structure.
A procedural language is based on top-down approach whereas object-oriented language is based on the bottom-up approach of the algorithm.
A procedural language is rigid in nature, but OOPS provide flexibility and reusability of code.

What are the basic pillars of OOPS?
Fundamental pillars of OOPS and are as follows:
1-Abstraction
Hiding the sensitive data or the way of implementation, but revealing the necessary data to the end user.It limits the scope of variables, methods.Abstraction is to implement the security features in OOPS and so in application.
2-Encapsulation
Encapsulation is the wrapping of data and data functions in a single block of codes. It contains the data definitions and behavior definition in a single block that is Class.
3-Inheritance
Inheritance provides flexibility features to OOPS. It allows a class to share its functions, variables to other classes and the classes that will be using the functions definitions and data variables of other class called child class, and this phenomenon is called Inheritance.
4-Polymorphism
Polymorphism is a way to reimplement the methods in child-class or in a subclass that is already implemented in the parent class.It gives flexibility to an object to represent it and to use its many forms as per the function definitions in classes.


What is a constructor in OOPS?
The constructor is a method which is called automatically when an Object of a class is instantiated.The constructor has the same name as that of the class and it does not return.


What is the basic difference between overloading and overriding?
Overloading
When two methods are defined with the same name in a single class but they must have different signatures, then this phenomenon is known as Overloading.
overriding
When a method defined in the parent class is reimplemented by subclass or child class, then this phenomenon is known as overriding.


Difference between class and an object?
A class is a definition and object is a prototype of that definition.An object contains states and behaviors and a class is a template of an object. An object is an instance of a class and it is working model of a definition that we can call a class.


What are the specifiers?
Specifiers or modifiers define the scope of variables and methods in OOPS.They implement a secure structure and access level in the architecture of an application.There is some important access specifiers are as follows:
Private:
Restricts the access to the class itself. Even Child or subclass are also not allowed to use private functions and data.
Protected:
A protected function or variable or member is accessible within the class and within the child class or subclass only. It cannot be accessible from anywhere else.
Public:
It provides an open accessibility to members. Either within the class or within child class or from anywhere in the script, It will be accessible.


What is a destructor?
the destructor is a function which can be called automatically when an object is deleted.

Spread the love