Java Interview Questions for Beginners — 5

Common Interview Questions

Compilation will get successful.

clone(), equals(), finalize(), hashCode(), wait(), notify(), notifyAll(), toString()

Before discussing the relationship between both Equals and hashcode, we first what those methods actually do.

First they are the methods from Object class.

equals:

This method accepts an Object as argument. The default implementation of this method in Object class simply checks if two object references x and y refer to the same object. i.e. It checks if x == y. This comparison is called Shallow Comparison

hashCode:

This method returns hash code value for the object on which this method is invoked​. ​This method is widely used by Hashtable, HashMap, HashSet

There is a contract between two methods, ​​Equal objects must produce the same hash code as long as they are equal​. So when you override equals method, you should override hashcode method as well.

Autoboxing — Conversion of primitive to corresponding wrapper class automatically by Java compiler​

Unboxing — Conversion of wrapper to corresponding wrapper class automatically by Java compiler

Yes we can overload main method. Still the method public static void main(String arg[]) only will get called at Java execution startup.

Like String, static keyword has a number of uses in Java. Hence we expect all of you to go through complete detail about static keyword which will help you in any interview.

Please read the following article completely & carefully. You can get back to us anytime on any issue in understanding.

http://www.journaldev.com/1365/java-static-keyword-class-method-variable-block-import

http://www.jusfortechies.com/java/core-java/static-blocks.php

Final have different meaning depends on the context its been used.

In class : Final class cannot be inherited further..simply there can’t be any subclass for a final class. String is the example for final class in Java.

In Property: If a class property is declared as final, it will be treated as constant.. so value can’t be changes further (so needs a value while creating itself).

In Method: if method declared as final, it can’t be overridden.

Conditional operator (?:) is also called the ternary operator in Java. Since its accepting three operands, it’s called ternary.

It’s the shorthand for if-else statement.

If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result.

String data = ​​​state.contains(“A”) ? “​state contains ‘A’” : “​state doesn’t contain ‘A’”;

​In the above statement, ​state.contains(“A”) before ? is the condition.

If the condition is true, then String data will be stored with “state contains A” message (first value). If the condition is false, String data will contain “state doesn’t contains A message (second value)​.

instanceof operator is used to check whether the object is an instance of the specified type. It will return either true or false.

Break — terminates the loop execution & next statement to loop will get executed.

Continue — skips the execution of current iteration of loop (Not the loop itself)

Java objects we create will reside in the heap memory space. When Heap memory gets full, JVM runs the garbage collection operation (objects which are not referred for long time will be erased from memory, so that space can be used for other objects) .

Java memory management is a vast area & suggest all of you to go through oracle definition to get more idea about it.

https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html

The Just-In-Time (JIT) compiler is a component of the Java™ Runtime Environment that improves the performance of Java applications at run time.

The JIT compiler helps improve the performance of Java programs by compiling bytecode into native machine code at run time. The JIT compiler is enabled by default, and is activated when a Java method is called.

The JIT compiler compiles the bytecode of that method into native machine code, compiling it “just in time” to run.

Java object is created in memory & we can’t use it in later time once it is later erased from memory by Garbage collector.

Serialization is the way to store/persist the state of Object in binary format so we can recreate it later. A class should implement Serializable interface (Interface with no method or simply called marker interface) in order to use serialization.

​Java class which follows the following conditions are called Java bean classes

  • ​should have a no-arg constructor​, ​
  • ​should ​implement ​​Serializable​ interface, ​
  • ​should provide methods to ​getter and setter methods for properties

An interface with no methods is called Market Interface. e.g. Serializable, Clonnable are marker interfaces (no methods in those interfaces) is used to indicate something to compiler or JVM that the class implementing any of these would have some special behavior.

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.

Default imported package in all Java class is java.lang.* To use a package, we use the keyword import.

If we use return statement in try block, finally will surely works.

If we use System.exit(0) in try block, finally won’t work

Other Stories

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store