Java Interview Question And Answers Pdf ( Quiz -4 )
- FizzBuzz: Write a program that prints the numbers from 1 to 100. But for multiples of three, print “Fizz” instead of the number, and for the multiples of five, print “Buzz.” For numbers that are multiples of both three and five, print “FizzBuzz.”
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
- Palindrome Checker: Create a function that checks if a given word or phrase is a palindrome (reads the same backward as forward), ignoring spaces and punctuation.
def is_palindrome(word):
cleaned_word = ''.join(char.lower() for char in word if char.isalnum())
return cleaned_word == cleaned_word[::-1]
# Example usage:
word_to_check = "A man, a plan, a canal, Panama!"
print(is_palindrome(word_to_check))
- Factorial Calculation: Write a function to calculate the factorial of a non-negative integer entered by the user. The factorial of a number is the product of all positive integers up to that number.
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
# Example usage:
user_input = int(input("Enter a non-negative integer: "))
print(factorial(user_input))
- Sum of Even Numbers: Write a program that calculates the sum of all even numbers from 1 to a given user-specified limit.
def sum_even_numbers(limit):
result = sum(i for i in range(2, limit+1, 2))
return result
# Example usage:
user_limit = int(input("Enter the limit: "))
print(sum_even_numbers(user_limit))
- Guess the Number Game: Develop a simple number guessing game where the computer randomly selects a number between 1 and 100, and the user has to guess it. Provide hints such as “too high” or “too low” after each guess until the correct number is guessed.
import random
def guess_the_number():
secret_number = random.randint(1, 100)
guess = None
while guess != secret_number:
guess = int(input("Guess the number (between 1 and 100): "))
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the correct number {secret_number}!")
# Example usage:
guess_the_number()
Java Interview Question And Answers Pdf ( Quiz -5 )
Prime Number Checker: Write a function to determine if a given positive integer is a prime number.
public class PrimeNumberChecker {
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int userInput = 17; // Example input
System.out.println(isPrime(userInput));
}
}
Reverse a String: Create a program or function that takes a string as input and outputs the string in reverse order.
public class ReverseString {
public static String reverseString(String input) {
return new StringBuilder(input).reverse().toString();
}
public static void main(String[] args) {
String inputString = "Hello, World!";
System.out.println(reverseString(inputString));
}
}
Check 100+ JAVA Spring Boot Projects with Source Code
Count Vowels and Consonants: Write a program or function that takes a string as input and counts the number of vowels and consonants in it.
public class CountVowelsConsonants {
public static void countVowelsConsonants(String input) {
int vowels = 0, consonants = 0;
String lowerInput = input.toLowerCase();
for (char ch : lowerInput.toCharArray()) {
if (ch >= 'a' && ch <= 'z') {
if ("aeiou".contains(String.valueOf(ch))) {
vowels++;
} else {
consonants++;
}
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
public static void main(String[] args) {
String userInput = "Programming is Fun!";
countVowelsConsonants(userInput);
}
}
Sum of Digits: Create a function that calculates the sum of the digits of a given positive integer.
public class SumOfDigits {
public static int sumOfDigits(int number) {
int sum = 0;
while (number != 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
public static void main(String[] args) {
int userInput = 12345; // Example input
System.out.println(sumOfDigits(userInput));
}
}
Check 50+ JAVA Projects with Source Code
Pattern Printing – Right Triangle: Write a program to print a right-angled triangle pattern using asterisks (*) where the height of the triangle is determined by user input.
import java.util.Scanner;
public class RightTrianglePattern {
public static void printRightTriangle(int height) {
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the height of the right-angled triangle: ");
int userHeight = scanner.nextInt();
printRightTriangle(userHeight);
scanner.close();
}
}
Latest Post :-
Keyword |
---|
java interview question and answers pdf |
java interview question and answer pdf |
java interview question |
java interview question on string |
java interview question and answers for freshers |
java interview question on collection |
java interview question book |
java interview question and answer for fresher pdf |
java interview question for 2 years experience |
core java interview question |
design patterns in java interview question |
what is classloader in java interview question |
java interview question pdf download |
java interview question pdf free download |
java interview question and answer for experienced |
java interview question and answer for 3 years experience |
Pingback: Free Java Interview Question And Answers Pdf [Quiz -7]
Pingback: Accenture Previous Papers 2023 : A Comprehensive Guide