Java Interview Questions and Answers – Part 3 1


Java Interview Questions and Answers
1. What is JIT Compiler ?

JIT compiler coverts a byte code of one operating system to the current operating system’s executable code. This instruction is given by JVM of the current system. The byte is compiled into native platform code. The execution speed is greatly improved by this process, because the platform is directly executing the native byte code.

2. What is the difference between the String and StringBuffer classes?

String class is immutable. The characters of string objects can not be changed.This is because the String class is declared as final due to which it cannot be inherited and so its functions cannot be overridden.

StringBuffer is mutable. The characters of StringBuffer objects can be modified. It has similar functionality of String class and can manipulate the string objects.

StringBuffer’s performance is faster than String when concatenating other strings. String class concatenates the other string by using + sign. After concatenation, it returns another String object. This string object can be reassigned to the original String reference.

Ex : str = str + “World!” ; // where str=”Hello”

The concatenation creates a temporary String object, and is reassigned to str.
Using StringBuffer, the concatenation is done by using append() method.

Ex : str.append(“World!”); // str is a StringBuffer object and assigned “Hello”.

String class provides the concatenation facility with concat() method. But it creates a new String object and returns it. Where as the StringBuffer just appends or adds the new string to the existing StringBuffer object. This process is pretty quicker than concatenation using + or concat() method of String class.

3. What is the difference between a break statement and a continue statement?

A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

4. Can an anonymous class be declared as implementing an interface and extending a class?

Partly Yes. An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

5. What are the legal operands of the instanceof operator?

The left operand is an object reference or null value and the right operand is a class, interface, or array type.

6. Can a method be overloaded based on different return type but same argument type ?

No, because the methods can be called without using their return type in which case there is ambiguity for the compiler.

7. What is constructor chaining and how is it achieved in Java ?

A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.

8. If we have defined only one constructor with ClassName(arg1) then can we access default constructor without any arguments.

No. We can’t make a call to new ClassName(). But we can create objects by passing arguments to constructor call. If we need to use default constructor, we must provide default constructor in the class. 

9. Is it mandatory to provide constructor in each class to instantiate its objects ?

No. It is not mandatory to provide constructor definition, if we do not provide any such constructors, java provides constructors automatically with the default values for all fields. But if we provide at least one constructor of our own, then we must provide default constructor to instantiate it with no arguments. Otherwise Java will not provide default constructor.

10. Can a for statement loop indefinitely?

Yes, a for statement can loop indefinitely. For example, consider the following: for(;;);

11. What are static methods?

Static methods are the methods, which are declared with the keyword as static. These methods are the modifiers for the class methods. They are used to affect the entire class not the instance of it. These methods are always invoked without reference to a particular instance of a class.

The restrictions that have been imposed are as follows:

  • A static method can only call and access other static methods and data respectively.
  • A static method cannot reference to the current object using keywords “super” or “this”.
12. What is method overloading?

Method overloading is a type of polymorphism that includes two or more methods with the same name in same class but, the condition is that it should have different arguments, otherwise an error might occur. This method overloading is very advantageous, as it allows us to implement many methods with the same syntax and semantics.

The Overloaded methods that should follow the criteria are as follows:

  • Overloaded methods can change the return type and access modifier
  • Overloaded methods can declare new or broader checked exceptions
  • A method can be overloaded in the same class or in a subclass
13. What is method overriding?

Method overriding is a type of polymorphism that is different from the overloading method, as it allows you to declare the method with the same arguments. The advantage of using this is that it defines the behavior of the specific subclass.

It doesn’t provide very strict restrictive access modifier. The method that marked as public and protected can’t be overridden. We also cannot override a method marked final and static.

14. What is super()?

super() keyword is used to access the method and member variables of the super-class. It can refer the member or the hidden variable of the super-class. It can also invoke the overridden method. super() should be used to access the hidden variable and it should be the first keyword written in the constructor.


Profile photo of Siva

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Java Interview Questions and Answers – Part 3

  • Sasidhar

    Dear Siva ,

     

    Thanks for the good stuff, I think for the question 13. What is method overriding?

    specified The method that marked as public and protected can’t be overridden.

     

    I think You have to remove can’t word. Since we can override those access specifier methods.


Review Comments
default image

I am a plsql developer. Intrested to move into bigdata.

Neetika Singh ITA Hadoop in Dec/2016 December 22, 2016

.