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

Java Coding Questions Explained

The document contains Java coding examples demonstrating various programming concepts, including exception handling, palindrome checking, string reversal, prime number checking, Fibonacci series generation, and finding the largest and smallest numbers in an array. Each example is presented as a complete Java class with a main method. These snippets serve as practical exercises for learning Java programming.

Uploaded by

srikarkokkula
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)
8 views2 pages

Java Coding Questions Explained

The document contains Java coding examples demonstrating various programming concepts, including exception handling, palindrome checking, string reversal, prime number checking, Fibonacci series generation, and finding the largest and smallest numbers in an array. Each example is presented as a complete Java class with a main method. These snippets serve as practical exercises for learning Java programming.

Uploaded by

srikarkokkula
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 Coding Questions with Short Codes

1. Exception Handling
public class ExceptionExample {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Error: Division by zero!");
} finally {
[Link]("Execution completed.");
}
}
}

2. Palindrome String
public class Palindrome {
public static void main(String[] args) {
String str = "madam";
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--)
rev += [Link](i);
[Link]([Link](rev) ? "Palindrome" : "Not Palindrome");
}
}

3. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Clonnect";
String rev = new StringBuilder(str).reverse().toString();
[Link]("Reversed: " + rev);
}
}

4. Prime Number Check


public class PrimeCheck {
public static void main(String[] args) {
int n = 7;
boolean flag = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = false;
break;
}
}
[Link](flag ? "Prime" : "Not Prime");
}
}

5. Fibonacci Series
public class Fibonacci {
public static void main(String[] args) {
int n1 = 0, n2 = 1, n3, count = 10;
[Link](n1 + " " + n2);
for (int i = 2; i < count; ++i) {
n3 = n1 + n2;
[Link](" " + n3);
n1 = n2;
n2 = n3;
}
}
}

6. Largest & Smallest Number in Array


public class LargestSmallest {
public static void main(String[] args) {
int[] arr = {25, 11, 7, 75, 56};
int largest = arr[0], smallest = arr[0];
for (int i : arr) {
if (i > largest) largest = i;
if (i < smallest) smallest = i;
}
[Link]("Largest: " + largest);
[Link]("Smallest: " + smallest);
}
}

You might also like