Java Coding Interview Questions - MNCs
Number/Logic-Based Questions
> Check if a number is Prime
int n = 7;
boolean prime = n > 1;
for(int i = 2; i <= [Link](n); i++)
if(n % i == 0) prime = false;
[Link](prime);
> Find the factorial of a number
int fact = 1, n = 5;
for(int i = 1; i <= n; i++) fact *= i;
[Link]("Factorial: " + fact);
String-Based Questions
> Reverse a string without using built-in functions
String s = "hello";
String rev = "";
for(int i = [Link]()-1; i >= 0; i--) rev += [Link](i);
[Link](rev);
> Check if two strings are Anagrams
String a = "listen", b = "silent";
char[] c1 = [Link](), c2 = [Link]();
[Link](c1);
[Link](c2);
[Link]([Link](c1, c2));
Array-Based Questions
> Find the largest element in an array
int[] arr = {1, 4, 2, 9};
int max = arr[0];
for(int i : arr) if(i > max) max = i;
[Link]("Max: " + max);
> Reverse an array
int[] arr = {1, 2, 3, 4};
for(int i = [Link] - 1; i >= 0; i--) [Link](arr[i] + " ");
Pattern Printing
> Print a right-angled triangle
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) [Link]("* ");
[Link]();
}
Recursion-Based Questions
> Factorial using recursion
int fact(int n) {
if(n == 0) return 1;
return n * fact(n-1);
}
Quiz/MCQ Type (Theory/Output Based)
> What is the output of this code?
int x = 10;
[Link](x++ + ++x);
Answer: 10 + 12 = 22
Explanation: x++ is 10, ++x becomes 12
> String s1 = "hello";
String s2 = new String("hello");
[Link](s1 == s2);
Answer: false
Explanation: == compares references, not values.