0% found this document useful (0 votes)
6 views2 pages

Java Coding Questions

The document contains Java coding questions and answers, including programs to reverse a string, check if a number is prime, and find the factorial of a number. Each section provides a brief description and a code example demonstrating the solution. These examples illustrate fundamental programming concepts in Java.
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)
6 views2 pages

Java Coding Questions

The document contains Java coding questions and answers, including programs to reverse a string, check if a number is prime, and find the factorial of a number. Each section provides a brief description and a code example demonstrating the solution. These examples illustrate fundamental programming concepts in Java.
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

Hands-on Java Coding Questions and Answers

Reverse a String in Java


Write a Java program to reverse a given string.

```java
public class ReverseString {
public static void main(String[] args) {
String str = "Hello World";
String reversed = new StringBuilder(str).reverse().toString();
[Link]("Reversed String: " + reversed);
}
}
```

Check if a Number is Prime


Write a Java program to check if a given number is prime.

```java
public class PrimeNumber {
public static boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) return false;
}
return true;
}

public static void main(String[] args) {


int number = 29;
[Link](number + " is prime: " + isPrime(number));
}
}
```

Find Factorial of a Number


Write a Java program to find the factorial of a given number.

```java
public class Factorial {
public static long factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}

public static void main(String[] args) {


int number = 5;
[Link]("Factorial of " + number + " is " + factorial(number));
}
}
```

You might also like