0% found this document useful (0 votes)
3 views

JAVA Basic Practice Questions

Uploaded by

gauri.rai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA Basic Practice Questions

Uploaded by

gauri.rai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

JAVA Basic Practice Questions

1.
Write a Java program to check if a given number is an Armstrong
number or not.

Here's an example of the expected input and output:


Test Cases:
Input number: 0
Output: "Yes, the number is an Armstrong number."
Input number: 1
Output: "yes, the number is an Armstrong number."
Input number: 153
Output: "Yes, the number is an Armstrong number."
Input number: 370
Output: "Yes, the number is an Armstrong number."
Input number: 371
Output: "Yes, the number is an Armstrong number."
Input number: 407
Output: "Yes, the number is an Armstrong number."
Input number: 2
Output: "No, the number is not an Armstrong number."
Input number: 3
Output: "No, the number is not an Armstrong number."
Input number: 154
Output: "No, the number is not an Armstrong number."
Input number: 372
Output: "No, the number is not an Armstrong number."

By –ER. Gauri Shankar Rai


2.
Write a Java program to implement a simple calculator. The program
should prompt the user to enter two numbers and an
operator (+, -, *, or /) and then perform the corresponding operation
and display the result.
Test Cases:
For example, if the user enters 4, 5, and +, the program should display
9 as the result.
Similarly, if the user enters 10, 3,
and *, the program should display 30 as the result.
if the user enters 10, 3,
and /, the program should display 3 as the result.
if the user enters 10, 3,
and -, the program should display 7 as the result
if the user enters 5,7,
and *, the program should display 35 as the result
if the user enters 10, 15,
and -, the program should display -5 as the result
if the user enters 3, 3,
and *, the program should display 9 as the result
if the user enters 10, 3,
and (, the program should display invalid input
if the user enters x, 3,
and +, the program should display invalid input

Your program should handle invalid inputs gracefully, for example, if


the user enters an operator that is not one of the four

By –ER. Gauri Shankar Rai


allowed operators or if the user enters non-numeric inputs.
3.
Find Characters of a string at odd index
Test Cases:
Arijit = rjt
priyanka = ryna
Raman = aa
Allahabad = laaa
Nagpur = gu
India = ni
gla= l
Mathura = ahr
x= odd index is not there
ra = a
4.
Find occurance of a character in a string
Test Cases:
Input:
Priyanka
a
Output:
2
Input:
Allahavad
a
Output:
3

By –ER. Gauri Shankar Rai


Input:
Agra
a
Output:
1

Input:
Mathura
a
Output:
2

Input:
Priyanka
y
Output:
1

Input:
Priyanka
n
Output:
1

Input:
Priyanka

By –ER. Gauri Shankar Rai


i
Output:
1

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:

By –ER. Gauri Shankar Rai


Input: I enjoy eating spaghetti and meatballs for dinner
Output: 2
Input: I live in Agra.
Output: 0
Input: We all are Indians.
Output: 0
Input: I like this Audio song.
Output: 1
Input: We have only 1 ceiling fan.
Output: 1
Input: I have one movie coupon.
Output: 1
Input: Suit
Output: 1
Input: I enjoy eating spaghetti and meatballs for dinner
Output: 2
Input: I enjoy eating spaghetti and meatballs for dinner
Output: 2

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.

By –ER. Gauri Shankar Rai


The Fibonacci series is a series of numbers in which each number is
the sum of the two preceding numbers.
The first two numbers in the series are 0 and 1. For example, the first
10 numbers in the Fibonacci series are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

The program should use a for loop to generate the series.


Test Cases:
Sample Input:
20

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

By –ER. Gauri Shankar Rai


Sample Input:
21

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

By –ER. Gauri Shankar Rai


Sample Output:
0 1 1 2 3 5 8 13

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.

Here's an example of the expected input and output:

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"

By –ER. Gauri Shankar Rai


Output: "a"
Input string: "teacher"
Output: "t"
Input string: "mathura"
Output: "m"
Input string: "aabbc"
Output: "c"
Input string: "agra"
Output: "g"

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:

By –ER. Gauri Shankar Rai


----------------
0
----------------
madam teaches nitin english

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

By –ER. Gauri Shankar Rai


----------------
I refer java language for practice

Output:
----------------
refer
I
----------------
Water level is high .

Output:
----------------
level
I

----------------
I am Rakesh.

Output:
----------------
0
----------------
I can speak malayalam

Output:
----------------
malayalam

By –ER. Gauri Shankar Rai


1
----------------
My name is nitin .
Output:
----------------
nitin
I
9.
Count the number of words in a sentence that start and end with the
same letter.(Ignore case)
Test Cases:
Input: Anna asked about the Ginseng recipe
Output: 2
Input: Anna asked about the city.
Output: 1
Input: Anna .
Output: 1
Input: I know the Ginseng recipe
Output: 1
Input: Mohan is a good boy.
Output: 0
Input: abca bab cabc.
Output: 3
Input: Rakesh is in Agra.
Output: 1
Input: Agra is good to speak Malayalam.
Output: 2

By –ER. Gauri Shankar Rai


Input: java is object oriented.
Output: 0
Input: Itarsi is a city near agra.
Output: 2

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

By –ER. Gauri Shankar Rai


Input: 3456789107
Output: 22
Input: 446698910
Output:0

By –ER. Gauri Shankar Rai

You might also like