
Practice 50 Arrays Coding Questions
Q1. Write a Rust program to create an array of integers and print all its elements.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
1 2 3 4 5
Q2. Write a Rust program to find the length of an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Length: 4
Q3. Write a Rust program to access and print the third element of an array.
Input:
Array: [10, 20, 30, 40, 50]
Expected Output:
Third element: 30
Q4. Write a Rust program to sum all the elements of an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Sum: 10
Q5. Write a Rust program to check if an array contains a specific value.
Input:
Array: [1, 2, 3, 4], Value: 3
Expected Output:
Array contains 3.
Q6. Write a Rust program to find the maximum element in an array.
Input:
Array: [10, 20, 5, 30]
Expected Output:
Maximum element: 30
Q7. Write a Rust program to find the minimum element in an array.
Input:
Array: [10, 20, 5, 30]
Expected Output:
Minimum element: 5
Q8. Write a Rust program to reverse the elements of an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Reversed array: [4, 3, 2, 1]
Q9. Write a Rust program to check if an array is sorted in ascending order.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Array is sorted in ascending order.
Q10. Write a Rust program to copy all elements from one array to another.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Copied array: [1, 2, 3, 4]
Q11. Write a Rust program to concatenate two arrays.
Input:
Array 1: [1, 2], Array 2: [3, 4]
Expected Output:
Concatenated array: [1, 2, 3, 4]
Q12. Write a Rust program to find the index of an element in an array.
Input:
Array: [10, 20, 30, 40], Element: 30
Expected Output:
Index of 30: 2
Q13. Write a Rust program to remove an element from an array by shifting the remaining elements.
Input:
Array: [1, 2, 3, 4], Element to remove: 3
Expected Output:
Array after removal: [1, 2, 4]
Q14. Write a Rust program to create an array of 10 elements initialized to 0.
Input:
Size: 10
Expected Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Q15. Write a Rust program to find the average of all elements in an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Average: 2.5
Q16. Write a Rust program to find the second largest element in an array.
Input:
Array: [10, 20, 5, 30]
Expected Output:
Second largest element: 20
Q17. Write a Rust program to check if an array contains only positive numbers.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Array contains only positive numbers.
Q18. Write a Rust program to find the sum of even numbers in an array.
Input:
Array: [1, 2, 3, 4, 5, 6]
Expected Output:
Sum of even numbers: 12
Q19. Write a Rust program to find the product of all elements in an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Product: 24
Q20. Write a Rust program to check if an array is empty.
Input:
Array: []
Expected Output:
The array is empty.
Q21. Write a Rust program to multiply each element of an array by 2.
Input:
Array: [1, 2, 3, 4]
Expected Output:
[2, 4, 6, 8]
Q22. Write a Rust program to find the even indexed elements in an array.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Even indexed elements: [1, 3, 5]
Q23. Write a Rust program to find the odd indexed elements in an array.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Odd indexed elements: [2, 4]
Q24. Write a Rust program to shift the elements of an array to the left by one position.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Array after shift: [2, 3, 4, 1]
Q25. Write a Rust program to shift the elements of an array to the right by one position.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Array after shift: [4, 1, 2, 3]
Q26. Write a Rust program to find the median of an array.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Median: 3
Q27. Write a Rust program to find the mode of an array.
Input:
Array: [1, 2, 3, 1, 2, 1]
Expected Output:
Mode: 1
Q28. Write a Rust program to check if two arrays are equal.
Input:
Array 1: [1, 2, 3], Array 2: [1, 2, 3]
Expected Output:
Arrays are equal.
Q29. Write a Rust program to check if two arrays are not equal.
Input:
Array 1: [1, 2, 3], Array 2: [1, 2, 4]
Expected Output:
Arrays are not equal.
Q30. Write a Rust program to merge two arrays into a third array.
Input:
Array 1: [1, 2], Array 2: [3, 4]
Expected Output:
Merged array: [1, 2, 3, 4]
Q31. Write a Rust program to replace an element at a specific index in an array.
Input:
Array: [1, 2, 3, 4], Index: 2, New value: 10
Expected Output:
Array after replacement: [1, 2, 10, 4]
Q32. Write a Rust program to print the elements of an array in reverse order.
Input:
Array: [1, 2, 3, 4]
Expected Output:
4 3 2 1
Q33. Write a Rust program to check if a number exists in an array.
Input:
Array: [1, 2, 3, 4], Number: 2
Expected Output:
2 exists in the array.
Q34. Write a Rust program to add an element to the end of an array.
Input:
Array: [1, 2, 3], Element to add: 4
Expected Output:
Array after addition: [1, 2, 3, 4]
Q35. Write a Rust program to add an element at the beginning of an array.
Input:
Array: [2, 3, 4], Element to add: 1
Expected Output:
Array after addition: [1, 2, 3, 4]
Q36. Write a Rust program to remove an element from the end of an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Array after removal: [1, 2, 3]
Q37. Write a Rust program to remove an element from the beginning of an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Array after removal: [2, 3, 4]
Q38. Write a Rust program to double each element of an array.
Input:
Array: [1, 2, 3]
Expected Output:
[2, 4, 6]
Q39. Write a Rust program to count how many times a specific element appears in an array.
Input:
Array: [1, 2, 3, 1, 4], Element: 1
Expected Output:
Element 1 appears 2 times.
Q40. Write a Rust program to generate an array of the first N natural numbers.
Input:
N = 5
Expected Output:
[1, 2, 3, 4, 5]
Q41. Write a Rust program to check if an array contains any negative numbers.
Input:
Array: [1, -2, 3, 4]
Expected Output:
Array contains negative numbers.
Q42. Write a Rust program to create an array from a string by splitting it.
Input:
String: "apple,banana,orange"
Expected Output:
["apple", "banana", "orange"]
Q43. Write a Rust program to sort an array in descending order.
Input:
Array: [1, 2, 3, 4]
Expected Output:
[4, 3, 2, 1]
Q44. Write a Rust program to rotate the elements of an array to the right by n positions.
Input:
Array: [1, 2, 3, 4], n = 2
Expected Output:
[3, 4, 1, 2]
Q45. Write a Rust program to rotate the elements of an array to the left by n positions.
Input:
Array: [1, 2, 3, 4], n = 2
Expected Output:
[3, 4, 1, 2]
Q46. Write a Rust program to create an array of boolean values.
Input:
Array: [true, false, true]
Expected Output:
[true, false, true]
Q47. Write a Rust program to check if an array contains only one distinct element.
Input:
Array: [3, 3, 3]
Expected Output:
Array contains only one distinct element.
Q48. Write a Rust program to square each element in an array.
Input:
Array: [1, 2, 3]
Expected Output:
[1, 4, 9]
Q49. Write a Rust program to swap two elements in an array.
Input:
Array: [1, 2, 3, 4], Index 1: 0, Index 3: 3
Expected Output:
Array after swap: [1, 4, 3, 2]
Q50. Write a Rust program to find the unique elements in an array.
Input:
Array: [1, 2, 3, 2, 4, 3, 5]
Expected Output:
Unique elements: [1, 4, 5]