Hey everyone,

In this post I am going to share some valuable Core Java Interview Questions..

These questions are not only good for practicing for your technical interviews, they are also good for improving your Java knowledge or remembering the details of the language. This post will be updated regularly so please do check it from time to time. Here are the questions:

1 - What does the following Java program print?

The Double.MIN_VALUE is 2^(-1074), a double constant whose magnitude is the least among all double values. So unlike the obvious answer, this program will print 0.0 because Double.MIN_VALUE is greater than 0.
2 - What gives Java its 'write once and run anywhere' nature?
The Bytecode
3 - Can a constructor be final?
No, constructors cannot be final.
4 - What's the difference between an interface and an abstract class?
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
5 - What's the difference between constructors and other methods?
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
6 - Can you call one constructor from another if a class has multiple constructors?
Yes. Use this() syntax.
7 - What is method overriding?
If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for run-time polymorphism and to provide the specific implementation of the method.
8 - What are the field/method access levels (specifiers) and class access levels?
Each field and method has an access level: • private: accessible only in this class • (package): accessible only in this package • protected: accessible only in this package and in all subclasses of this class • public: accessible everywhere this class is available Similarly, each class has one of two possible access levels: • (package): class objects can only be declared and manipulated by code in this package • public: class objects can be declared and manipulated by code in any package
9 - Why would you use a synchronized block vs. synchronized method?
Synchronized blocks place locks for shorter periods than synchronized methods.
10 - How can you force garbage collection?
You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.
11 - 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. For example: Object a; Customer b; b = (Customer) a; When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.
12 - What is the output of the following program?

Static method called
We can call static methods using reference variable which is pointing to null because static methods are class level so we can either call using class name and reference variable which is pointing to null.
13 - 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. Following table lists the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void
14 - What's the difference between the methods sleep() and wait()?
Thread.sleep() causes the current thread to suspend execution for a specified period. However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS.
Object.wait() causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

"The Thread.sleep method is not guaranteed to be precise so it may not be exactly the specified duration. Also, sleep can end early if the thread is interrupted. I think it would be more correct to say sleep suspends execution at least the specified time unless interrupted and wait suspends up to the specified time." Brooks Hagenow
15 - What's the main difference between a Vector and an ArrayList?
Java Vector class is internally synchronized and ArrayList is not.
16 - Can we write a Java class that could be used both as an applet as well as an application?
Yes. We can add a main() method to the applet.
17 - What would you use to compare two String variables? The operator "==" or the method "equals()"?
I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.
18 - What is the output of the following program?

256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. This caching functionality can be seen by looking at the inner class, IntegerCache, which is found in Integer class. So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned. Otherwise it will be different (for 128).
19 - Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.
20 - Can an inner class be declared inside of a method access local variables of this method?
It's possible if these variables are final.
21 - What can go wrong if you replace && with & in the following code: String a=null; if (a!=null && a.length()>10) {...}
A single ampersand here would lead to a NullPointerException.
22 - What's the difference between a queue and a stack?
Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.
23 - For concatenation of strings, which method is good, StringBuffer or String?
StringBuffer is faster and more memory efficient than String for concatenation.
24 - How can a subclass call a method or a constructor defined in a superclass?
Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.
25 - What is a collection?
A collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs Eg. HashMap and Hashtable.
26 - You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?
Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.
27 - What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.
28 - What is the difference between Collection and Collections?
Collection is an interface whereas Collections is a class. Collection interface provides normal functionality of data structure to List, Set and Queue. But, Collections class is to sort and synchronize collection elements.
29 - What is the output of the following code?

The given print statement will throw java.lang.NullPointerException because while evaluating the OR logical operator it will first evaluate both the literals and since str is null, .equals() method will throw exception. It's always advisable to use short circuit logical operators i.e "||" and "&&" which evaluates the literals values from left and since the first literal would return true, it would skip the second literal evaluation and the result would be true in that case.
30 - What is deadlock? When does it occur?
Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
31 - If you're overriding the method equals() of an object, which other method you might also consider?
hashCode() method.
32 - You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?
33 - How would you make a copy of an entire Java object with its state?
Have this class implement Cloneable interface and call its method clone().
34 - What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?
You do not need to specify any access level, and Java will use a default package access level.
35 - When do you declare a method as an abstract method?
When i want child class to implement the behavior of the method.
36 - Can I call an abstract method from a non abstract method?
Yes, We can call a abstract method from a Non abstract method in a Java abstract class.
37 - What is the difference between checked and Unchecked Exceptions in Java?
Checked exceptions must be caught using try .. catch() block or we should be declared using 'throws' clause. If you don't, compilation of the program will fail. However unchecked exceptions let the program compile but might cause a run-time exception after compilation.
38 - What are the uses of Serialization?
In some types of applications you have to write the code to serialize objects, but in many cases serialization is performed behind the scenes by various server-side containers. These are some of the typical uses of serialization:
• To persist data for future use.
• To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
• To "flatten" an object into array of bytes in memory.
• To exchange data between applets and servlets.
• To store user session in Web applications.
• To activate/passivate enterprise java beans.
• To send objects between the servers in a cluster.
39 - What is the Runnable interface ? Are there any other ways to make a java program multi-threaded ?
There are two ways to create new kinds of threads:
- Define a new class that extends the Thread class
- Define a new class that implements the Runnable interface, and pass an object of that class to a Thread's constructor.
- An advantage of the second approach is that the new class can be a subclass of any class, not just of the Thread class.
40 - Is the following code valid in Java? Is it an example of method overloading or overriding?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. In this example, method overloading is not possible by changing the return type of the method since ambiguity might occur in the program.

You have reached the end of the interview questions! These questions are updated regularly. Please leave a comment below if you have any questions or comments! Happy Coding!

Software Developer, Codemio Admin

Disqus Comments Loading..