These Java questions have been designed for various interviews, competitive exams and entrance tests. We have covered questions on both basic and advanced concepts which will help you improve your skills to face interview questions on Java.
Who is this Java interview questions designed for?
All the developers, programmers and software engineers will find these questions extremely useful. All freshers, BCA, BE, BTech, MCA and college students wanting to make a career in Software Development will be highly benefited by these questions.
Java interview questions topics
This section covers Java topics like - access specifiers, static methods, static variables, checked exceptions, inner class, sub-class, final, packages, abstract class, interface, serialization, garbage collection etc.
1. What are the various access specifiers for Java classes?
The access modifiers in java specify accessibility (scope) of a data member, method, constructor or class. There are 4 types of java access modifiers:
Public: Class, Method, Field are accessible from anywhere.
Protected: The protected access modifier is accessible within package and outside the package but through inheritance only.
Default: If you don't use any modifier, it is treated as default by default. The default modifier is accessible only within package.
Private: Methods and Fields are accessible only within class
Video : Java Interview Questions and Answers for Freshers
2. What is the purpose of static methods and static variables?
We use static keyword to make a method or variable shared for all objects. When we want to have a single copy of a variable or method to be shared among all the instances of the class, we create static methods and static variables.
3. What do you mean by Checked Exceptions?
A checked exception is a subclass of Exception. A Compiler checks to see if these exceptions have been properly caught or not else the code doesn't compile.
Thus, a programmer is forced to deal with the situations where an exception can be thrown. Checked exceptions must be either declared or caught at compile time.
4. Explain the difference between an Inner Class and a Sub-Class.
An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.
5. What is a singleton class?
A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. The purpose of singleton is to control the object creation, limiting the number of the objects to one only.
6. What is Final Keyword in Java?
In java, Final Keyword is used to define an entity that can only be assigned once.
Following is what happens when it is applied on classes, variables or methods:
final classes - A final class cannot have subclasses.
final variables - Once initialized, a final variable cannot be changed.
final Methods - A final method cannot be overridden by subclasses.
7. What are Java Packages?
A package is a collection of classes and interfaces which are kept together as they are related to each other.
It helps modularizing the code and group the code for proper re-use.