Java Interview Questions for Beginners

1. Why Java?

We use Java programming everywhere. We use Java to create standalone programs, web applications, and web services. We           can create distributed enterprise applications using Java EE frameworks.

2. What is difference between JavaScript and Java?

Some of the key differences between JavaScript and Java are:

  • Java is Object Oriented Programming Language. But, JavaScript is an Object Oriented Scripting language.
  • Java code runs in a virtual machine or browser (Applets) where JavaScript code runs on browser.
  • We have to compile Java source code to byte code before JVM can understand and execute it. JavaScript code is text based and we don’t need to compile it.
  • We use JavaScript to perform browser specific tasks. We use Java to create standalone utility apps, web applications, and web services.
  • JavaScript is lightweight whereas we have to install Java and configure it to run.

3. Which is better – Java or Python?

It’s like comparing Apples to Oranges. Both Java and Python are very popular programming languages. It depends on the project whether you want to use Java or Python or sometimes both together.

Here are some inputs based on my personal experience working with both Java and Python for a long time.

  • We prefer Java over Python to create web services or web applications. Java EE APIs and frameworks are robust and secure. Python frameworks such as Flask and Django are still evolving.
  • We prefer Python over Java to create a utility script because it’s more lightweight than Java.
  • We prefer Python for Data Science, Machine Learning, and Artificial Intelligence applications. It’s because most of the popular APIs in these areas are Python-based.

4. Why Java Doesn’t support multiple inheritance and Composition vs Inheritance

Because interfaces specify only what the class is doing, not how it is doing it. The problem with multiple inheritance is that two classes may define different ways of doing the same thing, and the subclass can’t choose which one to pick.

5.What is JVM? Why is Java called the “Platform Independent Programming Language”?

A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible, because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.

6.What is the Difference between JDK and JRE?

  • The Java Runtime Environment (JRE) is basically the Java Virtual Machine (JVM) where your Java programs are being executed. It also includes browser plugins for applet execution.
  • The Java Development Kit (JDK) is the full featured Software Development Kit for Java, including the JRE, the compilers and tools (like JavaDoc, and Java Debugger), in order for a user to develop, compile and execute Java applications.

7.What does System.gc() and Runtime.gc() methods do?

These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.

8.What is reflection and why is it useful?

The name reflection is used to describe code which is able to inspect other code in the same system (or itself) and to make modifications at runtime.

For example, say you have an object of an unknown type in Java, and you would like to call a ‘doSomething’ method on it if one exists. Java’s static typing system isn’t really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called ‘doSomething’ and then call it if you want to.

Method method = foo.getClass().getMethod("doSomething", null);

method.invoke(foo, null);

9.Compare the sleep() and wait() methods in Java?

  • sleep() is a blocking operation that keeps a hold on the monitor / lock of the shared object for the specified number of milliseconds.
  • wait(), on the other hand, simply pauses the thread until either (a) the specified number of milliseconds have elapsed or (b) it receives a desired notification from another thread (whichever is first), without keeping a hold on the monitor/lock of the shared object.

sleep() is most commonly used for polling, or to check for certain results, at a regular interval. wait() is generally used in multithreaded applications, in conjunction with notify() / notifyAll(), to achieve synchronization and avoid race conditions.

10.Is Java “pass-by-reference” or “pass-by-value”?

Java is always pass-by-value. Unfortunately, when we pass the value of an object, we are passing the reference to it. There is no such thing as “pass-by-reference” in Java. This is confusing to beginners.

The key to understanding this is that something like

Dog myDog;

is not a Dog; it’s actually a pointer to a Dog.

So when you have

Dog myDog = new Dog("Rover");foo(myDog);

you’re essentially passing the address of the created Dog object to the foo method.

11.What is difference between fail-fast and fail-safe?

 The Iterator’s fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification in the collection. All the collection classes in java.util package are fail-fast, while the collection classes in java.util.concurrent are fail-safe. Fail-fast iterators throw a ConcurrentModificationException, while fail-safe iterator never throws such an exception.

12.What is structure of Java Heap?

The JVM has a heap that is the runtime data area from which memory for all class instances and arrays is allocated. It is created at the JVM start-up. Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. Heap memory consists of live and dead objects. Live objects are accessible by the application and will not be a subject of garbage collection. Dead objects are those which will never be accessible by the application, but have not been collected by the garbage collector yet. Such objects occupy the heap memory space until they are eventually collected by the garbage collector.

12. What is JIT?

The JIT is the JVM’s mechanism by which it can optimise code at runtime.

JIT means Just In Time. It is a central feature of any JVM. Among other optimisations, it can perform code inlining, lock coarsening or lock eliding, escape analysis etc.

The main benefit of the JIT is on the programmer’s side: code should be written so that it just works; if the code can be optimised at runtime, more often than not, the JIT will find a way.

13.What is the volatile keyword useful for?

volatile has semantics for memory visibility. Basically, the value of a volatile field becomes visible to all readers (other threads in particular) after a write operation completes on it. Without volatile, readers could see some non-updated value.

14.How can Hash Functions be used to perform lookups?

Searching through large quantities of data is a very resource-intensive process. Imagine you’ve got a table listing every inhabitant of a big city, with lots of different fields for each entry (first name, second name, address, etc.). Finding just one term would be very time-consuming and require a lot of computing power. To simplify the process, each entry in the table can be converted into a unique hash value. The search term is then converted to a hash value. This limits the number of letters, digits and symbols that have to be compared, which is much more efficient than searching every field that exists in the data table.

15.What is Hash Collision?

There’s always a chance that two different inputs for hash function will generate the same hash value. This is known as a hash collision. If a collision happens, you just compare the actual objects you are hashing to see if they match. What is the probability of a hash collision? This question is just a general form of the birthday problem from mathematics:

Given k randomly generated values, where each value is a non-negative integer less than N, what is the probability that at least two of them are equal?

Take the well-known hash function CRC32, for example. If you feed this function the two strings “plumless” and “buckeroo”, it generates the same value. This is known as a hash collision.

Assuming your hash values are 32-bit, 64-bit or 160-bit, the following table contains a range of small probabilities:

16.What is MD5?

MD5 is a so-called cryptographic hash function. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities. It can still be used as a checksum to verify data integrity, but only against unintentional corruption.

This basically means that you can give in any bitstring as input for the function, and you will get out a fixed-size bitstring (128-bit in the case of MD5) as output. The output is usually called “digest”.

The digest depends solely on the input and nothing else. Thus in itself it can be used as an integrity proof, but not as authenticity, if the underlying hash function has the necessary properties . This means that for two different outputs the digest itself should be also different. The problem is that the digest’s size is fixed, which in turn means that with sufficient number of messages it will always be possible to find a collision (i.e., two different inputs yielding the same output).

One should also note that there is nowadays no justification to use MD5, as weaknesses have been discovered (namely post-fix collision attacks). Also using SHA-256/512 on modern hardware is usually faster then MD5.

Popular posts from this blog

How to change column name in Dataframe and selection of few columns in Dataframe using Pyspark with example

What is Garbage collection in Spark and its impact and resolution

Window function in PySpark with Joins example using 2 Dataframes (inner join)