// API callback
display_related_posts({"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$blogger":"http://schemas.google.com/blogger/2008","xmlns$georss":"http://www.georss.org/georss","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$thr":"http://purl.org/syndication/thread/1.0","id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141"},"updated":{"$t":"2023-11-16T21:40:23.225-05:00"},"category":[{"term":"java"},{"term":"collection"},{"term":"string"},{"term":"program"},{"term":"example"},{"term":"arraylist"},{"term":"interview"},{"term":"string-program"},{"term":"code"},{"term":"error"},{"term":"implementation"},{"term":"HashMap"},{"term":"conversion"},{"term":"difference"},{"term":"exception-example"},{"term":"source"},{"term":"exception-type"},{"term":"number-program"},{"term":"array"},{"term":"tutorial"},{"term":"thread"},{"term":"hashset"},{"term":"oops"},{"term":"Exception"},{"term":"algorithm"},{"term":"eclipse"},{"term":"java-programs"},{"term":"internal-working"},{"term":"games"},{"term":"method"},{"term":"operator"},{"term":"python"},{"term":"article"},{"term":"find"},{"term":"java8"},{"term":"simple"},{"term":"using"},{"term":"function"},{"term":"interview-preparation"},{"term":"number"},{"term":"spring-boot"},{"term":"treemap"},{"term":"treeset"},{"term":"best"},{"term":"case"},{"term":"explain"},{"term":"how to"},{"term":"numbers"},{"term":"strings"},{"term":"without"},{"term":"average"},{"term":"complexity"},{"term":"count"},{"term":"download"},{"term":"features"},{"term":"file"},{"term":"linkedlist"},{"term":"movies"},{"term":"online-course"},{"term":"remove"},{"term":"sort"},{"term":"sorting"},{"term":"space"},{"term":"spaces"},{"term":"stable"},{"term":"total"},{"term":"trim"},{"term":"white"},{"term":"worst"},{"term":"assignment"},{"term":"java-io"},{"term":"math"},{"term":"recursion"},{"term":"scjp"},{"term":"trimming"},{"term":"Books"},{"term":"all"},{"term":"annotation"},{"term":"character"},{"term":"class"},{"term":"concurrency"},{"term":"console"},{"term":"create"},{"term":"exact"},{"term":"examples"},{"term":"frequency"},{"term":"fun"},{"term":"get rid of"},{"term":"jdbc"},{"term":"kotlin"},{"term":"list"},{"term":"pattern-program"},{"term":"python-error"},{"term":"scheduling-algorithm"},{"term":"swing"},{"term":"time"},{"term":"trims"},{"term":"webservices"},{"term":"Double"},{"term":"Gui"},{"term":"Memory"},{"term":"Threads"},{"term":"add"},{"term":"blank"},{"term":"built"},{"term":"calculate"},{"term":"characters"},{"term":"check"},{"term":"collections"},{"term":"compiler"},{"term":"concurrenthashmap"},{"term":"data"},{"term":"definition"},{"term":"design-pattern"},{"term":"exactly"},{"term":"explanation"},{"term":"frequencies"},{"term":"generate"},{"term":"in"},{"term":"insertion"},{"term":"integer"},{"term":"iterator"},{"term":"largest"},{"term":"left"},{"term":"linkedhashmap"},{"term":"maths"},{"term":"maximum"},{"term":"methods"},{"term":"no."},{"term":"oracle"},{"term":"output"},{"term":"pdf"},{"term":"preparation"},{"term":"question"},{"term":"range"},{"term":"recursive"},{"term":"removing"},{"term":"restful"},{"term":"right"},{"term":"salary"},{"term":"security"},{"term":"value"},{"term":"wait"},{"term":"what is"},{"term":"whitespaces"},{"term":"why"},{"term":"word"},{"term":"words"},{"term":"Calculator"},{"term":"Calendar"},{"term":"Constructor"},{"term":"Deque"},{"term":"Ended"},{"term":"EnumMap"},{"term":"Fibonnaci"},{"term":"Hamming Distance"},{"term":"Hash"},{"term":"Hashed"},{"term":"Interrupted"},{"term":"Linear Probing"},{"term":"Quadratic probing"},{"term":"Queue"},{"term":"abstract"},{"term":"abstraction"},{"term":"achieve"},{"term":"anagram"},{"term":"anagrams"},{"term":"and"},{"term":"android"},{"term":"answer"},{"term":"answers"},{"term":"ant"},{"term":"any"},{"term":"api"},{"term":"append"},{"term":"appending"},{"term":"applet"},{"term":"application"},{"term":"arithmetic"},{"term":"armstrong"},{"term":"armstrong number"},{"term":"array list"},{"term":"arraydeque"},{"term":"arrays"},{"term":"asked"},{"term":"awt"},{"term":"basic"},{"term":"basics"},{"term":"beginner"},{"term":"between"},{"term":"bubble sort code"},{"term":"bubble sort example"},{"term":"bubble sort in java"},{"term":"buble"},{"term":"build"},{"term":"built in"},{"term":"c++"},{"term":"calci"},{"term":"calendar java"},{"term":"certification"},{"term":"change"},{"term":"char"},{"term":"chars"},{"term":"classes"},{"term":"cmd"},{"term":"combination"},{"term":"command prompt"},{"term":"companies"},{"term":"company"},{"term":"complete"},{"term":"concept"},{"term":"constructors"},{"term":"core-java-tutorial"},{"term":"counting"},{"term":"creating"},{"term":"creation"},{"term":"cycle"},{"term":"date"},{"term":"date picker"},{"term":"default"},{"term":"define"},{"term":"determine"},{"term":"developers"},{"term":"diagram"},{"term":"docs"},{"term":"double ended queue in data structure"},{"term":"end"},{"term":"enum"},{"term":"equals"},{"term":"exe"},{"term":"executable"},{"term":"experienced"},{"term":"fields"},{"term":"float"},{"term":"freshers"},{"term":"front"},{"term":"functions"},{"term":"game"},{"term":"generating"},{"term":"generator"},{"term":"hash table"},{"term":"hashcode"},{"term":"hashing"},{"term":"hashtable"},{"term":"highest"},{"term":"how"},{"term":"how to do"},{"term":"ide"},{"term":"identityhashmap"},{"term":"implemented"},{"term":"important"},{"term":"in java"},{"term":"infographic"},{"term":"infographics"},{"term":"inner-class"},{"term":"input"},{"term":"insertion sort code in java"},{"term":"insertion sort example"},{"term":"insertion sort in java"},{"term":"insrtion"},{"term":"intellij"},{"term":"introduction"},{"term":"jar"},{"term":"java code"},{"term":"java code examples"},{"term":"java coding"},{"term":"java developers"},{"term":"java interview programs"},{"term":"java prime number"},{"term":"java program"},{"term":"java program codes"},{"term":"java program to find prime numbers"},{"term":"java programming"},{"term":"java programming examples"},{"term":"java util calendar"},{"term":"java-commands"},{"term":"java.lang.object"},{"term":"jdbc questions and answers for freshers"},{"term":"jdk"},{"term":"jquery"},{"term":"jre"},{"term":"jumble"},{"term":"jumbled"},{"term":"key"},{"term":"lang"},{"term":"langton"},{"term":"langton's"},{"term":"langtonant"},{"term":"langtons"},{"term":"langtons ant"},{"term":"laptop"},{"term":"learn"},{"term":"learning"},{"term":"length"},{"term":"lengths"},{"term":"libraries"},{"term":"library"},{"term":"life"},{"term":"lifecycle"},{"term":"linear probing example"},{"term":"linkedhashset"},{"term":"list of all"},{"term":"list of prime numbers"},{"term":"listiterator"},{"term":"logic"},{"term":"long"},{"term":"lowercase"},{"term":"make"},{"term":"manually"},{"term":"many"},{"term":"mathematics"},{"term":"maven"},{"term":"max"},{"term":"meaning"},{"term":"merge"},{"term":"minimum"},{"term":"month"},{"term":"months"},{"term":"multithreading"},{"term":"names"},{"term":"narcissistic"},{"term":"netbeans"},{"term":"nextint"},{"term":"notify"},{"term":"object"},{"term":"objects"},{"term":"ocpjp"},{"term":"outlook"},{"term":"overloading"},{"term":"palindrome"},{"term":"peek"},{"term":"perfect"},{"term":"permutation"},{"term":"picker"},{"term":"poll"},{"term":"pop"},{"term":"position"},{"term":"possible"},{"term":"postman"},{"term":"previous"},{"term":"prime number java program"},{"term":"prime number logic in java"},{"term":"prime numbers upto 100"},{"term":"problem"},{"term":"produce"},{"term":"programming"},{"term":"projects"},{"term":"push"},{"term":"puzzle"},{"term":"quadratic vs linear"},{"term":"questions and answers"},{"term":"quick"},{"term":"quick sort code"},{"term":"quick sort code in  java"},{"term":"quick sort example"},{"term":"quiz"},{"term":"random"},{"term":"read"},{"term":"reading"},{"term":"rear"},{"term":"removes"},{"term":"reverse"},{"term":"runnable"},{"term":"scanner"},{"term":"scientific"},{"term":"selection"},{"term":"selection sort  example"},{"term":"selection sort code in java"},{"term":"selection sort implementation"},{"term":"series"},{"term":"short"},{"term":"simulation"},{"term":"sleep"},{"term":"smallest"},{"term":"snake and ladder"},{"term":"solver"},{"term":"squeeze"},{"term":"stack"},{"term":"stacks"},{"term":"stage"},{"term":"stages"},{"term":"starting"},{"term":"static"},{"term":"stream"},{"term":"stringbuffer"},{"term":"structure"},{"term":"structures"},{"term":"sum"},{"term":"swap"},{"term":"test"},{"term":"text"},{"term":"theorem"},{"term":"throws"},{"term":"to get"},{"term":"trailing"},{"term":"type"},{"term":"unique"},{"term":"unscrambler"},{"term":"uppercase"},{"term":"util"},{"term":"valid"},{"term":"validation"},{"term":"values"},{"term":"variables"},{"term":"vowel"},{"term":"vowels"},{"term":"weakhashmap"},{"term":"what"},{"term":"what is prime number"},{"term":"whatsapp"},{"term":"windows"},{"term":"write"}],"title":{"type":"text","$t":"Java Hungry"},"subtitle":{"type":"html","$t":"Java developers tutorials and coding."},"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https:\/\/javahungry.blogspot.com\/feeds\/posts\/default"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/-\/interview?alt=json-in-script"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/search\/label\/interview"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/-\/interview\/-\/interview?alt=json-in-script\u0026start-index=26\u0026max-results=25"}],"author":[{"name":{"$t":"§џβђά¶¥¶ "},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02288287727196282252"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"generator":{"version":"7.00","uri":"http://www.blogger.com","$t":"Blogger"},"openSearch$totalResults":{"$t":"31"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"25"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-1762963787414303538"},"published":{"$t":"2023-05-21T06:32:00.000-04:00"},"updated":{"$t":"2023-05-21T06:32:00.981-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 25 Java String Programming Interview Questions and Answers"},"content":{"type":"html","$t":"In this post, I have collected Java string programming and coding interview questions and answers that may help you to ace your next interview. These questions can be asked either to entry-level developers or experienced developers. Let's dive deep into the topic:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003ERead Also:\u003C\/span\u003E\u003C\/b\u003E\u003C\/i\u003E \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/09\/top-50-java-string-interview-questions-and-answers.html\" target=\"_blank\"\u003ETop 50 Java String Interview Questions and Answers\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ1 How to reverse a string in Java? (with or without using built-in functions)\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThis question is perfect for entry-level Java developers. You can find the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/12\/5-ways-to-reverse-string-in-java-with-example.html\" target=\"_blank\"\u003Esolution to reverse a string in Java here\u003C\/a\u003E. I have also shared how to \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/reverse-or-mirroring-string-without.html\" target=\"_blank\"\u003Ereverse a string in Java without using a built-in function\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ2 Write a Java program to find the first non-repeated character in the string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have already shared the easiest way to \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/12\/first-non-repeated-character-in-string-java-program-code-example.html\" target=\"_blank\"\u003Efind the first non-repeated character in the string in Java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ3 Write a Java program to count the number of words in a given string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThere are many ways to count the number of words in a given string. The simplest of them is given below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003ECountNumberOfWords\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        String input \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Alive is Awesome\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E words \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E input\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Etrim\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E().\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Esplit\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\" \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ewords\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ4 Write a Java program to count the number of occurrences of a given character in a given string?\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThe most common way of counting the number of occurrences of a given character in a given string is by using for loop. But in the below example, we are counting the number of occurrences of a given character in a given string without using for loop.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003ECountOccurrencesCharacter\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        String input \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Alive is Awesome\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Echar\u003C\/span\u003E ch \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0044dd;\"\u003E'i'\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E count \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E input\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E input\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EreplaceAll\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"i\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E).\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ecount\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ5 Write a Java program to find the duplicate characters in a string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThis question can easily be solved using the HashSet class as shown below in the example. The interviewer may ask to solve this question without using Set\/HashSet, so, be prepared for that also.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.HashSet\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.Set\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EDuplicateCharacters\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        String input \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Alive is Awesome\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        HashSet\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003ECharacter\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E set \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E HashSet\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u0026gt;();\u003C\/span\u003E\n        Set\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003ECharacter\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E result \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E HashSet\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u0026gt;();\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Echar\u003C\/span\u003E ch \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E input\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EtoCharArray\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E())\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(!\u003C\/span\u003Eset\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eadd\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ech\u003Cspan style=\"color: #333333;\"\u003E))\u003C\/span\u003E\n                result\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eadd\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ech\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eresult\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E       \n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ6 Write a Java program to count the number of occurrences of each character in a given string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have already shared the answer to this question in a separate post on\u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/10\/count-total-number-of-times-each-alphabet-appears-in-the-string.html\" target=\"_blank\"\u003Ehow to count the number of occurrences of each character in a given string\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ7 Write a Java program to remove all whitespaces from a given string.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThere are two ways to \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/squeeze-string-or-remove-all.html\" target=\"_blank\"\u003Eremove all whitespaces from a given string\u003C\/a\u003E. One is using built-in Java methods. Another way is without using built-in methods. You can prepare for both ways.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ8 Write a Java program to check if one given string is an anagram of another given string.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have shared in a separate post to \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/05\/anagram-program-in-java-with-code-example.html\" target=\"_blank\"\u003Echeck if one given string is an anagram of another given string in Java with examples\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ9 Write a Java program to check if one given string is the rotation of another given string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have already shared the code with examples to check if \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2023\/04\/check-if-one-string-is-rotation-of-other.html\" target=\"_blank\"\u003Eone given string is the rotation of another given string in Java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ10 Write a Java program to find the count of characters that are not repeated in the string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have already shared the 3 ways to \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2023\/03\/count-characters-that-are-not-repeated-in-string.html\" target=\"_blank\"\u003Efind the count of characters that are not repeated in the string in Java with examples\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ11 Write a Java program to print the last unique character of the string.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThis question is quite similar to Q2, here, we are looking to write a \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2023\/03\/print-last-unique-character-of-string.html\" target=\"_blank\"\u003EJava program to print the last unique character of the string\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ12 Write a Java program to count the number of digits in a given string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have shared \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2022\/07\/count-number-of-digits-in-given-string.html\" target=\"_blank\"\u003E2 ways to count the number of digits in a given string\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ13 Check if a given string is an isogram or not.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThis question is mainly asked to entry-level Java developers. I have already shared \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/07\/java-isogram.html\" target=\"_blank\"\u003Ehow to check if a given string is an isogram or not\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ14 Write a Java program to get the first character of the string in Java\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThis question is mostly asked to entry-level Java developers. You can find the Java program to get the first character of the string in Java \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/04\/get-first-character-of-string-java.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ15 Write a Java program to shuffle a string in Java.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThere are \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/04\/shuffle-string-in-java.html\" target=\"_blank\"\u003Etwo ways to shuffle a string in Java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ16 Write a Java program to count the number of commas in a string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have already shared \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/02\/count-number-of-commas-in-string.html\" target=\"_blank\"\u003E2 ways to count the number of commas in a string in Java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ17 Write a Java program to capitalize the first letter of a string in Java.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI have already shared the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/01\/capitalize-first-letter-of-string.html\" target=\"_blank\"\u003Etwo ways to capitalize the first letter of a string in Java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ18 Write a Java program to find duplicate words in a string?\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI shared 3 ways to \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/08\/find-duplicate-words-in-string.html\" target=\"_blank\"\u003Efind duplicate words in a string in Java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ19 Write a Java program to check if a string is a Pangram.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI already shared the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/07\/pangram-java.html\" target=\"_blank\"\u003EJava program to check if a string is a Pangram\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ20 Write a Java program to remove the first and last characters from the string.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYou can find the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/07\/remove-first-and-last-character-from-string.html\" target=\"_blank\"\u003EJava program to remove the first and last characters from the string here\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ21 Write a Java program to check if a string contains special characters.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nI shared the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/06\/check-string-contains-special-characters.html\" target=\"_blank\"\u003EJava program to check if a string contains special characters or not\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ22 Write a Java program to find all substrings of a string in Java.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYou can find the solution of the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/12\/substrings-of-a-string-in-java.html\" target=\"_blank\"\u003EJava program to find all substrings of a string here\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ23 Check if a string is an anagram in Java.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYou can find the answer \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/05\/anagram-program-in-java-with-code-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ24 Write a Java program to determine if a string has all unique characters.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYou can find the Java program to \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/11\/string-has-all-unique-characters-java-example.html\" target=\"_blank\"\u003Edetermine if a string has all unique characters here\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\nQ25 Write a Java program to find the permutation and combination of a given string.\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThis is one of the most important questions and should be on your to-do list before appearing for the interview. \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/05\/coding-interview-preparation.html\" target=\"_blank\"\u003EPermutation and Combination Java program you can find it here\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today. Please mention in the comments if you face any other string programming question in your interview which is not present in the above list.\u003Cbr \/\u003E"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/1762963787414303538"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/1762963787414303538"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2023\/03\/java-string-programming-interview-questions.html","title":"Top 25 Java String Programming Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-6171183603415591163"},"published":{"$t":"2023-03-27T09:14:00.000-04:00"},"updated":{"$t":"2023-03-27T09:14:06.200-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"hashset"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 10 HashSet Java Interview Questions and Answers"},"content":{"type":"html","$t":"We are sharing the 10 most frequently asked java developer interview questions on HashSet. These questions fully covered the technical questions which can be asked on HashSet. Apart from the below questions the interviewer may ask you to write code on HashSet object examples, and iterations on HashSet object. We try to cover important Java developer interview questions which are frequently asked by the big companies to the candidate on HashSet.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ1. How HashSet works internally in Java or how HashSet works in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question is asked by reputed firms especially \u003Ci\u003EGoldman Sachs, and Morgan Stanley\u003C\/i\u003E. You can find the answer here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/how-sets-are-implemented-internally-in.html\" target=\"_blank\"\u003EInternal implementation of HashSet or How HashSet ensures uniqueness in Java\u003C\/a\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ2. What copy technique is internally used by the HashSet clone() method?\u003C\/b\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere are two copy techniques in every object-oriented programming language, \u003Ci\u003Edeep copy, and shallow copy\u003C\/i\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTo create a clone or copy of the Set object, HashSet internally uses shallow copy in clone() method, the elements themselves are not cloned. In other words, a shallow copy is made by copying the reference of the object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ3 Why HashSet does not have the get(Object o) method?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr\/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003EMost people get puzzled by hearing this question. This question tests the deep understanding of the HashSet class. This question helps the interviewer to know whether the candidate has an idea about contains() method in the HashSet class or not. So let's jump to the answer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nget(Object o) is useful when we have one piece of information linked to other information just like the key-value pair found in HashMap. So using the get() method on one piece of information we can get the second information or vice-versa.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nUnlike HashMap, HashSet is all about having unique values or unique objects. There is no concept of keys in HashSet.\u003Cbr \/\u003E\nThe only information we can derive from the HashSet object is whether the element is present in the HashSet Object or not. If the element is not present in the HashSet then add it otherwise return true leaving the HashSet object unchanged. Here, contains() method helps to provide this information.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDue to the above reason, there is no get(Object o) method in HashSet.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ4 What is the difference between HashSet and TreeSet?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the most popular java interview questions. Please find the answer here: \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/03\/difference-between-hashset-and-treeset-similarities-and-example.html\" target=\"_blank\"\u003EDifference between HashSet and TreeSet\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EQ5 What is and when to use Collections.emptySet(). What is the advantage of having emptySet in the Collections class?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nAccording to \u003Ca href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Collections.html\" rel=\"nofollow\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E, Collections.emptySet() returns the empty immutable Set, not containing null.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EInterviewer: Why do we call the emptySet() method, as we can also create an empty Set using a constructor?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003EThe advantages of using the emptySet() method over creating an object using a constructor are :\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Ci\u003E1. Immutable:\u003C\/i\u003E\u003C\/b\u003E You should prefer to use immutable collections against mutable collections wherever possible. It becomes handy as multiple threads accessing the same instance of an object will see the same values.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Ci\u003E2. Concise:\u003C\/i\u003E\u003C\/b\u003E You do not need to manually type out the generic type of the collection - normally it is inferred from the context of the method call.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003E3. Efficient:\u003C\/b\u003E\u003C\/i\u003E As the emptySet() method doesn't create new objects, they just reuse the existing empty and immutable object. Although, practically, this trick is not that handy, and rarely improves the performance\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EQ6 \u003C\/b\u003E\u003Cb\u003EWhat are the default initial capacity and initial load factor of the HashSet object?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"http:\/\/2.bp.blogspot.com\/-xwtWtaPKvRU\/U0m44L5nwxI\/AAAAAAAAAU4\/Ve0sgn5eTWM\/s1600\/Hashset+interview+questions+and+answers.jpg\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg alt=\"Hashset interview questions and answers\" border=\"0\" height=\"200\" src=\"https:\/\/2.bp.blogspot.com\/-xwtWtaPKvRU\/U0m44L5nwxI\/AAAAAAAAAU4\/Ve0sgn5eTWM\/s1600\/Hashset+interview+questions+and+answers.jpg\" title=\"Hashset interview questions and answers\" width=\"194\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\nAs we already discussed that HashSet internally uses HashMap. The default initial capacity and initial load factor of HashSet are the same as that of HashMap, that is\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDefault Initial Capacity of HashSet Object: 16\u003Cbr \/\u003E\nInitial Load Factor of HashSet Object: 0.75\u003Cbr \/\u003E\n\u003Cbr \/\u003EThe iteration performance of the HashSet object depends on the above two factors which are initial capacity and load factor :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. It is very important not to set the initial capacity too high or the load factor too low if iteration performance is important.\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EQ7 HashMap is not thread-safe so we have ConcurrentHashMap(thread-safe).\u003C\/b\u003E\u003Cbr \/\u003E\u003Cb\u003EWhy Java does not have ConcurrentHashSet class just like ConcurrentHashMap, as we know HashSet is also not thread-safe and internally uses HashMap.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nYou can answer that there is no need to have ConcurrentHashSet class in Java. The reason is you can produce a ConcurrentHashSet backed by ConcurrentHashMap by using the newSetFromMap method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/util\/Collections.html#newSetFromMap%28java.util.Map%29\" rel=\"nofollow\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E, method newSetFromMap is defined as :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\"Returns a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to any Map implementation. There is no need to use this method on a Map implementation that already has a corresponding Set implementation (such as HashMap or TreeMap).\"\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSet\u0026lt;Object\u0026gt;weakHashSet = Collections.newSetFromMap( new WeakHashMap\u0026lt;Object, Boolean\u0026gt;());\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003EParameters:\u003C\/i\u003E\u003Cbr \/\u003E\nmap - the backing map\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003EReturns:\u003C\/i\u003E\u003Cbr \/\u003E\nthe set backed by the map\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EQ8 \u003C\/b\u003E\u003Cb\u003EWhat is HashSet?\u003C\/b\u003E \u003Cb\u003EWhat are the properties of a HashSet object?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/util\/HashSet.html\" rel=\"nofollow\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E,\u003Cbr \/\u003E\n\u003Cblockquote class=\"tr_bq\"\u003EThe HashSet class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.\u003C\/blockquote\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003EOne-line answer:\u003C\/b\u003E\u003C\/i\u003E HashSet allows unique elements in an unordered manner.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Ci\u003EProperties:\u003C\/i\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. HashSet ensures uniqueness, In other words, every object in the HashSet presents only once.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n2. Unlike LinkedList, HashSet does not store the objects in an orderly manner. It is possible that the object which we added first in the HashSet, may appear last in the output.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n3. It offers constant time performance for basic operations like add, remove, contains, and size assuming the hash function disperses the elements properly among the buckets.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n4. HashSet implementation is not synchronized.\u003Cbr \/\u003E\n* If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.\u003Cbr \/\u003E\n*This is typically accomplished by synchronizing on some object that naturally encapsulates the set.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ9 Why does HashSet implementation in Sun Java use HashMap as its backing?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E\u003Cbr \/\u003E\nTo reduce the Duplication of code and make it Memory Efficient, HashSet implements HashMap as its backing. \u003Cbr \/\u003E\n\u003Cbr \/\u003EOn both 32-bit and 64-bit, HashSet is 4x larger than necessary, and HashMap is 2x larger than necessary. HashMap could be implemented with an array with keys and values in it (plus chains for collisions). That means two pointers per entry or 16 bytes on a 64-bit VM. HashSet also uses 32 bytes per element, but the waste is 4x instead of 2x since it only requires 8 bytes per element.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EQ10 \u003C\/b\u003E\u003Cb\u003EWhat interfaces are implemented by the HashSet Class? Which is the superclass of HashSet Class?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHashSet Class implements three interfaces that are Serializable, Cloneable, and Set interfaces.\u003Cbr \/\u003E\nAbstractSet is the superclass of the HashSet Class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E Q11 What is an efficient and correct way to preview information from a HashSet?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\ncontains() method\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPlease mention in the comments if you have any questions or queries related to the HashSet interview questions and answers.\u003Cbr \/\u003E\n"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/6171183603415591163"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/6171183603415591163"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2014\/04\/top-10-hashset-java-interview-questions-collection.html","title":"Top 10 HashSet Java Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/2.bp.blogspot.com\/-xwtWtaPKvRU\/U0m44L5nwxI\/AAAAAAAAAU4\/Ve0sgn5eTWM\/s72-c\/Hashset+interview+questions+and+answers.jpg","height":"72","width":"72"}},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-4587601653805662784"},"published":{"$t":"2023-01-07T11:12:00.000-05:00"},"updated":{"$t":"2023-01-07T11:12:46.046-05:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 10 Java Generics Interview Questions and Answers"},"content":{"type":"html","$t":"In this post, I will be sharing the frequently asked top 10 Java generics interview questions and answers for experienced professionals. Generics was introduced in Java 5. Generics is used for type safety and it automatically manages the casting during compilation. That helps developers write the code without worrying about Type-management. It is introduced to enhance or supplement the Java collections. But we can use those outside collections as well.\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003ETop 10 Java Generics Interview Questions and Answers\u003C\/span\u003E\u003C\/b\u003E\u003C\/h2\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ1. How to create a Generic class in Java?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003EWe can declare a class using the following syntax:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EMyClassGeneric\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003ET\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nNote: We can use any type parameter instead of T.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ2. What are the different type parameters?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nType parameters are the keyword that we provide during declaring the class or method. There are totally different five commonly used type parameters:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n•\tT – Type\u003Cbr \/\u003E\n•\tV – Value\u003Cbr \/\u003E\n•\tK – Key \u003Cbr \/\u003E\n•\tE – Element\u003Cbr \/\u003E\n•\tN – Number\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can use your own customized type parameters as well.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ3. What is the purpose of generics in Java?\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nThis is mostly the first question asked on Generics. Generics in Java are used to provide type safety and to allow developers to write code that can work with multiple types while still providing compile-time type checks. This helps to prevent runtime errors caused by type mismatches and allows for more efficient code, as the compiler can perform more optimizations when it knows the specific types being used. We can divide its usability into major three parts: \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n• \u003Cb\u003EType checking at Compile time\u003C\/b\u003E - The Java compiler applies type checking to generic code at compile time and throes errors for type safety violations.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n• \u003Cb\u003ERemoved the need for implicit casting\u003C\/b\u003E - Java code that uses generics does not need explicit casting, whereas the code that is non-generic requires explicit casting.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n• \u003Cb\u003EGeneric algorithms implementations\u003C\/b\u003E - By using generics, programmers can develop generic algorithms designed to work on collections of different types that are type-safe and easier to use.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ4. Can you use primitives with Java generics? If not, how do you work around this limitation?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nIt is not possible to use primitives with Java generics, as generics only work with reference types. To work around this limitation, you can use the wrapper classes for the corresponding primitive types, such as Integer for int, Double for double, etc. But arrays work with the primitives. This is one of the major differences between arrays and Typed ArrayList.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ5. What is the difference between a raw type and a parameterized type in Java?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nA raw type is a generic type without any type arguments, such as List. A parameterized type is a generic type with one or more type arguments, such as List\u0026lt;String\u0026gt; or ArrayList\u0026lt;Object\u0026gt;. Raw types are not recommended to use, as they can lead to runtime errors due to the lack of type safety. Raw types are still available in Java for backward compatibility but it is strictly recommended not to use the raw types.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ6. Can you override a generic method with a non-generic one in Java? Can you override a non-generic method with a generic one?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can override a generic method with a non-generic one in Java, as the non-generic method will have a more specific type than the generic one.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHowever, you cannot override a non-generic method with a generic one, as the generic method will have a less specific type than the non-generic one.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ7. What is a wildcard in Java generics? Give an example of its usage.\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nA wildcard in Java generics is a placeholder for an unknown type. It is denoted with a question mark (?), and it can be used in situations where the exact type is not important or not known. For example, you might use a wildcard when declaring a method that accepts a list of any type, such as\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EprintArrayList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArrayList\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;?\u0026gt;\u003C\/span\u003E  myList\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nWildcard can be also unbounded and bounded.  Following is the example of an upper bounded wildcard\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EputCars\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECollection\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;?\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eextends\u003C\/span\u003E Car\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E carsList\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nThe above method demonstrates the use of a bounded wildcard in the method parameter.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ8. How do you create a generic array in Java?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nTo create a generic array in Java, you can use the following syntax:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #888888;\"\u003E\/\/ Create an array of a specific type\u003C\/span\u003E\nType\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E array \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EType\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E Object\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Esize\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E\/\/ Create an array of a generic type\u003C\/span\u003E\nE\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E array \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EE\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E Object\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Esize\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nHere, Type is the specific type that you want the array to hold, and E is the generic type parameter. The size parameter specifies the length of the array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nKeep in mind that this syntax is not the recommended way to create a generic array in Java, as it can cause a ClassCastException at runtime. A better way to create a generic array is to use a method that takes an array of the specific type as an argument and returns an array of the generic type, like this:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003ET\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E T\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E createArray\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ET\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E array\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E size\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n   \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ET\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E Array\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EnewInstance\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earray\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgetClass\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E().\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgetComponentType\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(),\u003C\/span\u003E size\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E\/\/ Usage:\u003C\/span\u003E\nString\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E stringArray \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E createArray\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E],\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nThis method uses reflection to create a new array of the same type as the input array, but with a specified length.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ9. How can you suppress unchecked warnings in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nWe can suppress these warnings using the @SuppressWarning(“unchecked”) annotation. This is useful when we are manually casting the Object to other types. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ10. How do you handle exceptions with generics in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nIn Java, you can use generics to specify the types of exceptions that a method can throw. This allows you to specify a specific exception type or a group of exception types that the method can throw, which can be useful for documenting the expected behavior of the method and for handling exceptions in a more type-safe way.\u003Cbr \/\u003E\nHere's an example of how you can use generics to specify the exceptions that a method can throw:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003ET \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eextends\u003C\/span\u003E Exception\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E doSomethingMethod\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrows\u003C\/span\u003E T \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n   \u003Cspan style=\"color: #888888;\"\u003E\/\/ Method body goes here\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nIn this example, the method doSomething can throw any exception type that is a subclass of Exception.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\nYou can also specify multiple exception types using the \u0026amp; operator, like this:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003ET \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eextends\u003C\/span\u003E Exception \u003Cspan style=\"color: #333333;\"\u003E\u0026amp;\u003C\/span\u003E Cloneable\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E doSomethingMethod \u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrows\u003C\/span\u003E T \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Method body goes here\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nThis method can throw any exception type that is a subclass of Exception and implements the Cloneable interface.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen you specify the exception types that a method can throw using generics, you can catch those exceptions using a catch block that specifies the specific exception type, like this:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Etry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n   doSomethingMethod\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ecatch\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EException e\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E   \/\/Exception handling code goes here\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nThis catch block will handle any exception that is thrown by the doSomething Method method and is a subclass of Exception.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E \nThat's all for today. Please mention in the comments if you have any questions related to the top 10 Java Generics interview questions and answers for experienced.\u003Cbr \/\u003E"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4587601653805662784"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4587601653805662784"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2023\/01\/generics-interview-questions.html","title":"Top 10 Java Generics Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-9128143124675549547"},"published":{"$t":"2022-11-30T18:19:00.000-05:00"},"updated":{"$t":"2022-11-30T18:19:43.336-05:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 11 Interview Questions on Static Keyword in Java"},"content":{"type":"html","$t":"In this post, I will be sharing the\u0026nbsp;\u003Cb\u003Etop 11 interview questions on static keyword in Java\u003C\/b\u003E. These questions are mostly asked of\u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/10\/java-interview-questions-for-2-years-experience.html\" target=\"_blank\"\u003E\u003Cb\u003Ejunior Java developers\u003C\/b\u003E\u003C\/a\u003E. Let's dive deep into the topic:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003ERead Also:\u003C\/span\u003E\u003C\/b\u003E\u003C\/i\u003E \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/05\/java-interview-questions-for-5-years-experience.html\" target=\"_blank\"\u003EJava Interview Questions for Senior Java Developers\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EInterview Questions on static keyword in Java\u003C\/span\u003E\u003C\/b\u003E\u003C\/h2\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ1. What is static in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn Java, static is a keyword and non-access modifier. static keyword can be applied to variables, methods, nested classes, and blocks(static block).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ2. Can we override the static method in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, we can not override the static method in Java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ3. Can we overload the static method in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, we can overload the static method in Java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ4. Why main() method is declared as public static in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nI have already shared the answer in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2022\/04\/java-main-method-interview-questions.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E \nQ5. What is a static block in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA static block is a block of code inside a Java class declared with static keyword and opening and closing curly braces. It is executed when a class is first loaded into the JVM i.e. before the main() method. It is mostly used to initialize the static data members as shown below in the example.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003EExample:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EStaticBlockExample\u003C\/span\u003E {\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ static block    \u003C\/span\u003E\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E\n    {\n        \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ static data member\u003C\/span\u003E\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E num = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E10\u003C\/span\u003E; \n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(num);\n    }\n\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString args[]\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Alive is Awesome\"\u003C\/span\u003E);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\n10\u003Cbr \/\u003E\nAlive is Awesome\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ6. Can we have or declare multiple static blocks in our code?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYes, we can have multiple static blocks in our code. They will be executed in the same order they are written.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ7. Can we declare constructors as static in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nConstructor is always invoked with respect to an object while static belongs to the class level. Hence, constructors can not be declared as static in Java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ8. Can we call superclass static method in subclass in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYes, we can call superclass static method in subclass in Java but we cannot override it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ9. Can we access non-static data member in static method in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nNo, we can only access static data members inside the static method in Java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ10. Can we use super or this in static context?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nNo, we can not use super or this in static context.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ11. Is the following line valid:\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, the above line is valid since we can change the order of modifiers in the main() method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nThat's all for today. Please mention in the comments if you have any questions related to interview questions on static keyword in Java. "},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/9128143124675549547"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/9128143124675549547"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2022\/11\/interview-questions-static-keyword.html","title":"Top 11 Interview Questions on Static Keyword in Java"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-4968143168380189742"},"published":{"$t":"2022-11-09T09:04:00.000-05:00"},"updated":{"$t":"2022-11-09T09:04:57.229-05:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 24 Polymorphism Interview Questions and Answers"},"content":{"type":"html","$t":"In this post, I will be sharing the most frequently asked \u003Cb\u003Epolymorphism interview questions and answers in Java\u003C\/b\u003E for both \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/05\/entry-level-interview-questions.html\" target=\"_blank\"\u003E\u003Cb\u003Efresher\u003C\/b\u003E\u003C\/a\u003E and \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/05\/java-interview-questions-for-5-years-experience.html\" target=\"_blank\"\u003E\u003Cb\u003Eexperienced professionals\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #990000;\"\u003E\u003Ci\u003E\u003Cb\u003E\nRead Also:\u003C\/b\u003E\u003C\/i\u003E\u003C\/span\u003E \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2022\/10\/polymorphism-mcq.html\" target=\"_blank\"\u003E\u003Cb\u003EPolymorphism quiz (multiple choice questions and answers)\u003C\/b\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EPolymorphism Interview Questions and Answers\u003C\/span\u003E\u003C\/b\u003E\u003C\/h2\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ1 What is Polymorphism in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYou can find the answer \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/11\/polymorphism-in-java-with-examples.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ2 What are the different types of Polymorphism in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThere are two types of Polymorphism in Java:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. Static Polymorphism (Compile time Polymorphism)\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n2. Dynamic Polymorphism (Runtime Polymorphism)\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ3 What is the difference between Polymorphism and Inheritance in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nThe major differences between Polymorphism and Inheritance in Java are listed below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/12\/inheritance-in-java.html\" target=\"_blank\"\u003E\u003Cb\u003EInheritance\u003C\/b\u003E\u003C\/a\u003E shows the parent-child relationship between two classes whereas Polymorphism takes the advantage of that relationship to make the program more dynamic.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n2. One of the advantages of Inheritance is code reusability in child class. The child class inherits behavior from the parent class whereas Polymorphism enables the child class to redefine already defined behavior inside the parent class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ4 What is static polymorphism (compile time polymorphism) in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nStatic Polymorphism is a form of polymorphism that decides which method to execute at compile time.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ5 How you can achieve static polymorphism in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nStatic polymorphism in Java can be achieved through Method overloading, \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/07\/constructor-overloading-chaining-example-basics-rules-important.html\" target=\"_blank\"\u003E\u003Cb\u003EConstructor overloading\u003C\/b\u003E\u003C\/a\u003E, and Operator overloading.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ6 What is Method overloading in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nYou can find the method overloading in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overloading-in-java-with-examples.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ7 What is dynamic polymorphism (runtime polymorphism) in Java?\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nDynamic polymorphism or runtime polymorphism is a form of polymorphism where a call to an overridden method is resolved at runtime rather than compile-time. It is also known as Dynamic method dispatch.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ8 How you can achieve dynamic polymorphism in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nDynamic polymorphism in Java can be achieved through \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overriding-in-java-with-examples.html\" target=\"_blank\"\u003E\u003Cb\u003EMethod overriding\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ9 Output of this program is :\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer3\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EPolymorphismDemoClass\u003C\/span\u003E \u003C\/span\u003E{\n    \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Em1\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E x\u003C\/span\u003E)\u003C\/span\u003E{\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"One\"\u003C\/span\u003E);\n    }\n}\n\u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EPolymorphismDemoClassChild\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eextends\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EPolymorphismDemoClass\u003C\/span\u003E \u003C\/span\u003E{\n\n}\n\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest\u003C\/span\u003E\u003C\/span\u003E{\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E[] args\u003C\/span\u003E)\u003C\/span\u003E{\n        PolymorphismDemoClass parent = \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E PolymorphismDemoClassChild();\n        parent.m1(\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E PolymorphismDemoClass());\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\na. Compile time error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Runtime error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Prints One\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Compiles properly and prints nothing\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is A.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ10 Which statements are true\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\na. Private methods can be overridden\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Static binding means overloading\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Overriding can only be done in subclass\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Main method can be overloaded\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answers are B, C, AND D.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ11 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer4\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EBoxing1\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E x\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"prints int\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger i\u003C\/span\u003E)\u003C\/span\u003E { \n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"prints Integer\"\u003C\/span\u003E);\n    }\n\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString args[]\u003C\/span\u003E)\u003C\/span\u003E{\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eshort\u003C\/span\u003E no=\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E10\u003C\/span\u003E;\n        method(no);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. prints int then prints Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. prints int\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. prints Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is C.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ12 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer5\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EPolymorphismDemoClass3\u003C\/span\u003E \u003C\/span\u003E{\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger x\u003C\/span\u003E)\u003C\/span\u003E {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger... x\u003C\/span\u003E)\u003C\/span\u003E {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer...\"\u003C\/span\u003E);\n    }\n\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E args[]\u003C\/span\u003E)\u003C\/span\u003E {\n        int x=\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E251\u003C\/span\u003E;\n        overloadedMethod(x);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Integer...\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Integer then Integer....\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is A.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ13 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer6\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EPolymorphismDemoClass4\u003C\/span\u003E\u003C\/span\u003E{\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003ELong l\u003C\/span\u003E)\u003C\/span\u003E {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Long\"\u003C\/span\u003E);\n    }\n\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E args[]\u003C\/span\u003E)\u003C\/span\u003E{\n        int a=\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E30\u003C\/span\u003E;\n        overloadedMethod(a);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. prints Long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Runtime Exception\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. None\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is A.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ14 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer7\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"int\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Elong\u003C\/span\u003E i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"long\"\u003C\/span\u003E);\n    }   \n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString a[]\u003C\/span\u003E)\u003C\/span\u003E {\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        Test.print(i);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. int\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Runtime Exception\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is C.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ15 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer8\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest2\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003ELong i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Long\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString a[]\u003C\/span\u003E)\u003C\/span\u003E {\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        Test2.print(i);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Runtime Exception\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is C.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ16 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer9\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest3\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EDouble i\u003C\/span\u003E)\u003C\/span\u003E{\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Double\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003ELong i\u003C\/span\u003E)\u003C\/span\u003E{\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Long\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString a[]\u003C\/span\u003E)\u003C\/span\u003E{\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        Test3.print(i);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. prints Long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. prints Double\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Runtime Exception\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is A.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ17 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer10\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest4\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EDouble i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Double\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003ELong i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Long\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003ENumber i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Number\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString a[]\u003C\/span\u003E)\u003C\/span\u003E {\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        Test4.print(i);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Double\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Number\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is D.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ18 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer11\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest5\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EDouble i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Double\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003ELong i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Long\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString a[]\u003C\/span\u003E)\u003C\/span\u003E{\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Ebyte\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        Test5.print(i);\n    }\n}\n\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. prints Long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. prints Double\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. prints Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is A.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ19 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer12\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest6\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Ebyte\u003C\/span\u003E... i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"byte\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Elong\u003C\/span\u003E i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"long\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString a[]\u003C\/span\u003E)\u003C\/span\u003E {\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Ebyte\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        Test6.print(i);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. byte\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is B.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ20 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer14\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest7\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E i\u003C\/span\u003E)\u003C\/span\u003E {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"int\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString a[]\u003C\/span\u003E)\u003C\/span\u003E {\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Ebyte\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        Test7.print(i);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. int\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Runtime error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is B.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ21 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer15\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest8\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString[] args\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E i = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        overloadedMethod(i);\n    }\n\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger x\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer\"\u003C\/span\u003E);\n    }\n\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Elong\u003C\/span\u003E x\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"long\"\u003C\/span\u003E);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. long\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. long Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is B.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ22 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer16\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest9\u003C\/span\u003E {\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString[] args\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Ebyte\u003C\/span\u003E b = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E5\u003C\/span\u003E;\n        overloadedMethod(b);\n    }\n\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E (\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EObject b\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        Byte obj = (Byte)b;\n        System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"byte: \"\u003C\/span\u003E+obj);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. byte : memory address of object obj\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. byte: 5\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. Runtime error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is C.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ23 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer17\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest10\u003C\/span\u003E\n\u003C\/span\u003E{\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Overloaded methods\u003C\/span\u003E\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger i\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer \"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E name\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"String \"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EDouble name\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Double \"\u003C\/span\u003E);\n    }\n\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Driver code\u003C\/span\u003E\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E [] args\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        Test10 obj = \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E Test10();\n        obj.overloadedMethod(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Enull\u003C\/span\u003E);\n    }\n}\n\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. prints Double\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. prints Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. prints String\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is A.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ24 Output of the program is\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer18\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest11\u003C\/span\u003E\n\u003C\/span\u003E{\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Overloaded methods\u003C\/span\u003E\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EInteger x\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Integer\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E name\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"String\"\u003C\/span\u003E);\n    }\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EoverloadedMethod\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EDouble name\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Double \"\u003C\/span\u003E);\n    }\n\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E [] args\u003C\/span\u003E)\u003C\/span\u003E\n    {\n        Test11 obj = \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E Test11();\n        Integer arg = \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Enull\u003C\/span\u003E;\n        obj.overloadedMethod(arg);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\na. Compile Time Error\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Double\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. Integer\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nd. String\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cblockquote style=\"border-left: 8px solid green; color: #4f4f4f; line-height: 1.8; padding-bottom: 20px; padding-left: 15px; padding-top: 20px;\"\u003E\u003Cspan face=\"Lato, \u0026quot;Helvetica Neue\u0026quot;, Helvetica, Roboto, Arial, sans-serif\" style=\"color: #0a0a0a; font-size: medium; font-weight: 700;\"\u003EThe correct answer is C.\u003C\/span\u003E\u003C\/blockquote\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today. Please mention in the comments if you have faced any other Polymorphism interview question that is not present in the above list.\u003Cbr \/\u003E"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4968143168380189742"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4968143168380189742"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2022\/11\/polymorphism-interview-questions.html","title":"Top 24 Polymorphism Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-8209943854313377566"},"published":{"$t":"2022-06-18T05:59:00.000-04:00"},"updated":{"$t":"2022-06-18T05:59:33.166-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java8"}],"title":{"type":"text","$t":"Top 10 Stream API Interview Questions and Answers"},"content":{"type":"html","$t":"In this post, I will be sharing top 10 \u003Cb\u003EStream API interview questions and answers for experienced Java professionals\u003C\/b\u003E. I have already shared the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/05\/java-8-coding-and-programming-interview-questions.html\" target=\"_blank\"\u003Efrequently asked Java 8 programming interview questions\u003C\/a\u003E. Please bookmark this page, I will keep adding more Stream related interview questions to this post.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cspan\u003E\u003Cspan style=\"color: #990000;\"\u003E\u003Ci\u003E\u003Cb\u003E\nRead Also:\u003C\/b\u003E\u003C\/i\u003E\u003C\/span\u003E\u003C\/span\u003E \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/java-8-interview-questions-and-answers.html\" target=\"_blank\"\u003EJava 8 Interview Questions and Answers\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EStream Interview Questions and Answers\u003C\/span\u003E\u003C\/b\u003E\u003C\/h2\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ1 Why Stream API is used?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003EStreams tend to not\u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/04\/fail-fast-iterator-vs-fail-safe-iterator-difference-with-example-in-java.html\" target=\"_blank\"\u003Emodify the underlying collection\u003C\/a\u003E. In a stream pipeline every element is visited only once.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFunctional programming, Stream provides the most natural \u0026amp; convenient way to apply functions to sequences of objects.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ2 What is intermediate operation?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nIntermediate operation process the current stream data (if any) and then return a new stream.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen an intermediate operation is executed, it actually does not do anything but actually creates a new stream.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen we will traverse this newly created stream, we will see that this stream contains data of the initial stream that satisfies the predicate defined by the stream.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\nExamples:\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003Emap\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nlimit\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nfilter\u003Cspan style=\"color: #333333;\"\u003E()\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\nskip\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nflatMap\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nsorted\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\ndistinct\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\npeek\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E    \nQ3 What is terminal operation?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nTerminal operations as the name suggests are the last in pipeline of operations performed on a stream.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\nTerminal operation traverses the stream and either produces a result or a collection but not a new stream.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA stream pipeline consists of a source (such as a Collection, an array, a generator function, or an I\/O channel), it will invoke all the intermediate operations in the pipeline and lastly the terminal operation is performed which marks the stream pipeline considered consumed and is marked closed.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWe can have only one terminal operation at the end of the pipeline. If any operation performed on closed stream, it will result in a \u003Cb\u003Ejava.lang.IllegalStateException: stream has already been operated upon or closed\u003C\/b\u003E.\u003Cbr \/\u003E\u003Cb\u003E\n\u003Cbr \/\u003E  \nExamples\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003Ecollect\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nforEach\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nforEachOrdered\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nfindAny\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nfindFirst\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\ntoArray\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nreduce\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\ncount\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nmin\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nmax\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nanyMatch\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nallMatch\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\nnoneMatch\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E    \nQ4 What is stateful and stateless intermediate operations?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nStateful operations are skip(), distinct(),limit() and sorted(). Rest all other stream operations are stateless.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen an operation requires to retain the information of the elements\/values it has processed so far in order to process the current element\/value then it is a stateful operation.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\nFor ex: Distinct operation requires to keep track of all the values it has processed so far, based on this information only it can decide that whether the current value is a unique value or it have been processed before and accordingly either will add the current value to the new stream(which is the output of the distinct operation) or neglect the value and not add it to the new stream.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\nQ5 How exceptions can be handled, in Stream API operations?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nBelow we have described a way to make sure the stream pipeline execution finishes and making sure all exceptions traces are kept.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer1\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eimport\u003C\/span\u003E java.io.IOException;\n\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eimport\u003C\/span\u003E java.util.ArrayList;\n\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eimport\u003C\/span\u003E java.util.List;\n\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eimport\u003C\/span\u003E java.util.stream.Collectors;\n\n\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest\u003C\/span\u003E \u003C\/span\u003E{\n\n\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eprivate\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E value;  \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/member variable\u003C\/span\u003E\n\n        \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Argument Constructor to initialize member variable value\u003C\/span\u003E\n        \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ with the value passed as argument while\u003C\/span\u003E\n        \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ object of the class is created\u003C\/span\u003E\n\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ETest\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E value\u003C\/span\u003E)\u003C\/span\u003E {\n\t\t\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Esuper\u003C\/span\u003E();\n\t\t\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Ethis\u003C\/span\u003E.value = value;\n\t}\n        \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/Getter Setter\u003C\/span\u003E\n\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EgetValue\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003C\/span\u003E)\u003C\/span\u003E {\n\t\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ereturn\u003C\/span\u003E value;\n\t}\n\n\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EsetValue\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E value\u003C\/span\u003E)\u003C\/span\u003E {\n\t\t\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Ethis\u003C\/span\u003E.value = value;\n\t}\n        \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/Member method\u003C\/span\u003E\n\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E doSomething(Test \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eobject\u003C\/span\u003E) throws IOException {\n\t\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Etry\u003C\/span\u003E {\n\t\t\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ereturn\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eobject\u003C\/span\u003E.getValue().toString();\n\t\t} \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ecatch\u003C\/span\u003E (Exception e) {\n\t\t\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ethrow\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E RuntimeException(e);\n\t\t}\n\t}\n\n\t\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E[] args\u003C\/span\u003E)\u003C\/span\u003E {\n\t\tList\u0026lt;Test\u0026gt; myList = \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E ArrayList\u0026lt;\u0026gt;(); \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/List of type Test class\u003C\/span\u003E\n\t\tmyList.add(\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E Test(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"test1\"\u003C\/span\u003E));\n\t\tmyList.add(\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E Test(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"test2\"\u003C\/span\u003E));\n\t\tmyList.add(\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E Test(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Enull\u003C\/span\u003E));\n\n\t\tList\u0026lt;Throwable\u0026gt; exceptions = \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E ArrayList\u0026lt;\u0026gt;(); \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ List of type Throwable class\u003C\/span\u003E\n        \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/creating stream from myList\t\t\u003C\/span\u003E\n        myList.stream().map(item -\u0026gt; {\n\t\t        \t        \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Etry\u003C\/span\u003E {\n\t\t        \t                \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ereturn\u003C\/span\u003E doSomething(item); \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ executing this line for each element in myList\u003C\/span\u003E\n\t\t\t                } \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ecatch\u003C\/span\u003E (IOException e) {\n\t\t                \t        exceptions.add(e); \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ adding exceptions as they occur to the list exceptions\u003C\/span\u003E\n\t\t\t\t                \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ereturn\u003C\/span\u003E \u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"\"\u003C\/span\u003E; \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ returning blank String in case of exception\u003C\/span\u003E\n\t\t\t                }\n\t\t                    }).filter(s -\u0026gt; s.length() \u0026gt; \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E0\u003C\/span\u003E).collect(Collectors.toList()); \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ collecting results in the list\u003C\/span\u003E\n\t}\n} \n\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EOutput:\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\nException in thread \"main\" java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke \"String.toString()\" because the return value of \"Test.getValue()\" is null\u003Cbr \/\u003E\n\u0026nbsp;\u0026nbsp;\u0026nbsp;at Test.doSomething(Test.java:30)\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHere we are keeping all the exception traces in a list that we can iterate to perform any further processing if needed or simply log them.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ6 How Stream API is used to handle infinite data?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nFirst, we create a stream object based on this infinite data\u003Cbr \/\u003E\nFor example \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003EStream stream \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Stream\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eiterate\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E value \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E value \u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\nHere we are using iterate method of stream API to create a stream object of infinite data.\u003Cbr \/\u003E \nValue starts with zero and then keeps incrementing by one. Iterate is an intermediate operation.\u003Cbr \/\u003E\nBefore applying a terminal operation we will have to limit data else the processing will never end as its infinite data.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLimit function is applied to limit the data.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003EStream\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eiterate\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E value \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E value \u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E).\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elimit\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E).\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ecollect\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECollectors\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EtoSet\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E());\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nSo here, we are creating a set of first 15 values out from the infinite data.\u003Cbr \/\u003E\nSkip method can also be used here\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003EStream\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eiterate\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E value \u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E\u0026gt; value \u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E).\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eskip\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E5\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E).\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elimit\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E).\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ecollect\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECollectors\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EtoSet\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E());\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nHere we skipped first five values and collected the next 15 values.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ7 What does short-circuit behavior of Streams mean?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nShort-circuit is an optimization technique supported by Stream API. It means that when we are processing input data, we arrive at a result, even before evaluating all the input data.\u003Cbr \/\u003E\nIt supports many short-circuiting operations like findFirst, allMatch, anyMatch, limit, findAny.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor example, when we use the findFirst operation of the Stream API, it returns the first element of the stream and will not process the rest of the elements of the stream.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe Stream API supports short-circuiting of both intermediate and terminal operations.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe only intermediate short-circuiting method currently defined in the Stream API is limit.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\nQ8 How Stream API supports parallelism?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nStream API uses fork and join default pool to achieve parallelism. Number of threads in the default pool is equivalent to the number of processor cores.\u003Cbr \/\u003E\nUsage:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cdiv style=\"background: rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003EOptional\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003Einteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E result \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E list\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EparallelStream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E().\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ereduce\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E((\u003C\/span\u003Ex\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003Ey\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E x\u003Cspan style=\"color: #333333;\"\u003E*\u003C\/span\u003Ey\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nReduce operation repeatedly applies a binary operation to every element in the stream. It takes two arguments, first argument is either the first element in the stream or the result of the previous reduce operation and second argument is the current element of the stream.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nReducing is the repeated process of combining all elements. Reduce operation applies a binary operation to each element in the stream where the first argument to the operation is the return value of the previous application and second argument is the current stream element.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\nQ9 What are the performance implications of using parallel streams?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E  \nWhen using parallel stream we need to consider few things like sometimes managing the splitting of source data between multiple threads and then merging the results becomes a very expensive task in comparison to the actual work itself and then also we may have to deal with issues related to memory locality .\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\nQ10 In case of an array what should be used Loop or Stream?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\nWhen we use for loop to iterate over an array it’s very lightweight in terms of CPU usage and heap memory.\nIf we want to consider only thriftiness of memory and raw speed then we should not use stream to iterate over an array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today. Please mention in the comments if you have faced a different Stream API based question in the interview.\u0026nbsp;"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/8209943854313377566"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/8209943854313377566"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2022\/06\/stream-interview-questions-and-answers.html","title":"Top 10 Stream API Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-6982877555525080756"},"published":{"$t":"2022-04-05T14:43:00.001-04:00"},"updated":{"$t":"2022-12-31T08:26:38.576-05:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 8 Java main method interview questions with answers [2023]"},"content":{"type":"html","$t":"In this post, I will be sharing the top 10 \u003Cb\u003EJava main method interview questions with answers\u003C\/b\u003E for both freshers and experienced. If you are \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/10\/java-interview-questions-for-2-years-experience.html\" target=\"_blank\"\u003E2 or 3 years of experience\u003C\/a\u003E, then you might get interview questions from the main() method topic. \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2021\/05\/java-interview-questions-for-5-years-experience.html\" target=\"_blank\"\u003ESenior Java developers\u003C\/a\u003E need not worry as it is rare you will be asked \u003Ca href=\"https:\/\/javahungry.blogspot.com\/p\/interview.html\" target=\"_blank\"\u003Einterview questions\u003C\/a\u003E on this topic. But it is good to have knowledge about the main() method and the\u0026nbsp; interviewer may choose any question. Let's dive deep into the main() method interview questions:\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EJava main() method Interview Questions with Answers\u003C\/b\u003E\u003C\/span\u003E\u003C\/h2\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ1 Why the main() method is public static in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the most important questions in the main() method interview questions.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Ea. Why the main() method is static in Java\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAs we all know at the time of starting JVM, there is no object of a class. We add the static keyword to the main() method so that JVM can load the class into the main memory.\u003Cbr \/\u003E \n\u003Cbr \/\u003E\nIf we declare the main() method as non-static then JVM can not call it without creating the object of the class which contains the main() method. \u003Cbr \/\u003E \n\u003Cbr \/\u003E\nIf we try to create an object, it has to call the constructor of that class. There would be ambiguity if there is more than one constructor present in the class - which constructor should be called by JVM and what parameters should be passed?\u003Cbr \/\u003E \n\u003Cbr \/\u003E\n\u003Cb\u003Eb. Why main() method is public in Java\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003EA method with a public modifier can be accessed or invoked by anyone. The reason behind making the\u0026nbsp; main() method public is that it has to be invoked by the JVM. If you assign an access modifier other than public to the main() method then compilation will be successful but you will get the runtime error \"main method not found\".\u003Cbr \/\u003E    \n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ2. Can we overload the main() method in Java?\u003C\/b\u003E \n\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nYes, we can \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overloading-in-java-with-examples.html\" target=\"_blank\"\u003Eoverload\u003C\/a\u003E the main() method in Java. In other words, a Java class can have any number of main() methods with different parameters. But execution will only start from the main() method with the following signatures:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E main(\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003EString\u003C\/span\u003E[] args)\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nor,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer2\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E main(\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003EString\u003C\/span\u003E args...)\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLet's find out with the help of an example that the main() method can be overloaded in Java:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E\n \u003Ccode id=\"htmlViewer3\" style=\"color:rgb(192, 197, 206); font-weight:400;background-color:rgb(28, 29, 33);background:rgb(28, 29, 33);display:block;padding: .5em;overflow:auto;width:auto;\"\u003E\u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Eimport\u003C\/span\u003E java.util.*;\n\u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color:rgb(192, 197, 206); font-weight:400;\"\u003E\u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color:rgb(120, 187, 101); font-weight:400;\"\u003EMainOverloadExample\u003C\/span\u003E \u003C\/span\u003E{\n    \u003Cspan style=\"color:rgb(182, 177, 139); font-weight:400;\"\u003E\/\/ Real main() method with String[] args \u003C\/span\u003E\n    \u003Cspan style=\"color:rgb(192, 197, 206); font-weight:400;\"\u003E\u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color:rgb(120, 187, 101); font-weight:400;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color:rgb(231, 206, 86); font-weight:400;\"\u003E(String[] args)\u003C\/span\u003E \u003C\/span\u003E{\n      System.out.println(\u003Cspan style=\"color:rgb(79, 180, 215); font-weight:400;\"\u003E\u0026quot;String[] args main method\u0026quot;\u003C\/span\u003E);\n      main(\u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Enew\u003C\/span\u003E Integer[]{\u003Cspan style=\"color:rgb(231, 206, 86); font-weight:400;\"\u003E10\u003C\/span\u003E, \u003Cspan style=\"color:rgb(231, 206, 86); font-weight:400;\"\u003E100\u003C\/span\u003E, \u003Cspan style=\"color:rgb(231, 206, 86); font-weight:400;\"\u003E1000\u003C\/span\u003E});\n    }\n\n    \u003Cspan style=\"color:rgb(182, 177, 139); font-weight:400;\"\u003E\/\/ Overloaded main() method with Integer[] args\u003C\/span\u003E\n    \u003Cspan style=\"color:rgb(192, 197, 206); font-weight:400;\"\u003E\u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color:rgb(180, 94, 164); font-weight:400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color:rgb(120, 187, 101); font-weight:400;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color:rgb(231, 206, 86); font-weight:400;\"\u003E(Integer[] args)\u003C\/span\u003E \u003C\/span\u003E{\n        System.out.println(\u003Cspan style=\"color:rgb(79, 180, 215); font-weight:400;\"\u003E\u0026quot;Integer[] args main method\u0026quot;\u003C\/span\u003E);\n        System.out.println(\u003Cspan style=\"color:rgb(79, 180, 215); font-weight:400;\"\u003E\u0026quot;Printing Integer[] args: \u0026quot;\u003C\/span\u003E + Arrays.toString(args));\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\nString[] args main method\u003Cbr \/\u003E\nInteger[] args main method\u003Cbr \/\u003E\nPrinting Integer[] args: [10, 100, 1000]\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ3. Can we override the main() method in Java?\u003C\/b\u003E \n\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nNo, \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overriding-in-java-with-examples.html\"\u003Eoverriding\u003C\/a\u003E the main() method is not allowed. Why? because in Java static method is bonded during compile-time and you can not override the static method in Java. If you define a static method in the child class(subclass) with the same method signature as the parent class(superclass) then it is called\u0026nbsp; method hiding in Java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ4. What are the valid access modifiers(public, protected, default, private) for the main() method in Java?\u003C\/b\u003E \n\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003EThe public is the only valid \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/12\/access-modifiers-in-java.html\" target=\"_blank\"\u003Eaccess modifier\u003C\/a\u003E for the main() method in Java. You will get a runtime error if you use a private, protected, or default access modifier with the main() method in Java as shown below in the example:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer4\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EMainExample2\u003C\/span\u003E \u003C\/span\u003E{\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Using protected access modifier for main() method\u003C\/span\u003E\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eprotected\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E[] args\u003C\/span\u003E)\u003C\/span\u003E {\n      System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Inside main method\"\u003C\/span\u003E);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\nError: Main method not found in class MainExample2, please define the main method as:\u003Cbr \/\u003E\n\u0026nbsp;\u0026nbsp;public static void main(String[] args)\u003Cbr \/\u003E\nor a JavaFX application class must extend javafx.application.Application\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ5. Can we make the main() method final in Java?\u003C\/b\u003E \n\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E \nYes, you can make the main() method \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/01\/final-finally-finalize.html\" target=\"_blank\"\u003Efinal in Java\u003C\/a\u003E. Like any final method, the main() method can not be overridden since it is static. Please find the example below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer5\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EMainExample3\u003C\/span\u003E \u003C\/span\u003E{\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Using final keyword with main() method\u003C\/span\u003E\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Efinal\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E(String[] args)\u003C\/span\u003E \u003C\/span\u003E{\n      System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"we can make main() method final\"\u003C\/span\u003E);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\nwe can make main() method final\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ6. Can we make the main() method synchronized in Java?\u003C\/b\u003E \n\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E \nYes, we can make main() method synchronized in Java as a synchronized modifier is allowed in the main() function method signature as shown below in the example:\u003Cbr \/\u003E \n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer6\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EMainExample4\u003C\/span\u003E \u003C\/span\u003E{\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Using synchronized keyword with main() method\u003C\/span\u003E\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Esynchronized\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E(String[] args)\u003C\/span\u003E \u003C\/span\u003E{\n      System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Valid to use synchronized keyword with main() method\"\u003C\/span\u003E);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\nValid to use synchronized keyword with main() method\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ7. Can we change the return type of the main() method in Java?\u003C\/b\u003E\u003C\/span\u003E \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E \nNo, we can not change the return type of the main() method in Java. It should be void. We will get a runtime error when we change the return type of the main() method in Java as shown below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer7\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003EMainExample5\u003C\/span\u003E {\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Change the return type of main() method\u003C\/span\u003E\n    \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Eint\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString[] args\u003C\/span\u003E)\u003C\/span\u003E {\n      System.\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Changed the return type of main() method\"\u003C\/span\u003E);\n      \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Ereturn\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E0\u003C\/span\u003E;\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\nError: Main method must return a value of type void in class MainExample5, please\u003Cbr \/\u003E \ndefine the main method as:\u003Cbr \/\u003E\n\u0026nbsp;\u0026nbsp;public static void main(String[] args)\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ8. How to call a non-static method from the main() method in Java?\u003C\/b\u003E\u003C\/span\u003E \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nAlways remember, non-static methods can not be called directly from the static context. First, you need to create the object of the class containing both static and non-static methods inside the static method. The next step is to call a non-static method using that object as shown below in the example:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer8\" style=\"background-color: #1c1d21; background: rgb(28, 29, 33); color: #c0c5ce; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003ECallingTest\u003C\/span\u003E \u003C\/span\u003E{\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ main() static method\u003C\/span\u003E\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003EString\u003C\/span\u003E[] args\u003C\/span\u003E)\u003C\/span\u003E {\n      \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ Creating CallingTest class object    \u003C\/span\u003E\n      CallingTest obj = \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Enew\u003C\/span\u003E CallingTest();\n      \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/* Calling non-static print() method from \n      static main() method *\/\u003C\/span\u003E\n      obj.print();\n    }\n    \u003Cspan style=\"color: #b6b18b; font-weight: 400;\"\u003E\/\/ print() non-static method\u003C\/span\u003E\n    \u003Cspan style=\"color: #b45ea4; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #c0c5ce; font-weight: 400;\"\u003E\u003Cspan style=\"color: #78bb65; font-weight: 400;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #e7ce56; font-weight: 400;\"\u003E\u003C\/span\u003E)\u003C\/span\u003E {\n      System.out.println(\u003Cspan style=\"color: #4fb4d7; font-weight: 400;\"\u003E\"Non-static method is called inside static main method\"\u003C\/span\u003E);\n    }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\nNon-static method is called inside static main method\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today, please mention in the comments in case you have any questions related to Java main method interview questions with answers."},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/6982877555525080756"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/6982877555525080756"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2022\/04\/java-main-method-interview-questions.html","title":"Top 8 Java main method interview questions with answers [2023]"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-7233996060227461696"},"published":{"$t":"2021-10-10T12:38:00.000-04:00"},"updated":{"$t":"2021-10-10T12:38:40.835-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 16 Java Interview Questions for 5 to 6 Years Experience "},"content":{"type":"html","$t":"In this post, I will be sharing the most frequently asked java interview questions for 5 to 6 years experienced. I have covered the questions from topics such as Java 8, Serialization, Design-patterns, String, etc. Please bookmark this page, I will keep adding more questions to this post. Let's dive deep into the interview questions.\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EJava Interview Questions for 5 to 6 Years Experience\u003C\/b\u003E\u003C\/span\u003E\u003C\/h2\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n1. What is the unique feature of Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPlatform independence is a unique feature in Java. The \"Write once run anywhere\" feature means that the compiled Java bytecodes can run in any operating system irrespective of the operating systems. This unique feature makes Java different from other programming languages.\u003Cbr \/\u003E\nYou can find it in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/05\/features-of-java.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n2. What is an instance and a local variable?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAn instance variable is a variable that is declared within a class and outside the method or a constructor or a block.\u0026nbsp;\u003Cbr \/\u003E\nA local variable is a variable that is declared within a method or a constructor or a block. The scope of the variable is finished within a method or a block.\u003Cbr \/\u003E\nYou can find about types of variables in Java in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/02\/variable-shadowing-and-variable-hiding.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n3. Difference between String and String pool?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA string pool is a storage area in heap memory where string literals are stored. When we create a string literal, the JVM checks for the string in the String pool. If the string literal is already found in the pool, it returns a reference of that string. If the string is not found, it creates a new string literal in the String pool.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\n4. Difference between == and equals() method in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn simple words, we can say that the == operator compares the object reference variables i.e. checks whether the objects refer to the same memory location and the equals() methods compare the values of the objects. You can find it in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-equals-and-double-equals-method-with-example-java-collections-interview-question.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer\" style=\"background-color: #2b2b2b; background: rgb(43, 43, 43); color: #f8f8f2; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #00e0e0; font-weight: 400;\"\u003EEqualsMethod\u003C\/span\u003E {\n  \u003Cspan style=\"color: #f8f8f2; font-weight: 400;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #00e0e0; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003EString[] args\u003C\/span\u003E)\u003C\/span\u003E\n  {\n    String s1 = \u003Cspan style=\"color: #abe338; font-weight: 400;\"\u003E\"hello world\"\u003C\/span\u003E;\n    String s2 = \u003Cspan style=\"color: #abe338; font-weight: 400;\"\u003E\"hello world\"\u003C\/span\u003E;\n    String s3 = \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Enew\u003C\/span\u003E String(\u003Cspan style=\"color: #abe338; font-weight: 400;\"\u003E\"hello world\"\u003C\/span\u003E);\n    System.\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(s1 == s2); \u003Cspan style=\"color: #d4d0ab; font-weight: 400;\"\u003E\/\/ true\u003C\/span\u003E\n    System.\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(s1 == s3); \u003Cspan style=\"color: #d4d0ab; font-weight: 400;\"\u003E\/\/ false\u003C\/span\u003E\n    System.\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(s1.\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eequals\u003C\/span\u003E(s2)); \u003Cspan style=\"color: #d4d0ab; font-weight: 400;\"\u003E\/\/ true\u003C\/span\u003E\n    System.\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eout\u003C\/span\u003E.println(s1.\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eequals\u003C\/span\u003E(s3)); \u003Cspan style=\"color: #d4d0ab; font-weight: 400;\"\u003E\/\/ true\u003C\/span\u003E\n  }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003Etrue\u003Cbr \/\u003E\nfalse\u003Cbr \/\u003E\ntrue\u003Cbr \/\u003E\ntrue\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n5. List some of the features in Java 8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n• Lambda expressions.\u003Cbr \/\u003E\n• Stream API.\u003Cbr \/\u003E\n• forEach method.\u003Cbr \/\u003E\n• Optional class.\u003Cbr \/\u003E\n• Functional interfaces.\u003Cbr \/\u003E\n• Default methods inside interfaces.\u003Cbr \/\u003E\n• Date\/Time API.\u003Cbr \/\u003E\n• Collectors and StringJoiner class.\u003Cbr \/\u003E\n• IO and JDBC enhancements. \u003Cbr \/\u003E\nYou can find the features of Java8 in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/10\/oracle-java-8-new-features-lambda-expressions-optional-defender-methods-example.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\n6. What is a Lambda expression and its syntax?\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA lambda expression is a short block of code that takes in parameters and returns a value. It is similar to the method but doesn't contain a name and be implemented in the body of the method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\nLambda Expression Syntax:\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer2\" style=\"background-color: #2b2b2b; background: rgb(43, 43, 43); color: #f8f8f2; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E(argument-list) -\u0026gt; {\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Ebody\u003C\/span\u003E}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA lambda expression consists of three elements,\u003Cbr \/\u003E\n1. Argument-list: It can be empty or non-empty.\u003Cbr \/\u003E\n2. Arrow-token: It is used to link the argument lists and body of expression.\u003Cbr \/\u003E\n3. Body: It contains expressions and statements for lambda expressions.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nUse a lambda expression in the ArrayList's forEach() method to print each element in the list:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer3\" style=\"background-color: #2b2b2b; background: rgb(43, 43, 43); color: #f8f8f2; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eimport\u003C\/span\u003E java.util.ArrayList;\n\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #f8f8f2; font-weight: 400;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #00e0e0; font-weight: 400;\"\u003ELambdaExp\u003C\/span\u003E \u003C\/span\u003E{\n  \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #f8f8f2; font-weight: 400;\"\u003E\u003Cspan style=\"color: #00e0e0; font-weight: 400;\"\u003Emain\u003C\/span\u003E(\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003E\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003EString\u003C\/span\u003E[] args\u003C\/span\u003E)\u003C\/span\u003E {\n    ArrayList numberList = \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Enew\u003C\/span\u003E ArrayList();\n    numberList.add(\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003E5\u003C\/span\u003E);\n    numberList.add(\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003E8\u003C\/span\u003E);\n    numberList.forEach( (\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003Enumber\u003C\/span\u003E) -\u0026gt; { System.out.print(\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003Enumber\u003C\/span\u003E); } );\n  }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E58\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n7. What is the difference between Collection and Streams in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003ECollection is used to store the data in a particular data structure like List, Set, or Map. But, streams are used to perform operations like filtering, matching, mapping, etc. on stored data such as arrays, a\u0026nbsp; collection like lists, or I\/O resources.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E  \nExample\u003C\/b\u003E: \u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer4\" style=\"background-color: #2b2b2b; background: rgb(43, 43, 43); color: #f8f8f2; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eimport\u003C\/span\u003E java.util.List;\n\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eimport\u003C\/span\u003E java.util.ArrayList;\n\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #f8f8f2; font-weight: 400;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #00e0e0; font-weight: 400;\"\u003ECollectionStream\u003C\/span\u003E\u003C\/span\u003E{\n  \u003Cspan style=\"color: #f8f8f2; font-weight: 400;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #00e0e0; font-weight: 400;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003E(String args[])\u003C\/span\u003E \u003C\/span\u003E{\n    List names = \u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Enew\u003C\/span\u003E ArrayList\u0026lt;\u0026gt;();\n    names.add(\u003Cspan style=\"color: #abe338; font-weight: 400;\"\u003E\"Kamal\"\u003C\/span\u003E);\n    names.add(\u003Cspan style=\"color: #abe338; font-weight: 400;\"\u003E\"Ram\"\u003C\/span\u003E);\n    names.add(\u003Cspan style=\"color: #abe338; font-weight: 400;\"\u003E\"Madhavi\"\u003C\/span\u003E);\n    names.add(\u003Cspan style=\"color: #abe338; font-weight: 400;\"\u003E\"Kamal\"\u003C\/span\u003E);\n    \u003Cspan style=\"color: #d4d0ab; font-weight: 400;\"\u003E\/\/ Use Streams to select unique names\u003C\/span\u003E\n    names.stream().distinct().forEach(System.out::println); \n  }\n}\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003Cbr \/\u003E\nKamal\u003Cbr \/\u003E\nRam \u003Cbr \/\u003E\nMadhavi\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n8. What are the advantages of design patterns in Java?\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \n• The design patterns provide a solution to define a software architecture.\u003Cbr \/\u003E\n• The design patterns are reusable in multiple projects.\u003Cbr \/\u003E\n• It is an outcome of experienced software developers.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\n9. What are functional or SAM interfaces?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \nThe functional interface is an interface with only one abstract method. So, it is also called the Single Abstract Method (SAM) interface. Functional interfaces can have any number of default, static, and overridden methods. To declare a functional interface, we can use \u003Cb\u003E@FunctionalInterface\u003C\/b\u003E annotation but it is optional. If this annotation is used for interfaces with more than one abstract method, it will generate a compiler error.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n10. What is the method reference in Java 8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \nMethod reference is used to refer method of functional interface. It is an easy way of a lambda expression. We can replace the lambda expression with a method reference.\u003Cbr \/\u003E\u003Cb\u003E\nSyntax:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre\u003E \u003Ccode id=\"htmlViewer5\" style=\"background-color: #2b2b2b; background: rgb(43, 43, 43); color: #f8f8f2; display: block; font-weight: 400; overflow: auto; padding: 0.5em; width: auto;\"\u003E\u003Cspan style=\"color: #f8f8f2; font-weight: 400;\"\u003E\u003Cspan style=\"color: #dcc6e0; font-weight: 400;\"\u003Eclass\u003C\/span\u003E::\u003Cspan style=\"color: #f5ab35; font-weight: 400;\"\u003Emethodname\u003C\/span\u003E\u003C\/span\u003E\u003C\/code\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTypes of method references in Java,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n• Reference to a static method.\u003Cbr \/\u003E\n• Reference to an instance method.\u003Cbr \/\u003E\n• Reference to a constructor.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n11. Why String is called an immutable object?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nStrings are immutable as their values cannot be changed. The Strings are used in many Java applications to store sensitive information like username, password, etc. The purpose of making string immutable is to ensure caching, security, synchronization, and performance of the application.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n12. What is an Optional class in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava 8 has introduced a new class Optional in java.util package. This class is introduced to avoid NullPointerException in java. It helps developers to write good code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\n13. What does the peek() method do? When should you use it? \u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \nThe peek() method of Stream class allows us to see through a Stream pipeline. We can peek through each step and print meaningful messages. It is generally used for debugging issues related to lambda expression and Stream processing.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n14. What are serialization and deserialization in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \nSerialization allows us to convert the state of an object into a byte stream, which then can be saved into a file on the local disk or sent over the network to any other machine. Deserialization allows us to reverse the process which means reconverts the serialized byte stream to an object again.\u003Cbr \/\u003E\n Example Server and Client application.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n15. Is it important to implement a Serializable interface if we want to serialize any object?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \nYes, it is necessary to implement a Serializable interface if we want to serialize any object. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\n16. Can you explain the concept of serialVersionUID?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E   \nserialVersionUID is used to ensure that the same class which was used during Serialization is loaded during deserialization. serialVersionUID is used for version control of the object.\u003Cbr \/\u003E\nYou can find the serialVersionUID in detail\u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/what-is-serialversionuid-and-why-need-it.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today. Please mention in the comments in case you have faced Java interview questions for 5-6 years of experience that is not present in the above list.\u003Cbr \/\u003E"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/7233996060227461696"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/7233996060227461696"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2021\/05\/java-interview-questions-for-5-years-experience.html","title":"Top 16 Java Interview Questions for 5 to 6 Years Experience "}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-3697094077518147666"},"published":{"$t":"2021-05-13T13:12:00.001-04:00"},"updated":{"$t":"2021-05-13T13:28:29.259-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview-preparation"}],"title":{"type":"text","$t":"Coding Interview Preparation [Core Java] "},"content":{"type":"html","$t":"In this post, I shall be sharing my notes of coding interview preparation for Java Developers. If you are about to give an interview for a Java developer in any of the MNC's like \u003Cb\u003ETCS, Infosys, Capgemini, Cognizant, Wipro, HCL\u003C\/b\u003E, etc. then you have landed in the right place. Suppose you are giving HackerRank or HackerEarth or any other coding test and you have to write the code for the question that requires iteration of the HashMap. Believe me, you will not be able to crack coding interview questions just because you don't know \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/11\/how-to-iterate-or-loop-over-hashmap-in-java-with-example.html\" target=\"_blank\"\u003Ehow to iterate a HashMap\u003C\/a\u003E. Let's dive deep into the questions:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Ci\u003ERead Also: \u003C\/i\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/12\/best-books-for-coding-interview-in-java.html\" target=\"_blank\"\u003EBest Books for Coding Interview in Java\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(223, 247, 223) none repeat scroll 25px 25px; border: 1px solid rgb(206, 235, 206); padding: 20px 10px;\"\u003E\n\u003Cb\u003ENote:\u003C\/b\u003E Before looking at the answer, I suggest writing the code in your favorite IDE (Eclipse\/IntelliJ) first. \u003Cbr \/\u003E\u003C\/div\u003E\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003ECoding Interview Preparation with Code Snippets\u003C\/b\u003E\u003C\/span\u003E\u003C\/h2\u003E\n\u003Ch3\u003E\u003Cb\u003E1. Sort Objects using Comparator \u003C\/b\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\nThis question is very important. This question has been asked to me in many coding interviews. Please go through it once before appearing for the coding interview. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\u003Cb\u003ECode Snippet\u003C\/b\u003E\u003C\/h4\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-kuOp7LUQwJE\/YJ1SsK8xjSI\/AAAAAAAABss\/by5CRNiL-KwEZbeNbvgu8tcGfXIvGZx1ACLcBGAsYHQ\/s879\/SortObjectUsingComparator.png\" style=\"display: block; padding: 1em 0px; text-align: center;\"\u003E\u003Cimg alt=\"sort objects using comparator\" border=\"0\" data-original-height=\"706\" data-original-width=\"879\" height=\"514\" src=\"https:\/\/1.bp.blogspot.com\/-kuOp7LUQwJE\/YJ1SsK8xjSI\/AAAAAAAABss\/by5CRNiL-KwEZbeNbvgu8tcGfXIvGZx1ACLcBGAsYHQ\/w640-h514\/SortObjectUsingComparator.png\" title=\"sort objects using comparator\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-2W2omus828Y\/YJ1S0FlU4yI\/AAAAAAAABs0\/Bzx9jw3vUaExaGAp2vwHqE1biY9_LXaUwCLcBGAsYHQ\/s624\/DeveloperClass.png\" style=\"display: block; padding: 1em 0px; text-align: center;\"\u003E\u003Cimg alt=\"Developer class java\" border=\"0\" data-original-height=\"313\" data-original-width=\"624\" height=\"321\" src=\"https:\/\/1.bp.blogspot.com\/-2W2omus828Y\/YJ1S0FlU4yI\/AAAAAAAABs0\/Bzx9jw3vUaExaGAp2vwHqE1biY9_LXaUwCLcBGAsYHQ\/w640-h321\/DeveloperClass.png\" title=\"Developer class java\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cb\u003E2. Sort HashMap by Values\u003C\/b\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\nI bet a few Java developers can sort HashMap by Values in the first attempt. If you find it hard to solve, then I will suggest to remember the code using Java 8. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\u003Cb\u003ECode Snippet\u003C\/b\u003E\u003C\/h4\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/--QNTX70VZMU\/YJ1SO3_QJII\/AAAAAAAABsU\/QexzmUM3oYgtliZlAQvhmigPs9pUySrKgCLcBGAsYHQ\/s790\/SortHashMapByValues.png\" style=\"display: block; padding: 1em 0px; text-align: center;\"\u003E\u003Cimg alt=\"Sort HashMap by Values\" border=\"0\" data-original-height=\"515\" data-original-width=\"790\" height=\"417\" src=\"https:\/\/1.bp.blogspot.com\/--QNTX70VZMU\/YJ1SO3_QJII\/AAAAAAAABsU\/QexzmUM3oYgtliZlAQvhmigPs9pUySrKgCLcBGAsYHQ\/w640-h417\/SortHashMapByValues.png\" title=\"Sort HashMap by Values\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cb\u003E3. Sort HashMap by Values in Descending Order\u003C\/b\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\nWhat if you need to sort the HashMap by Values in descending order. Just go through the below code snippet.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\u003Cb\u003ECode Snippet\u003C\/b\u003E\u003C\/h4\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-64Le1-LfWjo\/YJ1SW2GZ_XI\/AAAAAAAABsc\/xlHhuxiX8fcbuH9EHxAL29ZODCutUPzsACLcBGAsYHQ\/s800\/SortHashMapValuesDescOrder.png\" style=\"display: block; padding: 1em 0px; text-align: center;\"\u003E\u003Cimg alt=\"Sort HashMap Values Descending Order\" border=\"0\" data-original-height=\"577\" data-original-width=\"800\" height=\"462\" src=\"https:\/\/1.bp.blogspot.com\/-64Le1-LfWjo\/YJ1SW2GZ_XI\/AAAAAAAABsc\/xlHhuxiX8fcbuH9EHxAL29ZODCutUPzsACLcBGAsYHQ\/w640-h462\/SortHashMapValuesDescOrder.png\" title=\"Sort HashMap Values Descending Order\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cb\u003E4. Find Permutation and Combination of a String in Java\u003C\/b\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\nIt is difficult to code finding permutation or combination of a given String on the spot at the interview. I will suggest just go through the below code snippet and remember it as they are similar.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\u003Cb\u003ECode Snippet\u003C\/b\u003E\u003C\/h4\u003E\n\u003Cdiv style=\"background: rgb(223, 247, 223) none repeat scroll 25px 25px; border: 1px solid rgb(206, 235, 206); padding: 20px 10px;\"\u003E\n\u003Cb\u003ENote:\u003C\/b\u003E Code for finding Permutation or Combination is almost similar as shown below.\u003C\/div\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-qlJrtKDxbCg\/YJ1SeNbaY3I\/AAAAAAAABsg\/En2xp6fUV1wRpkl1CdMt_JobKt6mgASTACLcBGAsYHQ\/s789\/Permutation%2Band%2BCombination.png\" style=\"display: block; padding: 1em 0px; text-align: center;\"\u003E\u003Cimg alt=\"Permutation and Combination in Java\" border=\"0\" data-original-height=\"576\" data-original-width=\"789\" height=\"467\" src=\"https:\/\/1.bp.blogspot.com\/-qlJrtKDxbCg\/YJ1SeNbaY3I\/AAAAAAAABsg\/En2xp6fUV1wRpkl1CdMt_JobKt6mgASTACLcBGAsYHQ\/w640-h467\/Permutation%2Band%2BCombination.png\" title=\"Permutation and Combination in Java\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cb\u003E5. Iterate HashMap\u003C\/b\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\nIf you have more than 2 years of experience then you must know the code how to iterate the HashMap in Java. There are many ways to iterate through the HashMap. Below I shared the one which is easy to remember using Map.Entry class. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\u003Cb\u003ECode Snippet\u003C\/b\u003E\u003C\/h4\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-GgTFCqvzFjk\/YJ1RyFk6qKI\/AAAAAAAABsI\/FeJya7WGZGE4iTlxpIXdbnq2r3_Fd65BACLcBGAsYHQ\/s728\/IterateHashMap.png\" style=\"display: block; padding: 1em 0px; text-align: center;\"\u003E\u003Cimg alt=\"Iterate HashMap in Java\" border=\"0\" data-original-height=\"470\" data-original-width=\"728\" height=\"414\" src=\"https:\/\/1.bp.blogspot.com\/-GgTFCqvzFjk\/YJ1RyFk6qKI\/AAAAAAAABsI\/FeJya7WGZGE4iTlxpIXdbnq2r3_Fd65BACLcBGAsYHQ\/w640-h414\/IterateHashMap.png\" title=\"Iterate HashMap in Java\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today, please mention in the comments in case you know any other java code that is frequently asked and hard to solve at the first attempt. \n\u003Cbr \/\u003E"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3697094077518147666"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3697094077518147666"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2021\/05\/coding-interview-preparation.html","title":"Coding Interview Preparation [Core Java] "}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-kuOp7LUQwJE\/YJ1SsK8xjSI\/AAAAAAAABss\/by5CRNiL-KwEZbeNbvgu8tcGfXIvGZx1ACLcBGAsYHQ\/s72-w640-h514-c\/SortObjectUsingComparator.png","height":"72","width":"72"}},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-4941639069842104268"},"published":{"$t":"2020-10-18T13:05:00.001-04:00"},"updated":{"$t":"2021-04-28T10:01:08.391-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 32 Java Interview Questions for 2 to 3 Years Experience"},"content":{"type":"html","$t":"In this post, I will be sharing the most frequently asked java interview questions for 2 to 4 years experienced. Relax, the expectation from the 2 to 4 years experienced guy is not the same as for senior java developers. I have categorized the interview questions into topics such as the OOPs concept, Collection framework, Exception Handling, String, Serialization, Java Multi-threading, and Coding basic questions. Please bookmark this page. I have added many links in the article so that it will be helpful for you to get a detailed explanation of the question. Let's dive deep into the interview questions with answers.\u003Cbr \/\u003E\n\u003Ch2\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EJava Interview Questions for 2 to 3 Years Experience\u003C\/b\u003E\u003C\/span\u003E\u003C\/h2\u003E\n\u003Cbr \/\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ1 List down methods present in the Object class?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBelow are the important methods present in the Object class:\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. \u003Cb\u003Eclone():\u003C\/b\u003E this method is used to create a clone of the object\u003Cbr \/\u003E\nb. \u003Cb\u003Eequals():\u003C\/b\u003E this method is used to check whether objects are equal.\u003Cbr \/\u003E\nc. \u003Cb\u003EhashCode():\u003C\/b\u003E this method returns the hash value of the object.\u003Cbr \/\u003E\nd. \u003Cb\u003EtoString():\u003C\/b\u003E this method returns the String representation of the given object.\u003Cbr \/\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\ne. \u003Cb\u003Ewait():\u003C\/b\u003E\u0026nbsp; causes the current method to wait until another thread calls notify() or notifyAll() method.\u003Cbr \/\u003E\nf. \u003Cb\u003Enotify():\u003C\/b\u003E wakes up the single thread waiting for this object's monitor.\u003Cbr \/\u003E\ng. \u003Cb\u003EnotifyAll():\u003C\/b\u003E wakes up all the threads, waiting for this object's monitor.\u003Cbr \/\u003E\nh. \u003Cb\u003Efinalize():\u003C\/b\u003E this method is invoked by garbage collector just before the object is garbage collected.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOOPS Concept\u003C\/b\u003E\u003C\/span\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ2 What is an interface?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn simple words, Interface is a blueprint of the class. It contains static constants and abstract methods.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ3 What is the difference between Method Overriding and Method Overloading in Java? \u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nMethod Overloading is used to increase the readability of the program. Method Overriding is used to provide the specific implementation of the method. You can find a detailed explanation \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/6-difference-method-overloading-and-method-overriding.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ4 Is it possible to override the static method in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nNo, it is not possible to override the static method in Java. The reason is\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. The static method belongs to the class level, not the object level. In method overriding, it is the object that decides which method is to be called.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Also, for class-level methods i.e static methods, the type reference decides which method is called not the object being referred. \nIt concludes the method called is determined at compile time.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf a child class defines a static method with the same signature as a static method in the parent class, then the method in the child class hides the method in the parent class.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ5 What is the Dynamic method dispatch in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nDynamic method dispatch is also known as Run time polymorphism. It is a mechanism by which a call to an overridden method is resolved at run time.\u0026nbsp;At runtime, it is the type of object being referred to not the type reference of the variable that decides which version of an overridden method needs to be executed.\u003Cbr \/\u003E\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EException Handling \u003C\/b\u003E\u003C\/span\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ6 What is the difference between Checked and Unchecked Exception in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nYou can find a detailed explanation \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/08\/difference-checked-and-unchecked-exception.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ7 What is the difference between throw and throws in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nFind the answer in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/09\/difference-between-throw-and-throws.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ8 What is the try with resources statement in Java?\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nAccording to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/exceptions\/tryResourceClose.html\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E, try-with-resources is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. It ensures that each resource is closed at the end of the statement.\u003Cbr \/\u003E\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ9 Does finally block always execute in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, there is one scenario where the finally block does not execute. When you run System.exit(0) in the try or catch block, then finally block does not execute. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003ECollection\u003C\/b\u003E \u003C\/span\u003E\u003C\/h3\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ10 What is the difference between Array and ArrayList? Which is better?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \nThe detailed answer to the difference between Array and ArrayList can be found \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/03\/difference-between-array-and-arraylist-in-java-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ11 What is the difference between LinkedList and ArrayList in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe detailed answer to the difference between LinkedList and ArrayList can be found \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/04\/difference-between-arraylist-and-linkedlist-in-java-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ12 How HashMap works internally in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the most important questions that every java developer must know. Find the detailed answer to How HashMap works internally in Java \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ13 What is the difference between fail-safe iterators and fail-fast iterators?\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe detailed answer to the difference between fail-fast and fail-safe can be found \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/04\/fail-fast-iterator-vs-fail-safe-iterator-difference-with-example-in-java.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ14 What is the difference between HashMap and ConcurrentHashMap?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA detailed explanation of the difference between HashMap and ConcurrentHashMap can be found \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/02\/hashmap-vs-concurrenthashmap-java-collections-interview-question.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ15 What is the difference between HashMap and Hashtable?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003EThe detailed answer to the difference between HashMap and Hashtable can be found \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/03\/hashmap-vs-hashtable-difference-with-example-java-interview-questions.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ16 How ConcurrentHashMap works internally in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA detailed explanation of ConcurrentHashMap internal working can be found \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/02\/how-concurrenthashmap-works-in-java-internal-implementation.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EMultithreading\u003C\/b\u003E \u003C\/span\u003E\u003C\/h3\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ17 What is the difference between wait and sleep methods in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find all the differences between the wait and sleep method \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/11\/5-difference-between-sleep-and-wait-with-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ18 Explain the lifecycle of a Thread in java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe lifecycle of a thread in java is explained in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/07\/threads-lifecycle-example-java-methods-explanation.html\" target=\"_blank\"\u003Ehere.\u003C\/a\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ19 What is the difference between Runnable and Thread in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDifferences between Runnable and Thread are explained \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/05\/implements-runnable-vs-extends-thread-in-java-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ20 Which one is better to implement Runnable or to extends Thread?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nimplements Runnable is better than extends Thread. Using extends Thread we can not inherit any other class since Java does not allow multiple inheritance of classes. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EString\u003C\/b\u003E \u003C\/span\u003E\u003C\/h3\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ21 What is the difference between String, StringBuilder, and StringBuffer in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find the difference between String, StringBuilder, and StringBuffer \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-string-stringbuilder.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ22 Why String is immutable in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the important questions. You can find a detailed explanation \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2015\/07\/why-string-is-final-or-immutable-in-java-with-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ23 How many objects will be created in the below statement? \u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003EString strObj1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"JavaHungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\nString strObj2 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"JavaHungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003C\/pre\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\nThe answer is only 1 object is created. String strObj1 will create a new object in String constant pool whereas strObj2 will create the reference to the String strObj1.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003ESerialization\u003C\/b\u003E\u003C\/span\u003E\u003C\/h3\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ24 What is the marker interface? Name some marker interfaces in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAn empty interface i.e interface without methods and fields is called a marker interface in java.\u003Cb\u003E \u003C\/b\u003Ejava.io.Serializable and java.lang.Cloneable are some of the examples of marker interface.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ25 What is autoboxing and unboxing in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAutoboxing can be defined as converting primitive data type (int, float, double, etc.) into their corresponding object wrapper classes.\u003Cbr \/\u003E\nfor example: int to Integer, float to Float\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nUnboxing is the reverse of autoboxing. It converts Object wrapper classes into a primitive data types.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ26 What is a transient keyword in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\ntransient keyword in Java is used to avoid Serialization.\u0026nbsp; If the variable is marked as transient then it will not be serialized.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EMiscellaneous\u003C\/b\u003E\u003C\/span\u003E\u003C\/h3\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ27 What is the difference between final, finally, and finalize in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe difference between final, finally and finalize is answered in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/01\/final-finally-finalize.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ28 What are the different ways to call the garbage collector in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere are two ways to call the garbage collector in Java.\u003Cbr \/\u003E\n\na. System.gc()\u003Cbr \/\u003E\n\nb. Runtime.getRuntime().gc()\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ29 What is volatile keyword in Java?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf a variable is marked as volatile then this variable is read\u003Cb\u003E \u003C\/b\u003Efrom the main memory instead of cache memory.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ30 What are the features of Java 8?\u003C\/b\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question is very important. You can find the features of Java 8 \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/10\/oracle-java-8-new-features-lambda-expressions-optional-defender-methods-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch3\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003ECoding\u003C\/b\u003E \u003C\/span\u003E\u003C\/h3\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ31 How to reverse a string without using reverse() built-in method in Java?\u003C\/span\u003E (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/reverse-or-mirroring-string-without.html\" target=\"_blank\"\u003ESolution\u003C\/a\u003E)\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E  \n\u003Cspan style=\"color: #38761d;\"\u003EQ32 Write code for the producer-consumer problem in Java?\u003C\/span\u003E (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/07\/producer-consumer-problem.html\" target=\"_blank\"\u003ESolution\u003C\/a\u003E)\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today. Please mention in the comments in case you have faced Java interview questions  for 2-4 years experienced that is not present in the above list."},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4941639069842104268"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4941639069842104268"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2020\/10\/java-interview-questions-for-2-years-experience.html","title":"Top 32 Java Interview Questions for 2 to 3 Years Experience"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-3514325878535453919"},"published":{"$t":"2020-10-04T12:05:00.001-04:00"},"updated":{"$t":"2021-04-28T10:04:13.644-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java8"}],"title":{"type":"text","$t":"Java 8 Most Frequently Asked Interview Questions and Answers "},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\nI have already shared core java interview questions and answers. Java 8 was released in 2014. Now it has been 6 years since the release of java8. The job market is filled with java8 interview questions.\u0026nbsp; Today I will be sharing 30 most frequently asked\u0026nbsp; java8 interview questions and answers. I have already shared the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/10\/oracle-java-8-new-features-lambda-expressions-optional-defender-methods-example.html\" target=\"_blank\"\u003Ejava 8 features\u003C\/a\u003E. Please bookmark this page as I keep adding more questions to this post.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch2\u003E\n \u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E Java 8 Interview Questions and Answers\u003C\/span\u003E\u003C\/b\u003E\u003C\/h2\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ1. What are the java 8 new features?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is the most asked interview question on Java 8. Many of the java developers do not work on java 8. Below are the java 8 features:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1. Functional Interface:\u003C\/b\u003E Each functional interface has a single abstract method, called the functional method, implementation can be provided using the lambda expressions.\u003Cbr \/\u003E\n\u003Cb\u003E2. Lambda Expressions:\u003C\/b\u003E It is a feature derived from functional programming. It is a function that does not belong to any class.\u003Cbr \/\u003E\n\u003Cb\u003E3. Optional: \u003C\/b\u003EInstead of using null values Optional class is used for representing Optional values.\u003Cbr \/\u003E\n\u003Cb\u003E4. Stream API\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E5. Spliterator\u0026nbsp;\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E6. Method References\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E7. New Date and Time API.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find the important java 8 features \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/10\/oracle-java-8-new-features-lambda-expressions-optional-defender-methods-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ2 What is the difference between Collection API and Stream API?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003EDifferences between Collection API and Stream API are as follow :\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. Collection API was introduced in JDK 1.2 while Stream API is introduced in JDK 1.8\u003Cbr \/\u003E\n2. Collection objects are created eagerly while Stream API objects are created lazily.\u003Cbr \/\u003E\n3. Iterate and Consume elements at any number of times for the Collection object while iterate and consume elements only one time for the Stream object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ3 What is Lambda Expression?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003EAccording to Oracle docs,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLambda expressions are the method without name i.e Anonymous method. In other words, Lambda expression is a function that can be passed around and referenced as an object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor example:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EStructure of Lambda Expression\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nA lambda expression consists of three parts :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Parameter List\u003Cbr \/\u003E\nb. Lambda symbol -\u0026gt;\u003Cbr \/\u003E\nc. Expression\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(248, 248, 248) none repeat scroll 0% 0%; border-width: 0.1em 0.1em 0.1em 0.1em; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E(\u003C\/span\u003EParameter List\u003Cspan style=\"color: #666666;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E-\u0026gt;{\u003C\/span\u003Eexpression\u003Cspan style=\"color: #666666;\"\u003E;}\u003C\/span\u003E \n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ4. What is the difference between PermGenSpace and MetaSpace?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn JDK 8 onwards PermGenSpace is removed. Earlier PermGenSpace\u0026nbsp;is used for storing the metadata. Metadata means storing information about classes like bytecodes, names, and JIT information.\u003Cbr \/\u003E\nJava classes metadata now stored in a native heap and this space is called MetaSpace. Metaspace grows automatically by default and will be garbage collected.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSo the major difference between PermGenSpace and MetaSpace is that PermGenSpace was fixed in size and did not grow automatically, but MetaSpace does not have any size constraints.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor more information about the PermGenSpace and Metaspace in Java 8, you can check \u003Ca href=\"https:\/\/blogs.oracle.com\/poonam\/about-g1-garbage-collector,-permanent-generation-and-metaspace\" rel=\"nofollow\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ5 What is the Optional class in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava SE 8 introduces a new class in the util package i.e java.util.Optional. In a nutshell, you can view Optional as a single value container that either contains the value or not (then it is called as empty).\u003Cbr \/\u003E\nIt is used to avoid NullPointerException. This Optional class concept is inspired by Haskell and Scala.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EExample of Optional Class\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Here is an Empty Optional\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(248, 248, 248) none repeat scroll 0% 0%; border-width: 0.1em 0.1em 0.1em 0.1em; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003EOptional\u003Cspan style=\"color: #666666;\"\u003E\u0026lt;\u003C\/span\u003ESoundcard\u003Cspan style=\"color: #666666;\"\u003E\u0026gt;\u003C\/span\u003E  sc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E Optional\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #7d9029;\"\u003Eempty\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E();\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\nb. Here is an Optional with a non-null value\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(248, 248, 248) none repeat scroll 0% 0%; border-width: 0.1em 0.1em 0.1em 0.1em; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003ESoundcard soundcard \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: green; font-weight: bold;\"\u003Enew\u003C\/span\u003E Soundcard\u003Cspan style=\"color: #666666;\"\u003E();\u003C\/span\u003E\n\nOptional\u003Cspan style=\"color: #666666;\"\u003E\u0026lt;\u003C\/span\u003ESoundcard\u003Cspan style=\"color: #666666;\"\u003E\u0026gt;\u003C\/span\u003E sc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E Optional\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #7d9029;\"\u003Eof\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E(\u003C\/span\u003Esoundcard\u003Cspan style=\"color: #666666;\"\u003E);\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe Optional class has various utility methods such as isPresent() which help coders to avoid making use of null value checks.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u0026nbsp;Q6 What is Functional Interface in Java 8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn simple words, the Functional interface has exactly one abstract method. A compile-time error is thrown if an interface declaration is annotated with @FunctionalInterface\u0026nbsp; but is not, in fact, a functional interface.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EExample of Functional Interface\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nAlready there are many functional interfaces in java. For example, Comparable and Runnable.\u003Cbr \/\u003E\nThe functional interface does not count default methods.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ7 How to create a Functional Interface in Java8?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava is able to identify Functional Interface but you can declare one in the class by using the @FunctionalInterface.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBelow is the coding example of Functional Interface:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #555555; font-weight: bold;\"\u003E@FunctionalInterface\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Einterface\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EFuncInterfaceExample\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Esum\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E a\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E b\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Edefault\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emultiply\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E a\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E b\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #333333;\"\u003E*\u003C\/span\u003Eb\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EMainJava8Class\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n      FuncInterfaceExample obj \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ea\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003Eb\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E a\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E \n      System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Sum of two numbers is: \"\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003Eobj\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Esum\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E4\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003ESum of two numbers is: 12\u0026nbsp;\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ8 What do you understand by the term SAM interface?\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava 8 provided the feature of a functional interface. Since a functional interface can contain only one abstract method, hence, they are called SAM or \"Single Abstract Method\".\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ9 What is a default method in Java 8? When to use it?\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThe default method is also known as defender methods or virtual extension methods. It is a non-abstract method i.e have a body, which can be declared inside the interface.\u003Cbr \/\u003E\nThe default method is introduced in Java 8 for backward compatibility. That is if you add a new abstract method to the interface, all the implementing classes shall break. Implementing classes need to implement the added abstract method. This problem is solved by the default method of java 8.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ10 What is the difference between Iterator and Spliterator?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EDifference between Iterator and Spliterator\u0026nbsp; are as follow:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\u003Cb\u003E1. Introduction:\u0026nbsp;\u003C\/b\u003EIterator was introduced in JDK 1.2 while Spliterator\u0026nbsp; is introduced in JDK 1.8\u003Cbr \/\u003E\n\u003Cb\u003E2. Use in API:\u003C\/b\u003E\u0026nbsp;Iterator is used for Collection API while Spliterator is used for Stream API\u003Cbr \/\u003E\n\u003Cb\u003E3. Parallel programming:\u0026nbsp;\u003C\/b\u003EIterator can be used for iterating the elements in Collection in sequential order while\u0026nbsp; Spliterator can be used for iterating the Stream elements in parallel or sequential order.\u003Cbr \/\u003E\n\u003Cb\u003E4. Universal Iterator: \u003C\/b\u003EIterator is a universal iterator while Spliterator is not a universal iterator.\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ11 What is the difference and similarities between Function and Predicate in java 8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EDifference:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E1. Return Type: \u003C\/b\u003EFunction returns an Object and it is a single argument function.\u003Cbr \/\u003E\nThe predicate return type is boolean (i.e true or false) and it is also a single argument function.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ESimilarities:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1.\u003C\/b\u003E Both are functional interfaces i.e both contain a single abstract method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ12 What is the difference between Internal iteration and External iteration?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava 8 has introduced the new concept of \"internal iteration\". Prior to java 8, there is only external iteration. Let's dive into the differences between internal iteration and external iteration.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EAvailability:\u003C\/b\u003E Internal iteration is added in JDK 8 while external iteration is there before JDK 8.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EIteration behavior: \u003C\/b\u003EInternal iterator iterating an Aggregated Object elements like Collections, Arrays internally.\u003Cbr \/\u003E\nExternal iterator iterating an Aggregated Object elements externally.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EApproach:\u003C\/b\u003E\u0026nbsp; Internal iterator follows a functional programming approach that is declarative style.\u003Cbr \/\u003E\nMeanwhile, the External iterator follows the OOP approach i.e imperative style.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ13 What is the method reference in java 8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMethod reference is represented by using the double colon operator \"::\".\u0026nbsp; Lambda expressions are used to create a method anonymously. Sometimes, the sole purpose of lambda expressions is to call existing methods.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHere is the syntax of method reference :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nObject :: nameOfTheMethod\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/methodreferences.html\" rel=\"nofollow\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E,\u0026nbsp; Method references are compact, easy-to-read lambda expressions for methods that already have a name.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ14 What does String::valueOf expression means?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is a reference to a static method i.e valueOf method of String class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ15 Will the following code compile?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(248, 248, 248) none repeat scroll 0% 0%; border-width: 0.1em 0.1em 0.1em 0.1em; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #aa22ff;\"\u003E@FunctionalInterface\u003C\/span\u003E\n\u003Cspan style=\"color: green; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: green; font-weight: bold;\"\u003Einterface\u003C\/span\u003E \u003Cspan style=\"color: blue; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\u0026lt;\u003C\/span\u003EA\u003Cspan style=\"color: #666666;\"\u003E,\u003C\/span\u003E B\u003Cspan style=\"color: #666666;\"\u003E,\u003C\/span\u003E C\u003Cspan style=\"color: #666666;\"\u003E\u0026gt;\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: green; font-weight: bold;\"\u003Epublic\u003C\/span\u003E C \u003Cspan style=\"color: blue;\"\u003Eapply\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E(\u003C\/span\u003EA a\u003Cspan style=\"color: #666666;\"\u003E,\u003C\/span\u003E B b\u003Cspan style=\"color: #666666;\"\u003E);\u003C\/span\u003E\n \n    \u003Cspan style=\"color: green; font-weight: bold;\"\u003Edefault\u003C\/span\u003E \u003Cspan style=\"color: #b00040;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: blue;\"\u003EprintString\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E{\u003C\/span\u003E\n        System\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #7d9029;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #7d9029;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #ba2121;\"\u003E\"javahungry\"\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #666666;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #666666;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\nYes, the above code will compile. The above code follows the functional interface specification of allowing only a single abstract method. The default method printString() does not count as the abstract method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ16 What is the difference between skip(long) and limit(long) in Java 8?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp;The skip(long n) method returns the remaining elements after discarding the first n elements of the given stream.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor example:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.Stream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJava8SkipExample\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n      Stream\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eof\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E11\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E12\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E13\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E14\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E16\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n            \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eskip\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n            \u003Cspan style=\"color: #888888;\"\u003E\/* skip first three elements and print the\u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E            remaining elements of the stream *\/\u003C\/span\u003E\n            \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EforEach\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E((\u003C\/span\u003Enum\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Enum\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E13\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E14\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E15\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E16\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe limit(long maxSize) method returns the Stream of elements of a specified size.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor example:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.Stream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJava8LimitExample\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n      Stream\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eof\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E11\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E12\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E13\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E14\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E16\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n            \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elimit\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n            \u003Cspan style=\"color: #888888;\"\u003E\/* limit prints the first three elements *\/\u003C\/span\u003E\n            \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EforEach\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E((\u003C\/span\u003Enum\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Enum\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E10\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E11\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E12 \u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ17 What is Nashorn in java 8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNashorn is the latest javascript engine released with java8. Before JDK 8, the javascript engine was based on Mozilla Rhino.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt provides better compliance with ECMA normalized javascript specifications and better runtime performance.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ18 What are the issues of the old Date and Time APIs.\u0026nbsp; Can you explain the new Date and Time APIs in java 8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPrior to java 8, Old Date and Time API's are there. Let's find out what are the issues with them:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EPerformance:\u003C\/b\u003E\u0026nbsp; Java 8 APIs are better in terms of performance than older Date and Time APIs.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EStandards: \u003C\/b\u003EJava 8 Date and Time API\u0026nbsp; comply with ISO standard, meanwhile, older java 8 Date and Time API were hard to understand and poorly designed.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EThread-safe: \u003C\/b\u003EMost frequently used java.util.Date is mutable and not thread-safe. New Java 8 Date and Time API are thread-safe.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLocalDateTime, LocalDate, and LocalTime are few of the latest core API classes of java 8.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ19 What is Stream API in Java 8? Why do we need it?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u0026nbsp;\u003C\/b\u003EStream API is the new feature of Java 8. It is used to process or compute the data.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E\n\u003Cb\u003EWhy do we need Stream API\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E\na. Stream API supports aggregate operations that simplify the processing.\u003Cbr \/\u003E\nb. It provides Functional-Style programming.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ20 What is the difference between findAny() and findFirst() methods of Java 8?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. findAny() will return any element from the given stream while findFirst() will return the first element of the given stream.\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. findAny() shows non-deterministic behavior whereas findFirst() shows deterministic behavior. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ21-30\u003C\/b\u003E \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/05\/java-8-coding-and-programming-interview-questions.html\" target=\"_blank\"\u003E\u003Cb\u003EJava 8 Coding and Programming Interview Questions and Answers\u003C\/b\u003E\u003C\/a\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPlease mention in the comments if you know any other java 8 interview questions.\u0026nbsp;\u003C\/div\u003E\n"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3514325878535453919"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3514325878535453919"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2018\/10\/java-8-interview-questions-and-answers.html","title":"Java 8 Most Frequently Asked Interview Questions and Answers "}],"author":[{"name":{"$t":"$ubham"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/11571883502661591778"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-1694053281129491634"},"published":{"$t":"2020-05-24T05:42:00.002-04:00"},"updated":{"$t":"2021-04-28T10:33:01.611-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"beginner"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 30 Java Entry Level Interview Questions and Answers"},"content":{"type":"html","$t":"In this post, I will be sharing the top 30 java entry level interview questions and answers. If you are looking for placement in one of the top services companies like TCS, Wipro, Accenture, Infosys, Cognizant, etc. then you have landed in the right place. Let's dive deep into the interview questions.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Ci\u003ERead Also:\u003C\/i\u003E\u003C\/b\u003E \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/top-25-most-frequently-asked-core-java.html\" target=\"_blank\"\u003EJava Interview Questions for Experienced\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ1 What are the main features of Java?\u003C\/b\u003E\u003C\/h4\u003E\na.\u0026nbsp; Java is an Object-Oriented programming language\u003Cbr \/\u003E\nb.\u0026nbsp; Simple\u003Cbr \/\u003E\nc.\u0026nbsp; Distributed\u003Cbr \/\u003E\nd.\u0026nbsp; Java support Multithread\u003Cbr \/\u003E\ne.\u0026nbsp; Java is a Platform independent programming language\u003Cbr \/\u003E\nf.\u0026nbsp; Java is more secured than other languages\u003Cbr \/\u003E\ng.\u0026nbsp; Portable\u003Cbr \/\u003E\nh.\u0026nbsp; Robust\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ2 What is an Array?\u003C\/b\u003E\u003C\/h4\u003E\nThe collection of similar data types is known as Array. You can find it in detail \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/03\/array-interview-questions-and-answers-in-java.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E\u003C\/b\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ3 Is it possible to declare an Array without Array size?\u003C\/b\u003E\u003C\/h4\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003EIt is not possible to declare an Array without Array size. If you try to do so then you will get compile-time error.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ4 What do you understand by the term Object-Oriented Programming language?\u0026nbsp; \u003C\/b\u003E\u003C\/h4\u003E\nThe object-oriented programming language is a language that uses objects in programming.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ5 What are the basic principles of the OOPs concept?\u003C\/b\u003E\u003C\/h4\u003E\na. Abstraction\u003Cbr \/\u003E\nb. Polymorphism\u003Cbr \/\u003E\nc. Encapsulation\u003Cbr \/\u003E\nd. Inheritance\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ6. What is the abstraction in java?\u003C\/b\u003E\u003C\/h4\u003E\nAbstraction means hiding the detailed information from the user. Abstraction can be achieved by interfaces and abstract classes.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ7 What do you mean by inheritance in java?\u003C\/b\u003E\u003C\/h4\u003E\nInheritance is the main feature of java. Inheritance means java class or interface can inherit the properties and behavior from another class or interface. Inheritance can be gained by implementing interfaces or extending classes. You can find it in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/12\/inheritance-in-java.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ8 What do you mean by the Object in java?\u003C\/b\u003E\u003C\/h4\u003E\nThe object is the instance of a class. A class contains the basic properties and behavior of a real-world object. We can instantiate this class with a \u003Ci\u003Enew\u003C\/i\u003E keyword to make an object.\u003Cbr \/\u003E\nFor example, Animal.java has a class named Animal. Animal animal = new Animal() is an object or instance of this class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ9 What is a constructor?\u003C\/b\u003E\u003C\/h4\u003E\nJava provides the facility to declare a special member of the class with the same name, no return type, and with zero or many parameters is called a constructor.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ10 What is polymorphism in java?\u003C\/b\u003E\u003C\/h4\u003E\nPoly means \u003Ci\u003Emany\u003C\/i\u003E and Morph means \u003Ci\u003Eforms\u003C\/i\u003E of a method or constructor or operator. So finally, Polymorphism means many forms of the same method or constructor or operators. You can find it in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/11\/polymorphism-in-java-with-examples.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ11 What do you mean by the method overloading?\u003C\/b\u003E\u003C\/h4\u003E\nThe method with the same name but the number of arguments are different is called method overloading. Methods return type should be the same for all methods. You can find it in detail \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overloading-in-java-with-examples.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E\u003C\/b\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ12 What do you mean by the method overriding in java?\u003C\/b\u003E\u003C\/h4\u003E\nYou can find the details about method overriding \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overriding-in-java-with-examples.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ13 Does java support multiple inheritance?\u003C\/b\u003E\u003C\/h4\u003E\nJava classes do not support multiple inheritance but can gain multiple inheritance by using the interfaces.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ14 Describe constructor vs method?\u003C\/b\u003E\u003C\/h4\u003E\nThe constructor is the special member of the class with the same name as the class and no return type. But the method is the ordinary member of a class used to describe the behavior of some object or class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ15 Is it possible to overload the main() method?\u003C\/b\u003E\u003C\/h4\u003E\nYes, the main method can be overloaded. But we should declare original one like public static void main(String args[]){} because JVM will look for this when starting execution.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ16 What is the difference between String, StringBuilder and StringBuffer?\u003C\/b\u003E\u003C\/h4\u003E\nYou can find the answer in detail \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-string-stringbuilder.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E\u003C\/b\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ17 What is the difference between Collection and Collections.\u003C\/b\u003E\u003C\/h4\u003E\nThe difference between Collection and Collections you can find it in detail \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/03\/difference-between-collection-and-collections-in-java.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E\u003C\/b\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ18 What is multithreaded programming?\u003C\/b\u003E\u003C\/h4\u003E\nMultithreaded means multiple threads will run to execute the task simultaneously.\u0026nbsp; It's important in the programming world. The operating system is an example of a multithreaded system.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ19 What is the difference between Error and Exception?\u003C\/b\u003E\u003C\/h4\u003E\nError is generated by the environment at runtime. The exception is generated by the application intentionally or mistakenly. An exception can be checked or unchecked, but the error is only unchecked. You can find the difference between Error and Exception \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/10\/difference-between-error-and-exception.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ20 Difference between static and non-static methods?\u003C\/b\u003E\u003C\/h4\u003E\nStatic methods belong to a class but non-static methods belong to an object. You don’t need to instantiate a class to access the static methods, but you have to instantiate the class to get access to non-static methods.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ21 Difference between method overloading and method overriding?\u003C\/b\u003E\u003C\/h4\u003E\nYou can find the difference between method overloading and method overriding \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/6-difference-method-overloading-and-method-overriding.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ22 What are the different ways to create threads in java?\u003C\/b\u003E\u003C\/h4\u003E\nThere are two ways to create threads in java. Ways are listed below:\u003Cbr \/\u003E\na. Implement Runnable interface\u003Cbr \/\u003E\nb. By extending Thread class\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ23 What is the synchronization in java?\u003C\/b\u003E\u003C\/h4\u003E\nSynchronization is a technique to control access of a method with multiple threads at the same time. If we declare a method synchronized, then only one thread can use this method at a time. This is basically used for Thread safety. You can find it in detail \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/06\/thread-synchronization-and-synchronized-block.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E\u003C\/b\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ24 What is the use of the final keyword?\u003C\/b\u003E\u003C\/h4\u003E\nWe cannot change the value of a final variable. We are not able to inherit a final class and cannot override a final method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ25 What is the garbage collection of java?\u003C\/b\u003E\u003C\/h4\u003E\nTo clean the object from memory which has no reference is called garbage collection. It’s an important feature of java. Java automatically clears the objects from memory. We don’t need to clean the object manually from memory.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ26 What is the cloning in java?\u003C\/b\u003E\u003C\/h4\u003E\nJava cloning means create an exact copy of an object. Shallow or Deep cloning is used in java to clone a java object. The clone() method is used to clone an object. You can find the difference between Shallow and Deep cloning \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/05\/difference-between-shallow-copy-vs-deep-copy-in-java-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E\u003C\/b\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ27 What is JVM?\u0026nbsp;\u003C\/b\u003E\u003C\/h4\u003E\nJava virtual machine is called JVM. JVM compiles and interprets the java code. JVM manages the garbage collection mechanism and other things efficiently. JVM manages objects in heap memory.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ28 What are the difference between final, finally and finalize in java?\u003C\/b\u003E\u003C\/h4\u003E\nThe keyword final is used to make a class, variable, or methods final. Final classes cannot be inherited, variables value cannot be changed, and methods cannot be overridden.\u003Cbr \/\u003E\nThe keyword finally used in the exception handler to clear some post-task after the execution of try or catch block.\u003Cbr \/\u003E\nThe finalize keyword used to clean up some tasks before the object is removed from memory. The finalize method mainly used in garbage collection.\u003Cbr \/\u003E\nYou can find it in detail \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2020\/01\/final-finally-finalize.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E\u003C\/b\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ29 What is the difference between float and double datatypes?\u003C\/b\u003E\u003C\/h4\u003E\nYou can find the answer in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/11\/difference-between-float-and-double-in-java.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ30 Is it possible to override the overloaded method in java?\u003C\/b\u003E\u003C\/h4\u003E\nYes, in java it is possible to override the overloaded method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today. Please mention in comments in case you have any questions related to top 30 entry level interview questions and answers. "},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/1694053281129491634"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/1694053281129491634"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2020\/05\/entry-level-interview-questions.html","title":"Top 30 Java Entry Level Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-8442855100292099778"},"published":{"$t":"2020-05-17T13:42:00.002-04:00"},"updated":{"$t":"2021-11-03T15:05:38.192-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java8"}],"title":{"type":"text","$t":"Top 10 Java 8 Coding and Programming Interview Questions and Answers"},"content":{"type":"html","$t":"It has been 6 years since Java 8 was released. I have already shared the\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/java-8-interview-questions-and-answers.html\"\u003E Java 8 Interview questions and answers\u003C\/a\u003E. In this tutorial, I will be sharing the top 10 Java 8 \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/09\/70-top-java-interview-programs-with-coding-solutions.html\" target=\"_blank\"\u003Ecoding and programming interview questions and answers\u003C\/a\u003E. I have only used Stream API functions to solve the below questions. Please bookmark this page as I will keep adding more questions to it. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003ERead Also:\u003C\/span\u003E \u003C\/b\u003E\u003C\/i\u003E\u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/java-8-interview-questions-and-answers.html\"\u003EJava 8 Interview Questions and Answers\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ1 Given a list of integers, find out all the even numbers exist in the list \u003C\/b\u003E\u003Cb\u003E\u003Cb\u003Eusing Stream functions\u003C\/b\u003E?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n            myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Efilter\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003En \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E n\u003Cspan style=\"color: #333333;\"\u003E%\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EforEach\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ESystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E::\u003C\/span\u003Eprintln\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E10, 8, 98, 32 \u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ2 Given a list of integers, find out all the numbers starting with 1 \u003C\/b\u003E\u003Cb\u003E\u003Cb\u003Eusing Stream functions\u003C\/b\u003E?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n            myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Emap\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Es \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E s \u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ Convert integer to String\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Efilter\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Es \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E s\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EstartsWith\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"1\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E))\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EforEach\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ESystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E::\u003C\/span\u003Eprintln\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E10, 15\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\n\u003Cb\u003EQ3 How to find duplicate elements in a given integers list in java \u003C\/b\u003E\u003Cb\u003E\u003Cb\u003Eusing Stream functions\u003C\/b\u003E?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n            Set\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E set \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E HashSet\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n            myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Efilter\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003En \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E!\u003C\/span\u003Eset\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eadd\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003En\u003Cspan style=\"color: #333333;\"\u003E))\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EforEach\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ESystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E::\u003C\/span\u003Eprintln\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E98, 15\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ4 Given the list of integers, find the first element of the list \u003C\/b\u003E\u003Cb\u003E\u003Cb\u003Eusing Stream functions\u003C\/b\u003E?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n            myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EfindFirst\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                  \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EifPresent\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ESystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E::\u003C\/span\u003Eprintln\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E10\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ5 Given a list of integers, find the total number of elements present in the list \u003C\/b\u003E\u003Cb\u003E\u003Cb\u003Eusing Stream functions\u003C\/b\u003E?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n            \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Elong\u003C\/span\u003E count \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E  myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                                \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ecount\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ecount\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E                    \n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E9 \u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ6 Given a list of integers, find the maximum value element present in it using Stream functions? \u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n            \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E max \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E  myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                             \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Emax\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #997700; font-weight: bold;\"\u003EInteger:\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003Ecompare\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n                             \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eget\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Emax\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E                    \n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E98\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ7 Given a String, find the first non-repeated character in it using Stream functions?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.function.Function\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            String input \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Java Hungry Blog Alive is Awesome\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n            Character result \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E input\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Echars\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ Stream of String       \u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EmapToObj\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Es \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E Character\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EtoLowerCase\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECharacter\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EvalueOf\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E((\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Echar\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E s\u003Cspan style=\"color: #333333;\"\u003E)))\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ First convert to Character object and then to lowercase         \u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ecollect\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECollectors\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgroupingBy\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EFunction\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eidentity\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(),\u003C\/span\u003E \u003Cspan style=\"color: #997700; font-weight: bold;\"\u003ELinkedHashMap:\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E Collectors\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ecounting\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()))\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/Store the chars in map with count \u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EentrySet\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Efilter\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eentry \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E entry\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgetValue\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1L\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Emap\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eentry \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E entry\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgetKey\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E())\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EfindFirst\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eget\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eresult\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E                    \n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003Ej \u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ8 Given a String, find the first repeated character in it using Stream functions?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.function.Function\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            String input \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Java Hungry Blog Alive is Awesome\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n            Character result \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E input\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Echars\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ Stream of String       \u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EmapToObj\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Es \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E Character\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EtoLowerCase\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECharacter\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EvalueOf\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E((\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Echar\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E s\u003Cspan style=\"color: #333333;\"\u003E)))\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ First convert to Character object and then to lowercase         \u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ecollect\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECollectors\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgroupingBy\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EFunction\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eidentity\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(),\u003C\/span\u003E \u003Cspan style=\"color: #997700; font-weight: bold;\"\u003ELinkedHashMap:\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E Collectors\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ecounting\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()))\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/Store the chars in map with count \u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EentrySet\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Efilter\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eentry \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E entry\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgetValue\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1L\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Emap\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eentry \u003Cspan style=\"color: #333333;\"\u003E-\u0026gt;\u003C\/span\u003E entry\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EgetKey\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E())\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EfindFirst\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                                    \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eget\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eresult\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E                    \n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003Ea\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ9 Given a list of integers, sort all the values present in it using Stream functions?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.function.Function\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\n             myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                   \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Esorted\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                   \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EforEach\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ESystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E::\u003C\/span\u003Eprintln\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u0026nbsp;8\u003Cbr \/\u003E10\u003Cbr \/\u003E15\u003Cbr \/\u003E15\u003Cbr \/\u003E25\u003Cbr \/\u003E32\u003Cbr \/\u003E49\u003Cbr \/\u003E98\u003Cbr \/\u003E98\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ10 Given a list of integers, sort all the values present in it in descending order using Stream functions?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: none 0% 0% repeat scroll rgb(243, 243, 243); border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.stream.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.function.Function\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            List\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EInteger\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E myList \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E49\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E32\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E15\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\n             myList\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Estream\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E\n                   \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Esorted\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ECollections\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EreverseOrder\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E())\u003C\/span\u003E\n                   \u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EforEach\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ESystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E::\u003C\/span\u003Eprintln\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E98\u003Cbr \/\u003E98\u003Cbr \/\u003E49\u003Cbr \/\u003E32\u003Cbr \/\u003E25\u003Cbr \/\u003E15\u003Cbr \/\u003E15\u003Cbr \/\u003E10\u003Cbr \/\u003E8\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today regarding 10 java 8 coding and programming interview questions and answers. Please mention in the comments in case you know any other better approach to solving the above questions."},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/8442855100292099778"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/8442855100292099778"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2020\/05\/java-8-coding-and-programming-interview-questions.html","title":"Top 10 Java 8 Coding and Programming Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-3858618325133520536"},"published":{"$t":"2020-05-16T05:55:00.001-04:00"},"updated":{"$t":"2021-04-28T10:37:24.087-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 20 Serialization Interview Questions and Answers"},"content":{"type":"html","$t":"Serialization is the important topic of java interview. Java developers should know the answer to serialization interview questions.\u0026nbsp; In this article, we will look at 20 most important questions and answers of serialization. We have covered questions for both beginners and experienced professionals level.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Ci\u003E\u003Cspan style=\"color: #990000;\"\u003ERead Also:\u003C\/span\u003E \u003C\/i\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/what-is-serialversionuid-and-why-need-it.html\" target=\"_blank\"\u003EWhat is serialVersionUID in Java\u003C\/a\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n1. What is Serialization in java?\u003C\/b\u003E\u003C\/h4\u003E\nA very common question asked during the java serialization interview. Every Java developer should know the answer to this question.\u0026nbsp; Serialization needed to \u003Cb\u003Ewrite an object into a binary format that can be transferred over the network or stored in the database\u003C\/b\u003E. Serialization prepares a stream of bytes of an object and the byte array consists of versionUID, class of the object, and the internal state of the object.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n2. Describe the De-serialization process.\u003C\/b\u003E\u003C\/h4\u003E\nThis is also a common interview question during the java interview. To build a java object from a stream of bytes is called deserialization. When a stream of bytes transferred over the network so another side of the network needs to revert back to java object. This process is called deserialization.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n3. Why do we need to use Serialization in java?\u003C\/b\u003E\u003C\/h4\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003ESerialization actually needed for transferring the byte stream over the network. Some real-time applications using java streams are listed below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na)\u0026nbsp;\u0026nbsp;\u0026nbsp; Data transmission\u003Cbr \/\u003E\nb)\u0026nbsp;\u0026nbsp;\u0026nbsp; Persistence\u003Cbr \/\u003E\nc)\u0026nbsp;\u0026nbsp;\u0026nbsp; Deep cloning\u003Cbr \/\u003E\nd)\u0026nbsp;\u0026nbsp;\u0026nbsp; Cross JVM communication\u003Cbr \/\u003E\ne)\u0026nbsp;\u0026nbsp;\u0026nbsp; Stashing\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n4. How can we implement Serialization in java?\u003C\/b\u003E\u003C\/h4\u003E\nThe most common method to implement Serialization in java is by implementing the Serializable interface. If an object implements a Serializable interface then this object can be transferred as the stream of byte over the network. Another way is to implement the Externalizable interface.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n5. What is the Marker interface?\u003C\/b\u003E\u003C\/h4\u003E\nIf an interface does not contain any methods then it is known as Marker Interface. The Serializable interface is a Marker interface. JVM automatically identifies whether the class is Serializable by checking the Serializable interface is implemented or not.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n6. Why the Serializable interface is called the Marker interface in Java?\u003C\/b\u003E\u003C\/h4\u003E\nThis is the most important question in the java serialization interview question. The serializable interface has no methods. We know that the interface which does not contain any method is called a Marker interface. That’s why the Serializable interface is Marker Interface.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n7. What is serialVersionUID?\u003C\/b\u003E\u003C\/h4\u003E\nWhen we make a class Serializable we have to declare a final static long variable with a value named by serialVersionUID. This variable will be serialized and passed during the byte stream transfer. During deserialization, the compiler will compare this serialVersionUID with the saved one. You can find the detail about serialVersionUID \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/what-is-serialversionuid-and-why-need-it.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n8. Describe the benefits of serialVersionUID.\u003C\/b\u003E\u003C\/h4\u003E\nDuring the deserialization process, serialVersionUID is compared with senders serialized object. If both are not the same JVM gives InvalidClassException. This ensures the security of the class during network transmission.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n9. How can we restrict some variables to be serialized?\u003C\/b\u003E\u003C\/h4\u003E\nIf we want to restrict a variable from serialized, we should declare this variable as a \u003Cb\u003Etransient\u003C\/b\u003E \u003Cb\u003Evariable\u003C\/b\u003E. The transient variable can not be converted to a byte of a stream.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n10. Do static variables participate in serialization?\u003C\/b\u003E\u003C\/h4\u003E\nThis is also a tricky interview question. Static variables or members are belonging to a class not the instance of the class. During serialization, we only consider the current state of an object. So during Serialization, static members are ignored. There is a \u003Cb\u003Erule of thumb that static fields are not serialized but there is an exception serialVersionUID\u003C\/b\u003E. serialVersionUID is also saved in the serialized object. (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/what-is-serialversionuid-and-why-need-it.html\" target=\"_blank\"\u003Eanswer\u003C\/a\u003E)\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n11. What will happen if the reference variable is not serializable?\u003C\/b\u003E\u003C\/h4\u003E\nIt's an easy but tricky question during interview time. If a reference variable of a serializable class does not support the Serializable interface then it will throw NotSerializableException at runtime. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n12. What will happen if a class is serializable but its superclass does not?\u003C\/b\u003E\u003C\/h4\u003E\nThe serialization process will continue until the inherited class is serializable. If a superclass not serializable though it will instantiate the constructor of the superclass. When a constructor chain initiated it will not stop execution until it reaches the end of the hierarchy of classes implementing the Serializable interface.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n13. Can you customize the serialization process? How can you achieve this?\u003C\/b\u003E\u003C\/h4\u003E\nYes, we can customize the serialization process. JVM invokes \u003Cb\u003EObjectOutputStream.writeObject (saveThisobject)\u003C\/b\u003E to write an object and \u003Cb\u003EObjectInputStream.readObject()\u003C\/b\u003E to read an object. We can override these methods with our own implementation. We can add pre or post-process during Serialization or Deserialization. These methods are declared to restrict inheritance. Private methods of the integrity of class remain the same that’s why JVM can call those methods during Serialization and Deserialization.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n14. Suppose parent class of a new child class implement Serializable interface, how can you avoid new child class to being serialized?\u003C\/b\u003E\u003C\/h4\u003E\nThis is an important question for serialization interview. It’s a tricky question. We know child classes get the properties of parent class by default. So, child classes are also serializable. To avoid new child classes being serializable we have to override the writeObject(object) or readObject() method. Also, we have to throw the NotSerializableException exception from the methods.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n15. Find out the difference between Serialization and Externalizable?\u003C\/b\u003E\u003C\/h4\u003E\nFor default serialization, we use the Serializable interface but with the Externalization interface, we can have more control over serialization. Serializable is a marker interface. That’s why there are no user-defined methods. But in Externalization, we can write the method inside the interface and implement it to the child classes. For this reason, we have more control over serialization and deserialization. The Externalizable interface also extends Serializable interface\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSerializable like below snapshot:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Einterface\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003ESerializable\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\nAn externalizable interface like below snapshot:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Einterface\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EExternalizable\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eextends\u003C\/span\u003E java\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eio\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003ESerializable\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EwriteExternal\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EObjectOutput out\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrows\u003C\/span\u003E IOException\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EreadExternal\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EObjectInput in\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrows\u003C\/span\u003E IOException\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E ClassNotFoundException\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n16. When we should use Externalizable interface in Java?\u003C\/b\u003E\u003C\/h4\u003E\nIf we need to handle the custom serialization, we should use the Externalizable interface. With this implementation, we can control the serialization process during the read or write.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMethods of Externalization listed below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EwriteExternal\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EObjectOutput out\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrows\u003C\/span\u003E IOException\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EreadExternal\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EObjectInput in\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrows\u003C\/span\u003E IOException\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E ClassNotFoundException\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n17. Describe the methods of serialization and deserialization process.\u003C\/b\u003E\u003C\/h4\u003E\nThere are no methods for the Serializable interface. If a class implements Serializable interface JVM treat an instance of this class as a serializable object. But with the Externalize interface, we can override defined methods.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n18. How to serialize an ArrayList?\u003C\/b\u003E\u003C\/h4\u003E\nArrayList by default implements a Serializable interface but we just need to check the inside object of ArrayList. If the object does not contain serializable object it will throw NotSerializableException exception. You can find more about how to serialize ArrayList in java \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/11\/how-to-serialize-arraylist-in-java-with-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n19. In singleton design pattern serialization becomes an issue, how you will overcome this?\u003C\/b\u003E\u003C\/h4\u003E\nDuring serialization or deserialization process principle of singleton class breaks. So, the \u003Cb\u003EreadResolve() method\u003C\/b\u003E should be overridden during a singleton serializable class declaration. Then it will create the same instance every time.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\n20. What are the most important classes and interfaces using the Serialization?\u003C\/b\u003E\u003C\/h4\u003E\nMost commonly used classes and interfaces are listed below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na) java.io.Serializable\u003Cbr \/\u003E\nb) java.io.Externalizable\u003Cbr \/\u003E\nc) java.io.ObjectInputStream\u003Cbr \/\u003E\nd) java.io.ObjectOutputStream\u003Cbr \/\u003E\ne) java.io.FileInputStream\u003Cbr \/\u003E\nf) java.io.FileOutputStream\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today, please mention in comments in case you have any questions related to Serialization interview questions and answers.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nReference:\u003Cbr \/\u003E\n\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/Serializable.html\" target=\"_blank\"\u003ESerializable Java doc \u003C\/a\u003E"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3858618325133520536"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3858618325133520536"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2020\/05\/serialization-interview-questions.html","title":"Top 20 Serialization Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-5175891409264442873"},"published":{"$t":"2020-01-11T20:50:00.000-05:00"},"updated":{"$t":"2020-01-11T20:52:24.747-05:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"spring-boot"}],"title":{"type":"text","$t":"Top 40 Most Frequently Asked Spring Boot Interview Questions and Answers"},"content":{"type":"html","$t":"In this tutorial, I will be sharing 40 Spring Boot interview questions and answers for both beginners and experienced professionals. If you like the article please bookmark it. I will keep adding questions to this post with future releases of Spring Boot.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003ERead Also:\u003C\/b\u003E\u003C\/i\u003E\u0026nbsp; \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/10\/best-spring-boot-online-courses-for-beginners.html\" target=\"_blank\"\u003EBest Spring Boot Online Course for Beginners\u003C\/a\u003E\u003C\/b\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch2\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003ETop 40 Spring Boot Interview Questions and Answers\u003C\/b\u003E\u003C\/span\u003E\u003C\/h2\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\nQ1 What is Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nSpring Boot is an opensource Java-based framework that is built on top of the Spring framework.\u003Cbr \/\u003E\nIt is a framework that makes it easy to create production-grade, standalone Spring-based applications.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is developed by Pivotal Software. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\nQ2 What do you understand by the term \"Opinionated\" in the context of Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nSince Spring Boot auto-configure many dependencies that's why it is opinionated. It is Spring Boot that decides for you which defaults to use for the configuration. As per requirements, you can also override auto-configuration settings in Spring Boot. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ3 What are the features of the Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nThe main features of the Spring Boot are as follow:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\na. Spring Application\u003Cbr \/\u003E\nb. Externalized Configuration\u003Cbr \/\u003E\nc. Profiles\u003Cbr \/\u003E\nd. Logging\u003Cbr \/\u003E\ne. Internationalization\u003Cbr \/\u003E\nf. Developing Web Applications\u003Cbr \/\u003E\ng. Security\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find it in detail \u003Ca href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/current\/reference\/html\/spring-boot-features.html#boot-features-profiles\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003E\nQ4 What are the goals or advantages of the Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nThe main goals or advantages of the Spring Boot are :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Support RAD (rapid application development).\u003Cbr \/\u003E\nb. Be opinionated out of the box but you can customize it as per your requirements.\u003Cbr \/\u003E\nc. It provides many non-functional features such as embedded servers (tomcat, jetty, undertow), health checks, security, metrics, etc.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ5 Name the few embedded servlet containers of Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nThe embedded servlet containers of Spring Boot are:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Tomcat\u003Cbr \/\u003E\nb. Jetty\u003Cbr \/\u003E\nc. Undertow\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ6 What are the build tools? Which build tools are recommended by Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nAccording to \u003Ca href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_build_automation_software\" rel=\"nofollow\" target=\"_blank\"\u003EWikipedia\u003C\/a\u003E,\u003Cbr \/\u003E\nBuild automation tools are tools that involve automating the process of compiling computer source code into binary code.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSpring Boot recommends build tools that support dependency management. Mainly, two build tools are frequently used in Spring Boot applications:\u003Cbr \/\u003E\na. Maven\u003Cbr \/\u003E\nb. Gradle\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ7 What is the role of @SpringBootApplication annotation?\u003C\/b\u003E\u003C\/h4\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@SpringBootApplication\u003C\/span\u003E\u003C\/b\u003E is equivalent to the combination of three annotations i.e \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@EnableAutoConfiguration\u003C\/span\u003E\u003C\/b\u003E, \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@ComponentScan\u003C\/span\u003E\u003C\/b\u003E and \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@Configuration\u003C\/span\u003E\u003C\/b\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find in detail about all the three annotations \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/09\/spring-boot-hello-world-example-maven-eclipse.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ8 What is spring-boot-devtools in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nWhenever files on the classpath change then application using \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Espring-boot-devtools\u003C\/span\u003E\u003C\/b\u003E restart automatically.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ9 How does the spring-boot-devtools provided auto-restart work in the Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nThe restart technology provided by Spring Boot depends upon two classloaders i.e base classloader and restart classloader.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EBase classloader:\u003C\/b\u003E This classloader loads the classes that do not change. For example third party jars.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ERestart classloader: \u003C\/b\u003EThis classloader loads the classes that you are actively developing.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe \u003Ci\u003Erestart classloader\u003C\/i\u003E is thrown away when the application is restarted. Meanwhile, a new \u003Ci\u003Erestart classloader\u003C\/i\u003E is created. This process makes the application restarts much faster than \"cold starts\" since the \u003Ci\u003Ebase classloader\u003C\/i\u003E is already present and populated.\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ10 What is the role of @RestController annotation in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nThe \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@RestController\u003C\/span\u003E\u003C\/b\u003E annotation is also known as a stereotype annotation. It provides a hint to Spring that the class plays a specific role.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is a combination of \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E\u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@ResponseBody\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/b\u003E and \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@Controller\u003C\/span\u003E\u003C\/b\u003E annotations. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ11 What is lazy initialization in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nWhen the lazy initialization is enabled in Spring Boot, the beans are created as they are needed rather than during application startup. As a result, the application starts much faster.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ12 Why the lazy initialization is disabled by default for Spring Boot applications even though it helps in reducing the application startup time?\u003C\/b\u003E\u003C\/h4\u003E\nThere are a couple of issues arise if the lazy initialization is enabled by default:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Ea. Delay the discovery of problem:\u003C\/b\u003E If a bean is misconfigured than failure will no longer occur during application startup time and the problem will only become apparent when the bean is initialized.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Eb. JVM memory allocation:\u003C\/b\u003E Make sure JVM has enough memory to accommodate all of the application's beans and not just for those beans that are initialized during the startup of the application. That's why it is recommended that fine-tuning of JVM's heap size is done before enabling lazy initialization.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ13 What are profiles in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nProfiles is a feature of Spring Boot. Segregate parts of application configuration and make it available for specific environments(DEV, IT, UAT, PROD, etc.) that can be achieved through Spring Profiles.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EExample:\u003C\/b\u003E The below class ProductionConfiguration will be available only in the production environment.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #555555;\"\u003E@Configuration\u003C\/span\u003E\n\u003Cspan style=\"color: #555555;\"\u003E@Profile\u003C\/span\u003E(\u003Cspan style=\"background-color: #fff0f0; color: #dd2200;\"\u003E\"production\"\u003C\/span\u003E)\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EProductionConfiguration\u003C\/span\u003E {\n\n  \u003Cspan style=\"color: #888888;\"\u003E\/\/ ...\u003C\/span\u003E\n\n}\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ14 What is the name of the configuration file that you use in the Spring Boot?\u0026nbsp; \u003C\/b\u003E\u003C\/h4\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Eapplication.properties\u003C\/span\u003E\u003C\/b\u003E is the configuration file that you use in the Spring Boot application.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ15 What is Spring Boot Actuator?\u003C\/b\u003E\u003C\/h4\u003E\na. Spring Boot Actuator helps in monitoring and managing your application when you push it to production.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. By using HTTP endpoints you can manage and monitor your application.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ16 How do you change the default port of embedded server in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nThere are many ways we can change the default port of embedded server in Spring Boot. One of the ways is by changing \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Eserver.port\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp; property value in the \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Eapplication.properties\u003C\/span\u003E\u003C\/b\u003E file.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find the answer in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/09\/change-default-port-of-spring-boot.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ17 What is Spring Boot Starter?\u003C\/b\u003E\u003C\/h4\u003E\na. Spring Boot provides a number of starters which are also called dependency descriptors that you can include in your application.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Spring Boot starters accommodate all the Spring and related technologies without having to go through sample code and copy-paste loads of dependency descriptors.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nc. All the starters in the Spring Boot framework follow a similar naming pattern:\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Espring-boot-starter-*\u003C\/span\u003E\u003C\/b\u003E\u003C\/b\u003E\u0026nbsp; where * indicates an application. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf you want database access using Spring and JPA, then include the \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Espring-boot-starter-data-jpa\u003C\/span\u003E\u003C\/b\u003E dependency in your project.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf you want to create a web application using RESTful web services, then include \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Espring-boot-starter-web\u003C\/span\u003E\u003C\/b\u003E in your project. \n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ18 What is the default logging used in Spring Boot if you use the \"Starters\" in your application?\u003C\/b\u003E\u003C\/h4\u003E\nLogback is used for logging if you use the \"Starters\" in your application.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ19 How will you disable specific auto-configuration in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\n\u003Cb\u003Ea. At annotation level:\u003C\/b\u003E \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Eexclude\u003C\/span\u003E\u003C\/b\u003E property is used to disable specific auto-configuration classes in Spring Boot.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn the below example we are excluding DataSourceAutoConfiguration class:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #555555; font-weight: bold;\"\u003E@EnableAutoConfiguration\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eexclude\u003Cspan style=\"color: #333333;\"\u003E={\u003C\/span\u003EDataSourceAutoConfiguration\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eclass\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E})\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Eb. At property level: \u003C\/b\u003EYou can also use the \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Espring.autoconfigure.exclude\u003C\/span\u003E\u003C\/b\u003E property to exclude certain auto-configuration classes.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ20 What is Thymeleaf?\u003C\/b\u003E\u003C\/h4\u003E\nThymeleaf is a java based server-side template engine for the web application. The main advantage of Thymeleaf is to bring stylish natural templates to your web application.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ21 What dependency is required to use Thymeleaf in the Spring Boot application?\u003C\/b\u003E\u003C\/h4\u003E\nBelow dependency must be added in your application to use Thymeleaf.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;dependency\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;groupId\u0026gt;\u003C\/span\u003Eorg.springframework.boot\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/groupId\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;artifactId\u0026gt;\u003C\/span\u003Espring-boot-starter-thymeleaf\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/artifactId\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/dependency\u0026gt;\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ22 What is the role of @RequestMapping annotation in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nWeb requests are mapped by the \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@RequestMapping\u003C\/span\u003E\u003C\/b\u003E annotation. In other words, it provides routing information.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ23 What are the minimum requirements of the Spring Boot 2 System?\u003C\/b\u003E\u003C\/h4\u003E\nSpring Boot 2.2.2.RELEASE requires: \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EJava:\u003C\/b\u003E Minimum Java 8. It is compatible upto Java 13.\u003Cbr \/\u003E\n\u003Cb\u003ESpring Framework:\u003C\/b\u003E\u0026nbsp; 5.2.2.RELEASE and above.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EBuild Tools:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003EMaven:\u003C\/b\u003E 3.3 and above.\u003Cbr \/\u003E\n\u003Cb\u003EGradle:\u003C\/b\u003E 5.x and 6.x\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EEmbedded servlet containers:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003ETomcat:\u003C\/b\u003E 9.0\u003Cbr \/\u003E\n\u003Cb\u003EJetty:\u003C\/b\u003E 9.4\u003Cbr \/\u003E\n\u003Cb\u003EUndertow:\u003C\/b\u003E 2.0\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EServlet version:\u003C\/b\u003E\u003Cbr \/\u003E\nYou can deploy Spring Boot applications to any servlet 3.1 and above the compatible container.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ24 How to create a project using Spring Boot Initializr?\u003C\/b\u003E\u003C\/h4\u003E\nYou can find a complete step by step process to create a project using Spring Boot Initializr \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/09\/spring-boot-hello-world-example-maven-eclipse.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ25 How you can enable HTTP\/2 support in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nIt is a one-liner: Just add the following line in application.properties\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Eserver.http2.enabled=true\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ26 Which is the only endpoint disabled by default in Spring Boot Actuator? How do you enable it?\u003C\/b\u003E\u003C\/h4\u003E\nshutdown is the only endpoint that is disabled by default in Spring Boot Actuator.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can enable the shutdown endpoint by using the following property:\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Emanagement.endpoint.shutdown.enabled=true\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ27 What is shutdown in Spring Boot Actuator?\u003C\/b\u003E\u003C\/h4\u003E\nshutdown is an endpoint in Spring Boot Actuator that is used to gracefully shutdown the application.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ28 How do you configure Log4j2 for Logging in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nIf you are using \"Starters\" in your Spring Boot project, then, you have to exclude Logback and include Log4j2.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ29 What is the default embedded servlet container for web application in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nTomcat is the default embedded servlet container for web application in Spring Boot\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ30 How to use jetty instead of tomcat while using spring-boot-starter-web in Spring Boot project?\u003C\/b\u003E\u003C\/h4\u003E\nYou can use the jetty embedded servlet container instead of tomcat by excluding the spring-boot-starter-tomcat dependency from spring-boot-starter-web.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;dependency\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;groupId\u0026gt;\u003C\/span\u003Eorg.springframework.boot\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/groupId\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;artifactId\u0026gt;\u003C\/span\u003Espring-boot-starter-web\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/artifactId\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;exclusions\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E          \u0026lt;exclusion\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E               \u0026lt;groupId\u0026gt;\u003C\/span\u003Eorg.springframework.boot\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/groupId\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E               \u0026lt;artifactId\u0026gt;\u003C\/span\u003Espring-boot-starter-tomcat\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/artifactId\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E          \u0026lt;\/exclusion\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;\/exclusions\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/dependency\u0026gt;\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\nYou also need to add the following in pom.xml\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;dependency\u0026gt;\u003C\/span\u003E\n     \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;groupId\u0026gt;\u003C\/span\u003Eorg.springframework.boot\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/groupId\u0026gt;\u003C\/span\u003E\n    \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E \u0026lt;artifactId\u0026gt;\u003C\/span\u003Espring-boot-starter-jetty\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/artifactId\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/dependency\u0026gt;\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ31 How you can create an executable jar in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nYou can create an executable jar in Spring Boot by adding \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Espring-boot-maven-plugin\u003C\/span\u003E\u003C\/b\u003E in pom.xml as shown below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;build\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;plugins\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E          \u0026lt;plugin\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E               \u0026lt;groupId\u0026gt;\u003C\/span\u003Eorg.springframework.boot\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/groupId\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E               \u0026lt;artifactId\u0026gt;\u003C\/span\u003Espring-boot-maven-plugin\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/artifactId\u0026gt;\u003C\/span\u003E    \n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E          \u0026lt;\/plugin\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E     \u0026lt;\/plugins\u0026gt;\u003C\/span\u003E\n\u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003E\u0026lt;\/build\u0026gt;\u003C\/span\u003E \n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ32 How to use a random unassigned port in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nYou can\u0026nbsp; the random unassigned port for the Spring Boot application by using the property \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Eserver.port=0\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ33 Can you run Spring Boot application packaged as executable war using java -jar command?\u003C\/b\u003E\u003C\/h4\u003E\nYes, you can use java -jar command to run the Spring Boot application packaged as executable war.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ34 What is LiveReload in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Espring-boot-devtools\u003C\/span\u003E\u003C\/b\u003E module contains an embedded LiveReload server. It can be used to trigger a browser refresh when a resource is changed. At a time, you can only run one LiveReload server.\u0026nbsp;\u003Cb\u003E \u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ35 What is the difference between Spring and Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nSpring is a java based web application framework. You can create customized web applications by using tools and libraries\u003Cb\u003E \u003C\/b\u003Eprovided by the Spring framework.\u003Cbr \/\u003E\n\u003Cb\u003E \u003C\/b\u003E\u003Cbr \/\u003E\nSpring Boot is a module of Spring that is used to create production-grade and standalone Spring-based applications.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ36 What are the different ways to externalize configurations in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nThere are 4 ways to externalize configurations in Spring Boot.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Using properties files\u003Cbr \/\u003E\nb. Using YAML files\u003Cbr \/\u003E\nc. Environment variables\u003Cbr \/\u003E\nd. Command line arguments\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ37 How do you write a JSON REST web service in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nIf \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003E@RestController\u003C\/span\u003E\u003C\/b\u003E is present in the Spring Boot application, then, it will render the response by default in JSON format as long as\u0026nbsp; Jackson2 is on the classpath.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ38\u003C\/b\u003E \u003Cb\u003EWhat are the disadvantages of Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\na. Spring Boot can contain dependencies that are not in use thus causing the huge deployment file size.\u003Cbr \/\u003E\nb. Converting legacy Spring applications into Spring Boot application requires a significant effort and a time-consuming process.\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ39 Using Maven plugin can you run Spring Boot application?\u003C\/b\u003E\u003C\/h4\u003E\nYes, by using the below command we can run Spring Boot application using Maven plugin.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003Emvn spring-boot:run\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ch4\u003E\n\u003Cb\u003EQ40 What is the difference between CommandLineRunner and ApplicationRunner in Spring Boot?\u003C\/b\u003E\u003C\/h4\u003E\nCommandLineRunner and ApplicationRunner both are interfaces. Both works in the same way and provides a single \u003Cb\u003E\u003Cspan style=\"color: #ff0779; font-family: \u0026quot;consolas\u0026quot; , \u0026quot;liberation mono\u0026quot; , \u0026quot;menlo\u0026quot; , \u0026quot;courier\u0026quot; , monospace; font-size: 14px;\"\u003Erun()\u003C\/span\u003E\u003C\/b\u003E method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe difference between CommandLineRunner and ApplicationRunner is given below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nCommandLineRunner gives access to the application arguments as a simple String[] array where as ApplicationRunner uses the ApplicationArguments interface that provides access to both raw String[] arguments as well as parsed non-option and option arguments. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E\n\nThat's all for today, please mention in comments in case you have any questions related to Spring Boot interview questions and answers for beginners and experienced professionals.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nReferences:\u003Cbr \/\u003E\n\u003Ca href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/2.2.2.RELEASE\/reference\/htmlsingle\/#getting-started\" target=\"_blank\"\u003ESpring Boot Doc\u003C\/a\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/5175891409264442873"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/5175891409264442873"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2020\/01\/spring-boot-interview-questions.html","title":"Top 40 Most Frequently Asked Spring Boot Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-8061657299918912481"},"published":{"$t":"2019-11-29T13:55:00.000-05:00"},"updated":{"$t":"2020-06-06T12:00:50.864-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"oops"}],"title":{"type":"text","$t":"Top 50 OOPs Interview Questions and Answers in Java"},"content":{"type":"html","$t":"In this post, I will be sharing most frequently asked 50 core java OOPs concepts interview questions and answers in java for freshers (0 - 1 year) and experienced. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ1 What is OOPs?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nOOPs stands for Object-Oriented Programming System. It is a programming paradigm formulated around Objects. The state and behavior of an Object are represented as data members (variables) and methods (functions).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe two core concepts of OOPs are Object and Class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ2 What is Class?\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EClass:\u003C\/b\u003E\u0026nbsp;Class is a template that explains the kind of state and behavior that objects of its type support.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ3 What is an Object?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EObject:\u003C\/b\u003E At runtime, when JVM (Java Virtual Machine) finds the \u003Ci\u003Enew\u003C\/i\u003E keyword, it will use the appropriate class to create an object which is an instance of that class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ4 What do you understand by the terms state and behavior of an Object?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EState(instance variables):\u003C\/b\u003E Values assigned to the instance variables of an object make up the object's state.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EBehavior(functions\/methods):\u003C\/b\u003E\u0026nbsp;Methods\/functions are where the class logic is stored. This is where the data gets manipulated or algorithms get executed. Functions are also called procedures or methods.\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nVariables are also called members, fields or attributes.\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ5 Difference between a Class and an Object in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. An object is an instance of a class. Class is a blueprint from which the instance\/object is created.\u003Cbr \/\u003E\nb. An object is a physical entity whereas Class is a logical entity.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ6 Advantages of OOPs concepts in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Gives a clear modular structure for programs\u003Cbr \/\u003E\nb. Objects can also be reused within and across applications, Reuse also enables faster development.\u003Cbr \/\u003E\nc. Better Productivity as OOPs techniques enforce rules on a programmer.\u003Cbr \/\u003E\nd. It helps to model real-life problems.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ7 What are the 4 OOPs principles in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Abstraction\u003Cbr \/\u003E\nb. Encapsulation\u003Cbr \/\u003E\nc. Polymorphism\u003Cbr \/\u003E\nd. Inheritance\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ8 Why do we use OOPs in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nReduces complexity and also improves the maintainability of the system. When combined with the concepts of the Abstraction, Encapsulation, Inheritance, and \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/11\/polymorphism-in-java-with-examples.html\" target=\"_blank\"\u003EPolymorphism\u003C\/a\u003E\u003C\/b\u003E give more power to the Object-Oriented Programming languages.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ9 What is Method Overloading in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA class with multiple methods by the same name but different parameters called Method Overloading.\u003Cbr \/\u003E\nYou can find it in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overloading-in-java-with-examples.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ10 What is Method Overriding in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn Method Overriding a subclass\/child class has the same method name, same method signature and same return types (covariant return types) as a method in its parent class, then the child class method has overridden the parent class method.\u003Cbr \/\u003E\nYou can find the complete detail about Method Overriding \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/11\/method-overriding-in-java-with-examples.html\" target=\"_blank\"\u003E\u003Cb\u003Ehere\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ11 What is the difference between Method Overloading and Method Overriding?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nI have explained in detail about the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/10\/6-difference-method-overloading-and-method-overriding.html\" target=\"_blank\"\u003E\u003Cb\u003Edifference between Method Overloading and Method Overriding in java\u003C\/b\u003E\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ12 What is early and late binding in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen an object is assigned to an object variable the compiler performs a process called binding.\u003Cbr \/\u003E\nCompile-time binding is called early binding (static binding) whereas runtime binding is called late\nbinding (dynamic binding).\u003Cbr \/\u003E\nMethod Overloading is an example of early binding in java. Method Overriding is an example of late binding in java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ13 Can we override the overloaded method in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, we can override the overloaded method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ14 Can we overload a static method in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, overloading a static method in java is possible. You can have as many overloaded static methods having the same name as you like provided they have different method signatures. Static methods should be accessed by class names rather than an object. \u003Cb\u003EA static method can be overloaded, but can not be overridden in java.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ15 Can we override a static method in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, you can not override a static method in java. Static methods belong to class so they are resolved at compile time using the type of reference variable.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ16 What do you mean by covariant method overriding in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn the case of covariant method overriding, the return type must be the same as, or a subtype of,\u0026nbsp; the return type declared in the original overridden method in the superclass. The concept of covariant return type was introduced in java 1.5 version.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ17 Can we override a method declared as private in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, you can not override a private method in java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ18 Can you override the method which is declared as final?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, you can not override a method declared as final.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ19 What is inheritance in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nInheritance in java is the process in which the child class(subclass) object acquires all the properties and behaviors of a parent object (superclass).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nInheritance is achieved by using the\u003Cb\u003E extends\u003C\/b\u003E keyword. Only public and protected fields (attributes) can be accessed in child class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ20 What are the different types of inheritance in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSingle inheritance\u003Cbr \/\u003E\nMultiple inheritance\u003Cbr \/\u003E\nMultilevel inheritance\u003Cbr \/\u003E\nHierarchical inheritance\u003Cbr \/\u003E\nHybrid inheritance\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ21 Is a method declared as final inherited in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, it is inherited still you cannot override it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ22 What is multiple inheritance?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nIf a subclass(child class) acquires state and behavior from multiple parent classes(superclass) then it is known as multiple inheritance.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ23 Why multiple inheritance is not supported in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn order to reduce the complexity of java language, multiple inheritance is not supported in java.\u003Cbr \/\u003E\nSuppose there are 4 classes named A, B, C, and D. The relationship between A, B, C, and D is :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. class A has two child classes (subclass) B and C\u003Cbr \/\u003E\nb. class D has two parent classes (superclass) B and C\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSuppose you have a method named commonMethod() in class A which is overridden by both B and C classes.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf you want to call commonMethod() on the instance of D class, which method should get called (from class B or class C).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDue to this ambiguity, multiple inheritance is not supported in java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe above problem is known as the diamond problem.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ24 What are the problems might be created when you are trying to implement multiple inheritance\u0026nbsp;concept in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe operations like casting, constructor chaining might cause problems when trying to implement\u003Cbr \/\u003E\nmultiple inheritance. Even the compiler gets complications as to call which class method and which class gets priority.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ25 Why java is not a pure Object-Oriented Language?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava is not a pure Object Oriented Language because it uses primitive variables e.g int, float, double which are not objects.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ26 In a class which is the default access modifier in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nThe default access modifier is \"\u003Cb\u003Edefault\u003C\/b\u003E\" which is also known as \"package-private\" (though you can't use this explicitly). It means the field will be visible to the same package to which the class belongs. Java uses a \"\u003Cb\u003Edefault\u003C\/b\u003E\" access modifier when no access modifier is present.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ27 Can Operators be overloaded in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava doesn't allow user-defined overloaded operators, but internally java overloads operators like ‘+’. '+' is overloaded for concatenation of strings and addition of numbers.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ28 What is the difference between Inheritance and Encapsulation in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nInheritance has a parent-child relationship and it is one of the ways to reuse the code written for the parent\nclass but it forms the basis of Polymorphism.\u003Cbr \/\u003E\nEncapsulation is used to bind the code and data into a single unit.\u003Cbr \/\u003E\ne.g. HashMap encapsulates on how to store elements and how to calculate hash values.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ29 Does abstract class have constructor in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAbstract classes can have constructors. A class can not be instantiated once it is declared as an abstract class but that does not mean that an abstract class cannot have a constructor.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ30 Can a constructor be declared as private in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, you can declare the \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/07\/constructor-overloading-chaining-example-basics-rules-important.html\" target=\"_blank\"\u003Econstructor\u003C\/a\u003E\u003C\/b\u003E as private in java. If the constructor is declared as private, then the class cannot be instantiated, it can be instantiated from another method in the class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ31 From a method can we call a constructor directly in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, Constructors can't be called directly, they can be called implicitly when the \u003Ci\u003Enew\u003C\/i\u003E keyword creates an object. Methods can be called explicitly on an object that has already been created with the help of a\u003Ci\u003E new\u003C\/i\u003E keyword.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ32 Can Interfaces have a constructor in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, the interface cannot have constructors (Constructor is being called during the creation of object).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ33 Difference between constructors and methods in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. A constructor is used to initializing an object whereas method is used to exhibit functionality of an object.\u003Cbr \/\u003E\nb. Constructors are invoked implicitly but methods are invoked explicitly.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ34 Why Encapsulation in java is called as Data hiding in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen a field is declared private in a class then it cannot be accessed by anyone outside the class and\u003Cbr \/\u003E\nhides the fields within the class. Therefore, Encapsulation is also called data hiding.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ35 What is tightly encapsulated class in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf each variable in a class is declared as private then it is called a tightly encapsulated class in java. It is not required to check whether class contains getter and setter methods or not and if these methods are declared as public or not.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ36 What is a ternary operator in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe ternary operator is a conditional operator that takes three operands, and it evaluates the condition as true or false.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor example:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E                \n    \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E x \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E y \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E25\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E((\u003C\/span\u003Ex \u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E\u003C\/span\u003E y\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E?\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"x is greater than y\"\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\" x is less than y\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput:\u003C\/b\u003E\u003Cbr \/\u003E\nx is less than y\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ37 What is the use of the final keyword in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBy declaring the variable as final, the value of the final variable cannot be changed.\u003Cbr \/\u003E\nBy declaring the method as final, method cannot be overridden.\u003Cbr \/\u003E\nBy declaring the class as final, class cannot be extended.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ38 Can we stop method overriding without using the final keyword?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, we can stop the method overriding by declaring the method as private or static.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ39 Mention the difference between final and super keyword in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe super keyword is used to access superclass (Parent class) variables and methods by subclass objects\u003Cbr \/\u003E\nThe final keyword can be used in three places in java with different jobs as explained in Q37.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ40 Can we use \"super super\" in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, error occurs in line “super.super.print();”. A class cannot directly access the grandparent's members in java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ41 Can we override the constructor in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, A constructor cannot be overridden in java but a method can be overridden.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ42 What are the access modifiers available in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPublic, private, protected, default\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ43 What is a blank or uninitialized final variable?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA variable that is declared as final and is not initialized at the time of declaration is known as a blank final variable.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ44 What is a static blank final variable in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA final static variable that is not initialized at the time of declaration is known as a static blank final variable.\u003Cbr \/\u003E\nIt can be initialized only in static block.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ45 What is a wrapper class in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe wrapper class in java provides the mechanism to convert primitive type into object and object into primitive type.\u003Cbr \/\u003E\nThe automatic conversion of primitive type into an object is known as autoboxing and object to primitive type is unboxing.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ46 What is instanceof keyword in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\ninstanceof operator is used to check whether the object is an instance of the specific class or interface.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ47 What is object cloning in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe creation of an exact copy of an object using the clone() method of Object class is called object cloning.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ48 What is the use of this keyword in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nthis keyword is used to refer to the object within the current scope.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ49 Difference between Composition and Inheritance in OOPs in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n• Inheritance builds the IS-A relationship while Composition builds a HAS-A relationship.\u003Cbr \/\u003E\n• The parent-child relationship is best represented using Inheritance but if only the services of\u003Cbr \/\u003E\nanother class have to be used then Composition.\u003Cbr \/\u003E\n• Though both Inheritance and Composition allows reusing code, one of the disadvantages of\u003Cbr \/\u003E\nInheritance is that it breaks encapsulation.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ50 Can a class implement two interfaces wherein they both have a method with the same name and signature in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, a class can implement two interfaces wherein they both have a method with the same name and signature."},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/8061657299918912481"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/8061657299918912481"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2019\/11\/oops-interview-questions-and-answers-in-java.html","title":"Top 50 OOPs Interview Questions and Answers in Java"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-3087094595770147726"},"published":{"$t":"2019-11-16T13:11:00.000-05:00"},"updated":{"$t":"2019-11-16T15:35:54.216-05:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Exception"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 50 Exception Handling Interview Questions and Answers in Java"},"content":{"type":"html","$t":"In this post, I will be sharing the top 50 exception handling interview questions and answers in java for freshers (0-1 years) and experienced java developers. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nI have divided the post into two sections: \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. Face to Face Round Exception Handling Interview Questions and Answers\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n2. Written\/Coding Round Interview Questions\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003E\u003Ci\u003EFace to Face Round Exception Handling Interview Questions and Answers:\u003C\/i\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ1. What is an Exception in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAn Exception is a failure condition that occurs during the execution of a program and disrupts the normal flow of the program. It has to be handled properly, failing which program will be terminated abruptly.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ2. How the exceptions are handled in java? \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nExceptions handling can be done using try, catch and finally blocks.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Etry :\u003C\/b\u003E The code or set of statements that may raise exception should be try block.\u003Cbr \/\u003E\n\u003Cb\u003Ecatch :\u003C\/b\u003E This block catches the exceptions thrown in the try block.\u003Cbr \/\u003E\n\u003Cb\u003Efinally :\u003C\/b\u003E This block of code is always executed whether an exception has occurred in the try block or not except in one scenario explained in below question.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ3 Is finally block always get executed in the java program?\u003C\/b\u003E\u003C\/span\u003E \u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EThis question is very important\u003C\/b\u003E. finally block is always executed but there is one scenario when finally block does not execute.\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003EBy using \u003Cb\u003ESystem.exit(0)\u003C\/b\u003E in the try or catch block, results in \u003Cb\u003Efinally block does not execute\u003C\/b\u003E. The reason is System.exit(0) line terminates the running java virtual machine. Termination leads to no more execution of the program.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EThis is the only scenario when finally block fails to execute.\u003C\/b\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E args\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \n    \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Etry\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Inside try block \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n            \u003Cspan style=\"color: #888888;\"\u003E\/* After executing below line\u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E            jvm terminates the program *\/\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eexit\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E            \n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ecatch\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EException e\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Inside catch block\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efinally\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Inside finally block\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: black;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\nInside try block \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ4. What are the differences between Error and Exception in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMain differences between Error and Exception are : \u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Errors are caused by the JVM environment in which the application is running. Example: OutOfMemoryError while Exceptions are caused by the application itself. Example: NullPointerException.\u003Cbr \/\u003E\nb. Errors can only occur at runtime while Exceptions can occur at compile time or runtime. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find more differences between Error and Exception \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/10\/difference-between-error-and-exception.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ5. What statements can exist in between try, catch and finally blocks?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, try, catch and finally forms a single unit and no other statements should exist in between try, catch and finally blocks.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ6. Are we allowed to use only try block without a catch and finally blocks?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003EPrior to Java 7:\u003C\/b\u003E\u003C\/i\u003E \u003Cbr \/\u003E\nNo, it is not allowed. If used it shows compilation error. The try block must be followed by a catch block or finally block.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003EAfter Java 7 (Correct Answer): \u003C\/b\u003E\u003C\/i\u003E\u003Cbr \/\u003E\nYes, it is possible to have a try block without a catch and finally blocks. The introduction of try-with-resources concept makes it possible.\u003Cbr \/\u003E\nThe only constraint is resources which we are passing as a parameter in try block must implement \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/AutoCloseable.html\" target=\"_blank\"\u003EAutoCloseable\u003C\/a\u003E interface.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E args\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \n    \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #888888;\"\u003E\/* After completion of try block,\u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E        the Scanner object would be auto closed\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #888888;\"\u003E        as Scanner class implements AutoCloseable\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #888888;\"\u003E        interface. *\/\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Etry\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EScanner sc \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E Scanner\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ESystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Ein\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E))\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\" try without catch\/finally block \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: black;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\ntry without catch\/finally block \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ7. What are Checked and Unchecked exceptions in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nExceptions which are known to the compiler are called Checked exceptions. Checked exceptions are checked at compile-time only. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nUnchecked exceptions occur only at run time. Unchecked exceptions are also called as run time exceptions. All subclasses of java.lang.RuntimeException and java.lang.Error is of Unchecked type.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ8. What is the difference between Checked and Unchecked exceptions in java?\u003C\/b\u003E\u003C\/span\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the most popular interview questions for java developers. Make sure this question is in your to-do list before appearing for the interview.\u003Cbr \/\u003E\nMain differences between Checked and Unchecked exceptions are :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Checked exceptions are checked at compile time while Unchecked exceptions are checked at run time.\u003Cbr \/\u003E\nb. Checked exceptions must be handled by try\/catch block or throws keyword while Unchecked exceptions are not necessary to handle.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nfind a detailed explanation \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/08\/difference-checked-and-unchecked-exception.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ9. What is the difference between final, finally and finalize in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Efinal keyword:\u003C\/b\u003E\u003Cbr \/\u003E\nBy declaring a variable as final, the value of final variable cannot be changed.\u003Cbr \/\u003E\nBy declaring a method as final, method cannot be overridden.\u003Cbr \/\u003E\nBy declaring a class as final, class cannot be extended.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Efinally:\u003C\/b\u003E\u003Cbr \/\u003E\nUsed\n after try or try-catch block, will get executed after the try and \ncatch blocks without considering whether an exception is thrown or not.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Efinalize:\u003C\/b\u003E\u003Cbr \/\u003E\nFinalize\n method is the method that Garbage Collector always calls just before \nthe deletion\/destroying the object which is no longer in use in the \ncode.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ10 What is try-with-resources concept in java? How it differs from an ordinary try statement?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/exceptions\/tryResourceClose.html\" target=\"_blank\"\u003EJava docs\u003C\/a\u003E,\n try-with-resources statement is a try statement that declares one or more resources. It ensures that each resource is closed at the end of the statement. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\ntry-with-resources statement can have catch or finally block similar to ordinary try statement. In a \ntry-with-resources statement, JVM makes sure catch or finally block is run after the resources declared have been closed. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ11. What is RuntimeException in java. Give example?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe\n exceptions which occur at runtime are called as RuntimeException. These exceptions are unknown to the compiler. All subclasses of \njava.lang.RuntimeException are RuntimeExceptions.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor example:\u003Cbr \/\u003E\nNumberFormatException, \nNullPointerException, ClassCastException, ArrayIndexOutOfBoundException\u0026nbsp;\n etc.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ12. What is the difference between ClassNotFoundException and NoClassDefFoundError in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question is important because very few Java developers are aware of the difference between ClassNotFoundException and NoClassDefFoundError.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EClassNotFoundException:\u003C\/b\u003E\u003Cbr \/\u003E\nAn exception that occurs when you try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath is called ClassNotFoundException.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ENoClassDefFoundError:\u003C\/b\u003E\u003Cbr \/\u003E\nAn exception that occurs when a particular class is present at compile-time but was missing at run time is called NoClassDefFoundError.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nfind a detailed explanation \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2016\/04\/4-difference-between.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ13. Can we throw an exception manually\/explicitly?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, using \u003Cb\u003E\u003Ci\u003Ethrow\u003C\/i\u003E\u003C\/b\u003E keyword we can throw an exception manually.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ESyntax:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrow\u003C\/span\u003E InstanceOfThrowableType\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EFor example:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E args\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \n    \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Etry\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            \u003Cspan style=\"color: #888888;\"\u003E\/\/ Creating an object of ArithmeticException\u003C\/span\u003E\n            ArithmeticException ae \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E ArithmeticException\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n            \u003Cspan style=\"color: #888888;\"\u003E\/\/Manually throwing ArithmeticException\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrow\u003C\/span\u003E ae\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ecatch\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArithmeticException e\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Caught the manually thrown Exception\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: black;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\nCaught the manually thrown Exception\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ14. Does catch block rethrow an exception in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, catch block can rethrow an exception using throw keyword. It is called re-throwing an exception. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nFor example :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E args\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \n    \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Etry\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            \u003Cspan style=\"color: #888888;\"\u003E\/\/ Creating an object of ArithmeticException\u003C\/span\u003E\n            ArithmeticException ae \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E ArithmeticException\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n            \u003Cspan style=\"color: #888888;\"\u003E\/\/Manually throwing ArithmeticException\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ethrow\u003C\/span\u003E ae\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ecatch\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArithmeticException e\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Rethrowing the caught exception below \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\u0026nbsp;\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E            \u003Cspan style=\"color: #888888;\"\u003E\/\/Rethrowing ArithmeticException\u003C\/span\u003E \u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u0026nbsp;\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E           throw\u003C\/span\u003E e\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EOutput :\u003C\/b\u003E\u003Cbr \/\u003E\nRethrowing the caught exception below \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nException in thread \"main\" java.lang.ArithmeticException\u003Cbr \/\u003E\n\u0026nbsp;\u0026nbsp;\u0026nbsp; at JavaHungry.main(JavaHungry.java:7)\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ15. What is the use of throws keyword in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nthrows keyword is used to declare an exception. You can find a detailed explanation \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/09\/difference-between-throw-and-throws.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ16. Why it is always recommended that clean up activities like closing the DB connections and I\/O resources to keep inside a finally block?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nfinally block will always be executed except one scenario as discussed above in Q3. By ensuring the cleanup operations in finally block, you will assure that those operations will be always executed irrespective of whether an exception has occurred or not.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ17. What is OutOfMemoryError in Exception Handling?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nOutOfMemoryError is the subclass of java.lang.Error. It occurs when JVM runs out of memory. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ18. What is ClassCastException in Exception Handling?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nRunTimeException which occurs when JVM not able to cast an object of one type to another type is called ClassCastException.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ19. What is the difference between throws and throw in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the most frequently asked interview questions for java developers.\u003Cbr \/\u003E\nMain differences between throws and throw are :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. throws keyword is used when writing methods, to declare that the method in question throws the specified (checked) exception.\u003Cbr \/\u003E\nthrow is used when an instruction is to explicitly throw the exception.\u003Cbr \/\u003E\nb.\u0026nbsp; throws is used with a method signature while the throw is used inside a method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find a detailed explanation of the difference between throw and throws in java \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/09\/difference-between-throw-and-throws.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ20. What is StackOverflowError in Exception Handling?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nStackOverflowError is thrown by the JVM when stack overflows in a program.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ21. Which class is the root class for all types of errors and exceptions in Exception Hierarchy?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\njava.lang.Throwable is the superclass for all types of errors and exceptions in java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ22. When do we use printStackTrace() method in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nprintStackTrace() function is used to print the detailed information about the exception thrown by the try\/catch block.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ23. Give some examples of Checked exceptions?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSQLException, ClassNotFoundException, IOException\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ24. Give some examples of Unchecked exceptions?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArrayIndexOutOfBoundsException, NullPointerException, NumberFormatException\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ25. List the Methods in the Throwable class?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBelow are the important methods of Throwable class:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n•\u0026nbsp;\u0026nbsp; \u0026nbsp;getMessage()\u003Cbr \/\u003E\n•\u0026nbsp;\u0026nbsp; \u0026nbsp;Throwable getCause()\u003Cbr \/\u003E\n•\u0026nbsp;\u0026nbsp; \u0026nbsp;toString()\u003Cbr \/\u003E\n•\u0026nbsp;\u0026nbsp; \u0026nbsp;printStackTrace()\u003Cbr \/\u003E\n•\u0026nbsp;\u0026nbsp; \u0026nbsp;StackTraceElement [] getStackTrace()\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ26. What is a SQLException in Exception Handling?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAn exception that provides information related to database access error or other errors is called SQL Exception.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ27. What is NumberFormatException in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNumberFormatException is thrown when you try to convert a String into a number.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ28. What is ArrayIndexOutOfBoundsException in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArrayIndexOutOfBoundsException arises while trying to access an index of the array that does not exist or out of the bound of this array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ29. What will happen if an exception is thrown by the main method?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen an exception is thrown by the main method then JVM terminates the program. As a result, you will find the exception message and stack trace in the system console.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ30. Is it legal to have an empty catch block?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, we can have an empty catch block in java but it is not the best practice. If an exception is caught by the empty catch block, then we do not have any information about the exception occurred. You should provide at least the logging statement to log the exception details.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ31. What are the keywords in Java for Exception Handling?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nthrow, throws, try, catch and finally\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ32. List some important methods of Java Exception class?\u003C\/b\u003E\u003C\/span\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. printStackTrace()\u003Cbr \/\u003E\nb. toString()\u003Cbr \/\u003E\nc. getMessage()\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ33. What are the advantages of using Exceptions in your programs?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/exceptions\/advantages.html\" target=\"_blank\"\u003EJava doc\u003C\/a\u003E,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Separating \"regular\" code from error handling code.\u003Cbr \/\u003E\nb. The ability to propagate errors reporting up the call stack of methods.\u003Cbr \/\u003E\nc. Differentiating and grouping error types.\u0026nbsp; \u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #990000;\"\u003E\u003Cb\u003E\u003Ci\u003EWritten\/Coding round Interview Questions:\u003C\/i\u003E\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPlease go through the below link before attempting questions 46-50.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ34-45\u003C\/b\u003E\u0026nbsp; \u003Cb\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/09\/programs-on-exception-handling-in-java.html\" target=\"_blank\"\u003EPrograms on Exception Handling in Java with Answers.\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ46. What is unreachable catch block error?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u003Cbr \/\u003E\nWhen\n there is more than one catch block, then, the order of catch blocks must be from most specific to most general ones. In other words, subclasses of Exception must come first and superclasses later. If you try to keep superclasses first and subclasses later, the compiler will stop you and show unreachable catch block error.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ47. Can we provide the statements after finally block if the control is returning from the finally block itself?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, because control is returning from the finally block itself so it shows unreachable code error.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ48. Does finally block get executed if either try or catch blocks are returning the control?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, finally block will be always executed no matter whether try or catch blocks are returning the control or not. There is one scenario where finally block does not execute, for more information check out Q3.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ49.\u0026nbsp; Suppose there is a catch block corresponding to a try block with three statements – statement1, \nstatement2, and statement3. Assume that exception is thrown in statement2. Does statement3 get executed?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nStatement3 will not get executed because once a try block throws an exception, remaining statements will not be executed.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ50. What will happen if we override a superclass method which is throwing an unchecked exception with a checked exception in the subclass?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf the overridden method throwing an unchecked exception, then the subclass overriding method must have the same exception or any other unchecked exceptions. If you are trying to throw checked exception with subclass overriding method then it will give a compilation error.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003ERule of thumb : \u003C\/b\u003E\u003C\/span\u003EAn overriding method (subclass method) can not throw a broader exception than an overridden method (superclass method).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for today, please mention in comments if you have any questions related to exception handling in java interview questions and answers for freshers and experienced."},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3087094595770147726"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3087094595770147726"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2019\/11\/exception-handling-interview-questions-and-answers.html","title":"Top 50 Exception Handling Interview Questions and Answers in Java"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-7380600333419729622"},"published":{"$t":"2019-11-15T04:40:00.004-05:00"},"updated":{"$t":"2022-12-31T08:22:42.117-05:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"collection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java"}],"title":{"type":"text","$t":"[2023 Updated] Top 50 Java Collections Interview Questions and Answers "},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\nWe have already shared the most frequently asked java interview questions for experienced candidates. I also shared the tricky coding interview questions in our previous posts. Today, we will learn about the top 50 java collections interview questions and answers. We will divide this post into three categories \u003Cb\u003E(Updated for 2023) \u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBeginner level (0-1 year experience (Freshers)),\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIntermediate level (1, 2, 3 years experienced Java Developers)\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAdvanced level(3+\u0026nbsp;Experienced) java collections interview questions and answers\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNote : Please prepare all the questions. The interviewer may choose to ask any questions. \u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Ci\u003E\u003Cspan style=\"color: #990000;\"\u003EBeginner Level (0-1 yr): Java Collections Interview Questions \u0026nbsp;and Answers\u003C\/span\u003E\u003C\/i\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ1 \u0026nbsp;What is Collection? What is a Collections Framework? What are the benefits of the Java Collections Framework?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ECollection :\u003C\/b\u003E A collection (also called a container) is an object that groups multiple elements into a single unit.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ECollections Framework : \u003C\/b\u003ECollections framework provides a unified architecture for manipulating and representing collections.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EBenefits of Collections Framework :\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n1. Improves program quality and speed\u003Cbr \/\u003E\n2. Increases the chances of reusability of software\u003Cbr \/\u003E\n3. Decreases programming effort.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ2 What is the root interface in the collection hierarchy? \u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe root interface in the collection hierarchy is the\u003Cb\u003E Collection interface. \u003C\/b\u003EFew interviewers may argue that \u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003Ethe Collection interface extends the\u003Cb\u003E Iterable interface\u003C\/b\u003E. So iterable should be the root interface. But you should reply iterable interface present in java.lang package not in java.util package. It is clearly mentioned in \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Collection.html\" rel=\"nofollow\" target=\"_blank\"\u003EOracle Collection docs\u003C\/a\u003E, that Collection interface is a member of the Java Collections framework. \u0026nbsp;For the \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/Iterable.html\" rel=\"nofollow\" target=\"_blank\"\u003EIterable interface Oracle doc,\u003C\/a\u003E the iterable interface is not mentioned as a part of the Java Collections framework. So if the question includes collection hierarchy, then you should answer the question as Collection interface (which is found in java.util package).\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ3 What is the difference between Collection and Collections?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe Collection is an interface while Collections is a java class, both are present in java.util package and part of the java collections framework. (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/03\/difference-between-collection-and-collections-in-java.html\" target=\"_blank\"\u003Eanswer\u003C\/a\u003E)\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ4 Which collection classes are synchronized or thread-safe?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nStack, Properties, Vector, and Hashtable can be used in a multi-threaded environment because they are synchronized classes (or thread-safe).\u0026nbsp;\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ5 Name the core Collection interfaces?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-ifC30f-ZZ7M\/VU7wWRwqo3I\/AAAAAAAAAmQ\/b1CIhOP5NU8\/s1600\/Java_collection_framework.jpg\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg alt=\"Java Collection Interfaces or Hierarchy\" border=\"0\" height=\"326\" src=\"https:\/\/1.bp.blogspot.com\/-ifC30f-ZZ7M\/VU7wWRwqo3I\/AAAAAAAAAmQ\/b1CIhOP5NU8\/s640\/Java_collection_framework.jpg\" title=\"Java Collection Interfaces or Hierarchy\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\u0026nbsp;\u003Cspan style=\"font-size: xx-small;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cspan style=\"font-size: xx-small;\"\u003Esource of image : \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: xx-small;\"\u003EBy Ervinn at en.wikibooks [CC BY-SA 3.0 ], from Wikimedia Commons\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThe list of core collection interfaces are : just mention the important ones\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nImportant : Collection , Set , Queue , List , Map\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nOther interfaces also on the list : \u0026nbsp;SortedSet, SortedMap, Deque, ListIterator, etc.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ6 What is the difference between List and Set?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSet contains only unique elements while List can contain duplicate elements.\u003Cbr \/\u003E\nSet is unordered while the List is ordered. List maintains the order in which the objects are added.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ7 What is the difference between Map and Set?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMap object has unique keys each containing some value, while Set contains only unique values.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ8 What are the classes implementing List and Set interface?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003EClass implementing List interface :\u003C\/b\u003E\u003C\/i\u003E\u0026nbsp;\u0026nbsp;ArrayList, Vector, LinkedList\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci style=\"font-weight: bold;\"\u003EClass implementing Set interface : \u0026nbsp;\u003C\/i\u003EHashSet, TreeSet\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ9 What is an iterator?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe Iterator is an interface. It is found in java.util package. It provides methods to iterate over any Collection.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ10 What is the difference between Iterator and Enumeration?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe main difference between Iterator and Enumeration is that Iterator has remove() method while Enumeration doesn't.\u003Cbr \/\u003E\nHence, using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration behaves like a read-only interface as it can only traverse the objects and fetch it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ11 Which design pattern followed by Iterator?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt follows the iterator design pattern. An iterator design pattern provides us to navigate through the collection of objects by using a common interface without letting us know about the underlying implementation.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nEnumeration is an example of an Iterator design pattern.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ12 Which methods you need to override to use any object as a key in HashMap?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTo use any object as a key in HashMap, it needs to implement equals() and hashCode() method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ13 \u0026nbsp;What is the difference between Queue and Stack?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe Queue is a data structure that is based on FIFO ( first in first out ) property. An example of a Queue in the real-world is buying movie tickets in the multiplex or cinema theaters.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe Stack is a data structure that is based on LIFO (last in first out) property. An example of Stack in the real-world is the insertion or removal of CD \u0026nbsp;from the CD case.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ14 How to reverse the List in Collections?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere is a built-in reverse method in the Collections class. reverse(List list) accepts the list as a parameter.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ECollections.reverse(listobject);\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ15 How to convert the array of strings into the list?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArrays class of java.util package contains the method asList() which accepts the array as a parameter.\u003Cbr \/\u003E\nSo,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EString[] \u0026nbsp;wordArray = \u0026nbsp;{\"Love Yourself\" \u0026nbsp;, \"Alive is Awesome\" , \"Be in present\"};\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003EList wordList = \u0026nbsp;Arrays.asList(wordArray);\u0026nbsp;\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Ci\u003E\u003Cspan style=\"color: #990000;\"\u003EIntermediate Level (1-3 yrs): Java Collections Interview Questions \u0026nbsp;and Answers\u003C\/span\u003E\u003C\/i\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ16 What is the difference between ArrayList and Vector?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\nIt is one of the frequently asked collection interview questions, the main differences are\u003Cbr \/\u003E\nVector is synchronized while ArrayList is not. Vector is slow while ArrayList is fast. Every time when needed, Vector increases the capacity twice of its initial size while ArrayList increases its Array size by 50%. find detailed explanation \u0026nbsp; \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/12\/difference-between-arraylist-and-vector-in-java-collection-interview-question.html\" target=\"_blank\"\u003EArrayList vs Vector.\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ17 What is the difference between HashMap and Hashtable?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is one of the most popular collections interview questions for java developers. Make sure you go through this once before appearing for the interview.\u003Cbr \/\u003E\nMain differences between HashMap and Hashtable are :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values.\u003Cbr \/\u003E\nb. HashMap is not synchronized or thread-safe while Hashtable is synchronized or thread-safe.\u003Cbr \/\u003E\nfind a detailed explanation here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/03\/hashmap-vs-hashtable-difference-with-example-java-interview-questions.html\" target=\"_blank\"\u003EHashtable vs HashMap in Java\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ18 What is the difference between peek(), poll() and remove() method of the Queue interface?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBoth poll() and remove() method are used to remove the head object of the Queue. The main difference lies when the Queue is empty().\u003Cbr \/\u003E\nIf the Queue is empty then the poll() method will return null. While in similar case , remove() method will throw NoSuchElementException .\u003Cbr \/\u003E\npeek() method retrieves but does not remove the head of the Queue. If the queue is empty then the peek() method also returns null.\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ19 What is the difference between Iterator and ListIterator.\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nUsing Iterator we can traverse the list of objects in the forward direction. But ListIterator can traverse the collection in both directions that are forward as well as backward.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ20 What is the difference between Array and ArrayList in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question checks whether the student understands the concept of the static and dynamic array. Some main differences between Array and ArrayList are :\u003Cbr \/\u003E\na. Array is static in size while ArrayList is dynamic in size.\u003Cbr \/\u003E\nb. Array can contain primitive data types while ArrayList can not contain primitive data types.\u003C\/div\u003E\n\u003Cdiv\u003E\nfind detailed explanation\u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.ca\/2015\/03\/difference-between-array-and-arraylist-in-java-example.html\" target=\"_blank\"\u003EArrayList vs Array in Java\u003C\/a\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ21 What is the difference between HashSet and TreeSet?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMain differences between HashSet and TreeSet are :\u003Cbr \/\u003E\na. \u0026nbsp;HashSet maintains the inserted elements in random order while TreeSet maintains elements in the sorted order\u003Cbr \/\u003E\nb. HashSet can store the null object while TreeSet can not store the null object.\u003Cbr \/\u003E\nfind a detailed explanation here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/03\/difference-between-hashset-and-treeset-similarities-and-example.html\" target=\"_blank\"\u003ETreeSet vs HashSet in Java\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ22 Write java code showing insertion, deletion, and retrieval of HashMap object?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDo it yourself (DIY), if found any difficulty or doubts then please mention in the comments.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ23 What is the difference between HashMap and ConcurrentHashMap?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is also one of the most popular java collections interview questions. Make sure this question is in your to-do list before appearing for the interview.\u003Cbr \/\u003E\nMain differences between HashMap and ConcurrentHashMap are :\u003Cbr \/\u003E\na. HashMap is not synchronized while ConcurrentHashMap is synchronized.\u003Cbr \/\u003E\nb. HashMap can have one null key and any number of null values while ConcurrentHashMap does not allow null keys and null values.\u003Cbr \/\u003E\nfind a detailed explanation here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/02\/hashmap-vs-concurrenthashmap-java-collections-interview-question.html\" target=\"_blank\"\u003EConcurrentHashMap vs HashMap in Java\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ24 Arrange the following in the ascending order (performance):\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EHashMap, Hashtable, ConcurrentHashMap, and Collections.SynchronizedMap\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHashtable \u0026nbsp;\u0026lt; \u0026nbsp;Collections.SynchronizedMap \u0026nbsp;\u0026lt; \u0026nbsp;ConcurrentHashMap \u0026nbsp;\u0026lt; \u0026nbsp;HashMap\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ25 How HashMap works in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the most important questions for java developers. HashMap works on the principle of Hashing. Find detailed information here to understand \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003Ewhat is hashing and how hashmap works in java.\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ26 What is the difference between LinkedList and ArrayList in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMain differences between LinkedList and ArrayList are :\u003Cbr \/\u003E\na. LinkedList is the doubly linked list implementation of the List interface, while , ArrayList is the resizable array implementation of the List interface.\u003Cbr \/\u003E\nb. LinkedList can be traversed in the reverse direction using the descendingIterator() method provided by the Java API developers, while , we need to implement our own method to traverse ArrayList in the reverse direction. find the detailed explanation here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/04\/difference-between-arraylist-and-linkedlist-in-java-example.html\" target=\"_blank\"\u003EArrayList vs LinkedList in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ27 What are Comparable and Comparator interfaces? List the difference between them?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWe already explained what is comparable and comparator interface in detail along with examples here, \u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/difference-between-comparable-and.html\" target=\"_blank\"\u003EComparable vs Comparator in Java\u003C\/a\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ28 Why Map interface does not extend the Collection interface in Java Collections Framework?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nOne liner answer : \u003Cb\u003EMap interface is not compatible with the Collection interface.\u003C\/b\u003E\u003Cbr \/\u003E\nExplanation : Since Map requires a key as well as value, for example, if we want to add key-value pair then we will use put(Object key, Object value). So there are two parameters required to add an element to the HashMap object. In Collection interface add(Object o) has only one parameter. \u003Cbr \/\u003E\nThe other reasons are Map supports valueSet, keySet as well as other appropriate methods which have just different views from the Collection interface.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ29 When to use ArrayList and LinkedList in the application?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArrayList has a constant time search operation O(1). Hence, ArrayList is preferred when there are more get() or search operation.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nInsertion, Deletion operations take constant time O(1) for LinkedList. Hence, LinkedList is preferred when there are more insertions or deletions involved in the application.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ30 Write the code for iterating the list in different ways in java? \u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThere are two ways to iterate over the list in java :\u003Cbr \/\u003E\na. using Iterator\u003Cbr \/\u003E\nb. using for-each loop\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nCoding part : Do it yourself (DIY), in case of any doubts or difficulty please mention in the comments.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ31 Give a practical example of BlockingQueue?\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\nBlockingQueue can be used in the Producer-Consumer design pattern. You can find a detailed explanation with code \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/07\/producer-consumer-problem.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ32 What is the default capacity of mostly used java collections (like ArrayList and HashMap)?\u003C\/b\u003E\u003C\/span\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMake sure you understand the difference between the terms size and capacity.\u003Cbr \/\u003E\nSize represents the number of elements stored currently. Capacity indicates the maximum number of elements a collection can hold currently. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDefault capacity of ArrayList : 10\u003Cbr \/\u003E\nDefault capacity of HashMap : 16\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Ci\u003E\u003Cspan style=\"color: #990000;\"\u003EAdvance Level (3+ yrs): Java Collections Interview Questions \u0026nbsp;and Answers\u003C\/span\u003E\u003C\/i\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ33 How HashSet works internally in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is one of the popular interview questions. HashSet internally uses HashMap to maintain the uniqueness of elements. We have already discussed in detail \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/how-sets-are-implemented-internally-in.html\" target=\"_blank\"\u003Ehashset internal working in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ34 What is CopyOnWriteArrayList?\u0026nbsp; How it is different from \u0026nbsp;ArrayList in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/concurrent\/CopyOnWriteArrayList.html\" rel=\"nofollow\" target=\"_blank\"\u003ECopyOnWriteArrayList\u003C\/a\u003E\u0026nbsp;is a thread-safe variant of ArrayList in which all mutative operations like add, set are implemented by creating a fresh copy of the underlying array.\u003Cbr \/\u003E\nIt guaranteed not to throw ConcurrentModificationException.\u003Cbr \/\u003E\nIt permits all elements including null. It is introduced in JDK 1.5.\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ35\u0026nbsp; How HashMap works in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWe are repeating this question, as it is one of the most important question for java developer.HashMap works on the principle of Hashing. Please find the detailed answer here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003Ehashmap internal working in java.\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ36 How remove(key) method works in HashMap?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is the new question which is getting popular among java interviewers. We have shared a detailed explanation here about \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/03\/how-remove-method-internally-works-in-hashmap-java.html\" target=\"_blank\"\u003Ehow remove method works internally in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ37 What is BlockingQueue in Java Collections Framework?\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/util\/concurrent\/BlockingQueue.html\" rel=\"nofollow\" target=\"_blank\"\u003EBlockingQueue\u003C\/a\u003E\u0026nbsp;implements the java.util.Queue interface. BlockingQueue supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.\u003Cbr \/\u003E\nIt does not accept null elements.\u003Cbr \/\u003E\nBlocking queues are primarily designed for producer-consumer problems.\u003Cbr \/\u003E\nBlockingQueue implementations are thread-safe and can also be used in inter-thread communications.\u003Cbr \/\u003E\nThis concurrent Collection class was added in JDK 1.5\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ38 How TreeMap works in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTreeMap internally uses a Red-Black tree to sort the elements in a natural order. Please find the detailed answers here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/06\/how-treemap-works-ten-treemap-java-interview-questions.html\" target=\"_blank\"\u003Einternal implementation of TreeMap in java.\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ39 All the questions related to HashSet class can be found here\u003C\/span\u003E\u003C\/b\u003E \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/04\/top-10-hashset-java-interview-questions-collection.html\" target=\"_blank\"\u003Efrequently asked HashSet interview questions\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ40 What is the difference between Fail- fast iterator and Fail-safe iterator? \u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThis is one of the most popular interview questions for the higher experienced java developers.\u003Cbr \/\u003E\nMain differences between Fail-fast and Fail-safe iterators are :\u003Cbr \/\u003E\na. Fail-fast throws ConcurrentModificationException while Fail-safe does not.\u003Cbr \/\u003E\nb. Fail-fast does not clone the original collection list of objects while Fail-safe creates a copy of the original collection list of objects.\u003Cbr \/\u003E\nThe difference is explained in detail here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/04\/fail-fast-iterator-vs-fail-safe-iterator-difference-with-example-in-java.html\" target=\"_blank\"\u003Efail-safe vs fail-fast iterator in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ41 How ConcurrentHashMap works internally in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe detailed answer is already explained here \u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/02\/how-concurrenthashmap-works-in-java-internal-implementation.html\" target=\"_blank\"\u003Einternal implementation of ConcurrentHashMap\u0026nbsp;\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ42 How do you use a custom object as a key in Collection classes like HashMap?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf one is using the custom object as a key then one needs to override equals() and hashCode() method\u003Cbr \/\u003E\nand one also needs to fulfill the contract.\u003Cbr \/\u003E\nIf you want to store the custom object in the SortedCollections like SortedMap then one needs to make sure that equals() method is consistent with the compareTo() method. If inconsistent, then the collection will not follow their contracts, that is, Sets may allow duplicate elements.\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ43 What is hash-collision in Hashtable? How it was handled in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn Hashtable, if two different keys have the same hash value then it leads to hash -collision. A bucket of type linkedlist used to hold the different keys of same hash value.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ44 Explain the importance of hashCode() and equals() method ? Explain the contract also?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nHashMap object uses a Key object hashCode() method and equals() method to find out the index to put the key-value pair. If we want to get value from the HashMap same both methods are used. Somehow, if both methods are not implemented correctly, it will result in two keys producing the same hashCode() and equals() output. The problem will arise that HashMap will treat both outputs the same instead of different and overwrite the most recent key-value pair with the previous key-value pair.\u003Cbr \/\u003E\nSimilarly, all the collection classes that do not allow the duplicate values to use hashCode() and equals() method to find the duplicate elements. So it is very important to implement them correctly.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EContract of hashCode() and equals() method\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cspan style=\"color: #38761d;\"\u003Ea.\u003C\/span\u003E\u003Cb style=\"color: #38761d;\"\u003E\u0026nbsp; \u003C\/b\u003EIf \u0026nbsp;object1.equals(object2) , then\u003Cspan style=\"color: #38761d;\"\u003E \u0026nbsp;\u003C\/span\u003Eobject1.hashCode() == object2.hashCode() should always be true.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003Eb. \u003C\/span\u003EIf object1.hashCode() == object2.hashCode() is true does not guarantee object1.equals(object2)\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ45 What is EnumSet in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/EnumSet.html\" rel=\"nofollow\" target=\"_blank\"\u003EEnumSet is\u003C\/a\u003E a specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified explicitly or implicitly when the set is created.\u003Cbr \/\u003E\nThe iterator never throws ConcurrentModificationException and is weakly consistent.\u003Cbr \/\u003E\n\u003Cb\u003EAdvantage over HashSet:\u003C\/b\u003E\u003Cbr \/\u003E\nAll basic operations of EnumSet execute in constant time. It is most likely to be much faster than HashSet counterparts.\u003Cbr \/\u003E\nIt is a part of the Java Collections Framework since JDK 1.5.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ46 What are concurrentCollectionClasses?\u003C\/span\u003E\u003C\/b\u003E \u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nIn jdk1.5, Java Api developers had introduced a new package called java.util.concurrent that has thread-safe collection classes as they allow collections to be modified while iterating. The iterator is fail-safe that is it will not\u0026nbsp;throw ConcurrentModificationException.\u003Cbr \/\u003E\nSome examples of concurrentCollectionClasses are :\u003Cbr \/\u003E\na. CopyOnWriteArrayList\u003Cbr \/\u003E\nb. ConcurrentHashMap\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ47 How do you convert a given Collection to SynchronizedCollection?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nOne line code : Collections.synchronizedCollection(Collection collectionObj) will convert a given collection to synchronized collection.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ48\u0026nbsp; What is IdentityHashMap?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003EIdentityHashMap\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/IdentityHashMap.html\" rel=\"nofollow\" target=\"_blank\"\u003EI\u003C\/a\u003E\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/IdentityHashMap.html\" rel=\"nofollow\" target=\"_blank\"\u003EdentityHashMap\u003C\/a\u003E\u0026nbsp;is a class present in java.util package. It implements the Map interface with a hash table, using \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-equals-and-double-equals-method-with-example-java-collections-interview-question.html\" target=\"_blank\"\u003Ereference equality instead of object equality\u003C\/a\u003E\u0026nbsp;when comparing keys and values. In other words, in IdentityHashMap two keys, k1 and k2 are considered equal if only if (k1==k2).\u003Cbr \/\u003E\nIdentityHashMap is not synchronized.\u003Cbr \/\u003E\nIterators returned by the iterator() method are fail-fast, hence, they will throw ConcurrentModificationException. \u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ49 \u003C\/span\u003E\u003C\/b\u003E\u003Cb style=\"color: #38761d;\"\u003EWhat is WeakHashMap? \u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003EWeakHashMap :\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/WeakHashMap.html\" rel=\"nofollow\" target=\"_blank\"\u003EWeakHashMap\u003C\/a\u003E\u0026nbsp;is a class present in java.util package similar to IdentityHashMap. It is a Hashtable based implementation of Map interface with weak keys. An entry in WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector.\u003Cbr \/\u003E\nIt permits null keys and null values.\u003Cbr \/\u003E\nLike most collection classes this class is not synchronized. A synchronized WeakHashMap may be constructed using the Collections.synchronizedMap() method.\u003Cbr \/\u003E\nIterators returned by the iterator() method are fail-fast, hence, they will throw ConcurrentModificationException. \u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ50 How will you make Collections readOnly?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\nWe can make the Collection readOnly by using the following lines code:\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\nGeneral : Collections.unmodifiableCollection(Collection c)\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nCollections.unmodifiableMap(Map m)\u003C\/div\u003E\n\u003Cdiv\u003E\nCollections.unmodifiableList(List l)\u003C\/div\u003E\n\u003Cdiv\u003E\nCollections.unmodifiableSet(Set s)\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ51 What is UnsupportedOperationException?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThis exception is thrown to indicate that the requested operation is not supported.\u003Cbr \/\u003E\nExample of UnsupportedOperationException:\u003Cbr \/\u003E\nIn other words, if you call add() or remove() method on the readOnly collection. We know readOnly collection can not be modified. Hence, UnsupportedOperationException will be thrown.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ52 Suppose there is an Employee class. We add Employee class objects to the ArrayList. Mention the steps that need to be taken if I want to sort the objects in ArrayList using the employeeId attribute present in Employee class.\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cspan style=\"color: #38761d;\"\u003Ea.\u003C\/span\u003E Implement the Comparable interface for the Employee class and now to compare the objects by employeeId we will override the emp1.compareTo(emp2)\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003Eb.\u003C\/span\u003E\u0026nbsp;We will now call Collections class sort method and pass the list as an argument, that is,\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp;Collections.sort(empList) \u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nIf you want to add more java collections interview questions and answers or in case you have any doubts related to the Java Collections framework, then please mention in the comments.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ53 What are common algorithms used in the Collections Framework?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cspan style=\"color: black;\"\u003ECommon algorithms used for searching and sorting. For example, a red-black algorithm is used in the sorting of elements in TreeMap. Most of the algorithms are used for List interface but few of them are applicable for all kinds of Collection.\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ54 How to sort ArrayList in descending order?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cspan style=\"color: black;\"\u003EOne liner will be\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cspan style=\"color: black;\"\u003ECollections.sort(arraylist, Collections.reverseOrder()); \u003C\/span\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cspan style=\"color: black;\"\u003EPlease find the code here : \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/11\/how-to-sort-arraylist-in-descending-order-in-java.html\" target=\"_blank\"\u003EHow to sort ArrayList in descending order.\u003C\/a\u003E\u0026nbsp;\u003C\/span\u003E\u003Cb\u003E \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E \u003C\/div\u003E\n"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/7380600333419729622"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/7380600333419729622"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2015\/05\/50-java-collections-interview-questions-and-answers.html","title":"[2023 Updated] Top 50 Java Collections Interview Questions and Answers "}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-ifC30f-ZZ7M\/VU7wWRwqo3I\/AAAAAAAAAmQ\/b1CIhOP5NU8\/s72-c\/Java_collection_framework.jpg","height":"72","width":"72"}},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-6010859728580683774"},"published":{"$t":"2019-05-13T14:01:00.003-04:00"},"updated":{"$t":"2021-04-28T11:18:28.168-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"design-pattern"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 15 Java Design Pattern Interview Questions and Answers"},"content":{"type":"html","$t":"Design Patterns has been a hot topic when it comes to Java interviews. It is one of the most popular sections amongst the interviewers from which they test the interviewee’s awareness on how to solve a commonly occurring software problem. We are going to take a deep dive into the popular \u003Cb\u003Edesign pattern interview questions\u003C\/b\u003E that one can come across in the Java interviews.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003ERead Also :\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp; \u003C\/i\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/singleton-design-pattern-use-in-java.html\" target=\"_blank\"\u003ESingleton Design Pattern in Java\u003C\/a\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ1 What are Java Design Patterns?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nJava Design patterns are the best practices developed and evolved over a period of time by seasoned software developers to resolve any commonly encountered software development\/design problem. They are not mandatory to implement but using them wisely can do wonders as far as software development is considered.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ2 What are the different categories of Design Patterns?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDesign patterns can be categorized into 3 types. These are given below:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1. Creational Patterns\u0026nbsp;-\u003C\/b\u003E These are concerned with the way an object is created or instantiation of a class. They give freedom to choose the right object creation strategy in a particular situation. They can be further classified into object creational and class creational patterns.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. Structural Patterns\u0026nbsp;-\u003C\/b\u003E These design patterns are concerned with class and object composition in order to create larger structures. They work by identifying and simplifying the relationships between the entities.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Behavioral Patterns\u0026nbsp;-\u003C\/b\u003E These design patterns are more concerned with identifying the way in which objects communicate with each other.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003EQ3 What is Singleton Design Pattern?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSingleton design pattern is a sub type of creational pattern. It ensures that at any given instance, one and only one object of the class exists in JVM and provides a way to access to this object. These are widely implemented in applications while establishing database connection and configuring logging functionality.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ4 How to implement a Singleton Java class?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nGiven below is the most common way to construct a Singleton class. It works well in single threaded environment but has some drawback in multithreaded environment which will be explained as part of next question.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003ESingleton\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n \u003Cspan style=\"color: #888888;\"\u003E    \/\/reference variable\u003C\/span\u003E\n \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E    private\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E Singleton instance\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E     \/* prevent instantiation from outside \u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E        of class by making constructor private.*\/\u003C\/span\u003E\n     \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eprivate\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003ESingleton\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){}\u003C\/span\u003E\n\n \u003Cspan style=\"color: #888888;\"\u003E    \/* if object exists, return the existing \u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E        object. If not, create and return a\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #888888;\"\u003E   \u0026nbsp;    new object.*\/\u003C\/span\u003E\n \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E    public\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E Singleton \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EgetInstance\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E()\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E \n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Einstance \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enull\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n         instance \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E Singleton\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E \n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n  \n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E instance\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E \n     \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E    public\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EdisplayMessage\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Java Hungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n \u003Cspan style=\"color: #333333;\"\u003E    }\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ5 What happens if the method returning instance of Singleton class is not synchronized in multi-threaded environment?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn case of multi-threaded environment, the non-synchronized accessor method of Singleton class may create more than one instance of the class. This can happen when two threads enter the condition to check if the instance already exists at the same time.\u003Cbr \/\u003E\nSince, both the threads will find that the instance does not exist, they will get a new object created and returned and thus, we will have more than one instance of the class available.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ6 What are the different ways to create Singleton design pattern in Java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSingleton pattern object can be created in following ways:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EEager Loading\u003C\/b\u003E\u0026nbsp;–\n In Eager loading, the object of singleton class gets created when the \nclass is loaded into the memory. This method of implementation should be\n avoided as it creates the object even when it is not requested or used.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ELazy Loading\u003C\/b\u003E\u0026nbsp;–\n In Lazy loading, the object of singleton class is created when it is \nactually requested by the client. In this method, the object is created \nwithin the singleton instance accessor method. This is the desired way \nto create the singleton object as it will create the instance only when \nit is required by the client.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ7 How can you create a thread safe Singleton class in Java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThread safe singleton class can be created in three ways. They are:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EDouble check locking method:\u003C\/b\u003E Allows lazy initialization. Checks the existence of the instance before acquiring lock.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: 0.1em 0.1em 0.1em 0.1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EDoubleCheckSingleton\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #888888;\"\u003E\/\/reference variable\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eprivate\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E DoubleCheckSingleton instance\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #888888;\"\u003E\/*prevent instantiation from outside of\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #888888;\"\u003E      \u0026nbsp;class by making constructor private*\/\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eprivate\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EDoubleCheckSingleton\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){}\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #888888;\"\u003E\/\/instance accessor method\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E DoubleCheckSingleton \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EgetInstance\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){\u003C\/span\u003E\n\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Einstance \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enull\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E){\u003C\/span\u003E\n\n                \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Esynchronized\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EDoubleCheckSingleton\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eclass\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n                      \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Einstance \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enull\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E){\u003C\/span\u003E\n\n                            instance \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E DoubleCheckSingleton\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n\n                      \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n                \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n            \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E instance\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n     \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #888888;\"\u003E\/\/method with some logic\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EdisplayMessage\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){\u003C\/span\u003E\n\n          System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Java Hungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\n     \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EEnum singleton implementation:\u003C\/b\u003E Safest and shortest way to create singleton but has drawback of eager initialization.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: 0.1em 0.1em 0.1em 0.1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eenum\u003C\/span\u003E EnumSingleton \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n      INSTANCE\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E      \/\/method with some logic\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E      public\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EdisplayMessage\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){\u003C\/span\u003E\n           System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Java Hungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E      }\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ESynchronized instance accessor method:\u003C\/b\u003E Allow lazy initialization. Reduced performance as lock is acquired every time whenever the instance is required.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003ESynchronizedSingleton\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E       \/\/reference variable\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E       private\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E SynchronizedSingleton instance\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E       \/*prevent instantiation from outside of class\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #888888;\"\u003E        \u0026nbsp;by making\u003C\/span\u003E\u003Cspan style=\"color: #888888;\"\u003E \u003C\/span\u003E\u003Cspan style=\"color: #888888;\"\u003Econstructor private *\/\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E       private\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003ESynchronizedSingleton\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){}\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E       \/\/synchronized instance accessor method\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E       public\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Esynchronized\u003C\/span\u003E SynchronizedSingleton\u003Cspan style=\"color: #0066bb;\"\u003E \u003C\/span\u003E\u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EgetInstance\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){\u003C\/span\u003E\n\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E              if\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Einstance \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enull\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E){\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E                    if\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Einstance \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enull\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E){\u003C\/span\u003E\n\n                        instance \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E SynchronizedSingleton\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E                    }\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E              }\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E              return\u003C\/span\u003E instance\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E       }\u003C\/span\u003E\n\n\u003Cspan style=\"color: #888888;\"\u003E       \/\/method with some logic\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E       public\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EdisplayMessage\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(){\u003C\/span\u003E\n\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Java Hungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333333;\"\u003E       }\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ8 Is it possible to create a clone of a Singleton object?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, it is possible to a create clone of singleton object if the class implements Cloneable interface.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ9 How can the cloning of Single object be prevented?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt can be prevented by overriding clone() method and throwing an exception within it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ10 What is Factory Design Pattern?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is a sub type of creational design pattern which provides a specification for instantiation of a class by using an interface or abstract class. The actual logic of instantiation is deferred to the subclasses based on the type of object needed. The client can get the desired object created without knowing the actual logic.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ11 What is Abstract Factory Design Pattern?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is also a sub type of creational design pattern and is used where we require one more layer of abstraction up and above a family of factory pattern objects. This is the reason that it is also referred as factory of factories. The abstract factory pattern creates factories which in turn create the objects by further deferring the object creation logic to their sub classes.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ12 What is the difference between Factory and Abstract Factory Design Pattern?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe factory pattern uses inheritance whereas the abstract factory pattern uses composition as mechanism for object creation.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe factory pattern creates single object whereas abstract factory pattern creates a group of objects.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ13 What is Builder Design Pattern?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is a sub type of creation design pattern which is used to create complex object using simple objects. It is an alternative to Factory as well as Abstract Factory design pattern and is a better option to go with when lots of attributes are associated with the object.\u003Cbr \/\u003E\nIt works by assembling the complex object step by step and providing a method to access the same.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ14 Which design pattern is preferred for creating a complex object?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBuilder design pattern is more suitable for creating complex object as it is an extension of Factory pattern and is created for solving the issues associated with Factory and Abstract Factory design patterns.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ15 What is Prototype Design Pattern and when to use it?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIt is a creational design pattern which is used in cases when a large number of instances of a class are required and these instances are almost identical but may have slightly different properties. This pattern is useful in cases wherever there is high time and cost overhead associated with the object creation. This pattern mainly works by creating clone of the existing object instead of creating a new instance every time so that performance is optimized.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for the java design pattern interview questions. If you like this article then please share it with your friends. "},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/6010859728580683774"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/6010859728580683774"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2019\/05\/design-pattern-interview-questions-in-java.html","title":"Top 15 Java Design Pattern Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-4209306874260555494"},"published":{"$t":"2019-04-13T14:00:00.002-04:00"},"updated":{"$t":"2021-04-28T11:21:04.732-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"HashMap"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 15 Frequently Asked HashMap Interview Questions in Java"},"content":{"type":"html","$t":"HashMap is one of the tricky java class in java collections framework. Unlike other java classes its object contains both key and value pair. This is one of the most frequently used java class along side ArrayList. In this tutorial I will be sharing frequently asked HashMap interview questions with answers.\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ1 How HashMap internally works in java 8? (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003Esolution\u003C\/a\u003E)\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question is the most popular interview question for java developers. The get() method of HashMap works on the principle of Hashing. TreeNode concept is introduced in java 8 to store key-value pairs.\u0026nbsp;You can find the detailed explanation \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ2 What is the difference between HashMap and Hashtable in java ? (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/03\/hashmap-vs-hashtable-difference-with-example-java-interview-questions.html\" target=\"_blank\"\u003Esolution\u003C\/a\u003E)\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. The main difference between Hashtable and HashMap is that HashMap can contain one null key and any number of null values but Hashtable does not allow null values.\u003Cbr \/\u003E\n2. Hashtable is synchronized while HashMap is not synchronized.\u003Cbr \/\u003E\n3. HashMap is faster as compared to Hashtable because HashMap is not synchronized.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ3 What is the difference between HashMap and ConcurrentHashMap in java ? (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/02\/hashmap-vs-concurrenthashmap-java-collections-interview-question.html\" target=\"_blank\"\u003Esolution\u003C\/a\u003E)\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. ConcurrentHashMap is thread-safe that is at a time only one thread can access the code whereas HashMap is not thread-safe.\u003Cbr \/\u003E\n2. ConcurrentHashMap does not allow keys to contain null values whereas HashMap can contain one null key.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ4 What is the threshold value after which bucket converted from linked list to Tree?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003EIf for a given bucket, if there are more than 8 Nodes then the linked list is converted into a Red Black tree. This is represented in the HashMap class code as follows :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efinal\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E TREEIFY_THRESHOLD \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E8\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #333333;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #333333;\"\u003E\u0026nbsp; \u003C\/span\u003E\u003C\/pre\u003E\nBelow image displays both tree (at bucket 0), and linked lists (at bucket 1, 2, 3) . Bucket 0 is a Tree because it contains at least 8 nodes (containing Node A, Node B,\u0026nbsp; Node C, Node D, Node E, Node F , Node K , Node Q, Node O, Node M, Node P) \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/4.bp.blogspot.com\/-btSv072MD00\/XEX6bB3VNMI\/AAAAAAAAA74\/WyIfFj7nT6sgJNIAIcpIWC8r4jIJwx8gQCPcBGAYYCw\/s1600\/Tree%2Band%2BLinkedList%2Btogether%2Bin%2Ba%2BHashMap%2Bbucket.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg alt=\"Hashmap in java8\" border=\"0\" data-original-height=\"354\" data-original-width=\"571\" height=\"247\" src=\"https:\/\/4.bp.blogspot.com\/-btSv072MD00\/XEX6bB3VNMI\/AAAAAAAAA74\/WyIfFj7nT6sgJNIAIcpIWC8r4jIJwx8gQCPcBGAYYCw\/s400\/Tree%2Band%2BLinkedList%2Btogether%2Bin%2Ba%2BHashMap%2Bbucket.png\" title=\"Hashmap in java8\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ5 What is the threshold value after which Tree is converted back to linked list?\u003C\/b\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf for a given bucket, if there are less than 6 Nodes then the Tree is converted back to the linked list. This is represented in the HashMap class code as follows :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efinal\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E UNTREEIFY_THRESHOLD \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E6\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ6 What is the time complexity of basic operations get() and put() in HashMap class?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/HashMap.html\" target=\"_blank\"\u003EJava docs\u003C\/a\u003E,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe HashMap provides the constant time performance O(1)\u0026nbsp; for the basic operations get and put , assuming that the hash function disperses the elements properly among the buckets. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ7 What will happen if you try to store duplicate key in the HashMap?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf you try to store a key which is already present in the HashMap then it will override the old value with the new value. It will not throw error or exception. The size of the HashMap does not change. This is one of the reason that when you call keySet() method on HashMap to get all keys it will return Set instead of Collection because Set does not allow duplicates.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ8 What will happen if you store duplicate value in the HashMap? \u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, HashMap can store duplicate values in java. This is the reason when you call values() method of HashMap to get all values it will return a Collection not a Set. It also does not return List because HashMap does not maintain order guarantee for key and value.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ9 In HashMap, what is the requirement for an object to be used as key?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAny class(for e.g String) can serve as a key if and only if it overrides the hashCode() and equals() method. hashCode() method is used when you insert a key into the HashMap while equals() method is used to retrieve the value from HashMap.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ10 What will happen if you try to store null key in HashMap?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nOnly one null key is allowed in HashMap. If key of the HashMap is null then it will always be present in index 0. NullPointerException is thrown if you try to call hashCode() method on null key.\u003Cbr \/\u003E\nAs a result, when a get() method is called then value of the first index is returned.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ11 What is the difference between Collections.synchroizedHashMap(HashMap)\u0026nbsp; and \u003C\/b\u003E\u003Cb\u003E\u003Cb\u003EHashMap\u003C\/b\u003E?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp;The main difference between Collections.synchronizedHashMap(HashMap) and HashMap is that HashMap is non-synchronized while Collections.synchronizedHashMap(HashMap) is a wrapped instance of HashMap which has all put and get methods synchronized. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ12 What are the different ways to traverse HashMap in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere are various ways by which we can iterate HashMap in java :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. Using keySet() and iterator()\u003Cbr \/\u003E\n2. Using entrySet() and enhanced for loop\u003Cbr \/\u003E\n3. Using entrySet() and iterator()\u003Cbr \/\u003E\n4. Using keySet() and get() method\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ13\u0026nbsp; What is the difference between capacity and size of HashMap in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe capacity indicates the number of entries object a HashMap can store whereas size() denotes how many key-value pairs is currently present in the HashMap.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ14 What is the difference between HashMap and ArrayList ? (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/10\/difference-between-hashmap-and-arraylist-java-example.html\" target=\"_blank\"\u003Esolution\u003C\/a\u003E)\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. HashMap implements Map interface while ArrayList class implements List interface.\u003Cbr \/\u003E\n2. HashMap does not provide ordering guarantee where as ArrayList maintains the order of the object in which they are inserted.\u003Cbr \/\u003E\n3. ArrayList allows duplicate objects whereas HashMap does not allow duplicate keys.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ15\u0026nbsp; How will you measure the performance of HashMap?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/HashMap.html\" target=\"_blank\"\u003EJava docs\u003C\/a\u003E,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAn instance of HashMap has two parameters that affects its performance\u003Cbr \/\u003E\n1. load factor\u003Cbr \/\u003E\n2. capacity\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Eload factor : \u003C\/b\u003E\u003Cspan style=\"line-height: 18px;\"\u003EThe\u0026nbsp;\u003Cb\u003Eload factor\u003C\/b\u003E\u0026nbsp;is\n a measure of how full the hash table is allowed to get before its \ncapacity is automatically increased. When the number of entries in the \nhash table exceeds the product of the load factor and the current \ncapacity, the hash table is rehashed (that is, internal data structures \nare rebuilt) so that the hash table has approximately twice the number \nof buckets.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cb\u003Ecapacity : \u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"line-height: 18px;\"\u003EThe\u0026nbsp;\u003Cb\u003Ecapacity\u003C\/b\u003E\u0026nbsp;is\n the number of buckets in the hash table( HashMap class is roughly \nequivalent to Hashtable, except that it is unsynchronized and permits \nnulls.), and the initial capacity is simply the capacity at the time the\n hash table is created.\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cb\u003EQ16\u0026nbsp; What is the difference between HashMap and TreeMap in java ? (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/11\/difference-between-hashmap-and-treemap-with-example.html\" target=\"_blank\"\u003Esolution\u003C\/a\u003E)\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E1. HashMap does not maintain any ordering guarantee. TreeMap elements are sorted according to the natural ordering of the elements.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E2. HashMap implementation internally uses hashing. TreeMap internally uses Red-black tree implementation.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E3. HashMap can contain \u003C\/span\u003E\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cspan style=\"line-height: 18px;\"\u003Eany number of null values\u003C\/span\u003E and \u003C\/span\u003E\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cspan style=\"line-height: 18px;\"\u003Eone null key.\u003C\/span\u003E TreeMap can not contain null keys but may contain null values.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cb\u003E\u003Cspan style=\"line-height: 18px;\"\u003EQ17 What is the difference between clear() and remove() method of HashMap?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003Eclear() method removes all the entries object from the HashMap and returns void.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003Eremove() method removes the mapping for the key passed as a parameter.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"line-height: 18px;\"\u003EQ18 \u003C\/span\u003E\u003C\/b\u003E\u003Cspan style=\"line-height: 18px;\"\u003E\u003Cb\u003EHow HashMap remove() method internally works in java ? (\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/03\/how-remove-method-internally-works-in-hashmap-java.html\" target=\"_blank\"\u003Esolution\u003C\/a\u003E)\u003C\/b\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/03\/how-remove-method-internally-works-in-hashmap-java.html\" target=\"_blank\"\u003E \u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's all for this post \u003Cb\u003EHashMap interview questions and answers\u003C\/b\u003E . If you liked this article then please share it with your friends. If you have any questions or doubts then feel free to mention in the comments."},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4209306874260555494"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/4209306874260555494"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2019\/04\/hashmap-interview-questions-and-answers.html","title":"Top 15 Frequently Asked HashMap Interview Questions in Java"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/4.bp.blogspot.com\/-btSv072MD00\/XEX6bB3VNMI\/AAAAAAAAA74\/WyIfFj7nT6sgJNIAIcpIWC8r4jIJwx8gQCPcBGAYYCw\/s72-c\/Tree%2Band%2BLinkedList%2Btogether%2Bin%2Ba%2BHashMap%2Bbucket.png","height":"72","width":"72"}},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-334766977692198757"},"published":{"$t":"2019-03-23T15:33:00.001-04:00"},"updated":{"$t":"2021-04-28T11:25:55.665-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"array"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"Top 50 Java Array Interview Questions and Answers "},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\nArray is one of the topic you just can't miss for interview preparation. In this post I will be sharing 50 most frequently asked java Array interview questions and answers.\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ1 What is an Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Col\u003E\n\u003Cli\u003EArray is a collection of similar data types. It can not have different data type. It can hold both primitive types (int, float, double) and object references.\u003C\/li\u003E\n\u003Cli\u003EIt is fixed in length i.e static in nature.\u0026nbsp;\u003C\/li\u003E\n\u003Cli\u003EArrays are created on the heap memory not on the stack.\u003C\/li\u003E\n\u003Cli\u003EAccessing an invalid index of an Array will cause exception. \u003C\/li\u003E\n\u003C\/ol\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ2 How do you declare an Array in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can declare an Array in java by the following way :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\ndataType[] \u0026nbsp;\u0026nbsp; arrayVariableName\u0026nbsp; = new dataType[arraySize];\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nfor example for int data type, you can declare an int array as :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nint[]\u0026nbsp; temp = new int[256]\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ3 What is the default value of Array for different data types?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EData Type\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Default value \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\nbyte, short, int, long \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 0\u003Cbr \/\u003E\nfloat, double\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 0.0\u003Cbr \/\u003E\nboolean\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; false\u003Cbr \/\u003E\nAny object\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; null \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ4 Can you change size of Array in java after creation?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can not change the size of Array after creation. Although there are other data-structures which can change size after creation.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/2.bp.blogspot.com\/-LrPFeShml8c\/XJZJmwIFQ2I\/AAAAAAAAA_w\/X0HecDBL6ngLkD3kZGoarBqJks_EQDz-ACLcBGAs\/s1600\/Arrays%2Bin%2Bjava.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg alt=\"Java Array Interview Questions and Answers\" border=\"0\" data-original-height=\"170\" data-original-width=\"357\" height=\"190\" src=\"https:\/\/2.bp.blogspot.com\/-LrPFeShml8c\/XJZJmwIFQ2I\/AAAAAAAAA_w\/X0HecDBL6ngLkD3kZGoarBqJks_EQDz-ACLcBGAs\/s400\/Arrays%2Bin%2Bjava.png\" title=\"Java Array Interview Questions and Answers\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ5 Can you pass the negative number in Array size?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, you can not pass the negative number as Array size. If you pass a negative number in Array size then you will not get the compiler error. Instead, you will get the NegativeArraySizeException at run time.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ6 Can you declare an Array without Array size?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, you can not declare Array without Array size. You will get compile time error.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ7 Where does Array stored in JVM memory ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp;Array is an object in java. So, Array is stored in heap memory in JVM.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ8 Given a primitive Array in java, where does in JVM it is stored?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is a follow-up question of Q7. An Array will always be an object on heap memory, even if the Array is declared to hold primitive elements.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ9 What is ArrayStoreException ? When this exception is thrown ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAccording to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/ArrayStoreException.html\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E,\u003Cbr \/\u003E\nArrayStoreException is a runtime exception. Array must contain the same data type elements.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis exception is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. In other words, if you want to store the integer Object in an Array of String you will get ArrayStoreException.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe following code throws ArrayStoreException :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n       Object x\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n         x\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E Integer\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ10 What is the difference between ArrayStoreException and ArrayOutOfBoundsException ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArrayStoreException is thrown if you are trying to add incompatible data type. For example, if you try to add an integer object to String Array, then ArrayStoreException is thrown.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArrayOutOfBoundsException is thrown when an attempt is made to access the Array with illegal index. For example, illegal index means if the index is either negative or greater than or equal to the size of the Array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ11 What are the advantages of Array ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. We can sort multiple elements of Array at the same time.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. Using index, we can access the element of the Array in O(1) time.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ12 What are the disadvantages of Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. To create an Array, contiguous memory is required. It is possible in JVM that the memory is available to accommodate Array but memory available is not contiguous.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nb. The Array is static data structure. It is of fixed size. We can not increase or decrease the size of the Array after creation.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ13 Can we use Generics with an Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, we can not use generics with an Array.\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cb\u003EQ14 What is an Anonymous Array in Java ? Give example?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAn array without any name (or reference) is called an Anonymous Array. They are useful for the scenarios where we need one time usage of Array. For example,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAnonymous int array :\u0026nbsp; \u003Cbr \/\u003E\nnew int[] {2,3,4,5,6,7};\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAnonymous String array :\u003Cbr \/\u003E\nnew String[]{\"Java\", \"Hungry\"};\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ15 Write a program to print elements of Array ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003C!--HTML generated using hilite.me--\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: 0.1em 0.1em 0.1em 0.1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  rollNumber \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E23\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E17\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E20\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E29\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E30\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E temp \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E rollNumber\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Etemp\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\" \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E23 17 20 29 30\u0026nbsp; \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ16 Write a program to sort an Array in Java ?\u003C\/b\u003E\n\u003Cbr \/\u003E\nYou do not need to write quick sort or merge sort algorithm in order to sort an Array. You can sort an Array by using Arrays.sort() method. Check out the program below :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  rollNumber \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E23\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E17\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E20\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E29\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E30\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n    Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Esort\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003ErollNumber\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E temp \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E rollNumber\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Etemp\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\" \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E17 20 23 29 30\u0026nbsp; \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ17 Write a program to check whether two given Arrays are equal, given both contains same data type and same length ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTo check whether two given arrays are equal or not , we can use Arrays.equals() method. Check the program below :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E4\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr2 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n    System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eequals\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr1 \u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E arr2\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr3 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E4\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n    System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eequals\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr1 \u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E arr3\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003Efalse\u003Cbr \/\u003Etrue \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ18 Which is legal int[] arr or int arr[] ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBoth are legal statements. It is preferred to write int[] arr instead of int arr[].\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003EQ19 Write a program to throw ArrayOutOfBoundsException?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  rollNumber \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E23\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E17\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E20\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E29\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E30\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n    \u003Cspan style=\"color: #888888;\"\u003E\/* Index below is greater than the size \u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E    of the given Array *\/\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E element \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E rollNumber\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E6\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E temp \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E rollNumber\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Etemp\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\" \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003EException in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: 6\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; at JavaHungry.main(JavaHungry.java:8)\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ20 Write a program to throw ArrayStoreException ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n       Object x\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n         x\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E Integer\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003EException in thread \"main\" java.lang.ArrayStoreException: java.lang.Integer\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; at JavaHungry.main(JavaHungry.java:6)\u003Cb\u003E\u003Cbr \/\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ21 What is the difference between Array and ArrayList ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArray is static in size i.e of fixed length. Size can not be changed after declaration. ArrayList is dynamic in nature. If you add elements to an ArrayList, it will automatically increase its size.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArray can contain both primitive and Object data types. ArrayList does not contain primitive data types. It only contains object entries.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find the 8 difference between Array and ArrayList \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/03\/difference-between-array-and-arraylist-in-java-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ22 What is the difference between Array and LinkedList in java ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMemory required for storing the same number of elements in Array is less  as compared to LinkedList. Array only stores the data of the element whereas LinkedList stores data plus the address of the next node.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArray requires contiguous memory allocation where as LinkedList elements are present all over the heap memory. Unlike Array, LinkedList does not have limitation of contiguous memory. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find more differences between Array and LinkedList \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2019\/03\/difference-between-linked-list-and-array-in-java-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ23 How to find the missing number in a given Array from number 1 to 100 ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question is a popular interview question. I have shared the code with explanation \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/12\/find-the-missing-number-in-an-array.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ24 What are jagged arrays in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArrays containing arrays of different length is known as jagged arrays. Multidimensional arrays are also known as jagged arrays. For example,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ25 There are two arrays object one containing 100 elements and another containing 50 elements. Both are of same data type. Can we assign one Array to another Array.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes,\u0026nbsp; an Array of 100 elements can be assigned to an Array of 50 elements in java. The only criteria is that both arrays of same data type. It is because at the time of assigning the values compiler looks for the data type of Array not the size of Array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E50\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr2 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E100\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n    arr1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E arr2\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n    System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr1\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E100 \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ26 What are the different ways to copy one Array from another Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere are four ways by which we can copy an Array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. By using for loop\u003Cbr \/\u003E\nb. By using clone() method\u003Cbr \/\u003E\nc. By using Arrays.copyOf() method\u003Cbr \/\u003E\nd. By using System.arraycopy() method\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ27 Write a program to search a specific element in an Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can use binarySearch(int[], int) method. This method internally uses binary search algorithm.\u003Cbr \/\u003E\nIt is prerequisite for binary search algorithm to have elements sorted. In the given example I have taken an already sorted Array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Sorted Array\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E4\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/* if element present in Array, binarySearch()\u003C\/span\u003E\n\u003Cspan style=\"color: #888888;\"\u003E    method will return index *\/\u003C\/span\u003E\n    System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EbinarySearch\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr1\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E1 \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ28 What will happen if you do not initialize an Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nArray will take default value depending upon the data type.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ29 How to find duplicate elements in a given Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere are many ways by which you can find the duplicates in an Array. I am sharing two ways\u003Cbr \/\u003E\na. using for loop and compare\u003Cbr \/\u003E\nb. using HashSet\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"java\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"javahungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"java\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"javahungry\"\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E i \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003E arr1\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E++){\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E j\u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E j \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003E arr1\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E j\u003Cspan style=\"color: #333333;\"\u003E++)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            \n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr1\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E].\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eequals\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr1\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ej\u003Cspan style=\"color: #333333;\"\u003E])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E\u0026amp;\u0026amp;\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E!=\u003C\/span\u003Ej \u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n                System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Duplicate in Array is : \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E arr1\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ej\u003Cspan style=\"color: #333333;\"\u003E]);\u003C\/span\u003E\n\n            \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n  \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EDuplicate in Array is : java\u003Cbr \/\u003EDuplicate in Array is : javahungry \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTime Complexity O(n^2) i.e quadratic.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHashSet does not allow duplicate elements. You can traverse the array and insert each element into the HashSet. Add elements using add() method. If it returns true then continue to traverse the array. Otherwise if false then print out the duplicate value.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E  arr1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"java\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"javahungry\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"java\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"javahungry\"\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n    HashSet\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E set \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E HashSet\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;();\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString val \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E arr1\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eset\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eadd\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eval\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efalse\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E){\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprint\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eval\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\" \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n  \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003Ejava javahungry\u0026nbsp; \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTime Complexity O(n)\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ30 What are the different ways to traverse an Array in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Using for loop\u003Cbr \/\u003E\nb. Using for each loop\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ31 Is this a legal way to define arrays int[] arr = new int [4]{1, 2, 3, 4};\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is invalid way to initialize an Array in Java. You can not provide the size of the Array when you are declaring the elements in it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ32 What is two dimensional Array in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAn Array of an Array in java is called as two dimensional Array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ33 How do you declare a two dimensional Array in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nint[][] arr = new int[4][4];\u003Cbr \/\u003E\nThe above statement will create a 4 x 4 matrix.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ34 Can we make Array volatile using volatile keyword?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYes, we can make an Array volatile. Only variable representing an Array can be made volatile.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ35 Are Array thread-safe ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nReading an Array is a thread-safe operation but modifications are not.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ36 What is the time complexity O(n) of different operations of an Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Access operation : O(1) This means very fast given the index of the element.\u003Cbr \/\u003E\nb. Search operation : O(n) where n represents the number of elements in an Array.\u003Cbr \/\u003E\nc. Insertion operation : O(n) where n represents the number of elements in an Array.\u003Cbr \/\u003E\nb. Deletion operation : O(n) where n represents the number of elements in an Array.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ37\u0026nbsp; Given two arrays, find the intersection between them? \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/12\/intersection-of-two-arrays-in-java-code-with-example.html\" target=\"_blank\"\u003E(solution)\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIntersection means common elements. We need to find common elements between two given arrays.\u003Cbr \/\u003E\nFor example :\u003Cbr \/\u003E\nint[]\u0026nbsp; arr1 = {1, 2, 3, 4, 5, 6};\u003Cbr \/\u003E\nint[] arr2 = {2, 3, 4, 7, 8};\u003Cbr \/\u003E\nOutput : 2, 3, 4\u003Cbr \/\u003E\nYou can find the code \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/12\/intersection-of-two-arrays-in-java-code-with-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ38 Find the missing number in an Array between 1 to 100. Given only one number is missing. \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2018\/12\/find-the-missing-number-in-an-array.html\" target=\"_blank\"\u003E(solution)\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question is very popular among interviewers. It is one of the simplest question in Array topic.\u003Cbr \/\u003E\nThis question is generally asked during telephonic interview or as a warm up question in face to face round of interviews.\u003Cbr \/\u003E\nOne of the ways to solve this problem is to calculate sum of all numbers from 1 to 100 then subtract it from the sum of all the numbers in given array. Difference would be the missing number.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ39 Find out smallest and largest number in a given Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLogic to find the smallest and largest number in a given Array is given below :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Create two variables for storing largest and smallest number.\u003Cbr \/\u003E\nb. Initialize smallest variable with value Integer.MAX_VALUE\u003Cbr \/\u003E\nc. Initialize largest variable with value Integer.MIN_VALUE\u003Cbr \/\u003E\nd. In each traversal of for loop, we will compare the current element with the largest and smallest number. We will update the value.\u003Cbr \/\u003E\ne. If a number is larger than largest, then it can not be smaller than the smallest. So we can skip if first condition is true.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Given Array\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E inputArr \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E43\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E27\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E75\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E59\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E191\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Setting largest value\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E largest \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E inputArr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Setting smallest value\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E smallest \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E inputArr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Iterate through the Given Array\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E number \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E inputArr \u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Enumber \u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E largest\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            largest \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E number\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eelse\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Eif\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Esmallest \u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E number\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            smallest \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E number\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Largest and Smallest numbers are \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #333333;\"\u003E                        +\u003C\/span\u003E largest \u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\" \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003Esmallest\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003ELargest and Smallest numbers are 191 10 \u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ40 How to reverse an Array in java ? \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/06\/how-to-reverse-array-in-java-with-example.html\" target=\"_blank\"\u003E(solution)\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou will find this type of questions during telephonic round of interview or early rounds of face to face interview. Make sure you go through this question before appearing for the interview.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ41 Write a program to sum all the values of a given Array in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Given Array\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E inputArr \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E43\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E27\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E75\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E59\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E191\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E sum \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/Iterating through the Array\u003C\/span\u003E\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E num \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E inputArr\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        sum \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E sum \u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E num\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n    System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Esum\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E \n    \n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E503\u003Cbr \/\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ42 How to convert HashSet to Array in java ? \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/11\/convert-hashset-to-array-in-java-with-example.html\" target=\"_blank\"\u003E(solution)\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can convert HashSet to Array using toArray() method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ43 How do you separate zeros and non-zeros in a given Array in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLogic to separate zeros and non-zeros is given below :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Initialize variable counter to 0.\u003Cbr \/\u003E\nb. Iterating inputArr from left to right. If inputArr[i] is not zero then assign inputArr[i] to inputArr[counter].\u003Cbr \/\u003E\nc. Increment the counter by 1.\u003Cbr \/\u003E\nd. Assign the remaining elements with 0 value.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/ Given Array\u003C\/span\u003E\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E inputArr \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E43\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E27\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E75\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E59\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E191\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E counter \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #888888;\"\u003E\/\/Iterating through the Array\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003Ei \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003E inputArr\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E++)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EinputArr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E!=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            inputArr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ecounter\u003Cspan style=\"color: #333333;\"\u003E]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E inputArr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n            counter\u003Cspan style=\"color: #333333;\"\u003E++;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ewhile\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ecounter \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003E inputArr\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        inputArr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ecounter\u003Cspan style=\"color: #333333;\"\u003E]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        counter\u003Cspan style=\"color: #333333;\"\u003E++;\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n    System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EtoString\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EinputArr\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E \n\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E[10, 43, 27, 98, 75, 59, 191, 0, 0, 0]\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ44 How to convert Array to ArrayList in java ? \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/11\/how-to-convert-array-to-arraylist-in-java-example.html\" target=\"_blank\"\u003E(solution)\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe easy way to convert Array to ArrayList is using Arrays class asList() method. You need to pass the Array to the asList() method as argument. For example,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E   String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E cityNames \u003Cspan style=\"color: #333333;\"\u003E={\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Boston\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Chicago\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"San Francisco\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"New York\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n   \u003Cspan style=\"color: #888888;\"\u003E\/* One liner - Array to ArrayList conversion*\/\u003C\/span\u003E\n   ArrayList\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E cityList\u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E ArrayList\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E\n\n   \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EArrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EcityNames\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\u0026nbsp; \u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\nThere is another way to convert Array to ArrayList using addAll() method. For details check here.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ45 How to convert Array to TreeSet in java ? \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/11\/how-to-convert-array-to-treeset-in-java.html\" target=\"_blank\"\u003E(solution)\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTo convert Array to TreeSet in java, first we need to convert Array to List using Arrays class asList() method. After converting Array to List,\u0026nbsp; pass the list object to TreeSet constructor. That's it , Array has been converted to TreeSet. You can confirm by printing out the values of TreeSet object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ46 How to convert ArrayList to String Array in java ? \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/11\/how-to-convert-arraylist-to-string-array-java-example.html\" target=\"_blank\"\u003E(solution)\u003C\/a\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere are two ways to convert ArrayList to String Array in java. First method is using ArrayList get() method and second is using toArray() method. You can check both of the methods here.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ47 Write a program to find second largest element in a given Array in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe easiest way to solve this problem by using sorting. Sort the given Array and then iterate to the second last element.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003ELargestSmallest\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E args\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n  \u003Cspan style=\"color: #888888;\"\u003E\/\/ Given Array\u003C\/span\u003E\n  \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E inputArray\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E10\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E43\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E27\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E98\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E75\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E59\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E191\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n  \u003Cspan style=\"color: #888888;\"\u003E\/\/ Sort Array\u003C\/span\u003E\n  Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Esort\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EinputArray\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\n  \u003Cspan style=\"color: #888888;\"\u003E\/\/ Iterate Array to Second last element\u003C\/span\u003E\n\n  \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E i \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003E inputArray\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E++)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n      \u003Cspan style=\"color: #888888;\"\u003E\/\/ Print second last element\u003C\/span\u003E\n      \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ei \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E inputArray\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E \u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n          System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EinputArray\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E]);\u003C\/span\u003E\n  \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E98\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ48 How to check if Array contains the desired value or not ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSuppose we have a String Array temp.\u003Cbr \/\u003E\nString[] temp = new String[]{\"D\",\"H\",\"B\",\"R\"};\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf desired value exists in the temp Array then below method will return true otherwise false. The below program uses contains() method to check whether inputArray contains the desired element or not.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E inputArray \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E[]{\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"D\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"H\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"B\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"R\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n        \n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EisExist\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EinputArray\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"B\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ true\u003C\/span\u003E\n\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EisExist\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EinputArray\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"G\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ false\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eboolean\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EisExist\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efinal\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E array\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efinal\u003C\/span\u003E String obj\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E Arrays\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003EasList\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earray\u003Cspan style=\"color: #333333;\"\u003E).\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Econtains\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eobj\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\nBelow java program uses equals() method to check whether inputArray contains the desired element.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EJavaHungry\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n        String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E temp \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E[]{\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"D\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"H\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"B\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"R\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EisExist\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Etemp\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"B\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ true\u003C\/span\u003E\n\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EisExist\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Etemp\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"G\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/ false\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eboolean\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EisExist\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efinal\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E array\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efinal\u003C\/span\u003E String obj\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString str \u003Cspan style=\"color: #333333;\"\u003E:\u003C\/span\u003E array\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Estr\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eequals\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eobj\u003Cspan style=\"color: #333333;\"\u003E))\u003C\/span\u003E\n                \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Etrue\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efalse\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ49 Write a program to find the first repeating number in an integer Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\na. Logic is declare a variable minimum. Iterate through the Array in reverse order i.e from last element to first element. Add elements to the HashSet one by one.\u003Cbr \/\u003E\nb. If repeated value occurs then store the index of the repeated value to the minimum variable. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.*\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EFirstRepeating\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E arr \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E4\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E5\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E7\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E4\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E9\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n        firstRepeating\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EfirstRepeating\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E arr\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E minimum \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        HashSet set \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E HashSet\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E i \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E arr\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003Ei \u003Cspan style=\"color: #333333;\"\u003E\u0026gt;=\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E--)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eset\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Econtains\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E]))\u003C\/span\u003E\n                minimum \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eelse\u003C\/span\u003E\n                set\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eadd\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E]);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Eminimum \u003Cspan style=\"color: #333333;\"\u003E!=\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E){\u003C\/span\u003E\n            System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"first repeating element is  : \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E+\u003C\/span\u003E arr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Eminimum\u003Cspan style=\"color: #333333;\"\u003E]);\u003C\/span\u003E\n        \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003Efirst repeating element is\u0026nbsp; : 4\u003Cbr \/\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ50 Write a program to find the first non-repeating number in an integer Array?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u0026nbsp; \u003C\/b\u003E\u003Cbr \/\u003E\nCompare one element with rest of the remaining elements. If value does not match then exit the iteration and print the value.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: rgb(243, 243, 243) none repeat scroll 0% 0%; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: 0.2em 0.6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EFirstNonRepeating\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString args\u003Cspan style=\"color: #333333;\"\u003E[])\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E arr \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]{\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E4\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E5\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E5\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E9\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E};\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EfirstNonRepeating\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003Earr\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Elength\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\n   \n\n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003EfirstNonRepeating\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E arr\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E length\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n        \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E j\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n        \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Eint\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E i \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003E length \u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E++)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Efor\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ej\u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E0\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E j \u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003E length \u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003Ej\u003Cspan style=\"color: #333333;\"\u003E++)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n                \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Earr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E]==\u003C\/span\u003Earr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ej\u003Cspan style=\"color: #333333;\"\u003E]\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E\u0026amp;\u0026amp;\u003C\/span\u003E i\u003Cspan style=\"color: #333333;\"\u003E!=\u003C\/span\u003Ej\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n                    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ebreak\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n                \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n            \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E   \n            \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eif\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Ej \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E length\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E\n                \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E arr\u003Cspan style=\"color: #333333;\"\u003E[\u003C\/span\u003Ei\u003Cspan style=\"color: #333333;\"\u003E];\u003C\/span\u003E\n            \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n         \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #0000dd; font-weight: bold;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n    \u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EOutput :\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E4\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPlease mention in the comments if you have any doubts regarding arrays in java interview questions and answers. \u003C\/div\u003E\n"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/334766977692198757"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/334766977692198757"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2019\/03\/array-interview-questions-and-answers-in-java.html","title":"Top 50 Java Array Interview Questions and Answers "}],"author":[{"name":{"$t":"$ubham"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/11571883502661591778"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/2.bp.blogspot.com\/-LrPFeShml8c\/XJZJmwIFQ2I\/AAAAAAAAA_w\/X0HecDBL6ngLkD3kZGoarBqJks_EQDz-ACLcBGAs\/s72-c\/Arrays%2Bin%2Bjava.png","height":"72","width":"72"}},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-3791801141393795668"},"published":{"$t":"2018-09-09T10:02:00.001-04:00"},"updated":{"$t":"2021-04-28T11:35:56.767-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"string"}],"title":{"type":"text","$t":"Top 50 Java String Interview Questions and Answers  "},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\nString class needs no introduction. String class is one of the most used and very important class in java. Every java application is using String class. In this article I will be sharing 50 most frequently asked java string interview questions and answers. We will divide this post into three categories:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBeginner level (0-1 year experience) Freshers\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIntermediate level(1-7 years experienced)\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAdvanced level (7+ years experienced) java string interview questions and answers.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNote : Please go through all the questions. Interviewer may choose to ask any question.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003EBeginner Level (0-1 yr): Java String Interview Questions and Answers\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ1.\u0026nbsp; What is String?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nString is a class in java which is present in java.lang package. According to \u003Ca href=\"https:\/\/docs.oracle.com\/javase\/9\/docs\/api\/java\/lang\/String.html\" rel=\"nofollow\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E,\u003Cbr \/\u003E\nThe String class represents character strings. Strings are constant, their values can not be changed after they are created.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ2\u0026nbsp; Is String immutable in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003EYes, String class is immutable in java. Immutable means once the object is created, its value can not be changed.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ3 Is String a keyword in java ?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\nNo, String\u0026nbsp; is not a keyword in java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ4 How many objects are created using \u003Ci\u003Enew\u003C\/i\u003E keyword?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003EString str = \u003Cspan style=\"color: navy; font-weight: bold;\"\u003Enew\u003C\/span\u003E String(\u003Cspan style=\"color: blue;\"\u003E\"JavaHungry\"\u003C\/span\u003E);\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nTwo objects are created by the above statement. One object in the heap memory and one object in the String constant pool.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ5 Write a program to reverse a String in java ?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nI have already shared how to reverse a String in java by 6 different ways.\u0026nbsp;\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2014\/12\/5-ways-to-reverse-string-in-java-with-example.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ6 How to convert String to char Array?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nYou can convert String to char Array using \u003Ci\u003EtoCharArray()\u003C\/i\u003E method.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ7 How many different ways you can create a String object?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nYou can create a String object using two ways. First is using \u003Ci\u003Enew\u003C\/i\u003E operator and second is using string \u003Ci\u003Eliteral\u003C\/i\u003E. Objects created using \u003Ci\u003Enew\u003C\/i\u003E operator are stored in the heap memory while string \u003Ci\u003Eliterals\u003C\/i\u003E are stored in the string constant pool.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nString str = \"javahungry\";\u0026nbsp; \/\/ String literal\u003Cbr \/\u003E\nString str = new String(\"javahungry\"); \/\/ using new operator\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ8 Are String thread-safe in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nAs we know String objects are immutable. It means they are thread-safe also.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ9 Which class is final among String, StringBuilder and StringBuffer?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003EAll are final classes.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ10 Is String primitive type or object (derived) type in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nString is object(derived) type in java.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ11 Can we use String in switch statement?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nYes, you can use String in switch statement in java 7. Prior to java 7 , you had to use if-else statements to achieve the task.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003EIntermediate Level (2-7 years)- Java String Interview Questions and Answers\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ12\u0026nbsp; Write a program to reverse a String in java without using reverse() method?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThis is a very important question. Please make sure you have gone through this before appearing for the interview. I have already shared how to reverse a String in java by 6 different ways.\u0026nbsp;\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2014\/12\/5-ways-to-reverse-string-in-java-with-example.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ13 What is the difference between String and StringBuffer in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThis is one of the most asked question in the java developer interview.\u003Cbr \/\u003E\nString is immutable in java. Once created its value can not be changed. StringBuffer is mutable.\u003Cbr \/\u003E\nCheck what is the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-string-stringbuilder.html\" target=\"_blank\"\u003Edifference between String and StringBuffer in java.\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ14 What is the difference between StringBuilder and StringBuffer in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nBelow are the main differences between StringBuilder and StringBuffer in java.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n1. StringBuilder is not thread-safe. StringBuffer is thread-safe.\u003Cbr \/\u003E\n2. StringBuilder is not synchronized and StringBuffer is synchronized.\u003Cbr \/\u003E\n3. StringBuilder is faster while StringBuffer is slower as it is thread-safe.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ15\u0026nbsp; Explain the difference between below statements:\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003EString str \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\nString  str \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E  \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\nIn the first statement, String str = new String(\"abc\");\u003Cbr \/\u003E\nJVM will create one object in the heap memory. Another object in the String constant pool, if the object is not present. Otherwise,if present in the String constant pool ,it will return the reference to it.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nIn the second statement, String str = \"abc\";\u003Cbr \/\u003E\nJVM checks for the string \"abc\" in the String constant pool. If the string is not present in the constant pool then it will create a new String object and stores it in pool.\u003Cbr \/\u003E\nIf the string \"abc\" is found in string constant pool , then it simply creates a reference to it but does not create a new object.\u0026nbsp; \u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ16 How many objects will be created for the following code:\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003EString str1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\nString str2 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nTwo objects are created. Object created using \u003Ci\u003Enew\u003C\/i\u003E operator is stored in the heap memory (str2).\u003Cbr \/\u003E\nObject created using String literal str1 is stored in the string constant pool. \u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ17 How many objects will be created for the following code:\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003EString str1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\nString str2 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nOnly one object is created. String str1 will create a new object in String constant pool, while String str2 will create a reference to the String str1.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ18 How many objects will be created for the following code:\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003EString str1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\nString str2 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThree objects are created. For the first statement(str1) two objects are created one in String constant pool and one in heap memory.\u003Cbr \/\u003E\nBut for the second statement(str2), compulsory 1 new object is created in heap memory but no new object is created in string constant pool as it is already present.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHence , a total of 2+1 = 3 objects are created.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ19 What is String intern() method?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003EAccording to\u0026nbsp;\u003Ca href=\"https:\/\/docs.oracle.com\/javase\/9\/docs\/api\/java\/lang\/String.html\" rel=\"nofollow\" target=\"_blank\"\u003EOracle docs\u003C\/a\u003E,\u003Cbr \/\u003E\nWhen the intern method is invoked, if the String constant pool already contains a string equal to the String object as determined by the equals(Object) method, then the string from the pool is returned.\u003Cbr \/\u003E\nOtherwise the String object is added to the pool and a reference to the String object is returned.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe task of intern() method is to put String (which is passed to the intern method) into string constant pool.\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ20 What are mutable and immutable objects in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nMutable objects value can be changed. StringBuilder and StringBuffer are the examples of the mutable objects.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nImmutable objects value can not be changed once created. String is an immutable class in java.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ21 Why is String immutable in java ?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nThere are various reasons to make String immutable in java.\u003Cbr \/\u003E\n\u003Cb\u003E1. Security :\u0026nbsp; \u003C\/b\u003EString is made immutable to help increase the Security. Sensitive data like username,password can be stored as the Strings can't be modified once created.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. Class loading : \u003C\/b\u003EString objects are used for Class loading. It is possible that wrong class has been loaded in the JVM, if the String is mutable i.e modifiable.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Thread Safe : \u003C\/b\u003EImmutable Strings are thread-safe. Synchronization is not required when we use them in the multithreading environment.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can check more \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2015\/07\/why-string-is-final-or-immutable-in-java-with-example.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ22 How will you create an immutable class in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nYou can create immutable class in java by implementing below points:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. Make the class final so it can not be extended(inherited)\u003Cbr \/\u003E\n2. Make all fields private so one can not access them from outside the class.\u003Cbr \/\u003E\n3. Do not provide setter methods for the variables.\u003Cbr \/\u003E\n4. Declare all mutable fields as final so that it's value can be assigned only once.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ23 How will you create mutable String objects in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nAs we have discussed, by using StringBuffer and StringBuilder objects.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ24 What is the difference between Java String and C,C++ Strings ?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nIn C and Java both programming language treat String object as char Array.\u003Cbr \/\u003E\nJava String is an object while C String is a NULL terminated character array. Java String object allows calling different methods like toUpperCase(), length(), substring().\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ25 Why String is mostly used as a key in HashMap class?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nString is mostly\u0026nbsp; used as a key in HashMap class because it implements equals() and hashCode() methods which is required for an Object to be used as key in HashMap.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ26 Is it possible to call String class methods using String literals?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nYes, It is possible to call String class methods using String literals. For example\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\"javahungry\".indexOf(u)\u003Cbr \/\u003E\n\"javahungry\".charAt(0)\u003Cbr \/\u003E\n\"javahungry\".compareTo(\"javahungry\")\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ27 How to Split String in java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003EYou can use split() method of java.lang.String class or StringTokenizer to\u0026nbsp; split a comma separated String. String split() method is easier to use and better because it expects a regular expression and returns an array of String which you can manipulate in the program code.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ28 Write a java program to find the first non repeated character in the String?\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/12\/first-non-repeated-character-in-string-java-program-code-example.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is the starting question for the product companies, so make sure you go through it. Write a java program to find the first non repeated character in the String.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ29 How do you compare two Strings in Java?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003EUse equals() method to compare two Strings.Avoid using \"==\" operator. The main reason to use equals() method is that it always compare String values i.e content. == operator compares the reference in the memory. Check the \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-equals-and-double-equals-method-with-example-java-collections-interview-question.html\" target=\"_blank\"\u003Edifference between == and equals() method in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003EString str1 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n         \nString str2 \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Enew\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"abc\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n \nSystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Estr1 \u003Cspan style=\"color: #333333;\"\u003E==\u003C\/span\u003E str2\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/false\u003C\/span\u003E\n \nSystem\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Estr1\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eequals\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003Estr2\u003Cspan style=\"color: #333333;\"\u003E));\u003C\/span\u003E \u003Cspan style=\"color: #888888;\"\u003E\/\/true\u003C\/span\u003E  \n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ30 Explain the difference between str1.equals(\"abc\") and \"abc\".equals(str1), where str1 is any String object?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nIf str1 value is \"abc\" then both statements will give the result true. Main difference between the two statement arises when we pass str1 value as NULL. If the str1 is null then first statement will throw null pointer exception while second statement will return false.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ31\u0026nbsp;Find out if String has all Unique Characters?\u003C\/span\u003E\u0026nbsp;\u003C\/b\u003E\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2014\/11\/string-has-all-unique-characters-java-example.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite a java program to find out if the given String has all Unique Characters. There are 5 ways to determine String has all Unique Characters.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ32\u0026nbsp;How to Count number of words in the String?\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp;\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/06\/count-number-of-words-in-string-example-java-program-code.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis is an important phone interview coding question. Write a java program to count number of words in the String.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ33\u0026nbsp;How to remove all the white-spaces in the String?\u003C\/span\u003E\u0026nbsp;\u003C\/b\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/squeeze-string-or-remove-all.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite a java program to remove all the white-spaces in the String.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ34 Print all permutations of the String ?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite a java program to find all the permutations of the given String. Permutation is the all possible combinations of the Strings possible for any word.\u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/find-all-possible-permutations-of-given.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ35 How to calculate total number of characters in the String?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\u0026nbsp; Write a java program to calculate total number of characters in the String .\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/find-calculate-total-number-of-characters-in-string-example-java-program-code.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ36\u0026nbsp;How to calculate total number of vowels in String?\u003C\/span\u003E\u003C\/b\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/total-number-of-vowels-in-string-java.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite a java program to calculate total number of vowels in String .\u003Cbr \/\u003E\nfor example :\u003Cbr \/\u003E\nif the original string : \" Alive is awesome \"\u003Cbr \/\u003E\nthen the number of vowels is : 8\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ37\u0026nbsp;String concatenation in java?\u003C\/span\u003E\u003C\/b\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/11\/5-ways-of-string-concatenation-in-java.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003EWrite different ways for String concatenation in java?\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EInput:\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp;“Java” and “Hungry”\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EOutput:\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp;“JavaHungry”\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u2028Input:\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp;“1234” and “5678”\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EOutput:\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp;“12345678”\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ38 Find all possible combinations of String?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite a java program to find all possible combinations of String. This question can be asked in the phone interview, so make sure you go through it.\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/02\/algorithm-for-combinations-of-string-java-code-with-example.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ39 Write a java program to check if the input string is palindrome?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nI have already shared the code to check whether input string is palindrome or not. \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/palindrome-verification-console-based.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ40 What is String Constant Pool?\u0026nbsp; Why java provided String Constant pool as we can store String in the heap memory?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\nString constant pool is the memory space allocated in the heap memory to store the objects which are\u003Cbr \/\u003E\ncreated using String literals. String constant pool is unique, there are no two String o objects which has the same value(content).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EWhy String Constant Pool ?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\nString constant pool increases the reusability of the existing String objects.\u003Cbr \/\u003E\nIt also saves memory as no two objects with same content are created.\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ41 There are lot of String concatenation and String modification operations in my code. Which class should I use among String,StringBuffer and StringBuilder? Given I also want thread-safe code?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003EThis is scenario based question. You should give answer StringBuffer.\u003C\/b\u003E\u003Cbr \/\u003E\nYou can use String also but with every modification and concatenation operation, a new String is formed as String is immutable. It will lead to the memory allocation issues.\u003Cbr \/\u003E\nStringBuilder can not be used as it is not synchronized, i.e thread-safe.\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\nSo, the clear answer is StringBuffer.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #990000;\"\u003EAdvanced Level (8+ years)- Java String Interview Questions and Answers\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ42 Why char Array is preferred over String in storing passwords?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nOne of the main reason to prefer char Array over String is security risk of stealing passwords. Since String are reusable in the constant pool , there are high chances that they remain in the memory for the long duration. Anyone who has access to the memory dump can find the password in clear text.\u003Cbr \/\u003E\nThat's why password should be encrypted.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ43 What is Character encoding? Explain the difference between UTF-16 and UTF-8?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen you want to represent Character using bytes, character encoding is used.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe UTF-16 uses 2 bytes or 16 bits to represent a character while UTF-8 uses 1 byte or 8 bits to represent a character.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ44 How does substring() method works in java?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nsubstring shares the same character array as String. It can lead to the memory leak if the original String is quite big and not necessary to retain in the memory. It is unintentionally retained by substring as substring is smaller in size.It results in the prevention of large array being garbage collected.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ45 Anagram program in java?\u0026nbsp;\u003C\/b\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/05\/anagram-program-in-java-with-code-example.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite a java program to check whether two given strings are anagram. If two strings contain same set of characters but in different order then the two strings are called anagram.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ46 How to Count occurrences of each character in a String in java?\u003C\/b\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/10\/count-total-number-of-times-each-alphabet-appears-in-the-string.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite a java program to count occurrences of each character in String in java. If the String is\u0026nbsp;\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\"Java Hungry\"\u0026nbsp;\u003C\/b\u003Ethen the answer should be\u003Cbr \/\u003E\n\u003Cpre class=\"\"\u003E\u003Cb\u003E{ =1, a=2, r=1, u=1, v=1, g=1, H=1, y=1, J=1, n=1}\u003C\/b\u003E\u003C\/pre\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ47 Convert Lowercase to Uppercase in java and Uppercase to Lowercase without using built in method ?\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWrite\u0026nbsp; a java program to convert Lowercase to Uppercase and vice versa in a given String.\u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2013\/06\/change-case-of-all-characters-in-string.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003EQ48\u003C\/b\u003E\u0026nbsp;\u003C\/span\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EHow to remove specific characters in the String?\u003C\/span\u003E\u0026nbsp;\u003C\/b\u003E\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2014\/01\/remove-character-string-java-example-specific-certain-unwanted-characters.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTo remove specific characters in the String .For example,\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf the\u0026nbsp;\u003Ci\u003Eoriginal string is\u003C\/i\u003E\u0026nbsp;\"\u003Cb\u003EAlive is awesome\u003C\/b\u003E\" \u0026nbsp; and the\u0026nbsp;\u003Ci\u003Euser inputs string\u003C\/i\u003E\u0026nbsp;to\u0026nbsp;\u003Cb\u003Eremove \"alwsr\"\u003C\/b\u003E\u0026nbsp;\u0026nbsp;then it should print\u0026nbsp;\u003Cb\u003E\u0026nbsp;\"ive i eome\" as output .\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf the\u0026nbsp;\u003Ci\u003Eoriginal string is\u003C\/i\u003E\u0026nbsp;\"\u003Cb\u003ELearning never stops\u003C\/b\u003E\" \u0026nbsp; and the\u0026nbsp;\u003Ci\u003Euser inputs string\u003C\/i\u003E\u0026nbsp;to\u0026nbsp;\u003Cb\u003Eremove \"estp\"\u003C\/b\u003E\u0026nbsp;\u0026nbsp;then the\u0026nbsp;it should print\u0026nbsp;\u003Cb\u003E\u0026nbsp;\u0026nbsp;\"Larning nvr o\" as output .\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ49\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EHow to Convert Signed Integer to String?\u003C\/span\u003E\u0026nbsp;\u003C\/b\u003E\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2014\/04\/how-to-convert-signed-integer-to-string.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHere to convert signed int to string in java, two case arises :\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E1. If number is positive (+ve)\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp; \u0026nbsp;:\u003Cbr \/\u003E\n\u0026nbsp; \u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; Input : 84\u0026nbsp; \u0026nbsp;Output : \u0026nbsp;\"84\"\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E2. If number is negative (-ve)\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003C\/b\u003EInput : -84\u0026nbsp; Output : \"-84\"\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThat's it for today, if you find any new interview questions on String then please mention in the comments, I will add it to the above post. Thanks for reading top 50 java string interview questions and answers.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EQ50 Find the length of the String without using length() method?\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/06\/find-length-of-string-calculate-without-using-built-in-functions-code-program.html\" target=\"_blank\"\u003E[Solution]\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003EWrite a java program to find out the length of the String without using length() method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPlease mention in the comments if you know any other String interview questions.You can also share code of the above mentioned java string coding questions.\u0026nbsp;\u0026nbsp;\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3791801141393795668"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/3791801141393795668"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2018\/09\/top-50-java-string-interview-questions-and-answers.html","title":"Top 50 Java String Interview Questions and Answers  "}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-920820103807831489"},"published":{"$t":"2018-08-11T02:30:00.001-04:00"},"updated":{"$t":"2021-04-28T11:38:59.659-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java"}],"title":{"type":"text","$t":"Top 28 Most Frequently Asked Interview Core Java Interview Questions And Answers 2018"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cdiv\u003E\nWe are sharing 28 java interview questions , these questions are frequently asked by the recruiters.Java interview questions can be asked from any core java topic . So I try my best to provide you the java interview questions and answers for experienced which should be in your to do list before facing java questions in \u0026nbsp;technical interview \u0026nbsp;.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003ETop 28\u0026nbsp; Java Interview Questions :\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E1. Which two method you need to implement for key Object in HashMap ?\u003C\/b\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EIn order to use any object as Key in HashMap, it must implements equals and hashcode method in Java. Read\u0026nbsp;\u003C\/span\u003E\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" style=\"font-family: inherit;\" target=\"_blank\"\u003E\u003Cb\u003EHow HashMap works in Java\u003C\/b\u003E\u003C\/a\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u0026nbsp; for detailed explanation on how equals and hashcode method is used to put and get object from HashMap.\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. What is immutable object? Can you write immutable object?\u003C\/b\u003E\u003Cspan style=\"font-family: inherit;\"\u003EImmutable\n classes are Java classes whose objects can not be modified once \ncreated. Any modification in Immutable object result in new object. For \nexample is\u0026nbsp;String is immutable in Java. Mostly Immutable are also final \nin Java, in order to prevent sub class from\u0026nbsp;overriding methods in \nJava\u0026nbsp;which can compromise Immutability. You can achieve same \nfunctionality by making member as non\u0026nbsp;final\u0026nbsp;but private and not \nmodifying them except in constructor.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E3. What is the difference between creating String as new() and literal?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EWhen we create string with\u0026nbsp;new() Operator,\u0026nbsp;it’s created in\u0026nbsp;heap\u0026nbsp;and not added into string\u0026nbsp;pool\u0026nbsp;while\u0026nbsp;String\u0026nbsp;created using\u0026nbsp;literal\u0026nbsp;are created in String\u0026nbsp;pool\u0026nbsp;itself which exists in\u0026nbsp;PermGen area of heap.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background-color: white;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"color: #333333;\"\u003E\u003Cspan style=\"background-color: white;\"\u003EString s = new String(\"Test\");\u003C\/span\u003E\u003Cbr \/\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #333333;\"\u003Edoes\u0026nbsp;not\u0026nbsp; put the object in String\u0026nbsp;pool\u0026nbsp;, we need to call\u0026nbsp;String.intern()\u0026nbsp;method which is used to put\u0026nbsp; them into String\u0026nbsp;pool\u0026nbsp;explicitly. its only when you create String object as String literal e.g. String s = \"Test\" Java automatically put that into String\u0026nbsp;pool.\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"color: #333333;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"background-color: white; color: #222222; line-height: 18px;\"\u003E\n\u003Cb\u003E4. What is \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-string-stringbuilder.html\" target=\"_blank\"\u003Edifference between StringBuffer and StringBuilder\u003C\/a\u003E in Java ?\u003C\/b\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"background-color: white; color: #222222; line-height: 18px;\"\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"-webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: 'Times New Roman'; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;\"\u003E\n\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EClassic Java questions which some people thing tricky and some consider very easy.\u0026nbsp;StringBuilder\u0026nbsp;in Java is introduced in Java 5 and only difference between both of them is that Stringbuffer methods are\u0026nbsp;synchronized\u0026nbsp;while\u0026nbsp;StringBuilder\u0026nbsp;is non synchronized. See\u0026nbsp;\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-string-stringbuilder.html\" target=\"_blank\"\u003EStringBuilder vs StringBuffer\u003C\/a\u003E\u0026nbsp;for more differences.\u003C\/span\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E5.\u0026nbsp; Write code to find the First non repeated character in the String\u0026nbsp; ?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EAnother good Java interview question, This question is mainly asked by Amazon and equivalent companies. See \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/12\/first-non-repeated-character-in-string-java-program-code-example.html\" target=\"_blank\"\u003Efirst non repeated character in the string : Amazon interview question\u003C\/a\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003Cb\u003E \u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\n\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E6. What is the difference between ArrayList and Vector ?\u003C\/b\u003E\u003Cbr \/\u003E\nThis\n question is mostly used as a start up question in Technical interviews \n\u0026nbsp;on the topic of Collection framework . Answer is explained in detail \nhere \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/12\/difference-between-arraylist-and-vector-in-java-collection-interview-question.html\" target=\"_blank\"\u003EDifference between ArrayList and Vector\u003C\/a\u003E .\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E7. How do you handle error condition\u0026nbsp;\u0026nbsp;while writing stored procedure or accessing stored procedure from java?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EThis is one of the tough Java interview question and its open for all, my friend didn't know the answer so he didn't mind telling me. my take is that stored procedure should return error code\u0026nbsp;if some operation fails but if stored procedure itself fail than catching\u0026nbsp;SQLException\u0026nbsp;is only choice.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E8. What is difference between\u0026nbsp;Executor.submit()\u0026nbsp;and\u0026nbsp;Executer.execute()\u0026nbsp;method ?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cblockquote class=\"tr_bq\" style=\"border-color: rgb(187, 187, 187); border-style: dotted; border-width: 1px 0px; color: #558866; margin: 0.75em 0px; padding: 5px 15px;\"\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"color: black;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003EThere is a difference when looking at exception handling. If your tasks\u0026nbsp;throws an exception\u0026nbsp;and if it was submitted with\u0026nbsp;execute\u0026nbsp;this exception will go to the\u0026nbsp;uncaught exception handler\u0026nbsp;(when you don't have provided one explicitly, the default one will just print\u0026nbsp;the stack trace to System.err). If you submitted the task with\u0026nbsp;submit\u0026nbsp;any thrown exception,\u0026nbsp;checked exception\u0026nbsp;or not, is then part of the task's return status. For a task that was submitted with\u0026nbsp;submit\u0026nbsp;and that terminates with an exception, the Future.get will re-throw this exception, wrapped in an\u0026nbsp;ExecutionException.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003C\/blockquote\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"font-family: Arial, Helvetica, sans-serif; font-size: small;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E9. What is the difference between factory and abstract factory pattern?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cblockquote class=\"tr_bq\" style=\"border-color: rgb(187, 187, 187); border-style: dotted; border-width: 1px 0px; color: #558866; margin: 0.75em 0px; padding: 5px 15px;\"\u003E\n\u003Cspan style=\"color: black;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003EAbstract Factory provides one more level of\u0026nbsp;abstraction. Consider different factories each extended from an Abstract Factory and responsible for creation of different hierarchies of objects based on the type of factory. E.g.\u0026nbsp;AbstractFactory\u0026nbsp;extended by\u0026nbsp;AutomobileFactory,\u0026nbsp;UserFactory,\u0026nbsp;RoleFactory\u0026nbsp;etc. Each individual factory would be responsible for creation of objects in that genre.\u003C\/span\u003E\u003C\/span\u003E\u003C\/blockquote\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EYou can also refer\u0026nbsp;What is Factory method design pattern in Java\u0026nbsp;to know more details.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E10. What is Singleton? is it better to make whole method synchronized or only critical section synchronized ?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/08\/singleton-design-pattern-use-in-java.html\" target=\"_blank\"\u003ESingleton in Java is a class with just one instance in whole Java application\u003C\/a\u003E, for example\u0026nbsp;java.lang.Runtime\u0026nbsp;is a Singleton class. Creating Singleton was tricky prior Java 4 but once Java 5 introduced\u0026nbsp;Enum\u0026nbsp;its very easy. see my article\u0026nbsp;\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/08\/singleton-design-pattern-use-in-java.html\" target=\"_blank\"\u003EHow to create thread-safe Singleton in Java\u003C\/a\u003E\u0026nbsp;for more details on writing Singleton using\u0026nbsp;enum\u0026nbsp;and double checked locking which is purpose of this Java interview question.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"font-family: Arial, Helvetica, sans-serif; font-size: small;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"font-family: Arial, Helvetica, sans-serif; font-size: small;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E11. Can you write critical section\u0026nbsp;code\u0026nbsp;for singleton?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EThis core Java question is followup of previous question and expecting candidate to write Java singleton using double checked locking. Remember to use\u0026nbsp;volatile variable\u0026nbsp;to make Singleton thread-safe.\u003Ca href=\"http:\/\/javarevisited.blogspot.com\/2011\/03\/10-interview-questions-on-singleton.html\" style=\"color: #660099;\"\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003Cb\u003E12. Can you write\u0026nbsp;code\u0026nbsp;for \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-iterator-and-enumeration-collections-java-interview-question-with-example.html\" target=\"_blank\"\u003Eiterating \u003C\/a\u003Eover HashMap in Java 7 and Java 8 ?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003C\/div\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003ETricky one but he managed to write using while and for loop. You can find the answer here \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2017\/11\/how-to-iterate-or-loop-over-hashmap-in-java-with-example.html\" target=\"_blank\"\u003EHow to iterate or loop over HashMap in Java with Example\u003C\/a\u003E.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cb\u003E13. When do you override hashcode and equals() ?\u003C\/b\u003E\u003Cbr \/\u003EWhenever necessary especially if you want to do equality check or want to use your object as key in HashMap.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cb\u003E14. What will be the problem\u0026nbsp;if you don't override hashcode() method ?\u003C\/b\u003E\u003Cbr \/\u003EYou will not be able to recover your object from hash Map if that is used as key in HashMap.\u003Cbr \/\u003ESee here\u0026nbsp;\u0026nbsp;\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003EHow HashMap works in Java\u003C\/a\u003E\u0026nbsp;for detailed explanation.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cb\u003E15. Is it better to synchronize critical section of getInstance() method or whole getInstance() method ?\u003C\/b\u003E\u003Cbr \/\u003EAnswer is critical section because if we lock whole method than every time some one call this method will have to wait even though we are not creating any object)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cb\u003E16. What is the difference when String is gets created using literal or new() operator ?\u003C\/b\u003E\u003Cbr \/\u003EWhen we create string with new() its created in heap and not added into string\u0026nbsp;pool\u0026nbsp;while String created using literal are created in String\u0026nbsp;pool\u0026nbsp;itself which exists in Perm area of heap.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cb\u003E17.\u0026nbsp;Does\u0026nbsp;not overriding hashcode() method has any performance implication ?\u003C\/b\u003E\u003Cbr \/\u003EThis is a good question and open to all , as per my knowledge a poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cb\u003E18. What’s wrong using HashMap in multithreaded environment? When get() method go to infinite loop ?\u003C\/b\u003E\u003Cbr \/\u003EAnother good question. His answer was during concurrent access and\u0026nbsp;re-sizing.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cb\u003E19.\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003E\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u0026nbsp;What do you understand by thread-safety ? Why is it required ? And finally, how to achieve thread-safety in Java Applications ?\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-family: \u0026quot;trebuchet ms\u0026quot; , \u0026quot;verdana\u0026quot; , \u0026quot;arial\u0026quot; , sans-serif; font-size: x-small;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003EJava\u003C\/span\u003E \u003Cspan style=\"font-family: inherit;\"\u003EMemory Model defines the legal interaction of threads with the memory in a real computer system. In a way, it describes what behaviors are legal in multi-threaded code. It determines when a Thread can reliably see writes to variables made by other threads. It defines semantics for volatile, final \u0026amp; synchronized, that makes guarantee of visibility of memory operations across the Threads.\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003E\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003ELet's first discuss about Memory Barrier which are the base for our further discussions. There are two type of memory barrier instructions in JMM - read barriers and write barrier.\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003E\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003EA read barrier invalidates the local memory (cache, registers, etc) and then reads the contents from the main memory, so that changes made by other threads becomes visible to the current Thread.\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003EA write barrier flushes out the contents of the processor's local memory to the main memory, so that changes made by the current Thread becomes visible to the other threads.\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003E\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cb style=\"background-color: white; line-height: 18px;\"\u003EJMM semantics for synchronized\u003C\/b\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003EWhen a thread acquires monitor of an object, by entering into a synchronized block of code, it performs a read barrier (invalidates the local memory and reads from the heap instead). Similarly exiting from a synchronized block as part of releasing the associated monitor, it performs a write barrier (flushes changes to the main memory)\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003EThus modifications to a shared state using synchronized block by one Thread, is guaranteed to be visible to subsequent synchronized reads by other threads. This guarantee is provided by JMM in presence of synchronized code block.\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003E\u003C\/span\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cb style=\"background-color: white; line-height: 18px;\"\u003EJMM semantics for Volatile \u0026nbsp;fields\u003C\/b\u003E\u003Cbr style=\"background-color: white; line-height: 18px;\" \/\u003E\u003Cspan style=\"background-color: white; line-height: 18px;\"\u003ERead \u0026amp; write to volatile variables have same memory semantics as that of acquiring and releasing a monitor using synchronized code block. So the visibility of volatile field is guaranteed by the JMM. Moreover afterwards Java 1.5, volatile reads and writes are not reorderable with any other memory operations (volatile and non-volatile both). Thus when Thread A writes to a volatile variable V, and afterwards Thread B reads from variable V, any variable values that were visible to A at the time V was written are guaranteed now to be visible to B.\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: white; line-height: 18px;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background-color: white; line-height: 18px;\"\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003ELet's try to understand the same using the following code\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EData data = null;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Evolatile boolean flag = false;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EThread A\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E-------------\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Edata = new Data();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Eflag = true;\u003Cspan class=\"Apple-tab-span\" style=\"white-space: pre;\"\u003E \u003Cspan style=\"background-color: #ffe599;\"\u003E \u0026lt;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"background-color: #ffe599;\"\u003E-- writing to volatile will flush data as well as flag to main memory\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EThread B\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E-------------\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Eif(flag==true){\u003Cspan class=\"Apple-tab-span\" style=\"white-space: pre;\"\u003E \u003C\/span\u003E\u003Cspan style=\"background-color: #ffe599;\"\u003E\u003C-- as=\"\" barrier=\"\" data.=\"\" flag=\"\" font=\"\" for=\"\" from=\"\" perform=\"\" read=\"\" reading=\"\" volatile=\"\" well=\"\" will=\"\"\u003E\u003C!------\u003E\u003C!------\u003E\u003C!------\u003E\u003C!------\u003E\u003C!------\u003E\u003C!------\u003E\u003C!------\u003E\u003C\/--\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Euse data;\u003Cspan class=\"Apple-tab-span\" style=\"white-space: pre;\"\u003E  \u003C\/span\u003E\u003Cspan style=\"background-color: #ffe599;\"\u003E\u0026lt;!--- data is guaranteed to visible even though it is not declared volatile because of the JMM\u0026nbsp;semantics of volatile flag.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E20. \u0026nbsp;What will happen if you call return statement or System.exit on try or catch block ? will finally block execute?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EThis is a very\u0026nbsp;\u003Ci\u003Epopular tricky Java question\u003C\/i\u003E\u0026nbsp;and its tricky because many programmer think that finally block always executed. This question challenge that concept by putting\u0026nbsp;return\u0026nbsp;statement in try or catch block or calling\u0026nbsp;System.exit\u0026nbsp;from try or catch block. Answer of this tricky question in Java is that\u0026nbsp;finally\u0026nbsp;block will execute even if you put\u0026nbsp;return\u0026nbsp;statement in try block or catch block but finally block won't run if you call\u0026nbsp;System.exit\u0026nbsp;form try or catch.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E21. Can you override private or static method in Java ?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EAnother popular Java tricky question, As I said method overriding is a good topic to ask trick questions in Java.\u0026nbsp;\u0026nbsp;Anyway,\u0026nbsp;you can not override private or static method in Java, if you create similar method with same return type and same method arguments that's called method hiding.\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E22. What will happen if we put a key object in a HashMap which is already there ?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EThis tricky Java questions is part of \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003EHow HashMap works in Java\u003C\/a\u003E, which is also a popular topic to create confusing and tricky question in Java. well if you put the same key again than it will replace the old mapping because HashMap doesn't allow duplicate keys.\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E23. If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EOne more tricky Java questions from\u0026nbsp;overloading and overriding concept. Answer is you can very well throw super class of RuntimeException in overridden method but you can not do same if its checked\u0026nbsp;Exception.\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E24. What is the issue with following implementation of compareTo() method in Java\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Epublic int compareTo(Object o){\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;Employee emp = (Employee) emp;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;return this.id - o.id;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E25. How do you ensure that N thread can access N resources without deadlock\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"color: #222222;\"\u003EIf you are not well versed in writing multi-threading code\u003C\/span\u003E\u003Cspan style=\"color: #222222;\"\u003E\u0026nbsp;then this is real tricky question for you. This Java question can be tricky even for experienced and senior programmer, who are not really exposed to\u0026nbsp;deadlock\u0026nbsp;and\u0026nbsp;race conditions. Key point here is order, if you acquire resources in a particular order and release resources in reverse\u003C\/span\u003E\u003Cspan style=\"color: #222222;\"\u003E\u0026nbsp;order you can prevent deadlock.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E26. What is difference between CyclicBarrier and CountDownLatch in Java\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003ERelatively newer Java tricky question, only been introduced form Java 5. Main difference between both of them is that you can reuse\u0026nbsp;CyclicBarrier\u0026nbsp;even if Barrier is broken but you can not reuse\u0026nbsp;CountDownLatch\u0026nbsp;in Java. See\u0026nbsp;CyclicBarrier vs CountDownLatch in Java\u0026nbsp;for more differences.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E27. Can you access non static variable in static context?\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\" style=\"color: #222222;\"\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003EAnother tricky Java question from Java fundamentals. No you can not access\u0026nbsp;static variable\u0026nbsp;in non static context in Java. Read\u0026nbsp;why you can not access non-static variable from static method\u0026nbsp;to learn more about this tricky Java questions.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cb\u003E28. What is the difference between sleep() and wait() method?\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Esleep() does not release the lock while wait method release the lock. sleep() method is present in java.lang.Thread class while wait() method\u0026nbsp; is present in java.lang.Object class. For more differences\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-family: inherit;\"\u003Eplease check \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2015\/11\/5-difference-between-sleep-and-wait-with-example.html\" target=\"_blank\"\u003Edifference between sleep and wait method\u003C\/a\u003E.\u003Cb\u003E \u003C\/b\u003E\u0026nbsp; \u003C\/span\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/920820103807831489"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/920820103807831489"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2013\/06\/top-25-most-frequently-asked-core-java.html","title":"Top 28 Most Frequently Asked Interview Core Java Interview Questions And Answers 2018"}],"author":[{"name":{"$t":"§џβђά¶¥¶ "},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02288287727196282252"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"georss$featurename":{"$t":"United States"},"georss$point":{"$t":"37.09024 -95.712891000000013"},"georss$box":{"$t":"-41.116323 99.052733999999987 90 69.521483999999987"}},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-463866646526367206"},"published":{"$t":"2018-08-05T02:51:00.000-04:00"},"updated":{"$t":"2018-08-05T04:02:37.407-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"collection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"HashMap"},{"scheme":"http://www.blogger.com/atom/ns#","term":"hashtable"},{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"}],"title":{"type":"text","$t":"6 Difference between HashMap and HashTable : Popular Interview Question in Java with Example"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\nDifference between HashMap and Hashtable is one of the most popular java interview questions.\u003Cbr \/\u003E\nWe have already discussed other popular java interview questions like \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/12\/difference-between-arraylist-and-vector-in-java-collection-interview-question.html\" target=\"_blank\"\u003EArrayList vs Vector\u003C\/a\u003E and \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/08\/difference-between-comparable-and.html\" target=\"_blank\"\u003EComparator vs Comparable\u003C\/a\u003E .This question is generally asked in java intereview to check whether candidate understand correct usage of collection classes and has knowledge of alternative solutions. Difference between HashMap and Hashtable\u0026nbsp; includes five point \u0026nbsp;namely Synchronization,Null keys and values,Iterating values , Fail fast iterator ,Performance,Superclass .\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d; font-style: italic; font-weight: bold;\"\u003ERead Also : \u0026nbsp;\u003C\/span\u003E\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2014\/02\/hashmap-vs-concurrenthashmap-java-collections-interview-question.html\" target=\"_blank\"\u003EDifference between HashMap and ConcurrentHashMap with Example\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EDifference between HashMap and Hashtable \/ HashMap vs Hashtable\u003C\/b\u003E\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E1. Synchronization or Thread Safe :\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp; This is the most important difference between two . HashMap is non synchronized and not thread safe.On the other hand, Hashtable is thread safe and synchronized.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen to use HashMap ? \u0026nbsp;answer is if your application do not require any multi-threading task, in other words HashMap is better for non-threading applications. Hashtable should be used in multithreading applications. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E2. Null keys and null values :\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp; HashMap allows one null key and any number of null values, while Hashtable do not allow null keys and null values in the Hashtable object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003E3. Iterating the values:\u003C\/span\u003E\u003C\/b\u003E\u0026nbsp; HashMap object values are iterated by using iterator .Hashtable is the only class other than vector which uses enumerator to iterate the values of Hashtable object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"http:\/\/3.bp.blogspot.com\/-BvvI4qSJ5gs\/UymE9OXgBGI\/AAAAAAAAASA\/yXv2COAHm_U\/s1600\/difference+between+hashmap+and+hashtable.jpg\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg alt=\"difference between hashmap and hashtable\" border=\"0\" height=\"286\" src=\"https:\/\/3.bp.blogspot.com\/-BvvI4qSJ5gs\/UymE9OXgBGI\/AAAAAAAAASA\/yXv2COAHm_U\/s1600\/difference+between+hashmap+and+hashtable.jpg\" title=\"difference between hashmap and hashtable\" width=\"320\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E4.\u0026nbsp; Fail-fast iterator\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp; : The iterator in HashMap is fail-fast iterator while the enumerator for Hashtable is not.\u003Cbr \/\u003E\nAccording to \u003Ca href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Hashtable.html\" target=\"_blank\"\u003EOracle Docs\u003C\/a\u003E,\u0026nbsp; if the Hashtable is structurally modified at any time after the iterator is created in any way except the iterator's own remove method , then the iterator will throw ConcurrentModification Exception.\u003Cbr \/\u003E\nStructural modification means adding or removing elements from the Collection object (here HashMap or Hashtable) . Thus the enumerations returned by the Hashtable keys and elements methods are not fail fast.We have already explained the\u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/06\/difference-between-iterator-and-enumeration-collections-java-interview-question-with-example.html\" target=\"_blank\"\u003E difference between iterator and enumeration\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E5. Performance :\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp; HashMap is much faster and uses less memory than Hashtable as former is unsynchronized . Unsynchronized objects are often much better in performance in compare to synchronized\u0026nbsp; object like Hashtable in single threaded environment.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E6. Superclass and Legacy :\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp; Hashtable is a subclass of Dictionary class which is now obsolete in Jdk 1.7 ,so ,it is not used anymore. It is better off externally synchronizing a HashMap or using a ConcurrentMap implementation (e.g ConcurrentHashMap).HashMap is the subclass of the AbstractMap class. Although Hashtable and HashMap has different superclasses but they both are implementations of the \u003Ci\u003E\"Map\"\u003C\/i\u003E\u0026nbsp; abstract data type.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EExample of HashMap\u0026nbsp; and HashTable\u003C\/b\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: #F3F3F3; border-width: .1em .1em .1em .1em; border: 1px dotted; overflow: auto; padding: .2em .6em; width: auto;\"\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ejava.util.Hashtable\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E;\u003C\/span\u003E\n\n\n\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eclass\u003C\/span\u003E \u003Cspan style=\"color: #bb0066; font-weight: bold;\"\u003EHashMapHashtableExample\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\n    \n    \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Epublic\u003C\/span\u003E \u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Estatic\u003C\/span\u003E \u003Cspan style=\"color: #333399; font-weight: bold;\"\u003Evoid\u003C\/span\u003E \u003Cspan style=\"color: #0066bb; font-weight: bold;\"\u003Emain\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E[]\u003C\/span\u003E args\u003Cspan style=\"color: #333333;\"\u003E)\u003C\/span\u003E \u003Cspan style=\"color: #333333;\"\u003E{\u003C\/span\u003E\u0026nbsp;\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u0026nbsp;\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E       \u003Cb\u003E\u003C!--  Hashtable example --\u003E\u003C\/b\u003E  \u0026nbsp;\u003Cb\u003E\u003C!-- Hashtable Example  --\u003E\u003C\/b\u003E\u0026nbsp;\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u0026nbsp; \u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E       \u0026nbsp;Hashtable\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003EString\u0026gt; hashtableobj \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003E new\u003C\/span\u003E Hashtable\u003Cspan style=\"color: #333333;\"\u003E\u0026lt;\u003C\/span\u003EString\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E String\u003Cspan style=\"color: #333333;\"\u003E\u0026gt;\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E();\u003C\/span\u003E\n        hashtableobj\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eput\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Alive is \"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"awesome\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        hashtableobj\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eput\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\u003C\/span\u003E\u003Cspan style=\"background-color: #fff0f0;\"\u003E\"Love\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E,\u003C\/span\u003E \u003Cspan style=\"background-color: #fff0f0;\"\u003E\"yourself\"\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\n        System\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eout\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #0000cc;\"\u003Eprintln\u003C\/span\u003E\u003Cspan style=\"color: #333333;\"\u003E(\"Hashtable object output :\"+ \u003C\/span\u003Ehashtableobj\u003Cspan style=\"color: #333333;\"\u003E);\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #333333;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u003Cspan style=\"color: #333333;\"\u003E\u0026nbsp; \u003C\/span\u003E \u003Cb\u003E\u003C!-- HashMap Example  --\u003E\u003C\/b\u003E\u0026nbsp;   \u003Cb\u003E\u003C!--  HashMap example  --\u003E \u003C\/b\u003E \u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u0026nbsp;\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E        HashMap\u003Cstring tring=\"\"\u003E hashmapobj = new HashMap\u003Cstring string=\"\"\u003E();\n        hashmapobj.put(\"Alive is \", \"awesome\");  \n        hashmapobj.put(\"Love\", \"yourself\"); \n        System.out.println(\u003Cspan style=\"color: #333333;\"\u003E\"HashMap object output :\"+\u003C\/span\u003Ehashmapobj); \u0026nbsp; \u003C\/string\u003E\u003C\/string\u003E\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u0026nbsp;\u003C\/pre\u003E\n\u003Cpre style=\"line-height: 125%; margin: 0;\"\u003E\u0026nbsp;\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003Cspan style=\"color: #333333;\"\u003E}\u003C\/span\u003E\n\u003C\/pre\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003EOutput :\u003C\/span\u003E\u003C\/b\u003E\u003Cspan style=\"color: #333333;\"\u003E\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E\u0026nbsp; \u003C\/b\u003E\u003C\/span\u003E\u003Ci\u003EHashtable object output :\u003C\/i\u003E\u003C\/span\u003E\u003Ci\u003E\u003Cb\u003E{Love=yourself, Alive is =awesome}\u003C\/b\u003E\u003C\/i\u003E\u003Cbr \/\u003E\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003Ci\u003EHashMap object output :\u003Cb\u003E{Alive is =awesome, Love=yourself}\u003C\/b\u003E\u003C\/i\u003E\u003Cbr \/\u003E\n\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ESimilarities Between HashMap and Hashtable\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E1. Insertion Order :\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp;\u0026nbsp; Both HashMap and Hashtable\u0026nbsp; does not guarantee that\u0026nbsp; the order of the map will remain constant over time. Instead use LinkedHashMap, as the order remains constant over time.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E2. Map interface :\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp;\u0026nbsp; Both HashMap and Hashtable implements Map interface .\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E3. Put and get method :\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp; Both HashMap and Hashtable provides constant time performance for put and get methods assuming that the objects are distributed uniformly across the bucket.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: #38761d;\"\u003E\u003Cb\u003E4. Internal working :\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp; Both HashMap and Hashtable works on the Principle of Hashing . We have already discussed \u003Ca href=\"http:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\" target=\"_blank\"\u003Ehow hashmap works in java\u003C\/a\u003E .\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EWhen to use HashMap and Hashtable?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E1. Single Threaded Application\u003C\/i\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHashMap should be preferred over Hashtable for the non-threaded applications. In simple words , use HashMap in unsynchronized or single threaded applications .\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ci\u003E2. Multi Threaded Application\u003C\/i\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWe should avoid using Hashtable, as the class is now obsolete in latest Jdk 1.8 . Oracle has provided a better replacement of Hashtable named ConcurrentHashMap. For multithreaded\u0026nbsp; application prefer ConcurrentHashMap instead of Hashtable.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003ERecap\u0026nbsp; : Difference between HashMap and Hashtable in Java\u0026nbsp;\u003C\/b\u003E \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cstyle type=\"text\/css\"\u003E\n table.tableizer-table {\n border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif\n font-size: 12px;\n} \n.tableizer-table td {\n padding: 4px;\n margin: 3px;\n border: 1px solid #ccc;\n}\n.tableizer-table th {\n background-color: #104E8B; \n color: #FFF;\n font-weight: bold;\n}\n\u003C\/style\u003E\u003Cbr \/\u003E\n\u003Ctable class=\"tableizer-table\"\u003E\n\u003Ctbody\u003E\n\u003Ctr class=\"tableizer-firstrow\"\u003E\u003Cth\u003E\u003C\/th\u003E\u003Cth\u003EHashMap\u003C\/th\u003E\u003Cth\u003EHashtable\u003C\/th\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003ESynchronized\u003C\/td\u003E\u003Ctd\u003ENo\u003C\/td\u003E\u003Ctd\u003EYes\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003EThread-Safe\u003C\/td\u003E\u003Ctd\u003ENo\u003C\/td\u003E\u003Ctd\u003EYes\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003ENull Keys and Null values\u003C\/td\u003E\u003Ctd\u003EOne null key ,Any null values\u003C\/td\u003E\u003Ctd\u003ENot permit null  keys and values\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003EIterator type\u003C\/td\u003E\u003Ctd\u003EFail fast iterator\u003C\/td\u003E\u003Ctd\u003EFail safe iterator\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003EPerformance\u003C\/td\u003E\u003Ctd\u003EFast\u003C\/td\u003E\u003Ctd\u003ESlow in comparison\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003Ctd\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd\u003ESuperclass and Legacy\u003C\/td\u003E\u003Ctd\u003EAbstractMap , No\u003C\/td\u003E\u003Ctd\u003EDictionary , Yes \u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn case you have any other query please mention in the comments.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n"},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/463866646526367206"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/463866646526367206"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2014\/03\/hashmap-vs-hashtable-difference-with-example-java-interview-questions.html","title":"6 Difference between HashMap and HashTable : Popular Interview Question in Java with Example"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/3.bp.blogspot.com\/-BvvI4qSJ5gs\/UymE9OXgBGI\/AAAAAAAAASA\/yXv2COAHm_U\/s72-c\/difference+between+hashmap+and+hashtable.jpg","height":"72","width":"72"}},{"id":{"$t":"tag:blogger.com,1999:blog-3696016880415835141.post-7139243401404771397"},"published":{"$t":"2017-10-16T18:40:00.003-04:00"},"updated":{"$t":"2021-04-28T11:43:55.439-04:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"thread"}],"title":{"type":"text","$t":"Top 25 Most Frequently Asked Java Multithreading Interview Questions and Answers"},"content":{"type":"html","$t":"In interviews we usually come across \u0026nbsp;two topics \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/05\/50-java-collections-interview-questions-and-answers.html\" target=\"_blank\"\u003Ejava collection interview questions\u003C\/a\u003E and multithreading interview questions. Multithreading is all about concurrency and threads. It is good to be prepared, so do not leave this topic at all.We are sharing important \u0026nbsp;java multithreading interview questions and answers.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ1. \u0026nbsp; What is a thread?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThread is a lightweight subprocess. Thread class belongs to java.lang package.Threads have their \u0026nbsp;own stack.It's a way to take advantage of multiple cpu available in a machine. For example, if one thread takes 50 milliseconds to do a job, you can use 10 threads to reduce that task to 5 milliseconds.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ2 What is the difference between Thread and Process?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nA process can have many threads. Threads can execute any part of process.And same part of Process can be executed by multiple threads.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nProcesses have their own address while Thread share the address space of the process that created it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThread has its own stack while in process all threads share a common system resource like heap memory.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ3 What are the ways to implement Thread in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\nThere are two ways to implement Thread in java.\u003Cbr \/\u003E\n1. By implementing Runnable interface in java and then creating Thread object from it.\u003Cbr \/\u003E\n2. By extending the Thread class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ4 What are the differences between implementing Runnable and extending Thread while creating a thread in java? Which one is better?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nImplementing Runnable is better.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen we implement Runnable interface we can extend any other class as well but if we extends Thread class we can not extends any other class because java does not allow multiple inheritance.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find here the detailed answer of \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/05\/implements-runnable-vs-extends-thread-in-java-example.html\" target=\"_blank\"\u003Edifference between implementing Runnable and extends Thread\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ5 What is \u0026nbsp;a life cycle of a thread?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen we create a Thread instance in a java program, then its state is new. Then we start the Thread, then it's state changes to Runnable(ready to run but not running yet).Execution of Threads depends upon ThreadScheduler. ThreadScheduler is responsible to allocate CPUs to threads in Runnable thread pool and change their state to Running.Waiting,Blocked and Dead are the remaining states of the Thread.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSo in short \u003Cb\u003E\u003Cspan style=\"color: #38761d;\"\u003Enew,runnable,running.waiting,blocked and dead\u003C\/span\u003E\u003C\/b\u003E are the states a Thread can be in.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ6 What is the difference between sleep and wait method in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWait method releases the lock while sleep method doesn't release the lock.\u003Cbr \/\u003E\nWait method belongs to java.lang.Object class while sleep method belongs to java.lang.Thread class.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find the detailed answer here \u0026nbsp;\u003Ca href=\"https:\/\/javahungry.blogspot.com\/2015\/11\/5-difference-between-sleep-and-wait-with-example.html\" target=\"_blank\"\u003Edifference between wait and sleep method in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ7 What is the difference between starting a thread with start() method and run() method?\u0026nbsp;\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThis question is a bit tricky and might confuse you as well. The answer is when you call start() method, main method internally calls run() method to start newly created Thread, so run method is ultimately called by newly created Thread.\u003Cbr \/\u003E\nWhen you call run() method, its called in the same thread, no new thread is started which is the case when you call start() method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ8 What is the difference between user thread and daemon thread?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen we create a thread in java program, then it is called as user thread. We can not make a user thread to daemon thread if a thread is started.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe daemon threads are the low priority threads that provide the background support to the user threads.It provides services to the user threads. A child thread created from a daemon thread is also a daemon thread.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ9 How to create a Daemon thread in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBy setting the setDaemon(true) , we can create a daemon thread in java.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ10 What is the significance of using volatile keyword?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen we make a variable volatile, then all the threads reads its value directly from the memory and don't cache it. This make sure the shared variables are consistently updated.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nvolatile is a keyword that can only be used with variables.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ11 Is it possible to start a thread twice?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nNo, there is no possibility to start a thread twice. If we do so , then it will throw an Exception.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ12 What is synchronization?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSynchronization is the capability to control the access of multiple threads to any shared resource.\u003Cbr \/\u003E\nThe main advantage of synchronization is\u003Cbr \/\u003E\na. to avoid consistency problem\u003Cbr \/\u003E\nb. to avoid thread interference\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ13 Which is more preferred - synchronization block or synchronization method?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSynchronized block is the more preferred way because it doesn't lock the object while synchronized methods lock the object. Synchronized method will stop multiple synchronized blocks in the class, even though they are not related, from the execution and put them in the wait state to get the lock on the object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ14 Difference and similarities between sleep and yield method?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSleep method throws the interrupted exception if another thread interrupts the sleeping thread while yield method does not throw the interrupted exception.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThread.sleep() method does not \u0026nbsp;cause currently executing thread to give up monitors while yield method gives up the monitor.\u003Cbr \/\u003E\nYou can find detailed explanation of \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2016\/03\/difference-between-yield-and-sleep-method-with-example.html\" target=\"_blank\"\u003Edifference between sleep and yield method in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ15 What is deadlock?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nDeadlock is a situation where two threads are waiting for each other to release locks holded by them on resources.For example\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThread 1 : locks resource A, waits for resource B\u003Cbr \/\u003E\nThread 2 : locks resource B, waits for resource A\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ16 Write a program to create a Deadlock in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nYou can find the answer here \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/09\/program-code-to-create-deadlock-two-threads-in-java.html\" target=\"_blank\"\u003EProgram to create a deadlock in java\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ17 What measures you should take to avoid deadlock?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. Lock specific member variables of the class rather than locking whole class.\u003Cbr \/\u003E\n2. Use join() method, if possible try to use join method ,although it may refrain us from taking full advantage of multithreading environment because threads will start and end sequentially, but it can be handy in avoiding deadlocks.\u003Cbr \/\u003E\n3. If possible try to avoid nested synchronization blocks.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ18 What do you understand by Thread priority?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nEvery thread has a priority. Its value is \u003Ci\u003Eint\u003C\/i\u003E which ranges from 1 to 10 where 1 being the lowest priority and 10 being the highest priority.\u003Cbr \/\u003E\nUsually higher priority threads get higher precedence in execution but it depends on the ThreadScheduler implementation which is OS dependent.\u003Cbr \/\u003E\nWe can specify the priority of thread but it does not guarantee that higher priority thread will get executed before the lower priority thread.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ19 What is the difference between class lock and object lock?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThreads can acquire object lock by entering synchronized methods.Threads can acquire lock on class's class object by entering the static synchronized methods.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nMultiple objects of class may exist and every Object has its own lock.In class lock multiple objects of \u0026nbsp;class may exist but there is always one class's class object lock available.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EQ20 What is the difference\u0026nbsp;between Callable and Runnable?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nCallable throws checked exception while Runnable does not throw checked exception.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nReturn type of Runnable is void that is it does not return any value while Callable can return a Future object.\u003Cbr \/\u003E\nYou can find the detailed explanation of \u003Ca href=\"https:\/\/javahungry.blogspot.com\/2017\/07\/difference-between-callable-and-runnable-in-java.html\" target=\"_blank\"\u003Edifference between callable and runnable\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ21 What is the difference between time slicing and preemptive scheduling?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nIn preemptive scheduling the higher priority task executes until it enters the waiting or dead states or higher priority task comes into existence. In time slicing, a task runs for a predefined slice of time and then reenters the pool of ready tasks.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ22 Can a constructor be synchronized?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNo, Constructor can not be synchronized.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ23 What is race condition in java and how we can solve it?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen more than one thread try to access same resource without synchronization causes race condition.\u003Cbr \/\u003E\nWe can solve race condition by using a synchronized block or synchronized method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ24 How threads communicate with each other?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThreads can communicate with each other using wait(), notify(), notifyAll() methods.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ25 Why wait(), notify() and notifyAll() method have to be called from the synchronized context?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhen a Thread calls wait() on any Object, it must have the monitor on Object that it will leave and goes in wait state until any other Thread call notify() on this Object. Similarly when a thread calls notify() on any Object, it leaves the monitor on the Object and other waiting threads can get the monitor on the Object. Since all these threads require Thread to have a Object monitor,that can be achieved only by synchronization.That is why wait(),notify() and notifyAll() method have to be called from the synchronized context.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003EQ26 What is ThreadLocal variable in java?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\nThreadLocal can be used to create ThreadLocal variables. We know that all threads of an Object shares its variables.So if the variable is not thread safe then we can use synchronization. If we want to avoid synchronization then we can use ThreadLocal variables.\u003Cbr \/\u003E\nEach thread has its own ThreadLocal variable and they can use it's get() and set() methods to get the default value or change its value local to Thread.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ27 What is Threadpool?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThreadpool \u0026nbsp;manages the pool of worker threads. There is a queue in which the tasks are keep waiting for execution. \u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EQ28 Can you find whether thread holds lock() on an object or not?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nholdsLock(Object) method can be used to determine whether current thread holds the lock on monitor of specified object.\u003Cbr \/\u003E\nThe method holdsLock(Object) returns true if the thread holds lock or monitor of the specified object.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIf you face any java multithreading question which is not in above questions then please mention in the comments."},"link":[{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/7139243401404771397"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3696016880415835141\/posts\/default\/7139243401404771397"},{"rel":"alternate","type":"text/html","href":"https:\/\/javahungry.blogspot.com\/2017\/10\/java-multithreading-interview-questions-and-answers.html","title":"Top 25 Most Frequently Asked Java Multithreading Interview Questions and Answers"}],"author":[{"name":{"$t":"Subham Mittal"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/18336905445903248964"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}]}]}});