Skip to main content

Posts

21-50 Java Coding Questions with Answers (With Code Examples)

This collection of  50 Java coding questions   is designed to help you master the basics and build strong problem-solving skills. Whether you’re preparing for a   Java interview , studying for an exam, or simply practicing your logic, this guide is a great companion. Each question comes with: A clear problem statement Humanized logic explanation Java code Output example 🔹 21. Find Duplicate Elements in Array int [] arr = { 1 , 2 , 3 , 2 , 4 , 5 , 1 }; Set<Integer> set = new HashSet <>(); for ( int num : arr) { if (!set.add(num)) { System.out.println( "Duplicate: " + num); } } 🔹 22. Count Occurrence of Each Character String s = "programming" ; Map<Character, Integer> map = new HashMap <>(); for ( char c : s.toCharArray()) { map.put(c, map.getOrDefault(c, 0 ) + 1 ); } System.out.println(map); 🔹 23. Print Star Pyramid int n = 5 ; for ( int i = 1 ; i <= n; i++) { for ( int j = 1 ; j <= i; j++...