Interview Questions and Answers for 'Objects' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Objects' - 100 question(s) found - Order By Newest

next 30
Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems
  Q1. Difference between == and .equals() ?Core Java
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory.

x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object.

Sample code:

String x = new String("str");
String y = new String("str");

System.out.println(x == y); // prints false
System.out.println(x.equals(y)); // prints true

  Sample Code for equals

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   string comparison   string   object class   ==    equals   object equality  operator   == vs equals   equals vs ==     Asked in 294 Companies      basic        frequent

Try 6 Question(s) Test


Frequently asked to fresh graduates and less experienced developers. Among the first few questions in many interviews.
  Q2. What are the difference between composition and inheritance in Java?Core Java
Ans. Composition - has-a relationship between objects.
Inheritance - is-a relationship between classes.

Composition - Composing object holds a reference to composed objects and hence relationship is loosely bound.
Inheritance - Derived object carries the base class definition in itself and hence its tightly bound.

Composition - Used in Dependency Injection
Inheritance - Used in Runtime Polymorphism

Composition - Single class objects can be composed within multiple classes.
Inheritance - Single class can only inherit 1 Class.

Composition - Its the relationship between objects.
Inheritance - Its the relationship between classes.

  Sample Code for inheritance

  Sample Code for composition

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   java concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   inheritance  object oriented programming (oops)  inheritance vs composition  object oriented programming (oops)  oops concepts     Asked in 29 Companies      basic        frequent

Try 5 Question(s) Test


  Q3. Explain OOPs

or

Explain OOPs Principles

or

Explain OOPs Concepts

or

Explain OOPs features

or

Tell me something about OOPs
Core Java
Ans. OOPs or Object Oriented Programming is a Programming model which is organized around Objects instead of processes. Instead of a process calling series of processes, this model stresses on communication between objects. Objects that all self sustained, provide security by encapsulating it's members and providing abstracted interfaces over the functions it performs. OOP's facilitate the following features

1. Inheritance for Code Reuse
2. Abstraction for modularity, maintenance and agility
3. Encapsulation for security and protection
4. Polymorphism for flexibility and interfacing

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     oops  oops features     Asked in 260 Companies      basic        frequent


Very frequently asked to Fresh graduates.
  Q4. What is the difference between Encapsulation and Abstraction?Core Java
Ans. 1.Abstraction solves the problem at design level while encapsulation solves the problem at implementation level

2.Abstraction is used for hiding the unwanted data and giving relevant data. while Encapsulation means hiding the code and data into a single unit to protect the data from outside world.

3. Abstraction lets you focus on what the object does instead of how it does it while Encapsulation means hiding the internal details or mechanics of how an object does something.

4.For example: Outer Look of a Television, like it has a display screen and channel buttons to change channel it explains Abstraction but Inner Implementation detail of a Television how CRT and Display Screen are connect with each other using different circuits , it explains Encapsulation.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   oops concepts   encapsulation  object oriented programming (oops)  oops concepts   abstraction   basic interview question   encapsulation vs abstraction     Asked in 10 Companies      basic        frequent

Try 2 Question(s) Test


 Q5. Why do we need Inner classes ? Cant we just work with outer classes wherever we implement Inner classes ?Core Java
Ans. Yes, we can substitute outer classes wherever we need to have inner classes but Inner classes have advantage in certain cases and hence preferred -

Ease - Why to implement a class outside if its objects are only intended to be part of an outer object. Its easy to define the class within another class if the use is only local.

Protection - Making a call an outer exposes a threat of it being used by any of the class. Why should it be made an outer class if its object should only occur as part of other objects.

For example - You may like to have an class address whose object should have a reference to city and by design thats the only use of city you have in your application. Making Address and City as outer class exposes City to any of the Class. Making it an inner class of Address will make sure that its accessed using object of Address.

  Sample Code for inner class

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   inner classes   classes   objects   technical lead      intermediate

Try 1 Question(s) Test


Frequently asked in Infosys and HCL Technologies ( Based on 2 feedback )
 Q6. What are different ways of object creation in Java ?Core Java
Ans. Using new operator - new xyzClass()

Using factory methods - xyzFactory.getInstance( )

Using newInstance( ) method - (Class.forName(xyzClass))emp.newInstance( )

By cloning an already available object - (xyzClass)obj1.clone( )

  Sample Code for object initialization using clone

  Sample Code for object initialization using getInstance

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   object creation   new operator   class.forname   cloning   ebay     Asked in 9 Companies      expert


 Q7. What are the common uses of "this" keyword in java ?Core Java
Ans. "this" keyword is a reference to the current object and can be used for following -

