What Is The Purpose Of Static Methods And Variables
The methods or variables defined as static are shared among all the objects of the class. The static is the part of the class and not of the object. The static variables are stored in the class area, and we do not need to create the object to access such variables. Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class.
For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static.
How Can You Use The Hibernate Framework In Your Everyday Tasks
The interviewer may ask this question to test your knowledge of the Hibernate Framework. Your response can include a brief explanation of Hibernate Framework and its uses.
Example answer:”The Hibernate Framework is a Java framework that you can use to simplify Java application development and database interactions. As an open-source, object-relational mapping tool, it enables data creation, manipulation and access. It maps the object to the database data and internally uses the Java Database Connectivity API to interact with the data.
With the Hibernate Framework, you can automatically create tables in the database and gather data from multiple tables. It implements specifications of the Java Persistence API for data persistence. Since the Hibernate Framework internally uses a default first-level cache and a second-level cache, it performs fast. You can use the object-oriented Hibernate Query Language to generate database-independent queries. By using HQL, you do not need to change the SQL query and write database-specific queries to make any database changes.”
What Is Marker Interface In Java
An empty interface in Java is known as a marker interface i.e. it does not contain any methods or fields by implementing these interfaces, a class will exhibit a special behavior with respect to the interface implemented. If you look carefully at the marker interfaces in Java, e.g., Serializable, Cloneable, and Remote, it looks like they are used to indicate something to the compiler or JVM. So if JVM sees a Class is Serializable, it does some special operation on it, similar way if JVM sees one Class is implemented Clonnable, it performs some operation to support cloning. The same is true for RMI and Remote interface. In simplest Marker interface indicate a signal or a command to Compiler or JVM. > Practically we can create an interface like a marker interface with no method declaration in it but it is not a marker interface at all since it is not instructing something to JVM that provides some special behaviour to the class when our program is going to execute. For example, Serializable, Cloneable, etc. are marker interfaces. When my program gets executed, JVM provides some special powers to my class which has implemented the Marker Interfaces.
What Is Enumeration In Java
Enumeration means a list of named constants. In Java, enumeration defines a class type. An Enumeration can have constructors, methods, and instance variables. It is created using the enum keyword. Each enumeration constant is public, static, and final by default. Even though enumeration defines a class type and has constructors, you do not instantiate an enum using new. Enumeration variables are used and declared in much the same way as you do a primitive variable.
What Is Exception Handling In Java
Exception Handling in Java is a way to keep the program running even if some fault has occurred. An exception is an error event that can happen during the execution of a program and disrupts its normal flow. Java provides a robust and object-oriented way to handle exception scenarios, known as Java Exception Handling.
public class Exception_Handling void Check_Gender throws GenderException public static void maincatch } } class GenderException extends Exception }
Recommended Reading: What To Say In Thank You Interview Email
Explain The Various Directives In Jsp
These are the messages that give instructions to the web container for translating a JSP page into the corresponding servlet. When the JSP page is done with the compilation into a servlet, these directives set the page-level instructions, include external files, and create customized libraries. The syntax used to define a directive is as below:
< %@ directive attribute=value %>
In the above syntax, we can see the directive starts with %@ and ends with a percentage sign . In between, we pass the value and the attribute we need in our directive.
Three types of Directives are as follows:
- Page directive: The page directive defines the attributes that can be applied to a complete JSP page. The syntax of the page directive is as:
< %@ page attribute=value%>
The attributes that you can pass inside this definition are- import, isErrorPage, session, pageEncoding, contentType, extends, info, buffer, language, autoFlush, isThreadSafe, errorPage, etc.
- Include directive: The include directive is useful when we want to include the contents in our JSP file. The content may be any resource such as an HTML file or text file. The include directive is useful as it includes the original content at the page translation time. The syntax used for defining include directive is as:
< %@ include file=NameOfResource %>
In the above syntax, we can give the name of the resource that we want to define in our directive for eg:
< %@ taglib url=theURLofTheTagLibrary prefix = prefixOfLibrary%>
Question : Edit Distance Problem In Java
Given two strings string1 and string2, String1 is to be converted into String2 with the given operations available in the minimum number of steps. Using any one of the given operations contributes to the increment of steps by one.
Allowed Operations are : Remove : This operation allows the Removal any one character from String. Insert : This operation allows the Insertion of one character at any spot in the String. Replace : This operation allows the replacement of any one character in the string withany other character.
Solution: Edit distance problem in java.
Don’t Miss: What Are Some Interview Questions For Nurses
What Makes A Hashset Different From A Treeset
Although both HashSet and TreeSet are not synchronized and ensure that duplicates are not present, there are certain properties that distinguish a HashSet from a TreeSet.
- Implementation: For a HashSet, the hash table is utilized for storing the elements in an unordered manner. However, TreeSet makes use of the red-black tree to store the elements in a sorted manner.
- Complexity/ Performance: For adding, retrieving, and deleting elements, the time amortized complexity is O for a HashSet. The time complexity for performing the same operations is a bit higher for TreeSet and is equal to O. Overall, the performance of HashSet is faster in comparison to TreeSet.
- Methods: hashCode and equals are the methods utilized by HashSet for making comparisons between the objects. Conversely, compareTo and compare methods are utilized by TreeSet to facilitate object comparisons.
- Objects type: Heterogeneous and null objects can be stored with the help of HashSet. In the case of a TreeSet, runtime exception occurs while inserting heterogeneous objects or null objects.
What Is A Class In Java
A class in java is a template that describes the data and behaviour associated with instances of that class. When you instantiate a class you create an object that looks and feels like other instances of the same class. The data associated with a class or object is stored in variables the behaviour associated with a class or object is implemented with methods.
Recommended Reading: What To Write In Thank You Interview Email
Can We Assign The Reference To This Variable
No, this cannot be assigned to any value because it always points to the current class object and this is the final reference in Java. However, if we try to do so, the compiler error will be shown. Consider the following example.
Test.java:5: error: cannot assign a value to final variable this this = null ^1 error
Q 56 How To Handle Exceptions In Spring Mvc Framework
Spring MVC has two approaches for handling the exceptions:
- Exception handler method: In this kind of exception handling, the user will get the @ExceptionHandler annotation type used to annotate a method to handle exceptions.
- XML Configuration: The user can use the SimpleMappingExceptionResolver bean in Springs application file and map the exception.
Read Also: How To Make A Good Interview
Question 2 Does Not Overriding Hashcode Method Has Any Performance Implication
This is a good question and opens to all, as per my knowledge, a poor hash code function will result in the frequent collision in HashMap which eventually increases the time for adding an object into Hash Map.
From Java 8 onwards though collision will not impact performance as much as it does in earlier versions because after a threshold the linked list will be replaced by a binary tree, which will give you O performance in the worst case as compared to O of a linked list.
Q5 Will The Finally Block Get Executed When The Return Statement Is Written At The End Of Try Block And Catch Block As Shown Below
The finally block always gets executed even hen the return statement is written at the end of the try block and the catch block. It always executes , whether there is an exception or not. There are only a few situations in which the finally block does not execute, such as VM crash, power failure, software crash, etc. If you dont want to execute the finally block, you need to call the System.exit method explicitly in the finally block.
Also Check: Rating Scale For Interview Questions
How To Take Input In Java
The below code explains how to take input in java using a scanner
Input in Java Code 1
Scanner in = new Scanner System.out.print int hour1 = in.nextInt System.out.print int hour2 = in.nextInt System.out.print int min1 = in.nextInt System.out.print int min2 = in.nextInt
Input in Java Code 2
class MyClass }Then type this way:ab
Code on how to take character input in Java
import java.util.Scanner public class CharacterInputExample1 }
Code on how to take string input in java
import java.util.Scanner // Import the Scanner classclass MyClass }
Senior Java Developer Interview Questions And Answers
And you should already have basics such as data structure, algorithms, and object-oriented programming topics under your belt, but be prepared for these questions to be more complex with answers that need to be thorough and detailed.
Why is Java called the Platform Independent Programming Language?
Java was designed with versatility in mind. Its application programs can run on any platform without requiring a programmer to rewrite or recompile the data for each separate platform.
What is JVM?
JVM stands for Java virtual machine and is a process virtual machine capable of executing Java bytecode. Each Java source file is compiled into a bytecode file. A JVM is what makes Java so versatile because as it executes the bytecode file, its aware of the specific instruction lengths and other necessities of any underlying hardware platform.
What are Spring beans?
Beans are the objects that form the backbone of an application and are managed by the Spring IoC container. Beans are created with the configuration metadata that is supplied to the container. An example would be XML definitions.
Make all the class members and functions static.
Recommended Reading: How To Prepare For A Waitress Interview
How Would You Differentiate Between A String Stringbuffer And A Stringbuilder
- Storage area: In string, the String pool serves as the storage area. For StringBuilder and StringBuffer, heap memory is the storage area.
- Mutability: A String is immutable, whereas both the StringBuilder and StringBuffer are mutable.
- Efficiency: It is quite slow to work with a String. However, StringBuilder is the fastest in performing operations. The speed of a StringBuffer is more than a String and less than a StringBuilder.
- Thread-safe: In the case of a threaded environment, StringBuilder and StringBuffer are used whereas a String is not used. However, StringBuilder is suitable for an environment with a single thread, and a StringBuffer is suitable for multiple threads.Syntax:
// StringString first = "InterviewBit" String second = new String // StringBufferStringBuffer third = new StringBuffer // StringBuilderStringBuilder fourth = new StringBuilder
How To Remove Duplicate Elements From An Array In Java
public class Change int arrTemporary = new int int b = 0 for } arrTemporary = arrNumbers for return b } public static void main int len = arrInput.length len = removeDuplicate // printing elements for }}
You May Like: How To Crack Amazon Sql Interview
What Are The Possible Ways Of Making Object Eligible For Garbage Collection In Java
First Approach: Set the object references to null once the object creation purpose is served.
Second Approach: Point the reference variable to another object. Doing this, the object which the reference variable was referencing before becomes eligible for GC.
Third Approach: Island of Isolation Approach: When 2 reference variables pointing to instances of the same class, and these variables refer to only each other and the objects pointed by these 2 variables don’t have any other references, then it is said to have formed an Island of Isolation and these 2 objects are eligible for GC.
In The Below Java Program How Many Objects Are Eligible For Garbage Collection
In the above program, a total of 7 objects will be eligible for garbage collection. Lets visually understand what’s happening in the code.
In the above figure on line 3, we can see that on each array index we are declaring a new array so the reference will be of that new array on all the 3 indexes. So the old array will be pointed to by none. So these three are eligible for garbage collection. And on line 4, we are creating a new array object on the older reference. So that will point to a new array and older multidimensional objects will become eligible for garbage collection.
Question : What Are The Differences Between Jdk Jvm And Jre
JDK is a Java Development Kit. Its the main tool in Java used to compile the programs of this language. Within the package, it contains all of the necessary tools to start using it.
Ive already mentioned JVM previously – it stands for Java Virtual Machine. Its a machine that creates the environment in which Java bytecode can function properly.
JRE is Java Runtime Environment. This is the type of environment that JVM provides – it allows the Java bytecode to run and function properly.
Basic Java Interview Questions
Irrespective of your levels of experience or skills, you may encounter these basic Java interview questions:
Which Java project did you enjoy working on the most?
Have you ever used Java to manage a team?
Which object-orientated programming concepts have you used?
Have you used a Request Dispatcher in a project?
What do you know about JRE, JVM and JDK in Java?
What do you know about wrapper classes in Java?
Can you list the differences between the Array list and vector?
Can you list the differences between Heap and Stack Memory?
Does the garbage collection process clean Heap or Stack Memory?
What are the advantages of Java packages?
How would you define a Java Class?
What are the main OOP concepts in Java?
How would you reverse a string in Java?
How would you compare two strings in Java?
What do you know about API in Java?
Don’t Miss: How Do You Ace A Job Interview
How To Implement Binary Search
The array elements must be sorted for implementing binary search. The binary search algorithm is based on the following conditions. If the key is less than the middle element, then we now need to search only in the first half of the array. If the key is greater than the middle element, then we need to only search in the second half of the array. And if the key is equal to the middle element in the array, then the search ends. Finally, if the key is not found in the whole array, then it should return -1. This indicates that the element is not present.
public static int binarySearch else if else mid = / 2 }if return -1 }
Undefined: Undefined means the variable is declared but not assigned any value to it. It may be a variable itself does not exist. If we use it with typeof operator it gives the result undefined. It is not valid in JSON.
Note: Null and undefined both are primitive.
Lets understand it through an example.
When we execute the above code, it generates the following output:
Also Check: How Honest Should You Be In An Exit Interview