Here is a curated list of Java programming questions asked during the coding rounds of companies like TCS, Infosys, Wipro, Capgemini, and Cognizant. Each problem includes a simple and clean solution that’s ideal for freshers and entry-level roles.
---🧠 1. Reverse Each Word in a Sentence
Company: Infosys
Input: "Java is awesome"
Output: "avaJ si emosewa"
public class ReverseWords {
public static void main(String[] args) {
String input = "Java is awesome";
String[] words = input.split(" ");
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(new StringBuilder(word).reverse()).append(" ");
}
System.out.println(result.toString().trim());
}
}
---
🔢 2. Count Number of Digits, Letters, and Special Characters
Company: Capgemini
Input: "Java123@#!"
Output: Letters=4, Digits=3, Specials=3
public class CountTypes {
public static void main(String[] args) {
String str = "Java123@#!";
int letters = 0, digits = 0, specials = 0;
for (char ch : str.toCharArray()) {
if (Character.isLetter(ch)) letters++;
else if (Character.isDigit(ch)) digits++;
else specials++;
}
System.out.println("Letters=" + letters + ", Digits=" + digits + ", Specials=" + specials);
}
}
---
🔁 3. Print Fibonacci Series up to N Terms
Company: Wipro
Input: 6
Output: 0 1 1 2 3 5
public class FibonacciSeries {
public static void main(String[] args) {
int n = 6, a = 0, b = 1;
System.out.print(a + " " + b + " ");
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
}
}
---
🎯 4. Find Second Highest Number in Array
Company: TCS
Input: [2, 10, 5, 7]
Output: 7
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {2, 10, 5, 7};
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
System.out.println("Second largest: " + second);
}
}
---
✅ 5. Check if a String is Palindrome
Company: Cognizant
Input: "madam"
Output: true
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(reversed));
}
}
---
🧩 6. Print Pattern (Right Angled Triangle)
Company: TCS NQT
Input: 5
Output:
*
**
***
****
*****
public class TrianglePattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("*".repeat(i));
}
}
}
---
💡 7. Count Frequency of Each Character
Company: Infosys
Input: "apple"
Output:
a = 1
p = 2
l = 1
e = 1
import java.util.*;
public class CharFrequency {
public static void main(String[] args) {
String str = "apple";
Map<Character, Integer> map = new LinkedHashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
map.forEach((k, v) -> System.out.println(k + " = " + v));
}
}
---
📌 Conclusion
These questions are commonly asked during Java coding rounds for service-based and product-based companies. Practice writing clean and optimal solutions, and remember to explain your thought process during interviews.
Coming next: “Java Coding Round: Hard Level Challenges Asked in Product Companies”