300+ FRESHERS JAVA Interview Questions and Answers

java Interview Questions for freshers :-

1. What is Java?
Java is a object-oriented programming language originally developed by Sun Micro systems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

2. What is serialization?
Serialization converts an object to a stream of bytes for safety purposes. We can save it as a file, store in DB or send it by the network. We can also recreate the state of an item when needed.

3. What is a Java virtual machine?
JVM helps the computer to run the Java program or other languages. It acts as a run-time engine. Java code is compiled into bytecode to run on any computer.

4. What is constructor?
It is an instant and special method having the same name as the class. A constructor can be overloaded and a section of code to load newly created objects. If a constructor is created with a parameter then another constructor can be created without a parameter.

5. Describe variables? Name them
A name for a memory location is variable. And it is called a basic unit of storage in a program. You can change the stored value during the program execution.

  • Local variables
  • Instant variables
  • Static variables

6. What is the classloader?
Classloader is a Java runtime environment to load Java classes into Java virtual machine. It loads class files from the network, file system, and other sources. In java, there is 3 built-in classloader.

  • Extension
  • System/application
  • Bootstrap

7. What are the wrapper class?
A wrapper class wraps or contains primitive data types. The Java primitives change into the reference types. It contains a field when we create an object. Primitive data types are stored in this field. Thus a primitive value is also be wrapped.

8. Define a package and its advantages?
The collection of classes, sub-packages, and interfaces are bundled together is called packages. We can modularize the code and optimize its reuse. The code can also be imported by other classes and reused.

  1. It helps to remove name clashes.
  2. Provide access protection to manage the code.
  3. The classes and interfaces are easily maintained because of proper structure.
  4. It contains hidden classes to use within the packages.

9. What is the JIT compiler?
JIT is a program to help in changing the Java bytecode into instructions. It improves the performance of Java applications at the run time and known as the integral parts of the Java Runtime Environment.

10. What are the special keywords in Java? Name them
The special keywords in Java are access modifiers. Constructor, variable, scope of a class, data member or method is restricted. Default, private, protected and the public are four types of access modifiers.

java Interview Questions for freshers
java Interview Questions for freshers

11. What are garbage collectors? and types.
It implicit memory management and destroy the unused objects to relieve the memory. It automatically removes the unreferenced objects to make the memory efficient. There are 4 types of garbage collectors.

  • Serial garbage
  • G1 garbage
  • CMS garbage
  • Parallel garbage

12. Tell the smallest piece of programmed instructions? How does it create?
A thread, A scheduler executes independently. All programs having one thread is called the main thread. JVM creates the main thread when the program starts its execution. The main ( ) of the program is invoked by the main thread. Thread is created –

  • By implementing the Runnable interface and extending the thread.

13. What executes a set of statements?
Finally block always executes a set of statements. It is with a try block of any exception occurs or not. It does not execute the program by calling System.exit( ) or by a fatal error.

14. What refers to Multi-threading?
Synchronization, it keeps all concurrent threads in execution. Memory consistency errors are avoided by synchronization. Execution of multiple threads accesses the same objects and fields. The thread holds the monitor when the method is considered as synchronized.

15. Tell the purpose of final, finally and finalize?

  • Final cannot be overridden, inherited or change. For restrict the class, variable and method.
  • Finally, is used for putting important code and executed the exception is handled or not.
  • Finalize is for cleaning up processing before the object is collected in the garbage.

16. Where we use a different signature?
In method overloading signature must be different. The same class method shares the same name. Every single method should be the indifferent number of parameters or different orders or types of parameters. For “adding” or “extending” and known as a compile-time polymorphism.

17. What is Run-time polymorphism?
Method Overriding is run-time polymorphism having the same signature. Inheritance is required in Method Overriding. It is used to change existing behavior. The subclass has the same method od same number, name, parameters, return type.

18. Name the method that belongs to the class?
In Java, the static method belongs to the class and access only static data. It calls only other static methods and directly accessed by the class name. To define variables, static is used.

