Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers for 'Solution' - 87 question(s) found - Order By Newest | ||||
Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems | ||||
| ||||
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory. x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object. Sample code: String x = new String("str"); String y = new String("str"); System.out.println(x == y); // prints false System.out.println(x.equals(y)); // prints true | ||||
Sample Code for equals | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string comparison   string   object class   ==   equals   object equality  operator   == vs equals   equals vs == Asked in 294 Companies basic   frequent | ||||
Try 6 Question(s) Test | ||||
Advanced level question. Frequently asked in High end product companies. Frequently asked in Google , Cognizant and Deloitte ( Based on 2 feedback ) | ||||
| ||||
Ans. 1. String Pool - When a string is created and if it exists in the pool, the reference of the existing string will be returned instead of creating a new object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references. Example - String str1 = "String1"; String str2 = "String1"; // It doesn't create a new String and rather reuses the string literal from pool // Now both str1 and str2 pointing to same string object in pool, changing str1 will change it for str2 too 2. To Cache its Hashcode - If string is not immutable, One can change its hashcode and hence it's not fit to be cached. 3. Security - String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   string   string class   immutable  immutability   advanced Asked in 39 Companies expert   frequent | ||||
Try 4 Question(s) Test | ||||
Very frequently asked in different variations. Frequently asked in Deloitte ( 2 feedback ) , HCL Tech ( 3 feedback ), TCS and Coginizant (CTS) | ||||
| ||||
Ans. If the Object value will not change, use String Class because a String object is immutable. If the Object value can change and will only be modified from a single thread, use StringBuilder because StringBuilder is unsynchronized(means faster). If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized). | ||||
Sample Code for String Sample Code for StringBuffer Sample Code for StringBuilder | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string class   string   stringbuilder   stringbuffer   String vs StringBuffer   String vs StringBuilder   String vs StringBuilder vs StringBuffer   StringBuffer vs stringBuilder Asked in 29 Companies basic   frequent | ||||
Try 3 Question(s) Test | ||||
| ||||
Ans. OOPs or Object Oriented Programming is a Programming model which is organized around Objects instead of processes. Instead of a process calling series of processes, this model stresses on communication between objects. Objects that all self sustained, provide security by encapsulating it's members and providing abstracted interfaces over the functions it performs. OOP's facilitate the following features 1. Inheritance for Code Reuse 2. Abstraction for modularity, maintenance and agility 3. Encapsulation for security and protection 4. Polymorphism for flexibility and interfacing | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  oops  oops features Asked in 260 Companies basic   frequent | ||||
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies. Frequently asked in HCL Technologies, TCS and Accenture. | ||||
| ||||
Ans. final - constant variable, objects cannot be de-referenced, restricting method overriding, restricting class sub classing. finally - handles exception. The finally block is optional and provides a mechanism to clean up regardless of what happens within the try block. Use the finally block to close files or to release other system resources like database connections, statements etc. finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. | ||||
Sample Code for final Sample Code for finally Sample Code for finalize | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   final   finally   finalize   final vs finally vs finalize Asked in 61 Companies basic   frequent | ||||
Try 4 Question(s) Test | ||||
Very frequently asked to Fresh graduates. | ||||
| ||||
Ans. 1.Abstraction solves the problem at design level while encapsulation solves the problem at implementation level 2.Abstraction is used for hiding the unwanted data and giving relevant data. while Encapsulation means hiding the code and data into a single unit to protect the data from outside world. 3. Abstraction lets you focus on what the object does instead of how it does it while Encapsulation means hiding the internal details or mechanics of how an object does something. 4.For example: Outer Look of a Television, like it has a display screen and channel buttons to change channel it explains Abstraction but Inner Implementation detail of a Television how CRT and Display Screen are connect with each other using different circuits , it explains Encapsulation. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   oops concepts   encapsulation  object oriented programming (oops)  oops concepts   abstraction   basic interview question   encapsulation vs abstraction Asked in 10 Companies basic   frequent | ||||
Try 2 Question(s) Test | ||||
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies. | ||||
| ||||
Ans. Underlying data structure for ArrayList is Array whereas LinkedList is the linked list and hence have following differences - 1. ArrayList needs continuous memory locations and hence need to be moved to a bigger space if new elements are to be added to a filled array which is not required for LinkedList. 2. Removal and Insertion at specific place in ArrayList requires moving all elements and hence leads to O(n) insertions and removal whereas its constant O(1) for LinkedList. 3. Random access using index in ArrayList is faster than LinkedList which requires traversing the complete list through references. 4. Though Linear Search takes Similar Time for both, Binary Search using LinkedList requires creating new Model called Binary Search Tree which is slower but offers constant time insertion and deletion. 5. For a set of integers you want to sort using quicksort, it's probably faster to use an array; for a set of large structures you want to sort using selection sort, a linked list will be faster. | ||||
Sample Code for ArrayList Sample Code for LinkedList | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  collections   java   data structures   arraylist   linkedlist   arraylist vs linkedlist Asked in 61 Companies Basic   frequent | ||||
Try 1 Question(s) Test | ||||
Frequently asked question in companies using Hibernate. | ||||
| ||||
Ans. It's a feature to lazily initialize dependencies , relationship and associations from the Database. Any related references marked as @OneToMany or @ManyToMany are loaded lazily i.e when they are accessed and not when the parent is loaded. | ||||
Sample Code for Lazy Initialization | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  hibernate   lazy loading hibernate   lazy initialization hibernate   architecture Asked in 77 Companies Basic   frequent | ||||
Try 2 Question(s) Test | ||||
Very Frequently asked. Have been asked in HCL Technologies very frequently ( based on 3 feedback ). Among first few questions in many interviews. | ||||
| ||||
Ans. Abstract classes can have both abstract methods ( method declarations ) as well as concrete methods ( inherited to the derived classes ) whereas Interfaces can only have abstract methods ( method declarations ). A class can extend single abstract class whereas it can implement multiple interfaces. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   classes   abstract class   interfaces   abstract class vs interface   abstract classes vs interfaces Asked in 82 Companies basic   frequent | ||||
Basic and Very Frequently asked. | ||||
| ||||
Ans. Polymorphism means the condition of occurring in several different forms. Polymorphism in Java is achieved in two manners 1. Static polymorphism is the polymorphic resolution identified at compile time and is achieved through function overloading whereas 2. Dynamic polymorphism is the polymorphic resolution identified at runtime and is achieved through method overriding. | ||||
Sample Code for overloading Sample Code for overriding | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  polymorphism  object oriented programming (oops)  oops concepts  oops concepts Asked in 108 Companies Basic   frequent | ||||
Try 2 Question(s) Test | ||||
Frequently asked in all types of companies especially Indian Services companies. Frequently asked in CTS (Based on 2 feedback) | ||||
| ||||
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements. This bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to and if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   hashcode   advanced  hashtable Asked in 33 Companies intermediate   frequent | ||||
Try 1 Question(s) Test | ||||
Very Frequently asked to fresh graduates and less experienced. Favorite question in Walk in drives. Frequently asked in Indian Services companies. | ||||
| ||||
Ans. Overloading - Similar Signature but different definition , like function overloading. Overriding - Overriding the Definition of base class in the derived class. | ||||
Sample Code for overloading Sample Code for overriding | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   overloading   overriding   oops concepts   basic interview question   overloading vs overriding Asked in 86 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. Car Engine is an example of encapsulation and abstraction. You ignite the car using an interface called starter and least bothered about how the tire actually moves (This is abstraction). The engine encapsulates the complete process to itself only and doesn't allow you to start the other components like the radiator etc ( this is excapsulation ) | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  encapsulation  object oriented programming (oops)  oops concepts  abstraction  oops  oops features  java features Asked in 13 Companies | ||||
Very frequently asked across all types of companies. | ||||
| ||||
Ans. Hashtable is synchronized whereas HashMap is not.HashMap allows null values whereas Hashtable doesnt allow null values. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   hashmap   map   hashtable   hashmap vs hashtable Asked in 14 Companies basic   frequent | ||||
Try 2 Question(s) Test | ||||
Advanced level question usually asked in High end product companies. Have been asked in Google and Amazon (Based on 1 Feedback) | ||||
| ||||
Ans. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is known as garbage collection. The Java runtime environment supports a garbage collector that periodically frees the memory used by objects that are no longer needed. The Java garbage collector is a mark-sweep garbage collector that scans Java dynamic memory areas for objects, marking those that are referenced. After all possible paths to objects are investigated, those objects that are not marked (i.e. are not referenced) are known to be garbage and are collected. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   garbage collection   java memory management   advanced Asked in 21 Companies intermediate   frequent | ||||
Try 4 Question(s) Test | ||||
Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit. | ||||
| ||||
Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search. Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList. Further level up, we can have linked Hashmaps like , hashmap { a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value) b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value) .................................................................................... z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value) } upto n levels ( where n is the average size of the word in dictionary. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   collections   hashmap   binary search   search algorithm   advanced   architecture   data structure Asked in 6 Companies   frequent | ||||
Try 1 Question(s) Test | ||||
Frequently asked to fresh graduates and less experienced. | ||||
| ||||
Ans. 1. public is the access modifier that makes the method accessible from anywhere, static is the keyword that makes it accessible even without creating any object, void means it doesn't return anything , String args[] is the array of argument that the method receives. 2. If we use main without the string args , it will compile correctly as Java will treat it as just another method. It wont be the method "main" which Java looks for when it looks to execute the class and hence will throw Error: Main method not found in class , please define the main method as: public static void main(String[] args) 3. Main is not a keyword but a special string that Java looks for while initiating the main thread. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   main method Asked in 4 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
Frequently asked to fresh graduates. | ||||
| ||||
Ans. ACID stands for Atomicity, Consistency, Isolation, Durability is a set of properties of database transactions. Atomicity means all or nothing. i.e parts of a transaction shouldn't commit if any one of them fails. Either the whole transaction should succeed or it should be complete rollback. Consistency means that any transaction should lead database from one stabe state to another. Isolation means that the execution of transaction results in a system state that would be obtained if transactions were executed serially. Durability means that when a transaction is committed it forms the permanent state of database. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  database  acid Asked in 7 Companies Intermediate | ||||
Try 1 Question(s) Test | ||||
Almost sure to be asked in every company using any Dependency Injection framework ( Spring, Guice etc ) | ||||
| ||||
Ans. It is a Design Pattern that facilitates loose coupling by sending the dependency information ( object references of dependent object ) while building the state of the object. Objects are designed in a manner where they receive instances of the objects from other pieces of code, instead of constructing them internally and hence provide better flexibility. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  design patterns   ioc ( Inversion of Control )  dependency injection Asked in 83 Companies intermediate   frequent | ||||
Frequently asked question for intermediate developers. Frequently asked in HCL Technologies and EPAM. | ||||
| ||||
Ans. Volatile is a declaration that a variable can be accessed by multiple threads and hence shouldnt be cached. | ||||
Sample Code for volatile | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   synchronization   volatile   java keywords Asked in 42 Companies intermediate   frequent | ||||
Try 1 Question(s) Test | ||||
Very frequently asked.Usually among first few questions. | ||||
| ||||
Ans. MVC is a Design Pattern that facilititates loose coupling by segregating responsibilities in a Web application 1. Controller receives the requests and handles overall control of the request 2. Model holds majority of the Business logic, and 3. View comprise of the view objects and GUI component | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  j2ee   mvc   mvc design pattern   design pattern   struts   spring   web application   web frameworks   ebay Asked in 60 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
Frequently asked to fresh graduates and less experienced. | ||||
| ||||
Ans. Inheritance means a object inheriting reusable properties of the base class. Compositions means that an abject holds other objects. In Inheritance there is only one object in memory ( derived object ) whereas in Composition , parent object holds references of all composed objects. From Design perspective - Inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   oops concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   difference between   basic interview question Asked in 12 Companies basic   frequent | ||||
Try 2 Question(s) Test | ||||
Frequently asked in high end product companies. Frequently asked in Deloitte. | ||||
| ||||
Ans. https://medium.com/javarevisited/internal-working-of-hashmap-in-java-97aeac3c7beb#:~:text=HashMap%20internally%20uses%20HashTable%20implementation,the%20entries%20into%20the%20map. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  hashmap  collections  hashmap internal implementation Asked in 20 Companies expert   frequent | ||||
| ||||
Ans. 1. Query Optimization ( Query Rewriting , Prepared Statements ) 2. Restructuring Indexes. 3. DB Caching Tuning ( if using ORM ) 4. Identifying the problems ( if any ) with the ORM Strategy ( If using ORM ) | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   db   database   hibernate   orm   at&t   overstock.com   performance improvement   architecture   technical lead   architect intermediate | ||||
Frequently asked at HCL Technologies ( Based on 3 feedback ) | ||||
| ||||
Ans. Checked exceptions are the exceptions for which compiler throws an errors if they are not checked whereas unchecked exceptions are caught during run time only and hence can't be checked. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   exceptions   checked exceptions   unchecked exceptions   exception handling   checked vs unchecked exceptions Asked in 39 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. Vectors are synchronized whereas Array lists are not. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   basic interview question   vector   arraylist   collections   synchronization   vector vs arraylist Asked in 35 Companies basic   frequent | ||||
| ||||
Ans. Yes, We can call garbage collector directly but it doesn't guarantees that the gc will start executing immediately. This gc( ) method appears in both Runtime and System classes of java.lang package. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   garbage collection   java memory management   jvm   gc   yes no Asked in 4 Companies expert | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. In first case, there would be 2 DB calls in worst case and 1 in best case. In 2nd approach there will be always 2 DB calls. Decision on the approach should depend on the following considerations - 1. How costly is the call to DB ? Are we using indices , hibernate etc If calls to DB are costly , 1st approach should be the choice. 2. Exception Book keeping load upon exception. The benefit of saving 1st call in approach 1 should be bigger than the Book keeping for the exception. 3. Probability of the exception in first apparoach. If the DB Table is almost empty, it makes sense to follow Approach 1 as majority of the 1st calls will pass through without exception. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  database   insert-update   db exceution plan   db strategy   design   architecture   technical lead | ||||
Very Frequently asked. | ||||
| ||||
Ans. throw is used to re throw an exception.throws is used to declare that the method throws the respective exceptions.try block is used to identify if the respective block has thrown any exception.catch is used to catch the exception that has been thrown by the respective try block. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   exception handling   throw   throws   try   catch Asked in 5 Companies basic   frequent | ||||
This question can be asked in different variants like performace improvement of an application, improving performance for DB communication etc. | ||||
| ||||
Ans. This depends on entirely upon the code type. For example 1.If its just logic, we can try it to short circuit or put the if / switch case with maximum probability in the beginning. 2. Can use faster data structures, for eg - random retrieval instead of sequence / iterator 3. Working with primitive types or even bytes instead of Objects, even though it may result in marginal improvement. 4. If its service call, then service call with bulk load can help 5. If DB Operation, then with use of Indices , Views or using ORM , cache etc. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  performance improvement  code optimization Asked in 1 Companies | ||||