Skip to main content

Posts

Showing posts with the label INTERVIEW QUESTIONS

Does an interface extend Object class in java?

This is one of the toughest and trickiest question in java language. You may also know that only classes in java are inherited from java.lang.Object class. Interfaces in java don’t inherit from Object class. They don’t have default parent like classes in java. But, following two cases may surprise you. Case 1 : If an interface does not extend Object class, then why we can call methods of Object class on interface variable like below ? interface A { } class InterfaceTest { public static void main(String[] args) { A a = null; a.equals(null); a.hashCode(); a.toString(); } } Case 2 : If an interface does not extend Object class, then why the methods of Object class are visible in interface.? interface A { @Override public boolean equals(Object obj); @Override public int hashCode(); @Override public String toString(); } Explanation : Here is the answer, for every public method in Object class, there is an implicit abstract and public method declared in every interface which does not have di...

Java Interview Question : What is 'volatile' keyword and what are the use cases?

What is the Java volatile keyword? Daily we are reading about the java its key points, today we are going to get the internals of volatile keyword, volatile is used to indicate that a variable is going to reside in volatile memory only i.e. Main memory aka RAM as we know that when CPU operates on any variable it will copied that object into its registers and it will operate on the register and after completion of its operation it will send the modified value to RAM, so in case of a multi-threaded application it may possible that two thread will operate on same variable but under different CPU registers so two avoid this kind of situation we can use "volatile" keyword with the variable. Declaring a volatile Java variable means: The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory" aka RAM. Now we can say that The Java volatile keyword is used to mark a Java variable as "being stored in main memory...

Java Interview Question : What is 'Intern()' method in String class?

