Java Interview Questions - Basics
1. Does garbage collection guarantee that a program will not run out of memory?
Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.
2. What is JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class file.
3. Is JVM platform independent?
JVM’s are not platform independent. JVM’s are platform specific run time implementation provided by the vendor.
4. What is the difference between JDK and JVM?
JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.
5. What is the difference between JDK and JRE?
It is an implementation of the Java Virtual Machine* which actually executes Java programs. It is a bundle of software that you can use to develop Java based applications. Java Runtime Environment is a plug-in needed for running java programs.
6. What is class loader?
The Java Class loader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of class loaders.
7. What is JIT and its use?
Using the java just in time compiler (really a second compiler) at the particular system platform complies the byte code into particular system code, once the code has been re-compiled by the JIT complier, it will usually run more quickly in the computer.
8. Does Java allow Default Arguments from command line?
No Java doesn‟t supports any default arguments from command line arguments.
9. Is Java a pure object oriented language?
Java is not purely object-oriented.
10. What environment variables do I need to set on my machine in order to be able to run Java programs?
CLASSPATH and PATH are the two variables.
11. Where import statement is used in a Java program?
Import keyword is used to import a package in java.
12. What is an enumeration?
The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.
13. Which object oriented Concept is achieved by using overloading and overriding?
Polymorphism is an OOPs concept which supports overloading & overriding.
14. What is Unicode in java?
The evolution of Java was the time when the Unicode standards had been defined for very smaller character set. Java was designed for using Unicode Transformed Format (UTF)-16, when the UTF-16 was designed. The „char‟ data type in Java originally used for representing 16-bit Unicode. Hence Java uses Unicode standard.
15. Where did the Java name come from? What does it stand for?
“Java” is supposed to have got its name from one of the members of the original developer team Kim Polese. But it is widely believed that Java (originally called “Oak”) was conceived by the original creators at a local coffee bar.
16. What if I write static public void main instead of public static void main?
Yes possible, there is no difference between public static void main and static public void main.
17. Can an application have multiple classes having main method?
Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main () method.
18. What does it mean that a method or field is “static”?
Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That’s how library methods like System.out.println() work. out is a static field in the java.lang.System class.
19. Why Java does not support pointers?
Pointers are the notorious source of bugs, it makes the program less secured. So, java doesn’t support pointers directly.
20. Should a main() method be compulsorily declared in all java classes?
No not required. main() method should be defined only if the source class is a java application.
21. What is the return type of the main() method?
Main () method doesn’t return anything hence declared void.
- What is the argument of main () method?
Main () method accepts an array of String object as argument.
23. I want to print “Hello” even before main () is executed. How will you achieve that?
Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main () method. And it will be executed only once.
24. What is OOPS?
OOPS is nothing but collection of concepts, if a language supports that concepts then that language known as Object Oriented Language.
25. What is classpath?
Classpath is a parameter set either on the command-line, or through an environment variable that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.
26. What is path?
The PATH is the system variable that your operating system uses to locate needed executables from the command line or Terminal window. The PATH system variable can be set using System Utility in control panel on Windows, or in your shell’s startup file on Linux and Solaris.
27. Why java is secured?
Java Language Security and Byte code Verification. The Java language is designed to be type-safe and easy to use. It provides automatic memory management, garbage collection, and range-checking on arrays.
28. What is Java Beans?
According to Java Soft, “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
29. What is volatile and non-persistent program?
A volatile program is one whose result always store in main memory of the computer i.e. RAM.
30. What is nonvolatile and persistent program?
A non-volatile program is one whose result is stored in secondary storage devices i.e. hard disk, magnetic tapes etc.
31. Why main () method bound to be public in java?
As main () called by JVM which in-built to operating System, If a method called from outside the package, it should be declared as public in java. So main () method public in java.
32. What is use of javap?
The javap command disassembles one or more class files. Its output depends on the options used. If no options are used, javap prints out the package, protected, and public fields and methods of the classes passed to it. javap prints its output to stdout.
33. Which one is responsible for execute .class file?
Java Virtual Machine (JVM).
34. Who responsible for creating standalone application?
J2SE (Java 2 Standard Edition)
35. State the slogan of java? Ans:
WORA (Write Once Runs Anywhere)
36. Who responsible for creating distributed application?
Multithreading responsible for creating distributed application?
37. What is the difference between final, finally and finalize?
Final is used for variable, class or method. But finally is used in case of exception and finalize is used for garbage collection.