JAVA Basic Practice Questions
JAVA Basic Practice Questions
1.
Write a Java program to check if a given number is an Armstrong
number or not.
Input:
Mathura
a
Output:
2
Input:
Priyanka
y
Output:
1
Input:
Priyanka
n
Output:
1
Input:
Priyanka
Input:
Papaya
P
Output:
1
Input:
Priyanka
r
Output:
1
Input:
Priyanka
k
Output:
1
5.
Count the number of words in a sentence that contain at least two
vowels in a row (a, e, i, o, u) in them.
Here's a sample input and output:
Test Cases:
Explanation: There are two words in the sentence that contain at least
two consecutive vowels - "enjoy" and "meatballs".
6.
Write a Java program that takes a user input integer n and prints the
Fibonacci series up to n.
Sample Output:
0 1 1 2 3 5 8 13
Sample Input:
3
Sample Output:
0112
Sample Input:
4
Sample Output:
0112 3
Sample Input:
5
Sample Output:
011235
Sample Output:
0 1 1 2 3 5 8 13 21
Sample Input:
1
Sample Output:
011
Sample Input:
34
Sample Output:
0 1 1 2 3 5 8 13 21 34
Sample Input:
8
Sample Output:
0112358
Sample Input:
13
Sample Output:
0 1 1 2 3 5 8 13
Sample Input:
15
Explanation:
The Fibonacci series up to 20 is: 0, 1, 1, 2, 3, 5, 8, 13. Therefore, the
output is "0 1 1 2 3 5 8 13".
7.
Write a Java program to find the first non-repeated character in a
given string.
Test Cases:
Input string: "minimum"
Output: "n"
Input string: "Hodal"
Output: "H"
Input string: "maximum"
Output: "a"
Input string: "cat"
Output: "c"
Input string: "ball"
Output: "b"
Input string: "all"
8.
Find the palindrome words from the sentence and print them and also
count
Test Cases:
Input:
----------------
My name is nitin and I can speak malayalam
Output:
----------------
nitin
I
malayalam
1
----------------
This is good city.
Output:
Output:
----------------
madam
I
nitin
1
----------------
My name is nitin and I can speak malayalam because I know
malayalam
Output:
----------------
nitin
I
malayalam
2
----------------
This is a radar
Output:
----------------
radat
I
Output:
----------------
refer
I
----------------
Water level is high .
Output:
----------------
level
I
----------------
I am Rakesh.
Output:
----------------
0
----------------
I can speak malayalam
Output:
----------------
malayalam
Explanation: There are two words in the sentence that start and end
with the same letter - "Anna" and "Ginseng".
10.
Count the sum of prime digits in a given number
Test Cases:
Input: 2345678910
Output: 17
Input: 234568910
Output: 10
Input: 23468910
Output: 5
Input: 345678910
Output: 15
Input: 245678910
Output: 14
Input: 45678910
Output: 12
Input: 4678910
Output: 7
Input: 23456789107
Output: 24