1. Passing itself to another method.

2. Referring to the instance variable when local variable has the same name.

3. Calling another constructor in constructor chaining.

  Sample Code for this keyword

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   this   object reference   constructor chaining      intermediate        rare

Try 3 Question(s) Test


 Q8. What are points to consider in terms of access modifier when we are overriding any method?Core Java
Ans. 1. Overriding method can not be more restrictive than the overridden method.

reason : in case of polymorphism , at object creation jvm look for actual runtime object. jvm does not look for reference type and while calling methods it look for overridden method.

If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime when the JVM invokes the true objects version of the method rather than the reference types version then it will be problematic

2. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.

3. We can not override any private method because private methods can not be inherited and if method can not be inherited then method can not be overridden.

  Sample Code for Overriding

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   overriding   access specifier   inheritence   oops   polymorphism  object oriented programming (oops)  oops concepts   runtime polymorphism  object oriented programming (oops)  oops concepts     Asked in 4 Companies

Try 2 Question(s) Test


Basic and Very Frequently asked.
  Q9. What is Polymorphism in Java ?Core Java
Ans. Polymorphism means the condition of occurring in several different forms.

Polymorphism in Java is achieved in two manners

1. Static polymorphism is the polymorphic resolution identified at compile time and is achieved through function overloading whereas

2. Dynamic polymorphism is the polymorphic resolution identified at runtime and is achieved through method overriding.

  Sample Code for overloading

  Sample Code for overriding

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     polymorphism  object oriented programming (oops)  oops concepts  oops concepts     Asked in 108 Companies      Basic        frequent

Try 2 Question(s) Test


 Q10. Can you give a real world example of Encapsulation and Abstraction ?Core Java
Ans. Car Engine is an example of encapsulation and abstraction. You ignite the car using an interface called starter and least bothered about how the tire actually moves (This is abstraction). The engine encapsulates the complete process to itself only and doesn't allow you to start the other components like the radiator etc ( this is excapsulation )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     encapsulation  object oriented programming (oops)  oops concepts  abstraction  oops  oops features  java features     Asked in 13 Companies


Frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q11. What are the methods of Object Class ?Core Java
Ans. clone() - Creates and returns a copy of this object.

equals() - Indicates whether some other object is "equal to" this one.

finalize() - Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

getClass() - Returns the runtime class of an object.

hashCode() - Returns a hash code value for the object.

toString() - Returns a string representation of the object.

notify(), notifyAll(), and wait() - Play a part in synchronizing the activities of independently running threads in a program.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   object class     Asked in 16 Companies      basic        frequent

Try 1 Question(s) Test


 Q12. Why every object constructor automatically call super() in Object before its own constructors?Core Java
Ans. Derived object carries the body of its class as well as the body of the parent class. Its body ( member elements ) is initialized using its own class constructor whereas the body ( member elements ) carried from the parent class are initialized using super class constructor. So In order to initialize the elements of the parent class before its own elements are even initialized, super is called.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   constructor   super   inheritance  object oriented programming (oops)  oops concepts   inheritence     Asked in 1 Companies      intermediate


 Q13. Describe what happens when an object is created in Java ?Core Java
Ans. 1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implemenation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its superclasses. This process continues until the constructor for java.lang.Object is called,as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   memory management   object creation   advanced   ebay


 Q14. Does Constructor creates the object ?Core Java
Ans. New operator in Java creates objects. Constructor is the later step in object creation. Constructor's job is to initialize the members after the object has reserved memory for itself.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   constructor   object creation      intermediate        rare


 Q15. What will this code print ?

String a = new String ("TEST");
String b = new String ("TEST");
if(a == b) {
System.out.println ("TRUE");
} else {
System.out.println ("FALSE");
}
Core Java
Ans. FALSE. == operator compares object references, a and b are references to two different objects, hence the FALSE. .equals method is used to compare string object content.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string   string class   java   ==   object references   coding     Asked in 2 Companies      basic        frequent

Try 1 Question(s) Test


 Q16. How compiler handles the exceptions in overriding ?Core Java
Ans. 1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.

2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception

3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   overriding   exceptions   inheritence   inheritance  object oriented programming (oops)  oops concepts   oops


Frequently asked to fresh graduates and less experienced.
  Q17. Difference between Composition and Inheritance ?Core Java
Ans. Inheritance means a object inheriting reusable properties of the base class. Compositions means that an abject holds other objects.

In Inheritance there is only one object in memory ( derived object ) whereas in Composition , parent object holds references of all composed objects.

From Design perspective - Inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   oops concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   difference between   basic interview question     Asked in 12 Companies      basic        frequent