Java String intern The java string intern() method returns the interned string. So in java we have various ways to create a String. 1. By String class constructor. String str = new String ("Hello"); here two objects will get created in the memory one in side the heap on object area and another one inside the string constant pool but str will hold the reference of the string that is created under object area. 2. By literal. Here only one object will get created inside the string constant pool. 3. By toString() Method on any object (String representation of an object). (same as first) Now what if we want the string object from SCP(String constant pool), so to get the string reference from SCP java provides intern method. Method Signature The signature of intern method is given below: public String intern() Lets understand the things by example: package net.jubiliation.example; public class StringInterMethodExample { public static void main(String[] args) { String s1 = new S...

Interview Question : How garbage collector works?

How garbage collector works? Before going to understand the working of garbage collection functionality of Java, I hope you already gone through the previous post i.e. What is garbage collection? if not please go through it.  Let's start how garbage collection works, in general many people think garbage collection collects and discards dead objects. In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage. As you’ll see, this fundamental misunderstanding can lead to many performance problems. B efore moving ahead let's recall few important points about garbage collection in Java. Objects are created on the heap in Java irrespective of their scope e.g. local or member variable. while it's worth noting that class variables or static members are created in method area of Java memory space and both heap and method area is shared between different thread. Garbage collection is a mechanism provided by Java Virtual...

Interview Question : What is garbage collection in Java?

What is garbage collection? The definition say that garbage collection is a process that is responsible for making space in a computer's memory by removing data that is no longer required or in use. Java Garbage Collection: In java, garbage means unreferenced objects. Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects. To do so, we were using free() function in C language and delete() in C++. But, in java it is performed automatically. So, java provides better memory management. Advantage of Garbage Collection It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory. It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts. How can an object be unreferenced? 1) By nulling a reference: Employee e=new Employee(); e=null; 2) By assigning a reference to another: Employee e1=new Employee(); Empl...

Coding Interview : How to find duplicate characters from String in Java also print the frequency?

The problem statement say that we need to identify how many time a character appear in a string. Program :  package net.jubiliation.example; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class CharacterFrequencyExample {  public static void main(String[] args) {   String str = "yweeeeerwwesfsfsfsgsgdgxcbgd";   // convert this String into array of character   char[] characterArray = str.toCharArray();   final Map<Character, Integer> charactersMap = new HashMap<>();   // maintain for each loop to traverse through each element   for (Character character : characterArray) {    if (charactersMap.containsKey(character)) {     int count = charactersMap.get(character);     count++;     charactersMap.put(character, count);    } else {     charactersMap.put(character, 0);...

Coding Interview : How do you reverse word of a sentence in Java?

Problem statement says that we need to reverse the each word of a given sentence. Program :  package net.jubiliation.example; /**  * @author Gaurav  *  */ public class StringReverseExample {  public static void reverseWordInMyString(String sentence) {   String[] words = sentence.split(" ");   /*    * Here split() method of String class splits a string in chunks based    * on the delimiter passed as an argument to it.    */   String reversedSentence = "";   for (int index = 0; index < words.length; index++) {    String word = words[index];    String reverseWord = "";    for (int temp = word.length() - 1; temp >= 0; temp--) {     /*      * The charAt() function returns the character at the given position in a string.      */     reverseWord = reverseWord + word.charA...

Database and SQL Interview Questions

Can we create indexes for all the column? Truncate vs drop vs delete? What is a key? What is the difference among the Composite key, Candidate key, Primary key  and Foreign key? What is a view? What is the difference between Views & procedures? 2nd highest salary from table? Nth highest salary from table? Select top 5 rows from table? Trigger syntax? procedure syntax? View Syntax? Function syntax? Data base objects? Cursors vs ref cursors? What is Atomic transactions? What is fragment transactions? Why we need self join? How to create Many to Many relationship? How to create One to One relationship? How to create Many to One or One to Many relationship?

Hibernate : Interview Questions

How to configure second level cache? What is hibernate? why we use it over jdbc? How HQL queries are interacting with data base? How HQL queries are converting in to data base specific queries? What is the role of dialect class in hibernate? Difference between cache providers? cascade vs inverse? Why we use criteria in hibernate? Many to many example in hibernate batch query in hibernate Hibernate Lazy loading and early Loading Load vs Get in Hibernate Merge vs update in hibernate How exactly inverse will work in hibernate? discriminator column in hibernate Saving an object 2 times in hibernate? Second level cache can be disable? First level cache can be disable and if not then why we can't? Did you ever tried? Batch processing in Hibernate. What are Derived and its usage? What is the use of bag.

Spring Interview Questions

Spring Hibernate integration Spring injection Removing setter what kind of error/exception we will get? What are the injection types? let me give deep analysis How we can achieve setter based injection and what it does? IOC and DI explain? Spring bean Life cycle ProtoType vs Single ton in spring Spring controller resolving Spring request how going to match required resources Injecting prototype in to single ton and accessing using singleton bean? Injecting singleton in to prototype and accessing using prototype bean? How to exclude specific file or class from the Component scan. How to stop providing security to specific files or URI's in Spring security. Injecting spring bean in to normal java class(non Spring bean) How can we implement interface by 2 implementation classes and how to access them?

Core Java : Coding Interview Programs

How to remove duplicate elements from ArrayList in Java? How to find if given String is palindrome in Java? How to find a missing number in a sorted array? How to calculate factorial using recursion and iteration? How do you reverse word of a sentence in Java? How to find duplicate characters from String in Java? How to reverse an integer variable in Java? How to check if a year is a leap year in Java? Write code to implement Bubble sort algorithm in Java? Write code to implement Quicksort algorithm in Java? How do you swap two integers without using temporary variable? Write a program to check if a number is power of two or not? How to reverse String in Java without using StringBuffer? Write a program to code insertion sort algorithm in Java How to solve FizzBuzz problem in Java? Write a program to print highest frequency word from a text file? 

Advanced Java Interview Questions

Application server vs web server Explain the Servlet life cycle What are the implicit objects and how JSP implicit objects works or what are the usages of the JSP implicit objects? What is the difference between JBoss vs Tomact? How many ways error handling can be done in jsp? JSP include and JSP action? What is the difference between GET and POST? Dispatcher servlet functionality. How session can kill explicitly. Declaration in JSP? Statement vs Prepared statement vs Callable statement. Servlet collaboration? How exactly save points works? How to manage session? What are the various ways to keep the session? What is JDBC transaction management?

Core Java Interview Questions

How does serialization takes place in Java ? What is purpose of externalizable interface ? What are transient variables ? Why most of methods in Collections class declared as static ? What is difference between overloading and overriding ? What is hiding of members and methods - both static and instance one ? What is ThreadLocal class used for ? What is purpose of Classloader provided by Java ? What is effect of multiple classloaders on Singleton Class ? NoClassDefFoundError vs ClassNotFoundException ? What kind of exceptions we have in Java? What is run time and unchecked exceptions? How we can achieve exception free code? What we need to use among these Exception vs RuntimeException when we are creating custom exceptions? What we have to do when exception raise and when it will rise in case of checked? Hash set internal implementation? Array list how it works internally? How hash set works and what is the difference between hash map and hash set? Array list vs linked list? Comparable...

Core Java : JVM and Architecture related Questions

How garbage collector works? Which algorithm we write to avoid memory leaks? What is strong vs weak references? What is Java Memory Model and how it keeps the String in special area? How classloader works? How java supports platform independence? Diamond dead issue? What are the basic OOPS principals? Abstract class vs interface where we use? Enum use cases where we use? Overloading vs overriding vs Method hiding? What is Marker interface and how can you implement a custom marker interface? Difference between Cohension vs Coupling?

Design Pattern Interview Questions

Singleton object? How can you achieve effective thread safety? Single ton use cases? How exactly effective single ton creates? How volatile is used in singleton ? where exactly it is needed? What is Object design pattern and when it is required? What is Abstract factory design pattern and how it is helpful?. What is Proxy design pattern and what is the use? Where we use Factory design pattern? What is Factory design pattern?