Java Practice Programs
1. Swap two numbers. Given number a=10, b=20. Expected output a=20, b=10.
--logic 1: create a temprory variable
--logic 2: using plus and minus
a=a+b;
b=a-b;
a=a-b;
--logic 3: multiplication and division operator, only when a and b are non zero
a=a*b;
b=a/b;
a=a/b;
Note: what is XOR operator
2. Reverse a given number. Given number is 1234. Expected output 4321
or reverse a number using stirng buffer and string builder class
Note: What is scanner class in java? How to take input from scanner class in java
Note: What is difference between modulo % division and division /
Note: Revise java String class and its method
3. reverese a given string
Note: what is charAt() method
Note: what is toCharArray() method
4. how to check number is palindrome or not// use the logic used in question 2
hint: revers the number and compare with original number
5. how to check string is palindrome or not// use the logic used in question 3
hint: reverse the string and compare with original string.
Note: what is equals() method.
6. Count the digits in a given number// logic used in above program
hint: eliminate the last digit by dividing the num by 10 and then increase the count
7. how to count even and odd number in a given number
hint: extract the last number by num%10 and check if this even or not, and then eliminate the last digit by num/10.
8. how to calculate the sum of digits in a given number
hint: extract the last digit by num%10, do the sum, and then eliminate the last digit by num/10
9. Find the largest number among three numbers
Note: Read string in java.
Note: what is ternary operator in java. a>b?a:b
10. print the fibonacci series, sum of two preceding numbers e.g 0,1,1,2,3,5,8,13,21,34
Note: A series of num in which each num is the sum of the two preceding numbers
hint: take two num n1=0, n2=1, do the sum, now interchange num, assign n1=n2 and n2=sum.
11. Check if the number is prime or not.
Note: what is prime number. Any natural num which has only two factor or can be divided by 1 or itself
Note: what is natural num, num greater than 1
hint: check if num is greater than 1, and check if the num is divided by within the range of given num. e.g
in case of 3, num should be devided by only two num withing the range of 1, 2 and 3. If it is divided by more than 2
num then it is not prime num.
12. How to generate random numbers, decimal numbers and string. using Random class and math class in java
hint: use the nextInt() method of Random class, and pass the number, and nextDouble() method used to generate random decimal number
or use satic random() method of Math class
Note: What is Random class in java. Generate random no between 1 to 100
Note: What is Math class in java. Generate random no usinhg math class
Note: Other way to create random number or string is apache commons lang api
13. find a factorial of a number.
14. find sum of elements in an array.
Note: Practice array based programs.
15. Print even and odd numbers from an array.
hint: retrive each individual value and check if the number is even of not and increase the even and odd count variable
16. Check if both arrays are equal or not/ how to check the equality of two arrays
hint: use Arrays.equals(arr1,arr2) method and store the status in boolean variable or check if the length of both arrays are same or not,
after that compare each element and store the status in boolean variable
17. Find missing number in an array.
prerequisite: Array should not have duplicate, arrays no need to be in sorted order, values should be in range
hint: find the sum of given array, then do the sum of array including missing num/sum of range, and then do sum2 - sum1.
18. Find maximum and minimum elements in an array
hint: suppose at index 0 is the maximum element, max=arr[0], then traverse the array comare the other element with the max element, and assignn new max
element in max variable. similarly do it for minimum element
19. Find duplicate elements in an array.
hint: traverse both array and compare the element, if they are equal make flag true
hint: by using hashset data structure in java, if you add duplicate element in hashset it will return false
20. Search a given element in an array of integer by using linear search.
hint: use linear search, compare each element one by one, break if the element is found
21. search a given element in an array of integer by using binary search.*****
hint: use binary seach algorithm.
hint: find a mind value by index 0 + last index divided by 2. and compare the mid element from searchable element,
if the searchable element is greather then mid element than search in the right side of mid element , and if
searchable element is less than mid element then search in the left side of mid element
Prerequisite: elements should be in sorted order
22. how to sort element using bubble sort algorithm.
23. how to count occurrences of character in a string.
hint: find the length of the string, replace the character by str.replace(a,""). after that do total length - length after replacing character
24. how to count number of words in a string.
hint: find the length of the string, traverse the string. apply the condition, check for space and after space there should be a character
str.charAt(i)==' ' && str.charAt(i+1)!=' ') if the condition is true then increase the count
25.
26.
27.
28.
------------------------------------------------------------Interval-Revise-Practice----------------------------------------
Refernce:
youtube search keyword: java programming interview questions
SDET- QA Automation Techie: https://www.youtube.com/watch?v=dCr67d0QBCA&list=PLUDwpEzHYYLtgkXK6YaZ4I2XcsjMqIaEa&index=17
Learn With KrishnaSandeep: youtube.com/watch?v=I03_pV3xB0w&list=PLF9tovyahfL020hGgLIsRMZY4bfSLCFUa&index=114
Comments
Post a Comment