19. Describe Object-oriented paradigm?
The programming paradigm based on data and methods is called object-oriented programming. Paradigm incorporates the advantages of reusability and modularity. Objects are instances of classes to interact with one another. It designs programs and applications.

20. In Java, what is an object?
A real-time entity of some behavior and state is called an object. It is an instance of a class with instance variables. The new keyword is for creating the object of the class.

21. What is an object-based programming language?
JavaScript, VBScript is an object-based language and has in-built objects. All the concepts of OOPs such as polymorphism and inheritance are not followed by object-based languages.

22. Name the constructor used in Java?
Default constructor – It is used to perform the task on object creation and to initialize the instance variable with default values. The constructor does not accept any value. If no constructor is defined in the class, then a default constructor is invoked implicitly by the compiler.
Parameterized constructor – This constructor accepts the arguments. It initializes the instance variables with the given values.

23. How to copy the values of the item to others in Java?
We can print the values by using the constructor, to allow the values of one object to other and the design of clone. In Java, there is no copy constructor.

24. Explain the Java method?
The Java method is invoked explicitly and is to disclose the nature of an item, it has an entry. Its name is not the same as the class name. It is not given by the finder.

25. Describe the static variables?
The static variable belongs to the class and makes the memory-efficient of the program. It refers to the common property of all objects. At the moment when class is loading, it gets the memory in the department field.

26. What is aggregation?
Aggregation is described as a has-a relationship and has an entity reference. And having a relationship between two classes. The weak relationship is represented by aggregation.

27. What is a super keyword? Mention its uses.
The Super keyword is a hint variable is to mention at once parent class item with the concept of inheritance, the keyword comes into the picture.

  • It appeals soon the parent class technique and constructor.
  • Refers directly parent class instance variable.
  • It is used to initialize the base class variables.

28. What is method overriding? Name the rules and use them.
In Java method overriding is when a subclass has the specific implementation or the same method as in the parent class.

Rules :

  1. It should have the same name and signature as in the parent class.
  2. IS-A relationship should be in between two classes.

Uses :

  1. For runtime polymorphism
  2. To give the specific implementation already provided by its superclass.

29. What are polymorphism and its types?
Polymorphism is used in a different context and define the situations. When a single object refers to the super or sub-class depends on the reference type is polymorphism. Many forms are considered as polymorphism.

30. Explain the OOPs concepts in Java?
For programmers to create the components for re-using in different ways. OOPs is known as programming style. The four concepts are

  • Abstraction
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Interface

31. What is stored in a heap memory?
Java String pool is a collection of Strings are stored. If you create a new object, the string pool confirms the object is in the pool or not. The same reference is returned to the variable if it is already in the pool.

32. How to call one constructor to another to the current object?
By constructor chaining. It is occurred by inheritance. The task of a subclass constructor is to call the superclass constructor. There are two ways to do constructor chaining.

Within the same class using the keyword “this” ( )
From base class using the keyword “super” ( )

33. Tell the nature of Java strings?
The nature of string objects are immutable which is cached in the string pool. The state of string object cannot be modified after the creation. Java creates a new string object if you update the value of the particular object.

34. What is the interface of the Util package? It’s characteristics.
Map in Java is the interface of the Util package. It is used to map a key and a value. It is different from other collection types. And it is not the subset of the main collection interface.

  • Duplicate keys are not contained
  • The order of a map is depending upon the specific implementation

35. Describe Java Stack memory?
Stack memory is in the Last-In-First-Out order to free memory. It is used for the execution of a thread. It contains local primitives and reference to other objects. To define the stack memory, we use –Xss. The memory of the stack is considered short-lived.

36. Why Runnable Interface is used in Java?
Runnable interface is used in java for implementing multi threaded applications. Java.Lang.Runnable interface is implemented by a class to support multi threading.

37. What are the two ways of implementing multi-threading in Java?
Multi threaded applications can be developed in Java by using any of the following two methodologies:

  1. By using Java.Lang.Runnable Interface. Classes implement this interface to enable multi threading. There is a Run() method in this interface which is implemented.
  2. By writing a class that extend Java.Lang.Thread class.

