Over a million developers have joined DZone.

Core Java Interview Questions List: Part IV

This quiz will test your knowledge of constructors, pass by value, autoboxing, and some big O efficiency.

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

The name of the game is core java interview questions and I will be your quiz master. Each week I will publish 10 new quick fire questions and answers.

1. What is the Big O of object retrieval in a hashmap and why?

In the best case of no clashes it will be O(1).  The hashing function will result in direct access to the element, irrelevant of how many other objects are in the map.  However, in the worst case it could be O(n), as if the hashing function results in all the objects landing in the same bucket it will be the same complexity as linked list (which is the backing structure; see Java Data Structures Interview Questions for more).

2. What is the result of the following statement? 

int[] ints = {1, 2, 3, 4, 5};
System.out.println((ints.length/2) * (ints.length + 1));

This outputs 12. Because array.length is an integer, ints.length/2 = 2, not 2.5.

3. What is the difference between the JVM, JRE and JDK?

JVM is the Java Virtual Machine. All java applications execute on the Java Virtual Machine. (Read more in JVM and Garbage Collection Interview Questions: The Beginners Guide).

The JRE is the Java Runtime Environment. The JRE contains the JVM, along with supporting files such as libraries.

The JDK is the Java Development Kit and contains the JRE (and JVM) along with the tools developers need to build and create applications, such as a compiler.

4. What is a constructor? Why is it different from other methods?

A constructor is used to create an object.  It does not specify a return type (as the return type is implied; the constructor for MyObject will return a MyObject type). Constructor must be called via the new keyword, whereas regular methods will be called on an existing object (or static methods on a class). Constructors must have the same name as the class.  A default constructor is created by the JVM if one is not coded. If a class is a subclass then the first line must be to call super() to initialise the variables etc. inherited from the parent class.

5. Does importing a package, e.g. import com.cjiq.*, also import it’s subpackages e.g. com.cjiq.hello.*

No, They must me imported explicitly.

6. Explain pass by reference and pass by value

In pass by reference, a reference to the object is passed around, not the actual object.

In pass by value, a copy of the actual object is passed around. This means it cannot be modified by the method it is passed to.

7. Is Java Pass by reference or pass by value?

Java is always pass by value. However, when passing an object to a method, the reference is passed by value.  This means the same object is referenced by both the method and the caller despite being pass by value.

8. Are Java variables initialised with default values?

Static and Instance variables are given default values (e.g. false, 0, and null for Objects.  Local variables are not, and attempting to access them before initialisation will result in a compiler error.

9. What is Autoboxing?

Autoboxing is the process of the JVM automatically converting between a primitive type and it’s companion Object equivalent, e.g. int and Integer, boolean and Boolean. We should be careful when relying on this as it can have huge performance impacts if not used correctly as well as causing some very subtle bugs.

10. What access modifiers can I have on a class?

On a top level class, only public or default. Marking a class private or protected will result in a compiler error.  Inner classes can be private and static.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Published at DZone with permission of Sam Atkinson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}