Java is the keystone for creating something new in the cybersphere. The computer language, which once written can be used anywhere without even recompiling, is one of the most popular computer programming language today. If you are aiming to become a Java programmer or a software developer, then you should prepare thoroughly for your interview. Java is the topic from where many important questions can be asked. In order to be successful in your interview, you should prepare the most common questions on Java that are given below.
Top 10 Java Interview Questions and Answers
1. Name the various access specifiers for Java Classes?
Ans: There are four kinds of access specifiers which act as a keyword used before a class name defining the access scopes:
- Public: Class, Method and Field can be accessed from anywhere.
- Protected: Method and Field can be accessed from the same class or the sub class, not from the outside.
- Default: Method, field and Class can be accessed from the same package, not outside of the native package.
- Private: Method and Field can be accessed from the same class to which they belong.
2. What are Static Methods and Static Variables?
Ans: When a Method or a variable is to be shared between multiple objects of a class, creating separate copies for each object, a static keyword is used to make the method and variable shared to all objects.
3. Mention the Differences between double variables and float variables.
Ans: Float variable is a Single Precision Floating-point decimal number and Double is a Double Precision Decimal Number. Float variables take 4 bytes in memory whereas double variables take 8 bytes.
4. How can you generate random numbers in Java?
Ans: Using Math.random() a random number can be generated within the range of greater than or equal to 0.1 and less than 1.0.
5. Can you use main() method in Java?
Ans: If we use main() in Java, then there is always a void return type result generated as it cannot generate any data.
6. What is the base class in Java from which all classes are derived?
7. What is a default switch case?
Ans: When no other switch condition is matched in a switch statement, you have to use the default case. It can be only declared if other switch cases have been coded.
8. What are Java Packages and also mention their significances?
Ans: In Java, a package is a collection of co related classes which are bundled together. By using a package, a developer can modularize and group the code for proper re-use.
9. How is an Object serialized in Java?
Ans: To convert an object into byte stream by serialization, an interface named ‘Serializable’ is to be implemented by the class.
10. What is the Final Keyword in Java? Give an example?
Ans: A constant is declared in Java by using the keyword Final. Value of a constant can only be added once which cannot be changed later.
E.g. A constant with the name const_val is declared and assigned with a value:
Private Final int const_val=100