1. How do you reverse a string in Java?
You can reverse a string in Java by using the StringBuilder class. The StringBuilder class has a reverse() method, which can be used to reverse a string. Here is an example of how to use the reverse() method:
String str = "Hello World!";
StringBuilder sb = new StringBuilder(str);
sb.reverse();
System.out.println(sb); // prints "!dlroW olleH"
2. How do you swap two numbers without using a third variable in Java?
You can swap two numbers without using a third variable in Java by using XOR (^) operator. Here is an example of how to swap two numbers:
int x = 10;
int y = 20;
x = x ^ y; // x now becomes 30 (10 ^ 20)
y = x ^ y; // y becomes 10 (30 ^ 20)
x = x ^ y; // x becomes 20 (30 ^ 10)
System.out.println("x = " + x); // prints x = 20
System.out.println("y = " + y); // prints y = 10
3. Write a Java program to check if a vowel is present in a string.
You can check if a vowel is present in a string in Java by using a for loop to iterate through each character of the string and check if the character is a vowel or not. Here is an example of how to check if a vowel is present in a string:
public static boolean isVowelPresent(String str) {
for (char c : str.toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
}
}
return false;
}
4. Write a Java program to check if the given number is a prime number.
public class PrimeNumberCheck {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for(int i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
5. Write a Java program to print a Fibonacci sequence using recursion.
public class Fibonacci {
static int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
public static void main (String args[])
{
int n = 9;
System.out.println(fib(n));
}
}
6. Write a Java program to print a Fibonacci sequence using recursion.
public class Fibonacci {
static int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
public static void main(String[] args) {
int n = 10;
System.out.println("The Fibonacci series upto " + n + "th number is: ");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) +" ");
}
}
}
7. How do you check if a list of integers contains only odd numbers in Java?
import java.util.List;
public class OddNumbers {
public static boolean isOddList(List<Integer> list)
{
return list.stream().allMatch(i -> i % 2 != 0);
}
public static void main(String[] args)
{
List<Integer> list = List.of(1, 3, 5, 7);
System.out.println(isOddList(list));
}
}
9. How do you check whether a string is a palindrome in Java?
To check whether a string is a palindrome in Java, you can use the following code:
String originalString = "racecar";
String reversedString = new StringBuffer(originalString).reverse().toString();
if (originalString.equals(reversedString))
System.out.println("The string is a palindrome");
else
System.out.println("The string is not a palindrome");
10. How do you remove spaces from a string in Java?
You can remove spaces from a string in Java by using the replaceAll() method. This method takes a regular expression as an argument and replaces all occurrences of the substring with an empty string. For example:
String s = "This is a string with spaces";
s = s.replaceAll("\\s", "");
System.out.println(s); // Thisisastringwithoutspaces
11. How do you sort an array in Java?
To sort an array in Java, you can use the Arrays.sort() method. This method takes the array as an argument and sorts the array in ascending order. For example,
int[] array = {3, 2, 5, 1, 4};
Arrays.sort(array);
// array is now [1, 2, 3, 4, 5]
NOTE : आप Computer Science से सम्बंधित content हमे इस Mail Id पर भेजे जो हम आपके द्वारा भेजे हुए content को वेबसाइट पर आपकी photo सहित प्रकाशित करेंगे।
official.cswithgk@gmail.com