[Go to site: main page, start]

0% found this document useful (0 votes)
2 views9 pages

Java Problem

The document contains Java practice questions and solutions focused on arrays, strings, and 2D arrays. It includes various tasks such as summing elements, finding maximum values, counting even and odd numbers, and performing linear searches on arrays, along with string manipulation techniques. Additionally, it provides a list of important questions related to Java strings and their functionalities.

Uploaded by

dwarkat223
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)
2 views9 pages

Java Problem

The document contains Java practice questions and solutions focused on arrays, strings, and 2D arrays. It includes various tasks such as summing elements, finding maximum values, counting even and odd numbers, and performing linear searches on arrays, along with string manipulation techniques. Additionally, it provides a list of important questions related to Java strings and their functionalities.

Uploaded by

dwarkat223
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

Java Array Practice Questions with Solutions

Q1. Sum of All Elements

Ek array lo user se input me, aur uske sare elements ka sum nikaalo.
import [Link];

public class SumArray {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of array: ");
int n = [Link]();
int[] arr = new int[n];
int sum = 0;

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
sum += arr[i];
}
[Link]("Sum = " + sum);
}
}

Q2. Find Maximum Element

Array me sabse bada element kaunsa hai, wo print karo.


import [Link];

public class MaxElement {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of array: ");
int n = [Link]();
int[] arr = new int[n];

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

int max = arr[0];


for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
[Link]("Maximum element: " + max);
}
}
Q3. Count Even and Odd Numbers

Array ke andar kitne even aur odd numbers hain, uska count nikaalo.
import [Link];

public class EvenOddCount {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of array: ");
int n = [Link]();
int[] arr = new int[n];
int even = 0, odd = 0;

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
if (arr[i] % 2 == 0)
even++;
else
odd++;
}

[Link]("Even: " + even + ", Odd: " + odd);


}
}

Q4. Linear Search

User se ek number lo, aur check karo wo number array me hai ya nahi.
import [Link];

public class LinearSearch {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of array: ");
int n = [Link]();
int[] arr = new int[n];

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

[Link]("Enter number to search: ");


int x = [Link]();

boolean found = false;


for (int val : arr) {
if (val == x) {
found = true;
break;
}
}

if (found)
[Link](x + " is present in the array.");
else
[Link](x + " is not present in the array.");
}
}

Q5. Reverse the Array

Array ke elements ko reverse order me print karo.


import [Link];

public class ReverseArray {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of array: ");
int n = [Link]();
int[] arr = new int[n];

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

[Link]("Reversed array:");
for (int i = n - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
}
}
Java DSA Placement Sheet - Arrays, Strings, and 2D Arrays

I. Arrays (20 Questions)

1. Find the maximum and minimum element in an array

2. Reverse an array in place

3. Find the 'Kth' max and min element of an array

4. Sort an array of 0s, 1s and 2s (Dutch National Flag Problem)

5. Move all negative numbers to beginning and positive to end

6. Find Union and Intersection of two arrays

7. Cyclically rotate an array by one

8. Kadane's Algorithm - Maximum Subarray Sum

9. Check if array is sorted and rotated

10. Leaders in an array

11. Rearrange array in alternating positive & negative items

12. Count the number of occurrences of an element

13. Find all pairs with a given sum

14. Subarray with given sum (Two pointer / Sliding window)

15. Missing number in array [1 to n]

16. Find duplicate number in array

17. Find intersection of two sorted arrays

18. Trapping Rain Water

19. Merge two sorted arrays without using extra space

20. Maximum Product Subarray

II. Strings (15 Questions)

21. Reverse a string

22. Check for palindrome


23. Remove duplicates from a string

24. Print all permutations of a string

25. Check if two strings are anagrams

26. Count and say problem

27. Longest common prefix

28. Convert string to integer (like atoi)

29. Implement strstr() (substring search)

30. Valid Palindrome after removing at most one character

31. Compress the string (like Leetcode 443)

32. Longest substring without repeating characters

33. Group anagrams together

34. Check if a string is a rotation of another string

35. Check if one string is a subsequence of another

III. 2D Arrays (15 Questions)

36. Transpose of a matrix

37. Rotate matrix by 90 degrees clockwise

38. Search in a row-wise and column-wise sorted matrix

39. Spiral traversal of a matrix

40. Matrix multiplication

41. Set entire row and column to 0 if any element is 0

42. Print diagonals of a matrix

43. Snake pattern printing

44. Boundary traversal of matrix

45. Pascal's Triangle

46. Search a 2D matrix (Leetcode 74)

47. Count islands (DFS on matrix)


48. Find median in a row-wise sorted matrix

49. Maximum size rectangle of all 1s in binary matrix

50. Boolean Matrix problem (set row/column to 1 if any element is 1)


Java String Important Questions with Solutions

1. What is the difference between String, StringBuilder, and StringBuffer?

- String is immutable.

- StringBuilder is mutable and not thread-safe.

- StringBuffer is mutable and thread-safe.

2. Are Strings immutable in Java? Why?

- Yes, Strings are immutable.

- Reason: Security, caching, synchronization, and class loading performance.

3. What is the String pool?

- A special memory region where Java stores string literals.

- Duplicate string literals refer to the same object in the pool.

4. What will be the output of this: "hello" == new String("hello")?

- Output: false

- Because new String("hello") creates a new object in heap memory, while "hello" is from the string

pool.

5. Use of substring(), indexOf(), and charAt():

String str = "CodingThinker";

[Link]([Link](0, 6)); // Output: Coding

[Link]([Link]('T')); // Output: 6

[Link]([Link](3)); // Output: i
6. Count vowels in a string:

String input = "Rupesh Kumar";

int count = 0;

for (char c : [Link]().toCharArray()) {

if ("aeiou".indexOf(c) != -1) {

count++;

[Link]("Vowels: " + count); // Output: Vowels: 4

7. Check if a string is a palindrome:

String str = "racecar";

String rev = "";

for (int i = [Link]() - 1; i >= 0; i--) {

rev += [Link](i);

[Link]([Link](rev)); // Output: true

8. Ways to compare Strings:

String s1 = "Java";

String s2 = "java";

[Link]([Link](s2)); // false

[Link]([Link](s2)); // true

[Link]([Link](s2)); // Negative value

9. Replace spaces with '-':

String s = "Java is fun";


String result = [Link](" ", "-");

[Link](result); // Output: Java-is-fun

10. Reverse a String manually:

String s = "Coding";

String rev = "";

for (int i = [Link]() - 1; i >= 0; i--) {

rev += [Link](i);

[Link](rev); // Output: gnidoC

Prepared by: Rupesh Kumar

[Link]

You might also like