0% found this document useful (0 votes)
7 views4 pages

Java Basic Coding Questions

The document contains a collection of basic Java coding problems and their solutions, including reversing a string, checking for prime numbers, generating a Fibonacci series, and more. Each problem is presented with a code snippet that demonstrates the solution in Java. The examples cover fundamental programming concepts and logical reasoning.

Uploaded by

nandhinicse2004
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)
7 views4 pages

Java Basic Coding Questions

The document contains a collection of basic Java coding problems and their solutions, including reversing a string, checking for prime numbers, generating a Fibonacci series, and more. Each problem is presented with a code snippet that demonstrates the solution in Java. The examples cover fundamental programming concepts and logical reasoning.

Uploaded by

nandhinicse2004
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
You are on page 1/ 4

Basic Java Logical Coding Questions with Answers

1. Reverse a String

public class ReverseString {


public static void main(String[] args) {
String str = "hello";
String reversed = "";
for(int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println("Reversed: " + reversed);
}
}

2. Check if a Number is Prime

public class PrimeCheck {


public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
if (num <= 1) isPrime = false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(num + " is prime? " + isPrime);
}
}

3. Fibonacci Series

public class Fibonacci {


public static void main(String[] args) {
int n = 10, a = 0, b = 1;
System.out.print("Fibonacci: " + a + ", " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
System.out.print(", " + next);
a = b;
b = next;
}
}
}

4. Palindrome Check

public class Palindrome {


public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
System.out.println(str + " is palindrome? " + str.equals(rev));
}
}

5. Factorial of a Number

public class Factorial {


public static void main(String[] args) {
int num = 5;
long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);
}
}

6. Swap Two Numbers without 3rd Variable

public class Swap {


public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a: " + a + ", b: " + b);
}
}

7. Count Vowels and Consonants

public class VowelConsonantCount {


public static void main(String[] args) {
String str = "Hello World";
int vowels = 0, consonants = 0;
str = str.toLowerCase();
for (char ch : str.toCharArray()) {
if (Character.isLetter(ch)) {
if ("aeiou".indexOf(ch) != -1)
vowels++;
else
consonants++;
}
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
}

8. Armstrong Number Check

public class Armstrong {


public static void main(String[] args) {
int num = 153, sum = 0, temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
System.out.println(num + " is Armstrong? " + (num == sum));
}
}

9. Find Largest in Array

public class LargestInArray {


public static void main(String[] args) {
int[] arr = {12, 45, 67, 23, 89, 5};
int max = arr[0];
for (int num : arr) {
if (num > max)
max = num;
}
System.out.println("Largest: " + max);
}
}

10. Find Duplicate Characters

import java.util.HashMap;

public class DuplicateChars {


public static void main(String[] args) {
String str = "programming";
HashMap<Character, Integer> map = new HashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
System.out.println("Duplicates:");
for (char c : map.keySet()) {
if (map.get(c) > 1)
System.out.println(c + " - " + map.get(c));
}
}
}

You might also like