{"id":994,"date":"2021-07-29T19:29:37","date_gmt":"2021-07-29T19:29:37","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/07\/29\/25-java-programming-interview-questions-part-1\/"},"modified":"2021-07-29T19:29:37","modified_gmt":"2021-07-29T19:29:37","slug":"25-java-programming-interview-questions-part-1","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/07\/29\/25-java-programming-interview-questions-part-1\/","title":{"rendered":"25 Java Programming Interview Questions \u2013 Part 1"},"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=\"994\" 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-17af3accae7\" data-ct=\"toc-60671\" data-ct-name=\"Table of Contents 03\" data-element-name=\"Table of Contents\" data-headers=\"h2,h3,h4\" data-heading-style='{\"0\":\"tve-u-17af3accaec\",\"1\":\"tve-u-17af3accaee\",\"2\":\"tve-u-17af3accaef\"}' data-highlight=\"section\" data-number-style='{\"0\":\"tve-u-17af3accaed\",\"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-17af3accae8\" 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-17af3accae9\" 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-17af3accaea\" 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-17af3accaeb\" 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-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706406\" jump-animation=\"smooth\">Question 1: Write a Java Program to reverse a String.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706407\" jump-animation=\"smooth\">Question 2:\u00a0 Write a Java Program to swap two given Strings<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706408\" jump-animation=\"smooth\">Question 3: Write a Java Program to print the Fibonacci Series.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706409\" jump-animation=\"smooth\">Question 4: Write a Java Program to check if a number is Armstrong number or not.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706410\" jump-animation=\"smooth\">Question 5: Write a Java Program to print the sum of digits of a given integer.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706411\" jump-animation=\"smooth\">Question 6: Write a Java Program to check if a number is Prime or not.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706412\" jump-animation=\"smooth\">Question 7:\u00a0 Write a Java Program to count the number of characters 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-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706413\" jump-animation=\"smooth\">Question 8: Write a Java Program to find the duplicates of a given String.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706414\" jump-animation=\"smooth\">Question 9: Write a Java Program for swapping of 2 numbers.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706415\" jump-animation=\"smooth\">Question 10: Write a Java Program to reverse an array?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706416\" jump-animation=\"smooth\">Question 11: Write a Java Program to get the count of Capitalized words in a String.<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17af3accaee\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1627585706417\" jump-animation=\"smooth\">Question 12: Write a Java Program to find the longest consecutive occurrence of integers 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-17af3accae8\" 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<h3 class=\"\" id=\"t-1627585706406\">Question 1: Write a Java Program to reverse a String.<\/h3>\n<p><strong>Solution 1: <\/strong>As asked in the above question, we need to reverse a String. <strong>For Ex:<\/strong><br \/> The Input is : &#8220;RahulShettyAcademy&#8221;<br \/>Output should be : &#8220;ymedacAyttehSluhaR&#8221;<\/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<p>This problem can be solved by writing a simple for loop and print the string in the reverse.<\/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-17af3ae29c4\" style=\"\">\n<p data-css=\"tve-u-17af3af2bec\" style=\"\">public class ReverseAStringExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>String inputString = &#8220;RahulShettyAcademy&#8221;;<br \/>String outputString = &#8220;&#8221;;<\/p>\n<p>\/*<br \/>* Writing a for loop and looping in the reverse order to get the reverse String<br \/>* as output.<br \/>*\/<\/p>\n<p>for (int i = inputString.length() &#8211; 1; i &gt;= 0; i&#8211;) {<br \/>outputString = outputString + inputString.charAt(i);<br \/>}<\/p>\n<p>System.out.println(outputString);<br \/>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>This will print the outputString which will be the reverse of the given inputString.<\/p>\n<h3 class=\"\" id=\"t-1627585706407\">Question 2:\u00a0 Write a Java Program to swap two given Strings<\/h3>\n<p><strong>Solution 2.<\/strong> As asked in the above question, for swapping of two strings we need to use the substring method of the String class in Java.<br \/> Substring method returns part of the string. <\/p>\n<p>We have the start index and end index in the substring method where <br \/> start index is inclusive and end index is exclusive.<\/p>\n<p>Below is the Java Program to swap two Strings.<\/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-17af3b0ab56\" style=\"\">\n<p data-css=\"tve-u-17af3b0dcf8\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class StringSwapExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>\/\/ Take two string s1 and s2 which we need to swap.<br \/>String s1 = &#8220;Rahul&#8221;;<br \/>String s2 = &#8220;Shetty&#8221;;<\/p>\n<p>\/\/ Combine both the strings s1 and s2 using the concatenation(+) operator<br \/>s1 = s1 + s2;<\/p>\n<p>\/\/ Use the substring method to get the subset of the combined string<br \/>s2 = s1.substring(0, s1.length() &#8211; s2.length());<br \/>s1 = s1.substring(s2.length());<\/p>\n<p>System.out.println(&#8220;s1 =&#8221; + s1);<br \/>System.out.println(&#8220;s2 =&#8221; + s2);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The above program will swap the two Strings s1 and s2.<\/p>\n<h3 class=\"\" id=\"t-1627585706408\">Question 3: Write a Java Program to print the Fibonacci Series.<\/h3>\n<p><strong>Solution 3.<\/strong> As we know Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers.<\/p>\n<p>For writing a Java Program, first we declare an array and assign the values 0 and 1 to the indexes 0 and 1 of the defined array. <br \/> Then we write the logic to get the values of all the indexes of the total Fibonacci series length.<\/p>\n<p>Below the java program to print the Fibonacci series.<\/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-17af3b1cf1d\" style=\"\">\n<p data-css=\"tve-u-17af3b23bdb\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.Scanner;<\/p>\n<p>public class FibonacciSeriesExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int fiblength;<\/p>\n<p>\/\/ Use scanner class to get the user input.<br \/>Scanner sc = new Scanner(System.in);<br \/>System.out.println(&#8220;Enter the number : &#8220;);<br \/>fiblength = sc.nextInt();<\/p>\n<p>int[] num = new int[fiblength];<br \/>\/\/ Assign values 0 and 1 to the 0 and 1 indices of the defined array<br \/>num[0] = 0;<br \/>num[1] = 1;<\/p>\n<p>\/\/ Logic for getting the values of all the indices of the array using for loop<br \/>for (int i = 2; i &lt; fiblength; i++) {<br \/>num[i] = num[i &#8211; 1] + num[i &#8211; 2];<br \/>}<\/p>\n<p>System.out.print(&#8220;fibonacci series: &#8220;);<br \/>\/\/ Printing the fibonacci series<br \/>for (int i = 0; i &lt; fiblength; i++) {<br \/>System.out.print(num[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<p>The above program will print the Fibonacci series upto the total length i.e. fiblength entered by the user.<\/p>\n<h3 class=\"\" id=\"t-1627585706409\">Question 4: Write a Java Program to check if a number is Armstrong number or not.<\/h3>\n<p><strong>Solution 4:<\/strong> An Armstrong Number is a number in which the sum of the cubes of each digit is equal to the number itself. For ex:<\/p>\n<p>\u00a0<em><strong>371 = 3*3*3 + 7*7*7 + 1*1*1<\/strong><\/em><\/p>\n<p>The sum of the cubes is 3,7,1 is equal to 371.Another example is 153 which is an Armstrong number. You can explore other numbers too!!!<\/p>\n<p>Below is the Java Program to check the number is Armstrong or not.<\/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-17af3b3711e\" style=\"\">\n<p data-css=\"tve-u-17af3b3aa45\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class CheckArmstrongNumberExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int num = 371;<br \/>int actualnum = num;<br \/>double result = 0;<\/p>\n<p>while (actualnum != 0) {<br \/>int n = actualnum % 10;<br \/>result = result + Math.pow(n, 3);<br \/>actualnum = actualnum \/ 10;<br \/>}<\/p>\n<p>if (result == num) {<br \/>System.out.println(num + &#8221; is an Armstrong number&#8221;);<br \/>} else {<br \/>System.out.println(num + &#8221; is not an Armstrong number&#8221;);<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 above program would give the output whether a number is Armstrong number.<\/p>\n<h3 class=\"\" id=\"t-1627585706410\">Question 5: Write a Java Program to print the sum of digits of a given integer.<\/h3>\n<p><strong>Solution 5:<\/strong> We can solve the question using a while loop. Using the % operator we get the remainder and keep adding to the sum variable until the number becomes 0. The total sum will be the total of all the digits of the given integer.<\/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-17af3b5af9f\" style=\"\">\n<p data-css=\"tve-u-17af3b62050\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class SumDigitsExample {<\/p>\n<p>public static int getSumOfAllDigits(int num) {<\/p>\n<p>\/\/ Declare variable sum which will store the sum of all digits.<br \/>int sum = 0;<\/p>\n<p>\/\/ Run a while loop until the num becomes 0.<br \/>while (num != 0) {<br \/>int rem = num % 10;<br \/>sum = sum + rem;<br \/>num = num \/ 10;<br \/>}<br \/>return sum;<\/p>\n<p>}<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>\/\/ We can pass any integer value of whose sum of the digits is required.<br \/>System.out.println(getSumOfAllDigits(549));<br \/>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The above program will print the sum of all digits in the console. In the above case, as the input value is 549<br \/> the output returned will be 18.<\/p>\n<h3 class=\"\" id=\"t-1627585706411\">Question 6: Write a Java Program to check if a number is Prime or not.<\/h3>\n<p><strong>Solution 6. <\/strong>A prime number is a number which is divisible by 1 and itself. This problem can be solved by a simple for loop and we check the condition that the given number returns a remainder value.<\/p>\n<p>If the remainder is 0, then the number is not prime. Else its a prime number.<\/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-17af3b76eeb\" style=\"\">\n<p data-css=\"tve-u-17af3b7d5fd\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class PrimeNumberExample {<\/p>\n<p>public static boolean isPrime(int num) {<br \/>\/\/ Validate that the number is neither less than 1 nor equal to 1.<br \/>\/\/ It should return false as it cannot be a prime number<br \/>if (num &lt;= 1)<br \/>return false;<\/p>\n<p>\/\/ Logic to check if a number is prime or not. If its Prime, returns true else<br \/>\/\/ return false<br \/>for (int i = 2; i &lt; num; i++) {<br \/>if (num % i == 0) {<br \/>return false;<br \/>}<br \/>}<\/p>\n<p>return true;<\/p>\n<p>}<\/p>\n<p>public static void main(String[] args) {<br \/>\/\/ Pass the integer value to check whether it is prime or not.<br \/>System.out.println(isPrime(8));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The above program will return false as 8 is not a prime number.<\/p>\n<h3 class=\"\" id=\"t-1627585706412\">Question 7:\u00a0 Write a Java Program to count the number of characters in a given String.<\/h3>\n<p><strong>Solution 7: <\/strong>We can use hashmap for counting the number of each character in the given string.<br \/>We create hashmap object to store each character as key and the total count of characters as its value.<\/p>\n<p>Then we print the hashmap object.<\/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-17af3bacb6f\" style=\"\">\n<p data-css=\"tve-u-17af3bb2748\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.HashMap;<\/p>\n<p>public class CountNumOfCharsInStringExample {<\/p>\n<p>static void characterCount(String inputString) {<\/p>\n<p>\/\/ Creating a hashmap object.<br \/>HashMap&lt;Character, Integer&gt; hash_map = new HashMap&lt;&gt;();<br \/>char[] strArray = inputString.toCharArray();<\/p>\n<p>for (char c : strArray) {<br \/>if (hash_map.containsKey(c)) {<br \/>hash_map.put(c, hash_map.get(c) + 1);<br \/>}<\/p>\n<p>else {<br \/>hash_map.put(c, 1);<br \/>}<\/p>\n<p>}<br \/>\/\/ Print the hashmap object which gives the number of each character in String.<br \/>System.out.println(hash_map);<\/p>\n<p>}<\/p>\n<p>public static void main(String[] args) {<br \/>\/\/ Input value which needs to be passed in the below method.<br \/>characterCount(&#8220;rahulshettyacademy&#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 as we are printing the hashmap object, it will print each character with the total number of count in the console.<\/p>\n<h3 class=\"\" id=\"t-1627585706413\">Question 8: Write a Java Program to find the duplicates of a given String.<\/h3>\n<p><strong>Solution 8: <\/strong>HashMap is used to store elements in the form of key value pairs. We create a hashmap object and store each character as key <br \/> and the total count of each character as value.<\/p>\n<p>Then we check the keys whose values are more than 1 using the keySet() method. Thus we get the duplicates in the given String.<\/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-17af3bc1e50\" style=\"\">\n<p data-css=\"tve-u-17af3bc6a11\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>import java.util.HashMap;<br \/>import java.util.Set;<\/p>\n<p>public class DuplicateCharsInStringExample {<\/p>\n<p>static void duplicateChars(String inputString) {<\/p>\n<p>\/\/ Creating a hashmap object.<br \/>HashMap&lt;Character, Integer&gt; hash_map = new HashMap&lt;&gt;();<br \/>char[] strArray = inputString.toCharArray();<\/p>\n<p>for (char c : strArray) {<br \/>if (hash_map.containsKey(c)) {<br \/>hash_map.put(c, hash_map.get(c) + 1);<br \/>}<\/p>\n<p>else {<br \/>hash_map.put(c, 1);<br \/>}<\/p>\n<p>}<br \/>\/\/ Store the key values in a set and then get the number of each duplicate character.<br \/>Set&lt;Character&gt; keys = hash_map.keySet();<br \/>for (char c : keys) {<br \/>if (hash_map.get(c) &gt; 1) {<br \/>System.out.println(c + &#8220;&#8211;&gt;&#8221; + hash_map.get(c));<br \/>}<br \/>}<\/p>\n<p>}<\/p>\n<p data-css=\"tve-u-17af3bc6a11\" style=\"\">public static void main(String[] args) {<br \/>\/\/ Input value which needs to be passed in the below method.<br \/>duplicateChars(&#8220;rahulshettyacademy&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The above program will print all the duplicates in the given String in the console.<br \/>\u00a0<strong>The output would be :<\/strong>\u00a0<\/p>\n<p>a&#8211;&gt;3<br \/>t&#8211;&gt;2<br \/>e&#8211;&gt;2<br \/>h&#8211;&gt;2<br \/>y&#8211;&gt;2<\/p>\n<h3 class=\"\">Question 9: Write a Java Program for swapping of 2 numbers.<\/h3>\n<p><strong>Solution 9:<\/strong> In this problem , we would be given two numbers as input. We need to swap those 2 numbers which can be done a simple logic. <\/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-17af3c1f428\" style=\"\">\n<p data-css=\"tve-u-17af3c22e5f\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class SwapNumbersExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int num1 = 10;<br \/>int num2 = 20;<\/p>\n<p>\/\/ Logic to swap 2 numbers.<br \/>num1 = num1 + num2;<br \/>num2 = num1 &#8211; num2;<br \/>num1 = num1 &#8211; num2;<\/p>\n<p>System.out.println(&#8220;num1 = &#8221; + num1);<br \/>System.out.println(&#8220;num2 = &#8221; + num2);<br \/>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The above program would swap the two integer values num1 and num2.<\/p>\n<h3 class=\"\">Question 10: Write a Java Program to reverse an array?<\/h3>\n<p><strong>Solution 10:<\/strong> In this problem, we are given an array of characters as input. This problem can be solved by writing a while loop.<\/p>\n<p>By using a third variable, we reverse the array as below.<\/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-17af3c329bb\" style=\"\">\n<p data-css=\"tve-u-17af3c35102\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class ReverseAnArray {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>char[] s = { &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, &#8216;e&#8217; };<\/p>\n<p>int right = s.length &#8211; 1;<br \/>int left = 0;<\/p>\n<p>while (left &lt; right) {<br \/>char c = s[left];<br \/>s[left] = s[right];<br \/>s[right] = c;<\/p>\n<p>\/\/ Increment the left by 1 and Decrement the right by 1<br \/>left += 1;<br \/>right -= 1;<br \/>}<\/p>\n<p>for (int i = 0; i &lt; s.length; i++) {<br \/>System.out.print(s[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<p>The above program gives the output: e d c b a. Thus reverse the array.<\/p>\n<h3 class=\"\">Question 11: Write a Java Program to get the count of Capitalized words in a String.<\/h3>\n<p><strong>Solution 11: <\/strong>\u00a0Capitalized words in a given String which are those words which starts with a Capital Letter. <br \/>We need the total count of these words. This problem can be solved by using a for loop and iterate through each character in the String.\u00a0<\/p>\n<p>We also take a counter variable and increment it when any Capital character is encountered.<\/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-17af3c4c584\" style=\"\">\n<p data-css=\"tve-u-17af3c4f1e0\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class CountCapitalizedWordsInString {<\/p>\n<p>public static int getCapsWordsInString(String inputString) {<\/p>\n<p>int counter = 0;<br \/>for(int i = 0; i &lt; inputString.length(); i++) {<br \/>if(inputString.charAt(i) &gt;= &#8216;A&#8217; &amp;&amp; inputString.charAt(i) &lt;= &#8216;Z&#8217;) {<br \/>counter++;<br \/>}<br \/>}<br \/>return counter;<br \/>}<\/p>\n<p>public static void main(String[] args) {<br \/>System.out.println(getCapsWordsInString(&#8220;RahulShettyAcademy&#8221;));<br \/>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The above program will return the total count of Capitalized String i.e. 3.<\/p>\n<h3 class=\"\">Question 12: Write a Java Program to find the longest consecutive occurrence of integers in a given array.<\/h3>\n<p><strong>Solution 12: <\/strong>In this problem, we need to find the length of the longest consecutive occurrence.<br \/>As given in the array below, we have an array of integers. As you can see the longest consecutive occurrence of integers are 6,7,8,9. There is a consecutive increment of 1 . So the output will be 4.<\/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-17af3c62199\" style=\"\">\n<p data-css=\"tve-u-17af3c66a00\" style=\"\">package JavaInterviewQuestions;<\/p>\n<p>public class LongestConsecutiveOccurenceExample {<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>int[] arr = { 4, 3, 25, 6, 7, 8, 9, 2, 3, 10 };<\/p>\n<p>int count = 0;<br \/>int max =0;<\/p>\n<p>for(int i = 0; i &lt; arr.length-1; i++) {<br \/>if(arr[i] +1== arr[i+1]) {<br \/>count++;<br \/>}else {<br \/>count = 0;<br \/>}<\/p>\n<p>max = Math.max(max, count+1);<br \/>}<br \/>System.out.println(max);<br \/>}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The above program will give the output as 4. We use the Math.max() to obtain the maximum value between the max and the counter variable.<\/p>\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 1: Write a Java Program to reverse a String. Question 2:\u00a0 Write a Java Program to swap two given Strings Question 3: Write a Java Program to print the Fibonacci Series. Question 4: Write a Java Program to check if a number is Armstrong number or not. Question 5: Write a [&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-994","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\/994","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=994"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/994\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}