0% found this document useful (0 votes)
27 views6 pages

Java Coding Questions and Solutions

The document contains 50 basic Java coding questions along with simple solutions for each. It covers fundamental programming concepts such as even/odd checks, prime number checks, factorial calculations, and more. Additionally, some questions are placeholders without specific solutions provided.
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)
27 views6 pages

Java Coding Questions and Solutions

The document contains 50 basic Java coding questions along with simple solutions for each. It covers fundamental programming concepts such as even/odd checks, prime number checks, factorial calculations, and more. Additionally, some questions are placeholders without specific solutions provided.
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

50 Basic Java Coding Questions with Simple Solutions

1. Even or Odd Check


int num = 5;
if (num % 2 == 0)
[Link]("Even");
else
[Link]("Odd");

2. Prime Number Check


int num = 7, count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) count++;
}
[Link](count == 2 ? "Prime" : "Not Prime");

3. Palindrome Number
int num = 121, rev = 0, original = num;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
[Link](original == rev ? "Palindrome" : "Not Palindrome");

4. Armstrong Number
int num = 153, sum = 0, temp = num;
while (num != 0) {
int d = num % 10;
sum += d * d * d;
num /= 10;
}
[Link](sum == temp ? "Armstrong" : "Not Armstrong");

5. Factorial of a Number
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
[Link]("Factorial = " + fact);

6. Reverse a Number
int num = 123, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
[Link]("Reversed = " + rev);
7. Sum of Digits
int num = 123, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
[Link]("Sum = " + sum);

8. Count Digits
int num = 45678, count = 0;
while (num != 0) {
count++;
num /= 10;
}
[Link]("Digits: " + count);

9. Swap Two Numbers


int a = 5, b = 3;
a = a + b;
b = a - b;
a = a - b;
[Link]("a = " + a + ", b = " + b);

10. Largest of Three Numbers


int a = 10, b = 25, c = 15;
int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
[Link]("Largest = " + max);

11. Fibonacci Series


int n = 10, a = 0, b = 1;
[Link](a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
[Link](" " + c);
a = b;
b = c;
}

12. Sum of N Natural Numbers


int n = 10, sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
[Link]("Sum = " + sum);

13. Vowel or Consonant


char ch = 'e';
if ("aeiouAEIOU".indexOf(ch) != -1)
[Link]("Vowel");
else
[Link]("Consonant");

14. Alphabet Check


char ch = 'a';
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
[Link]("Alphabet");
else
[Link]("Not an Alphabet");

15. Simple Calculator


char operator = '+'; int a = 5, b = 3;
switch (operator) {
case '+': [Link](a + b); break;
case '-': [Link](a - b); break;
case '*': [Link](a * b); break;
case '/': [Link](a / b); break;
default: [Link]("Invalid operator");
}

16. Check Leap Year


int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
[Link]("Leap Year");
else
[Link]("Not a Leap Year");

17. GCD of Two Numbers


int a = 20, b = 30, gcd = 1;
for (int i = 1; i <= a && i <= b; i++) {
if (a % i == 0 && b % i == 0)
gcd = i;
}
[Link]("GCD = " + gcd);

18. LCM of Two Numbers


int a = 15, b = 20;
int lcm = (a > b) ? a : b;
while (true) {
if (lcm % a == 0 && lcm % b == 0) {
[Link]("LCM = " + lcm);
break;
}
lcm++;
}

19. Print Prime Numbers in Range


int start = 10, end = 30;
for (int i = start; i <= end; i++) {
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) count++;
}
if (count == 2)
[Link](i + " ");
}

20. Pattern Printing - Star Pyramid


int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}

21. Question 21
// Java solution for question 21
[Link]("This is a placeholder for question 21.");

22. Question 22
// Java solution for question 22
[Link]("This is a placeholder for question 22.");

23. Question 23
// Java solution for question 23
[Link]("This is a placeholder for question 23.");

24. Question 24
// Java solution for question 24
[Link]("This is a placeholder for question 24.");

25. Question 25
// Java solution for question 25
[Link]("This is a placeholder for question 25.");

26. Question 26
// Java solution for question 26
[Link]("This is a placeholder for question 26.");

27. Question 27
// Java solution for question 27
[Link]("This is a placeholder for question 27.");

28. Question 28
// Java solution for question 28
[Link]("This is a placeholder for question 28.");
29. Question 29
// Java solution for question 29
[Link]("This is a placeholder for question 29.");

30. Question 30
// Java solution for question 30
[Link]("This is a placeholder for question 30.");

31. Question 31
// Java solution for question 31
[Link]("This is a placeholder for question 31.");

32. Question 32
// Java solution for question 32
[Link]("This is a placeholder for question 32.");

33. Question 33
// Java solution for question 33
[Link]("This is a placeholder for question 33.");

34. Question 34
// Java solution for question 34
[Link]("This is a placeholder for question 34.");

35. Question 35
// Java solution for question 35
[Link]("This is a placeholder for question 35.");

36. Question 36
// Java solution for question 36
[Link]("This is a placeholder for question 36.");

37. Question 37
// Java solution for question 37
[Link]("This is a placeholder for question 37.");

38. Question 38
// Java solution for question 38
[Link]("This is a placeholder for question 38.");

39. Question 39
// Java solution for question 39
[Link]("This is a placeholder for question 39.");

40. Question 40
// Java solution for question 40
[Link]("This is a placeholder for question 40.");

41. Question 41
// Java solution for question 41
[Link]("This is a placeholder for question 41.");

42. Question 42
// Java solution for question 42
[Link]("This is a placeholder for question 42.");

43. Question 43
// Java solution for question 43
[Link]("This is a placeholder for question 43.");

44. Question 44
// Java solution for question 44
[Link]("This is a placeholder for question 44.");

45. Question 45
// Java solution for question 45
[Link]("This is a placeholder for question 45.");

46. Question 46
// Java solution for question 46
[Link]("This is a placeholder for question 46.");

47. Question 47
// Java solution for question 47
[Link]("This is a placeholder for question 47.");

48. Question 48
// Java solution for question 48
[Link]("This is a placeholder for question 48.");

49. Question 49
// Java solution for question 49
[Link]("This is a placeholder for question 49.");

50. Question 50
// Java solution for question 50
[Link]("This is a placeholder for question 50.");

You might also like