Try 2 Question(s) Test


 Q18. Which String class does not override the equals() and hashCode() methods, inheriting them directly from class Object?Core Java
Ans. java.lang.StringBuffer.

  Sample Code for StringBuffer

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   object class   stringbuffer      expert        rare


 Q19. There are two objects a and b with same hashcode. I am inserting these two objects inside a hashmap.

hMap.put(a,a);
hMap.put(b,b);

where a.hashCode()==b.hashCode()

Now tell me how many objects will be there inside the hashmap?
Core Java
Ans. There can be two different elements with the same hashcode. When two elements have the same hashcode then Java uses the equals to further differentation. So there can be one or two objects depending on the content of the objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   hashcode   map   hashmap   object reference   advanced     Asked in 1 Companies


 Q20. Why Java is not considered pure OOP's language ?Core Java
Ans. There are 2 reasons for it.

1. Usage of Primitive types - Though Java provides classes for the primitive data types but as the usage of primitives is permissible, its considered unpure OOP's language.

2. Usage of Static members - Static members belong to the class and not objects and hence not considered fit for pure OOP's programming.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     oops  object oriented programming  object oriented language  primitive data types     Asked in 2 Companies


 Q21. What one should take care of, while serializing the object?
Ans. One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializable Exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     serialization   java   oops   object      expert


 Q22. Difference between object instantiation and construction ?Core Java
Ans. Though It's often confused with each other, Object Creation ( Instantiation ) and Initialization ( Construction ) are different things in Java. Construction follows object creation.

Object Creation is the process to create the object in memory and returning its handler. Java provides New keyword for object creation.

Initialization is the process of setting the initial / default values to the members. Constructor is used for this purpose. If we don't provide any constructor, Java provides one default implementation to set the default values according to the member data types.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   constructor   object instantiation


Frequently asked to fresh graduates and less experienced.
 Q23. Which of the following is tightly bound ? Inheritance or Composition ?Core Java
Ans. Inheritance.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     inheritance  object oriented programming (oops)  oops concepts   java   oops   composition  object oriented programming (oops)  oops concepts     Asked in 1 Companies      basic        frequent


 Q24. Difference Between this() and super() ?Core Java
Ans. 1.this is a reference to the current object in which this keyword is used whereas super is a reference used to access members specific to the parent Class.

2.this is primarily used for accessing member variables if local variables have same name, for constructor chaining and for passing itself to some method whereas super is primarily used to initialize base class members within derived class constructor.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   class   objects   inheritence   constructor   this   super   this vs super     Asked in 1 Companies

Try 1 Question(s) Test


  Q25. Why java doesn't support multiple Inheritance ?

or

Explain Java Diamond Problem.
Core Java
Ans. class A {
void test() {
System.out.println("test() method");
}
}

class B {
void test() {
System.out.println("test() method");
}
}

Suppose if Java allows multiple inheritance like this,

class C extends A, B {
}

A and B test() methods are inheriting to C class.

So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem     Asked in 20 Companies      basic        frequent


 Q26. What is the use of Object class ?Core Java
Ans. 1. Default definition for some of the methods , like equals, hashcode etc that gets carried to all objects even if you don't define anything. But that default definition is kind of like assigning 0 to integer, it just provide a safe state , nothing much for comparison.

2. Places where you have no idea about what object you may receive and just want to perform out of 8 basic methods of object class, something like printing their string representation ( defined by their toString method ), or equality ( using their equals method )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     object class


 Q27. Does Declaring an object "final" makes it immutable ?Core Java
Ans. Only declaring variables as final makes them immutable. Making objects final means that the object handler cannot be used to target some other object but the object is still mutable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   final   immutable  immutability   immutable  immutability object


 Q28. What are the features of encapsulation ?Core Java
Ans. Combine the data of our application and its manipulation at one place.

Encapsulation Allow the state of an object to be accessed and modified through behaviors.

Reduce the coupling of modules and increase the cohesion inside them.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   oops concepts   encapsulation  object oriented programming (oops)  oops concepts   basic interview question     Asked in 2 Companies      basic        frequent


 Q29. Difference between new operator and Class.forName().newInstance() ?
Ans. new operator is used to statically create an instance of object. newInstance() is used to create an object dynamically ( like if the class name needs to be picked from configuration file ). If you know what class needs to be initialized , new is the optimized way of instantiating Class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   object instantiation   object creation   class.forname   newinstance   new operator   difference between   advanced      intermediate


 Q30. Will this code compile fine ?

ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
Core Java
Ans. Yes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   io   file   fileio   coding   code   objectoutputstream   fileoutputstream   yesno  file handling


next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: