0% found this document useful (0 votes)
84 views4 pages

Java Interview Programs for LTIMindtree

Uploaded by

woleni8582
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views4 pages

Java Interview Programs for LTIMindtree

Uploaded by

woleni8582
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Comprehensive Java Programs for LTIMindtree

Technical Interview

1. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "HelloWorld";
String reversed = new StringBuilder(str).reverse().toString();
[Link]("Reversed: " + reversed);
}
}

2. Check Palindrome String


public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
[Link]([Link](rev) ? "Palindrome" : "Not Palindrome");
}
}

3. Factorial Using Recursion


public class FactorialRecursion {
static int factorial(int n) {
return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}
public static void main(String[] args) {
int num = 5;
[Link]("Factorial: " + factorial(num));
}
}

4. Fibonacci Series
public class FibonacciSeries {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
[Link](a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
[Link](" " + c);
a = b;
b = c;
}
}
}

5. Prime Number Check


public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean prime = true;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
prime = false;
break;
}
}
[Link](prime ? "Prime" : "Not Prime");
}
}

6. Armstrong Number
public class Armstrong {
public static void main(String[] args) {
int num = 153, sum = 0, temp = num;
while (num > 0) {
int digit = num % 10;
sum += [Link](digit, 3);
num /= 10;
}
[Link](sum == temp ? "Armstrong" : "Not Armstrong");
}
}

7. Swap Two Numbers Without Temp Variable


public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
[Link]("a = " + a + ", b = " + b);
}
}

8. Find Largest and Smallest Element in Array


public class LargestSmallest {
public static void main(String[] args) {
int[] arr = {12, 45, 2, 67, 33};
int max = arr[0], min = arr[0];
for (int num : arr) {
if (num > max) max = num;
if (num < min) min = num;
}
[Link]("Max: " + max + ", Min: " + min);
}
}

9. Sort an Array
import [Link];
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 9};
[Link](arr);
[Link]([Link](arr));
}
}

10. Remove Duplicates from Array


import [Link].*;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 5};
Set<Integer> set = new LinkedHashSet<>();
for (int i : arr) [Link](i);
[Link](set);
}
}

11. Count Occurrence of Character in String


public class CharCount {
public static void main(String[] args) {
String str = "programming";
char c = 'g';
long count = [Link]().filter(ch -> ch == c).count();
[Link]("Count of " + c + ": " + count);
}
}

12. Check Even or Odd


public class EvenOdd {
public static void main(String[] args) {
int n = 7;
[Link](n % 2 == 0 ? "Even" : "Odd");
}
}

13. Check Leap Year


public class LeapYear {
public static void main(String[] args) {
int year = 2024;
boolean leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
[Link](leap ? "Leap Year" : "Not Leap Year");
}
}

14. Reverse an Integer


public class ReverseInteger {
public static void main(String[] args) {
int num = 1234, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
[Link]("Reversed: " + rev);
}
}

15. Check String Anagram


import [Link];
public class AnagramCheck {
public static void main(String[] args) {
String s1 = "listen", s2 = "silent";
char[] a = [Link]();
char[] b = [Link]();
[Link](a);
[Link](b);
[Link]([Link](a, b) ? "Anagram" : "Not Anagram");
}
}

16. Count Vowels and Consonants


public class VowelConsonantCount {
public static void main(String[] args) {
String str = "HelloWorld";
int vowels = 0, consonants = 0;
str = [Link]();
for (char c : [Link]()) {
if ("aeiou".indexOf(c) != -1) vowels++;
else if ([Link](c)) consonants++;
}
[Link]("Vowels: " + vowels + ", Consonants: " + consonants);
}
}

17. Find Missing Number in Array


public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6};
int n = 6;
int sum = n * (n + 1) / 2;
for (int i : arr) sum -= i;
[Link]("Missing Number: " + sum);
}
}

18. Print Pattern (Right Triangle)


public class Pattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) [Link]("*");
[Link]();
}
}
}

19. Demonstrate Inheritance


class Animal {
void eat() { [Link]("Eating"); }
}
class Dog extends Animal {
void bark() { [Link]("Barking"); }
}
public class InheritanceExample {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}

20. Constructor Example


public class ConstructorExample {
String name;
ConstructorExample(String name) {
[Link] = name;
}
public static void main(String[] args) {
ConstructorExample obj = new ConstructorExample("LTIMindtree");
[Link]("Hello, " + [Link]);
}
}

Common questions

Powered by AI

The algorithm counts occurrences of a specific character in a string using Java Streams. It converts the string into an IntStream of characters and filters those matching the target character, then counts the matches. This stream processing method is efficient due to its declarative nature, allowing precise and concise data processing and parallell optimization .

The technique used to remove duplicates from an array involves using a LinkedHashSet to store elements. This data structure inherently ensures that only unique elements are retained and maintains the insertion order. The array elements are iterated over and added to the LinkedHashSet, automatically filtering out duplicates .

The algorithm checks if a number is prime by iterating from 2 to the square root of the number and checking for divisibility. It is efficient because it reduces the number of iterations necessary to find factors, taking advantage of the property that a larger factor of a number must be paired with a smaller factor less than or equal to the square root .

The document determines leap years using a boolean condition that checks if a year is evenly divisible by 4, not evenly divisible by 100, or evenly divisible by 400. This conditional logic aligns with the Gregorian calendar rules for leap years, ensuring correct identification by addressing exceptions that occur every 100 years .

Inheritance is demonstrated through a class 'Dog' extending another class 'Animal.' The purpose is to allow the 'Dog' class to reuse and extend the functionality of the 'Animal' class, promoting code reuse and a hierarchical structuring of classes. It allows 'Dog' to access methods of 'Animal,' like 'eat,' along with its own methods like 'bark' .

To detect an Armstrong number, the algorithm calculates the sum of each digit raised to the power of three (since it checks for a three-digit number) and compares this sum to the original number. The fundamental dependency of the algorithm is on the definition of an Armstrong number, where the sum of the nth powers of its digits equals the number itself .

The code calculates the factorial of a number using recursion by defining a method that calls itself with the value decremented by one. The base case used is when the number is 0 or 1, returning 1 in these cases to terminate the recursion and provide a value for further multiplications up the call stack .

The process to identify a missing number in a sequence involves calculating the expected sum of the entire sequence using the formula for the sum of an arithmetic series, and then subtracting the sum of the provided sequence. The mathematical concept it relies on is the sum formula for a series, n(n + 1)/2, which represents the sum of the first n natural numbers .

The code's efficiency in swapping two numbers without a temporary variable leverages three arithmetic operations: addition and subtraction. This works based on arithmetic properties, where adding and then subtracting values provides a means of swapping without external storage; however, it assumes no integer overflow .

The code checks whether a given string is a palindrome by reversing the string and comparing it to the original. It uses a StringBuilder to reverse the string and the equals method to check equality. The major criterion it relies on is that a palindrome reads the same forward and backward, making the reversed string identical to the original if it is a palindrome .

You might also like