JAVA Interview Questions and Answers For Freshers
Q1) Explain Java? and how does Java enables high performance?
Answer: 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.
Q2) What is serialization?
Answer: 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.
Q3) What is a Java virtual machine?
Answer: 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.
Q4) Define constructor?
Answer: 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.
Q5) Describe variables? Name them
Answer: 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
Q6) Define the classloader?
Answer: 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.
Q7) What are the wrapper class?
Answer: 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.
Q8) Define a package and its advantages?
Answer: 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.
- It helps to remove name clashes.
- Provide access protection to manage the code.
- The classes and interfaces are easily maintained because of proper structure.
- It contains hidden classes to use within the packages.
Q9) Define the JIT compiler?
Answer: 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.
Q10) What are the special keywords in Java? Name them
Answer: 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.
Q11) What are garbage collectors? and types.
Answer: 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
Q12) Tell the smallest piece of programmed instructions? How does it create?
Answer: 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.
Q13) What executes a set of statements?
Answer: 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.
Q14) What refers to Multi-threading?
Answer: 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.
Q15) 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.
Q16) Where we use a different signature?
Answer: 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.
Q17) What is Run-time polymorphism?
Answer: 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.
Q18 ) Name the method that belongs to the class?
Answer: 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.
Q19) Describe Object-oriented paradigm?
Answer: 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.
Q20) In Java, what is an object?
Answer: 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.
Q21) What is an object-based programming language?
Q22) 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.
Q23) How to copy the values of the item to others in Java?
Answer: 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.
Q24) Explain the Java method?
Answer: 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.
Q25) Describe the static variables?
Answer: 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.
Q26) Define aggregation?
Answer: 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.
Q27) What is a super keyword? Mention its uses.
Answer: 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.
Q28) What is method overriding? Name the rules and use them.
Answer: In Java method overriding is when a subclass has the specific implementation or the same method as in the parent class.
- It should have the same name and signature as in the parent class.
- IS-A relationship should be in between two classes.
- For runtime polymorphism
- To give the specific implementation already provided by its superclass.
Q29) What are polymorphism and its types?
Answer: 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.
Q30) Explain the OOPs concepts in Java?
Answer: For programmers to create the components for re-using in different ways. OOPs is known as programming style. The four concepts are
Q31) What is stored in a heap memory?
Answer: 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.
Q32) How to call one constructor to another to the current object?
Answer: 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” ( )
Q33) Tell the nature of Java strings?
Answer: 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.
Q34) What is the interface of the Util package? It’s characteristics.
Answer: 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
Q35) Describe Java Stack memory?
Answer: 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.