Java Interview Questions For Freshers
Java Interview Questions For Freshers
In this tutorial, we are going to discuss the interview questions for the freshers'
candidates. These interview questions are helpful in cracking the technical rounds of
the Java interviews.
JRE (Java Runtime Environment): JRE is the runtime environment in which bytecode
can be executed. It does the implementation of the JVM (Java Virtual Machine) and
facilitates all of the class libraries and other support files that JVM uses at runtime. So
JRE is the software package that fulfills the requirement of what is required in order to
run/execute a Java program.
JDK(Java Development Kit): It is the tool that is required for the compilation,
documentation, and packaging of Java programs. The JDK completely includes JRE,
which contains tools for Java programmers. The Java Development Kit is provided free
of charge. Along with JRE, it includes an interpreter/loader, a compiler (javac), an
archiver (jar), a documentation generator (Javadoc), and other tools needed in Java
development. In short, it contains JRE + development tools.
static: It is a Java keyword that denotes a class-based object as one that may be
accessed without first generating an instance of a Class. We use static because we want
the main() method to run without any instances as well.
void: It is the return type of the main method. Void states that the main() method will
not return any value.
main: It is the name of the method, which is main() in our case. It is the first method
executed by JVM.
3) Define class and object. Explain them with an example using
Java.
Class: A user-defined class serves as a template or prototype from which objects can
be built. It stands for the collection of attributes or operations that are shared by all
objects of a particular type. In general, these elements can be found in the following
sequence in class declarations.
Identity: It provides a thing with a special name and makes it possible for one object
to communicate with another.
1. class Student
2. {
3. // instance variables declaration
4. // Methods definition
5. }
6. An object of a Student is a specific student
7. Student studObj = new Student ();
Signatures of methods: The method's name, return type, and the number of
parameters comprise the method signature.
A method can have the following elements in its signature:
ADVERTISEMENT
ADVERTISEMENT
With or without parameters - (int number, String name); (parenthesis are mandatory)
1. class TestClass
2. {
3. void m1()
4. {
5. }
6. public double m2(double y)
7. {
8. }
9. public static void m3()
10. {
11.
12. }
13. public static void m4(String y)
14. {
15.
16. }
17. }
1. class Student
2. {
3. int rollNo; // instance variable
4. String studName; // instance variable
5. int std; // 1, 2, 3, etc. // instance variable
6. String address;
7. static String schoolName; // class variable
8. }
When a class has no other parents, the default constructor invokes the class parent's
no-argument constructor (because it only has one statement, super()); or the Object
class constructor (since the Object class is either a direct or indirect parent of all
classes).
1. Parameterized constructor
2. Default constructor
These variables are declared in a manner similar to instance variables, with the
exception that static variables are declared in a class outside of any method
constructor or block using the static keyword.
No of how many objects we generate, we are only allowed to have one instance of a
static variable per class.
Static variables are generated at the beginning of program execution and immediately
removed at its conclusion.
We don't need to build an object of that class in order to access static variables.
Static techniques: You can call a static method without constructing any objects. The
method may be accessed simply by typing in the Class name. Only static variables, not
local or global non-static variables, may be accessed using the static method.
Filename: ClassMain.java
1. class MethodStatic
2. {
3. public static void displayMe()
4. {
5. System.out.println("Static Method can be accessed directly using the class na
me!");
6. }
7. }
8. public class ClassMain
9. {
10. public static void main(String args[])
11. {
12. MethodStatic.displayMe();
13. }
14. }
Output:
ADVERTISEMENT
o Local Variables
o Instance Variables
o Static Variables
Filename: LocVariable.java
Output:
Explanation: In the above program, the locVar variable is the local variable to the
method getLocalVariableValue(). If we use the variable locVar outside the
getLocalVariableValue() method, the compiler produces an error. The following
program shows the same.
Filename: LocVariable1.java
Output:
^
symbol: variable locVar
location: class LocVariable1
1 error
Instance Variables: Non-static variables are instance variables. These variables are
declared in a class outside any constructor, method, or block. Since the instance
variables are declared inside a class, these variables come into the picture when the
class is instantiated. When the objects of the class are destroyed, these variables are
also destroyed. Unlike the local variables, we can apply the access specifiers to the
instance variables. When one does not apply the access specifier, the default access
specifier is used.
Filename: VariableInstance.java
Output:
o Static variables are also declared in a similar fashion as the instance variables.
The difference is that instance variables do not come with the static keyword. In
contrast, the static variables are declared with the static keyword inside a class
and outside any block or constructor.
o Unlike the instance variables, there is only one copy of the static variables, no
matter how many objects we create of that class.
o Static variables are created at the beginning of the program execution and get
automatically destroyed when the execution ends.
To access the static variables, one does not need to create an object of that class. The
static variables can be accessed using the class names. Static keyword plays a key role
in the implementation of the singleton design pattern. The illustration of it is shown in
the following program.
Filename: VarStatic.java
Output:
In the above-mentioned program, stVarOb1 and stVarOb2 share the same instance of
the static variable cnt. Therefore, if the value gets incremented by any one of the
objects, the incremented value gets reflected on both of the
objects stVarObj1 and stVarObj2. It is because only copies of the static variables are
available.
HashTable is synchronized, and hence, it is thread-safe and can easily be shared with
many threads. On the other hand, the HashMap is not synchronized and is not thread-
safe, and hence, can not be shared (without any synchronization code) among many
threads.
HashTable does not allow any null value or key, whereas HashMap allows multiple null
values and one null key.
Filename: OverloadMain.java
Output:
o Sorting
o Searching
o Insertion
o Manipulation
o Deletion
A group of objects is called a collection. All interfaces and classes for the collection are
available in the java.util package.
o Single-level inheritance
o Multiple Inheritance (Using the Java Interfaces)
o Multi-level inheritance
o Hybrid Inheritance
o Hierarchical Inheritance
Constituents - Interface can only contain constants, whereas an abstract class contains
instance variables.
Methods Types - An abstract class has both: non-abstract as well as abstract methods.
On the other hand, in the case of interfaces, we see only abstract methods.
New - In the life cycle of a thread, it is the first state. The instance of the thread is
created, and the method start() is not invoked yet. Now, the thread is considered to be
alive.
Runnable - After calling the method start(), but before calling the method run(), a
thread is said to be in the state of runnable. A thread may also go back to the runnable
state from the sleeping or waiting state.
Running - When the thread enters the state of running after the method run() is
invoked. It is when the execution of the thread starts.
Non-Runnable - Even though the thread is alive, it is not apt to run. Typically, it goes
back to the state of runnable after some time.
Terminated - When the method run() does the completion of its execution, the thread
enters the terminated state. Now, the thread is not alive now.
o It is used to keep the local variables and the order of execution of methods.
o When the memory of the stack gets filled then it returns the
java.lan.StackOverFlowError.
o Accessing the stack memory is faster than accessing the memory as compared
to the heap.
o Methods that are currently running utilize the stack space.
Heap Memory:
23) Can one modify the throws clause of the superclass method
while overriding it in the subclass?
Yes, one can do the modification of the throws clause of the superclass method while
overriding it in the subclass. But there are some rules that needs to be taken into
consideration while overriding in the case of exception handling.
25) What are the benefits of passing 'this' into a method in lieu
of the object of the current class itself?
We know that 'this' is referred to the object of the current class. Hence, it should be
similar to the objects of the current class. However, there are two main benefits of
passing "this" in lieu of the object of the current class.
1. 'this' is a final variable. Hence, 'this' can not be assigned to any new value,
whereas the object of the current class may not be final, and it can be changed.
2. In the synchronized block, 'this' can be used.