Java Interview Questions And Answers

Interview is the most difficult part of getting job. They can ask any technical questions. You must be aware of all the basic concepts that are related to a programming language. Here, we will learn basic questions of Java Programming Language asked frequently in interview. You can comment at the bottom of this page and tell us what was asked in your interview.


Java Interview Questions List

1. Why is JAVA called as architectural neutral ?

=> The compiler generates an architecture neutral object file format - the compiled code is executable on many processors, given the presence of Java runtime system. The Java compiler does this by generating bytecode instructions which have nothing to do with a particular computer architecture.In java an int will always be an int of 32 bits but in C++ it depends on architecture it can be both 32 bit and 16 bits in older systems. It is interesting to note that JVM itself depends on architecture of the system but it not the byte code

2. Why java uses unicode encoded sytem ?

=> Before unicode , there were many different standards : ASCII in USA , KOI-8 in Russia. This causes two problems : - a particular code value corresponds to different letters in various coded schemes. Encodings for languages with large character sets have variable length. So, a fixed 2-byte width code was more than sufficient to encode all characters used in all languages in the world.

3. How Java provide high performance ?

=>  Java uses just-in-time compiler which checks which code has executed frequently and optimise just that code for speed.

4. What are the features of Java programming language ?

=>  It is Simple , Object Oriented , Network Savvy , Robust , Secure , Architecture neutral , Portable , Interpreted , High performance , Multithreaded , Dynamic.

5. Does Java uses call by reference for objects ?

=> No, Java does not use call by reference for objects. Object references are passed by value.

6. Why you cannot access instance fields from static method ?

=>  Because static methods do not operate on objects. You can only access the static fields of the class by static methods.

7. Is Java compeletely object-oriented programming language ?

=> No , Since it uses primitive data types (numbers , characters , and boolean values ) which are not objects.

8. Can we declare return type of main method other than the void ?

=> It will compile successfully if you declare return type other than void but will give runtime - "Error: Main method must return a value of type void in class yourClassName, please define the main method as: public static void main(String[] args) ". Java language specification says that on "Execution- Virtual Machine Start up(12.1.4)  main method must be declared as public static void and must accept single argument of array of String.

9. Of which Class the equals Method is ?

=> Object class. It is used to tests whether one object is considered equal to another.

10. Can we declare the abstract class as final ?

=> No, you can declare class either abstract or final, but not both.We know that abstract class cannot be instantiated and a class declared as final will not let itself derived.We have to inherit the abstract class by the class which will implements all its missing functionality and will access all its method to execute them.

11. Name two classes that are declared as final in Java API ?

=> String and Math -> java.lang.String and java.lang.Math

12. In what ways final keyword is used in Java ?

=> final is used with variable to declare it as a constant.
final is used with method to stop it from being overriden by methods from subclasses.
final is used with class to stop it from being inherited by other classes.

13. What is the difference between StringBuffer and StringBuilder and String class ?

=> All three classes provide similar functionality, but hava different characteristics and uses
- String class is final and immutable i.e. once it is created, its content cannot be changed and a new object is created to address that change. It is stored in "String-pool".
- StringBuilder and String Buffer both are mutable i.e. the same object is modified, no new object is created.
- String Buffer is thread safe (synchronized) whereas String Builder is not thread safe(non-synchronized).
- String Builder is faster than String Buffer (only slightly due to the point above)

14. What is Connection Pooling in Java ?

=> Another common interview questions in Java is from JDBC connection pooling. In Java, we need connection to communicate with the database(MySQL,Oracle,SQL Server etc) for every operation. Creating a new Connection object is an expensive operation and affect system performance. Connection pooling in a concept says that few connections should be made either on startup or lazily as per need basis and stored in connection pool. Whenever any communication with database is needed, take a connection from connection pool and return it back to connection pool when the operation is completed. This reduces cost of creating and destroying new connection everytime. "pool-size" defines tota number of connections inside the pool. Singleton object is usually made to access connection pool

15. What is difference between 7 and 7L ? or What is difference between 8.5 and 8.5f ?

=> The value that we provide from our end are called Literals. 7 is an int type literal and 7L is a long type literal. Similarly 8.5f is a float literal whereas 8.5 is of type double Click to know more