Basic Java Interview Questions

Q1. What is Java?
Ans1. Java is a general purpose object oriented programming language. It was developed by sun Microsystems in 1991.

Q2. What was the first name of Java?
Ans2. First name is oak which was renamed to java in 1995 due to some copyright issues.

Q3. What are main features of java language?
Ans3. Main features of java are.
  • Compiled and Interpreted
  • Platform Independent and Portable
  • Robust and Secure
  • High Performance

 Q4. What is JIT?
Ans4. JIT is an acronym of Just In Time Compiler. It compiles the part of the byte code that have the similar functionality at the same time and hence reduces the amount of time need for compilation.

Q5. Explain Static variable in Java?
Ans5. It refers to the common property of all the objects of the class
  • It is a variable which belongs to the class and not to the object (instance)
  • Static variables are initialized only once.
  • A static variable can be accessed directly by the class name.
  • A single copy to be stored by all the instance of the class.
Q6. Explain Static Method in Java?
Ans6. Static Method:
  • It is a method which belongs to the class and not to the object.
  • It can call only other static Method.
  • It can access only static data.
  • It accessed directly by the class name
Q7. Why Java is called Write once and run anywhere?
Ans7. Java is write once run anywhere because of byte code. The java program compiles to byte code which is the intermediate code between source code and machine code and it is platform independent.

Q8. Why main method is static?
Ans8. Main method is static because object is not required to call static method.

Q9. Define this in java?
Ans9. “this” is a keyword in java mainly it is a reference variable that refer to the current object.

Q10. Define Garbage Collection in Java?
Ans10. The JVM heap stores all objects created by an executing java program. Objects are created by java by the new operator and allocate a memory in the heap at runtime. Garbage Collection is the process of automatically freeing objects that are no longer referenced by the program.

Q11. What happen if main method is defined as private?
Ans11. The program compiles properly but at runtime it will give “Main Method not public” message.

Q12. How we call garbage collection explicitly?
Ans12. System.gc()

Q13. What is a resource bundle?
Ans13. In its simplest form, a resource bundle is represented by a text file containing keys and a text value for each key.

Q14. What is transient variable?
Ans14. Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.

Q15. Why java does not have multiple inheritance?
Ans15. The Java design team strove to make Java:
• Simple, object oriented, and familiar
• Robust and secure
• Architecture neutral and portable
• High performance
• Interpreted, threaded, and dynamic
The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple). In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.

Q16. What is Collection API?
Ans16. The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces. Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap. Example of interfaces: Collection, Set, List and Map.

Q17. Difference between Integer and int?
Ans17. Integer is a class where as int is a primitive data type.

Q18. Difference between String and StringBuffer?
Ans18. String class supports constant strings where as string buffer class supports growable and modifiable string.

Q19. What is difference between Path and Classpath?
Ans19. Path and Classpath are operating system level environment variables. Path is used define where the
system can find the executables(.exe) files and classpath is used to specify the location .class files. 

Q20. What is the arguement of main method?
Ans20. main method accepts an array of String object as arguement.


Q21. What is a JVM?
Ans21. JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

Q22.Does Java support multiple inheritance?
Ans22. Java doesn't support multiple inheritance.

Q23. What are local variables?
Ans23. Local variables are those variables which are declared within a block of code like methods. Local variables should be initialized before accessing them.

Q24. What are instance variables?
Ans24. Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

Q25. How to define a constant variable in Java?
Ans25. The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also. static final int PI = 2.14; is an example for constant.

Q26. Should a main method be compulsorily declared in all java classes?
Ans26. No not required. main method should be defined only if the source class is a java application.

Q27. What is the return type of the main method?
Ans27. Main method doesn't return anything hence declared void.

Q28. Why is the main method declared static?
Ans28. main method is called by the JVM even before the instantiation of the class hence it is declared as static.



Post a Comment

0 Comments