38. When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?
Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it’s always a good choice to use StringBuffer when data is being changed too much. If we use String in such a case, for every data change a new String object will be created which will be an extra overhead.

39. What’s the purpose of using Break in each case of Switch Statement?
Break is used after each case (except the last one) in a switch so that code breaks after the valid case and doesn’t flow in the proceeding cases too.

If break isn’t used after each case, all cases after the valid case also get executed resulting in wrong results.

40. How garbage collection is done in Java?
In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method.

41. How we can execute any code even before main method?
If we want to execute any statements before even creation of objects at load time of class, we can use a static block of code in the class. Any statements inside this static block of code will get executed once at the time of loading the class even before creation of objects in the main method.

42. Can a class be a super class and a sub-class at the same time? Give example.
If there is a hierarchy of inheritance used, a class can be a super class for another class and a sub-class for another one at the same time.

In the example below, continent class is sub-class of world class and it’s super class of country class.

public class world {

……….

}
public class continenet extends world {

…………

}
public class country extends continent {

………………….

}

43. How objects of a class are created if no constructor is defined in the class?
Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is implicitly used for object creation. This constructor has no parameters.

44. In multi-threading how can we ensure that a resource isn’t used by multiple threads simultaneously?
In multi-threading, access to the resources which are shared among multiple threads can be controlled by using the concept of synchronization. Using synchronized keyword, we can ensure that only one thread can use shared resource at a time and others can get control of the resource only once it has become free from the other one using it.

45. Can we call the constructor of a class more than once for an object?
Constructor is called automatically when we create an object using new keyword. It’s called only once for an object at the time of object creation and hence, we can’t invoke the constructor again for an object after its creation.

46. There are two classes named classA and classB. Both classes are in the same package. Can a private member of classA can be accessed by an object of classB?
Private members of a class aren’t accessible outside the scope of that class and any other class even in the same package can’t access them.

47. Can we have two methods in a class with the same name?
We can define two methods in a class with the same name but with different number/type of parameters. Which method is to get invoked will depend upon the parameters passed.

For example in the class below we have two print methods with same name but different parameters. Depending upon the parameters, appropriate one will be called:

public class methodExample {

public void print() {

system.out.println(“Print method without parameters.”);

}

public void print(String name) {

system.out.println(“Print method with parameter”);

}

public static void main(String args[]) {

methodExample obj1 = new methodExample();

obj1.print();

obj1.print(“xx”);

}

}

48. How can we make copy of a java object?

  • We can use the concept of cloning to create copy of an object. Using clone, we create copies with the actual state of an object.
  • Clone() is a method of Cloneable interface and hence, Cloneable interface needs to be implemented for making object copies.

49. What’s the benefit of using inheritance?

Key benefit of using inheritance is reusability of code as inheritance enables sub-classes to reuse the code of its super class. Polymorphism (Extensibility ) is another great benefit which allow new functionality to be introduced without effecting existing derived classes.

50. What’s the default access specifier for variables and methods of a class?
Default access specifier for variables and method is package protected i.e variables and class is available to any other class but in the same package,not outside the package.

51. Give an example of use of Pointers in Java class.
There are no pointers in Java. So we can’t use concept of pointers in Java.

52. How can we restrict inheritance for a class so that no class can be inherited from it?
If we want a class not to be extended further by any class, we can use the keyword Final with the class name.

In the following example, Stone class is Final and can’t be extend

public Final Class Stone {
// Class methods and Variables
}

53. What is Java? and how does Java enables high performance?
Java is a platform-independent, object-oriented, portable, multi-thread, high-level programming language. The collection of objects is considering as Java. We use Java for developing lots of websites, games and mobile applications. For high performance, Java uses Just in time compiler.

java Interview Questions and Answers for freshers Pdf free Download

Leave a Reply

Your email address will not be published. Required fields are marked *