Monday, April 18, 2011

Core Java Interview Questions - Objects

The following are some questions you might encounter with respect to Java Objects in any Java Interview. Everything in Java is an Object and the term Objects includes a lot of concepts. The questions below can be used to test a programmers understanding of the basic Object concepts in Java.

Apart from the questions below, there are a few articles that I have put up (as part of the SCJP Certification series) on Objects that you might find useful. You can use them to revise/review your understanding of Servlets.

They are:

Wrapper Classes


1. How do you know if an explicit object casting is needed?

If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. Whereas, When you assign a subclass to a variable having a supeclass type, the casting happens automatically.

2. What's the difference between constructors and other methods?

Constructors must have the same name as the class and cannot return a value, whereas methods can have any name and can return any value.

Also, A constructor is called only called once when the class is instantiated, while methods could be called many times.

3. Can you call one specific constructor of a class, when it has multiple constructors

Yes. Use this() syntax and pass the arguments pertaining to the constructor you wish to invoke

4. How can a subclass call a method or a constructor defined in a superclass?

Use the; syntax to call methods of the super class and to just call the constructor use super();

5. If you're overriding the method equals() of an object, which other method you might also consider?


6. How would you make a copy of an entire Java object with all its state information?

Have the class implement Cloneable interface and call its clone() method. If the class doesnt implement the interface and still the clone() is called, you will get an exception.

7. Describe the wrapper classes in Java ?

Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type and creates an Object.

Ex: java.lang.Boolean is the Wrapper for boolean, java.lang.Long is the wrapper for long etc.

8. Which class is extended by all other classes?

The Object class is extended by all other classes.

9. Does a class inherit the constructors of its superclass?

A class does not inherit constructors from any of its superclasses. But, a class can invoke the constructors of its super class by calling super();

10. When does the compiler supply a default constructor for a class?

The compiler supplies a default constructor for a class if no other constructors are coded by the programmer who created the class.

11. What is casting?

There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

12. How are this and super used?

this is used to refer to the current object instance. super is used to refer to the variables and methods of the superclass of the current object instance.

13. When a new object of derived Class is created, whose constructor will be called first, childs or parents

Even when the new object of child class is created, first the Base class constructor gets executed and then the child classes constructor. This is because, super() is the first line of code in any constructor and so, classes get created top to bottom and hence, the constructor of the Object class gets created first and then all the other classes in the hierarchy.

14. Can constructors be overloaded


15. If you use super() or this() in a constructor where should it appear in the constructor

It should always be the first statement in the constructor and a point to note is, you cannot have both super() and this() in the same constructor for a simple reason - you cannot have two first lines in a method.

16. What is the ultimate ancestor of all java classes

Object class is the ancestor of all the java classes

17. What are important methods of Object class

wait(), notify(), notifyAll(), equals(), toString().

If you have any questions that you want answer for - please leave a comment on this page and I will answer them.

If you have any more questions on Objects that you have faced during your interviews and wish to add them to this collection - pls drop a note to and I shall be glad to add them to this list.

More Java & J2EE Interview Questions with Answers - Click Here

No comments:

Post a Comment

© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.