{"id":1002,"date":"2021-07-30T08:40:55","date_gmt":"2021-07-30T08:40:55","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/07\/30\/top-25-java-programming-interview-questions-part-2\/"},"modified":"2021-07-30T08:40:55","modified_gmt":"2021-07-30T08:40:55","slug":"top-25-java-programming-interview-questions-part-2","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/07\/30\/top-25-java-programming-interview-questions-part-2\/","title":{"rendered":"Top 25 Java Programming Interview Questions \u2013 Part 2"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"1002\" id=\"tve_editor\">\n<div class=\"thrv_wrapper tve-toc tve-elem-scroll tve-toc-expandable tcb-local-vars-root\" data-animation=\"slide\" data-animation-speed=\"fast\" data-bullet-style='{\"0\":\"tve-u-17399ffbddd\",\"1\":\"tve-u-1739a0466e5\",\"2\":\"tve-u-1739a04a2bb\"}' data-columns=\"1\" data-columns-d=\"1\" data-css=\"tve-u-17af67eb058\" data-ct=\"toc-60671\" data-ct-name=\"Table of Contents 03\" data-element-name=\"Table of Contents\" data-headers=\"h1,h3,h5\" data-heading-style='{\"0\":\"tve-u-17af67eb05d\",\"1\":\"tve-u-17af67eb05f\",\"2\":\"tve-u-17af67eb060\"}' data-highlight=\"section\" data-number-style='{\"0\":\"tve-u-17af67eb05e\",\"1\":\"tve-u-173dc888a58\",\"2\":\"tve-u-173dc8891dc\"}' data-numbering=\"basic\" data-state-default=\"expanded\" data-state-default-m=\"collapsed\" data-transition=\"fade\" style=\"\">\n<div class=\"thrive-colors-palette-config\" style=\"display: none !important\"><\/div>\n<div class=\"tve-toc-divider\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\">\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgba(0, 0, 0, 0)\" data-css=\"tve-u-17af67eb059\" data-style=\"tve_sep-1\" data-thickness-d=\"1\">\n<hr class=\"tve_sep tve_sep-1\" style=\"\"\/><\/div>\n<\/div>\n<p><svg class=\"toc-icons\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\" version=\"1.1\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><symbol data-id=\"icon-circle-outlined\" id=\"toc-bullet-0\" viewbox=\"0 0 512 512\"><path d=\"M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200z\"><\/path><\/symbol><symbol data-id=\"icon-circle-outlined\" id=\"toc-bullet-1\" viewbox=\"0 0 512 512\"><path d=\"M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200z\"><\/path><\/symbol><symbol data-id=\"icon-circle-outlined\" id=\"toc-bullet-2\" viewbox=\"0 0 512 512\"><path d=\"M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200z\"><\/path><\/symbol><\/svg><\/p>\n<div class=\"tve-content-box-background\" data-css=\"tve-u-17af67eb05a\" style=\"\"><\/div>\n<div class=\"thrv_wrapper tve-toc-title show-icon tcb-icon-display tve-no-dropzone tve-prevent-content-edit\" data-css=\"tve-u-17af67eb05b\" style=\"\">\n<div class=\"tve-content-box-background\"><\/div>\n<div class=\"tve-cb\" style=\"\">\n<div class=\"tve-toc-title-icon\" data-icon-code=\"icon-arrow-circle-down-solid\" style=\"\"><svg class=\"tcb-icon\" data-id=\"icon-arrow-circle-down-solid\" data-name=\"\" viewbox=\"0 0 512 512\"><path d=\"M504 256c0 137-111 248-248 248S8 393 8 256 119 8 256 8s248 111 248 248zm-143.6-28.9L288 302.6V120c0-13.3-10.7-24-24-24h-16c-13.3 0-24 10.7-24 24v182.6l-72.4-75.5c-9.3-9.7-24.8-9.9-34.3-.4l-10.9 11c-9.4 9.4-9.4 24.6 0 33.9L239 404.3c9.4 9.4 24.6 9.4 33.9 0l132.7-132.7c9.4-9.4 9.4-24.6 0-33.9l-10.9-11c-9.5-9.5-25-9.3-34.3.4z\"><\/path><\/svg><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve_no_icons\">\n<div class=\"tcb-plain-text\" style=\"\">Table of Contents<\/div>\n<\/p><\/div>\n<\/div>\n<\/div>\n<div class=\"tve-cb tve-toc-content tve-prevent-content-edit\" style=\"\">\n<div class=\"thrv_wrapper tve-toc-list tcb-no-delete tcb-no-save tcb-no-clone tve-no-dropzone\" data-css=\"tve-u-17af67eb05c\" style=\"\">\n<div class=\"tve-content-box-background\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"tve_ct_content tve_clearfix\">\n<div class=\"ct_column\">\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073424\" jump-animation=\"smooth\">Question 13: Write a Java Program to generate Output &#8220;aabbbcccc&#8221; with the input &#8220;a2b3c4&#8221;<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073425\" jump-animation=\"smooth\">Question 14: Write a Java Program to print the product of an array except self?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073426\" jump-animation=\"smooth\">Question 15: Write a Java Program to reverse a number using Stack.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073427\" jump-animation=\"smooth\">Question 16:\u00a0 Write a Java Program to print the second largest number in a given array.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073428\" jump-animation=\"smooth\">Question 17: Write a Java Program to print the first non repeating character in a given String.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073429\" jump-animation=\"smooth\">Question 18: Write a Java Program to print the maximum consecutive ones in a given array.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073430\" jump-animation=\"smooth\">Question 19: Write a Java Program to print the first and last position of a number in a given array.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073431\" jump-animation=\"smooth\">Question 20: Write a Java Program to print numbers 1 to 100 without using any loop(for\/while\/do-while).<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073432\" jump-animation=\"smooth\">Question 21: Write a Java Program to shift all zeros on the right.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073433\" jump-animation=\"smooth\">Question 22: Write a Java Program to remove the duplicates from an array.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073434\" jump-animation=\"smooth\">Question 23: Write a Java Program to iterate through the HashMap.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073435\" jump-animation=\"smooth\">Question 24: Write a Java Program to convert HashMap to ArrayList.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af67eb05f\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627633073436\" jump-animation=\"smooth\">Question 25: Write a Java Program to print the missing number in a given Array.<\/a><\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgba(0, 0, 0, 0)\" data-css=\"tve-u-17af67eb059\" data-style=\"tve_sep-1\" data-thickness-d=\"1\">\n<hr class=\"tve_sep tve_sep-1\" style=\"\"\/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>This post is a continuation of <a class=\"tve-froala fr-basic\" data-css=\"tve-u-17af68133cb\" href=\"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/07\/29\/25-java-programming-interview-questions-part-1\/\" style=\"outline: none;\" target=\"_blank\">Top 25 Java Programming Interview Questions \u2013 Part 1<\/a> for any QA Automation interview.\u00a0<\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<h3 class=\"\" id=\"t-1627633073424\">Question 13: Write a Java Program to generate Output &#8220;aabbbcccc&#8221; with the input &#8220;a2b3c4&#8221;<\/h3>\n<p><strong>Solution 13: <\/strong>In the above problem, we have the numbers in the input. We need to print the as many characters same the number value.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af6835028\" style=\"\">\n<p data-css=\"tve-u-17af6839817\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class ConvertNumricToCharExample {<\/p>\n<p>static void convertNumToChar(String s) {<\/p>\n<p>for(int i =0; i &lt; s.length(); i++) {<br \/>if(Character.isAlphabetic(s.charAt(i))){<br \/>System.out.print(s.charAt(i));<br \/>}else {<br \/>int a = Character.getNumericValue(s.charAt(i));<br \/>for(int j =1; j &lt;a; j++) {<br \/>System.out.print(s.charAt(i-1));<br \/>}<br \/>}<br \/>}<\/p>\n<p>}<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>String str = &#8220;a2b3c4&#8221;; \/\/output = aabbbcccc <br \/>convertNumToChar(str);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1627633073425\">Question 14: Write a Java Program to print the product of an array except self?<\/h3>\n<p><strong>Solution 14: <\/strong>In this problem, We need the output which is the product of the all the numbers in the array except self.<br \/>\u00a0Here we take two arrays, left_products and right_products in which we store the products of all numbers except self. <\/p>\n<p>Then we multiply the left_products and right_products to get the output.<\/p>\n<p>Below is the Java Program:<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af68450af\" style=\"\">\n<p data-css=\"tve-u-17af6848792\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class ProductArrayExceptSelfExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int[] arr = {1,2,3,4};<br \/>int N = arr.length;<\/p>\n<p>int[] left_products = new int[N];<br \/>int[] right_products = new int[N];<\/p>\n<p>int[] output_array = new int[N];<br \/>left_products[0] = 1;<br \/>right_products[N-1] = 1;<\/p>\n<p>for(int i = 1; i &lt; N; i++) {<br \/>left_products[i] = arr[i-1] * left_products[i-1];<br \/>}<\/p>\n<p>for(int i = N-2; i &gt;=0; i&#8211;) {<br \/>right_products[i] = arr[i+1] * right_products[i+1];<br \/>}<\/p>\n<p>for(int i = 0; i &lt; N; i++) {<br \/>output_array[i] = left_products[i] * right_products[i];<br \/>}<\/p>\n<p>for(int i =0; i &lt; N; i++) {<br \/>System.out.print(output_array[i] + &#8221; &#8220;);<br \/>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1627633073426\">Question 15: Write a Java Program to reverse a number using Stack.<\/h3>\n<p><strong>Solution 15: <\/strong>In the above program, we will be using Stack to reverse a number. The idea is to get the digits using the % operator \u00a0and push each digit in the Stack.\u00a0<\/p>\n<p>Once all digits are pushed in the stack, we pop each digit using the pop method.<br \/> Stack is LIFO datastructure thus the output number will be a number in a reverse order.<\/p>\n<p>\u00a0Below is the Java Program:<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af6859289\" style=\"\">\n<p data-css=\"tve-u-17af686106d\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.Stack;<\/p>\n<p>public class ReverseNumberUsingStack {<\/p>\n<p>static Stack&lt;Integer&gt; st = new Stack&lt;&gt;();<\/p>\n<p>static void push_digits(int number) {<br \/>while (number != 0) {<br \/>st.push(number % 10);<br \/>number = number \/ 10;<br \/>}<br \/>}<\/p>\n<p>static int reverse_number(int number) {<br \/>push_digits(number);<br \/>int reverse = 0;<br \/>int i = 1;<\/p>\n<p>while (!st.isEmpty()) {<br \/>reverse = reverse + (st.peek() * i);<br \/>st.pop();<br \/>i = i * 10;<br \/>}<\/p>\n<p>return reverse;<br \/>}<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int number = 3997;<br \/>System.out.println(reverse_number(number));<br \/>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The output to the above program would be 7993.<\/p>\n<h3 class=\"\" id=\"t-1627633073427\">Question 16:\u00a0 Write a Java Program to print the second largest number in a given array.<\/h3>\n<p><strong>Solution 16: <\/strong>In this problem, to find the second largest number in the array, first we sort the array and then using the for loop we find the second largest number in reverse order.<\/p>\n<p>\u00a0Below is the Java Program:<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af68784db\" style=\"\">\n<p data-css=\"tve-u-17af687ab58\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class SecondLargestElementOfArray {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int[] arr = {2,3,1,6,9,9};<br \/>int temp;<br \/>for (int i = 0; i &lt; arr.length; i++) {<br \/>for (int j = i + 1; j &lt; arr.length; j++) {<br \/>if (arr[i] &gt; arr[j]) {<br \/>temp = arr[i];<br \/>arr[i] = arr[j];<br \/>arr[j] = temp;<br \/>}<br \/>}<br \/>}<\/p>\n<p>for(int i = arr.length -2; i &gt;=0; i&#8211;) {<br \/>if(arr[i] != arr[arr.length-1]) {<br \/>System.out.println(&#8220;The second largest element &#8221; + arr[i]);<br \/>break;<br \/>}<br \/>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The output of the above program is 6. In this it also checks that the second largest number is not just based on index. It should be the second largest number in the array.<\/p>\n<h3 class=\"\" id=\"t-1627633073428\">Question 17: Write a Java Program to print the first non repeating character in a given String.<\/h3>\n<p><strong>Solution 17: <\/strong>In the above problem, to find the first non repeating character, first we create a hashmap object and store each character as key and its respective count as values.<\/p>\n<p>Then we run a for loop, and check the value of each character is equal to 1. If it matches then that is the first non repeating character.<\/p>\n<p>Below is the java program:<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af688c423\" style=\"\">\n<p data-css=\"tve-u-17af689061f\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.HashMap;<\/p>\n<p>public class FirstNonRepeatingCharacterinString {<\/p>\n<p>public static char firstNonRepeat(String s) {<\/p>\n<p>HashMap&lt;Character, Integer&gt; char_count = new HashMap&lt;&gt;();<br \/>for(int i = 0; i &lt;s.length(); i++) {<br \/>char c = s.charAt(i);<br \/>if(char_count.containsKey(c)) {<br \/>char_count.put(c, char_count.get(c) + 1);<br \/>} else {<br \/>char_count.put(c, 1);<br \/>}<br \/>}<\/p>\n<p>for(int i = 0; i &lt; s.length(); i++) {<br \/>char c = s.charAt(i);<br \/>if(char_count.get(c)== 1) <br \/> return c;<br \/>}<br \/>return &#8216;_&#8217;;<\/p>\n<p>}<\/p>\n<p>public static void main(String[] args) {<br \/>System.out.println(firstNonRepeat(&#8220;GeeksForGeeks&#8221;));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>In the above program, the output is F.<\/p>\n<h3 class=\"\" id=\"t-1627633073429\">Question 18: Write a Java Program to print the maximum consecutive ones in a given array.<\/h3>\n<p><strong>Solution 18: <\/strong>In the above problem, we need to find the maximum consecutive ones in a given array.<br \/>To solve this, we run a for loop and check each index if it is equal to 1, we increase the counter. Then we use the Math.max() to find out the max counter value.<\/p>\n<p>Below is the Java Program:<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af68a465b\" style=\"\">\n<p data-css=\"tve-u-17af68a7b8f\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class MaxConsecitivesOnesExample {<\/p>\n<p>static void findConsectiveOnes(int[] nums) {<\/p>\n<p>int count = 0;<br \/>int max = 0;<\/p>\n<p>for (int i = 0; i &lt; nums.length; i++) {<br \/>while( i &lt; nums.length &amp;&amp; nums[i] ==1) {<br \/>count++;<br \/>i++;<br \/>}<\/p>\n<p>if(max&lt; count) {<br \/>max = count;<br \/>}<\/p>\n<p>count=0;<\/p>\n<p>}<br \/>System.out.println(max);<br \/>}<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int[] array = { 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1 };<br \/>findConsectiveOnes(array);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>In the above program the output would be 5.<\/p>\n<h3 class=\"\" id=\"t-1627633073430\">Question 19: Write a Java Program to print the first and last position of a number in a given array.<\/h3>\n<p><strong>Solution 19: <\/strong>In this problem, we have been given a target value(in this case it is 8) and we need to find its first and last position of the target value.<\/p>\n<p>Below is the java program :<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af68bb298\" style=\"\">\n<p data-css=\"tve-u-17af68be964\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class FirstAndLastPositionExample {<\/p>\n<p> public static void main(String[] args) {<\/p>\n<p>int[] array = {5,7,7,8,8,10};<\/p>\n<p>int firstIndex = -1;<br \/>int lastIndex = -1;<\/p>\n<p>int target =8;<\/p>\n<p>for(int i =0; i &lt; array.length; i++) {<br \/>if(array[i] == target &amp;&amp; firstIndex == -1) {<br \/>firstIndex = i;<br \/>}<\/p>\n<p>if(array[i]== target &amp;&amp; firstIndex != -1) {<br \/>lastIndex = i;<br \/>}<br \/>}<\/p>\n<p>System.out.println(&#8220;First Index is: &#8221; + firstIndex + &#8221; Last Index is: &#8221; + lastIndex);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1627633073431\">Question 20: Write a Java Program to print numbers 1 to 100 without using any loop(for\/while\/do-while).<\/h3>\n<p><strong>Solution 20: <\/strong>In this problem, to print the numbers between 1 to 100 we will use the if statement and check the condition.<\/p>\n<p>We will check the condition i.e. num value should be less than equal to 100 and then increment it till it reaches to 100 and thus printing each number.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af68cf0c6\" style=\"\">\n<p data-css=\"tve-u-17af68d29d8\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class PrintNumWithoutUsingLoopExample {<\/p>\n<p>public static void main(String[] args) {<br \/>printnum(1);<\/p>\n<p>}<\/p>\n<p>public static void printnum(int num) {<br \/>if (num &lt;= 100) {<br \/>System.out.println(num);<br \/>num++;<br \/>printnum(num);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1627633073432\">Question 21: Write a Java Program to shift all zeros on the right.<\/h3>\n<p><strong>Solution 21: <\/strong>In this Java Program, we create a new array of the same length as the given array and assign all the non zero values in the new array.<\/p>\n<p>Below is the Java Program:<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af68dccd5\" style=\"\">\n<p data-css=\"tve-u-17af68e0588\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.Arrays;<\/p>\n<p>public class ShiftAllZerosRightExample {<\/p>\n<p>public static int[] shiftAllZerosRight(int[] a) {<br \/>if (a.length == 1) {<br \/>return a;<br \/>}<\/p>\n<p>int[] newArray = new int[a.length];<\/p>\n<p>int count = 0;<br \/>for (int number : a) {<br \/>if (number != 0) {<br \/>newArray[count] = number;<br \/>count++;<\/p>\n<p>}<br \/>}<br \/>return newArray;<\/p>\n<p>}<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int[] inputArray = { 1, 0, 2, 0, 3, 0, 0, 0 };<\/p>\n<p>System.out.println(Arrays.toString(shiftAllZerosRight(inputArray)));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1627633073433\">Question 22: Write a Java Program to remove the duplicates from an array.<\/h3>\n<p><strong>Solution 22:<\/strong> In this problem, we need to find the total count of unique elements. We iterate through the array and check if the adjacent elements are not duplicate. <\/p>\n<p>Below is the Java Program:<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af68e9ab8\" style=\"\">\n<p data-css=\"tve-u-17af68ed2f5\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.Arrays;<\/p>\n<p>public class RemoveDuplicateElementsExample {<\/p>\n<p>static int removeDupElements(int[] arr) {<br \/>Arrays.sort(arr);<br \/>if (arr.length == 0)<br \/>return 0;<br \/>int j = 0;<br \/>for (int i = 1; i &lt; arr.length; i++) {<br \/>if (arr[i] != arr[j]) {<br \/>j++;<br \/>arr[j] = arr[i];<br \/>}<br \/>}<\/p>\n<p>return j + 1;<br \/>}<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int[] array = { 3, 2, 1, 1, 2, 2, 3, 2, 1, 3 };<br \/>System.out.println(removeDupElements(array));<br \/>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>In the above program, the unique elements are 1,2,3. So the total count is 3.<\/p>\n<h3 class=\"\" id=\"t-1627633073434\">Question 23: Write a Java Program to iterate through the HashMap.<\/h3>\n<p><strong>Solution 23:<\/strong> There are multiple ways to iterate through the HashMap. We can either use the keySet() or entrySet() and then using the iterator we can iterate through the given HashMap.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af6906acc\" style=\"\">\n<p data-css=\"tve-u-17af690993f\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.HashMap;<br \/>import java.util.Iterator;<br \/>import java.util.Map.Entry;<\/p>\n<p>public class HashMapIterateExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>HashMap&lt;String, String&gt; hash_map = new HashMap&lt;&gt;();<\/p>\n<p>hash_map.put(&#8220;India&#8221;, &#8220;New Delhi&#8221;);<br \/>hash_map.put(&#8220;France&#8221;, &#8220;Paris&#8221;);<br \/>hash_map.put(&#8220;Germany&#8221;, &#8220;Berlin&#8221;);<br \/>hash_map.put(&#8220;Australia&#8221;, &#8220;Canberra&#8221;);<\/p>\n<p>\/\/ Iterator : Iterate by using keySet()<br \/>Iterator&lt;String&gt; it = hash_map.keySet().iterator();<\/p>\n<p>while (it.hasNext()) {<br \/>String key = it.next();<br \/>String value = hash_map.get(key);<br \/>System.out.println(&#8220;key: &#8221; + key + &#8220;==&gt;&#8221; + &#8220;value: &#8221; + value);<br \/>}<\/p>\n<p>System.out.println(&#8220;**********************************&#8221;);<\/p>\n<p>\/\/ Iterate by using entrySet()<\/p>\n<p>Iterator&lt;Entry&lt;String, String&gt;&gt; map_entry = hash_map.entrySet().iterator();<\/p>\n<p>while (map_entry.hasNext()) {<br \/>Entry&lt;String, String&gt; entry = map_entry.next();<br \/>System.out.println(&#8220;key: &#8221; + entry.getKey() + &#8220;==&gt;&#8221; + entry.getValue());<br \/>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1627633073435\">Question 24: Write a Java Program to convert HashMap to ArrayList.<\/h3>\n<p><strong>Solution 24:<\/strong> To solve this problem, we can convert the HashMap keys into an ArrayList and then the HashMap values into an ArrayList.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af6912a1f\" style=\"\">\n<p data-css=\"tve-u-17af6916485\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.ArrayList;<br \/>import java.util.HashMap;<br \/>import java.util.List;<\/p>\n<p>public class HashMapToArrayListExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>HashMap&lt;String, String&gt; hash_map = new HashMap&lt;&gt;();<\/p>\n<p>hash_map.put(&#8220;India&#8221;, &#8220;New Delhi&#8221;);<br \/>hash_map.put(&#8220;France&#8221;, &#8220;Paris&#8221;);<br \/>hash_map.put(&#8220;Germany&#8221;, &#8220;Berlin&#8221;);<br \/>hash_map.put(&#8220;Australia&#8221;, &#8220;Canberra&#8221;);<\/p>\n<p>\/\/ convert hashmap keys to ArrayList<\/p>\n<p>List&lt;String&gt; countryNameList = new ArrayList&lt;&gt;(hash_map.keySet());<br \/>System.out.println(&#8220;HashMap keys are: &#8220;);<br \/>for (String s : countryNameList) {<br \/>System.out.println(s);<br \/>}<\/p>\n<p>System.out.println(&#8220;************************&#8221;);<\/p>\n<p>\/\/ convert hashmap values to ArrayList<br \/>List&lt;String&gt; capitalNameList = new ArrayList&lt;String&gt;(hash_map.values());<br \/>System.out.println(&#8220;HashMap values are: &#8220;);<br \/>for (String s : capitalNameList) {<br \/>System.out.println(s);<br \/>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1627633073436\">Question 25: Write a Java Program to print the missing number in a given Array.<\/h3>\n<p><strong>Solution 25: <\/strong>To solve this problem, we first add the elements of the given array(sum1) and then add the range of elements of the array(sum2).<br \/>Then we subtract sum2 &#8211; sum1 which would give us the missing number in the given array.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17af6921c97\" style=\"\">\n<p data-css=\"tve-u-17af692538c\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class MissingNumberInArrayExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int[] arr = { 1, 2, 3, 4, 5, 7, 8, 9, 10 };<\/p>\n<p>int sum1 = 0;<\/p>\n<p>for (int i = 0; i &lt; arr.length; i++) {<br \/>sum1 = sum1 + arr[i];<br \/>}<\/p>\n<p>int sum2 = 0;<br \/>for (int i = 1; i &lt;= 10; i++) {<br \/>sum2 = sum2 + i;<br \/>}<\/p>\n<p>System.out.println(&#8220;Missing number in Array: &#8221; + (sum2 &#8211; sum1));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents Question 13: Write a Java Program to generate Output &#8220;aabbbcccc&#8221; with the input &#8220;a2b3c4&#8221; Question 14: Write a Java Program to print the product of an array except self? Question 15: Write a Java Program to reverse a number using Stack. Question 16:\u00a0 Write a Java Program to print the second largest [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-1002","post","type-post","status-publish","format-standard","hentry","category-interview-questions","category-java","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1002"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1002\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}