Moksha - Test 3 - CoreJava
Java Coding
Test Paper
Duration: 120 minutes
Total Score: 100 points
Instructions:
·
This coding test consists of 10 programming
questions.
·
Each question is worth 10 points.
·
You have 2 hours (120 minutes) to complete the
test.
·
Write Java code for each question.
·
You can use any standard Java libraries and
features.
·
Ensure your code is well-documented and follows
best practices.
·
Please write the code in the space provided.
·
Write efficient algorithms for each question and
include them in your solution.
Question 1 (10 points): Fibonacci Series Write a Java
program to print the Fibonacci series up to a specified number of terms.
Algorithm:
- Initialize
variables a, b, and sum to 0, 1, and 0, respectively.
- Print
the initial values of a and b.
- Use
a loop to generate the Fibonacci series:
- Set
sum to the sum of a and b.
- Update
a to the value of b.
- Update
b to the value of sum.
- Print
the value of sum.
- Repeat
the loop for the specified number of terms.
Question 2 (10 points): Armstrong Number Write a Java
program to check if a given number is an Armstrong number or not.
Algorithm:
- Read
the input number.
- Calculate
the number of digits in the input number.
- Initialize
a variable temp to store a temporary copy of the input number.
- Initialize
a variable sum to 0.
- Use
a loop to extract each digit from temp:
- Calculate
the digit^numberOfDigits for each digit.
- Add
the result to sum.
- Check
if sum is equal to the input number.
- If
equal, it's an Armstrong number; otherwise, it's not.
Question 3 (10 points): Prime Numbers in a Range
Write a Java program to find and print all prime numbers within a given range.
Algorithm:
- Read
the lower and upper bounds of the range.
- Initialize
a variable isPrime to true.
- Use
a loop to iterate through numbers within the range:
- For
each number, check if it's prime:
- Start
a loop from 2 to the square root of the number.
- If
the number is divisible by any value in that range, set isPrime
to false and break.
- If isPrime
is still true, print the number.
Question 4 (10 points): Reverse a String Write a Java
program to reverse a string without using any library function.
Algorithm:
- Read
the input string.
- Initialize
an empty string reverseStr.
- Use
a loop to iterate through the characters of the input string from the end
to the beginning:
- Append
each character to reverseStr.
- reverseStr
now contains the reversed string.
Question 5 (10 points): Factorial using Recursion
Write a Java program to find the factorial of a given number using recursion.
Algorithm:
- Read
the input number.
- Create
a recursive function calculateFactorial that takes an integer
parameter n.
- In
the function:
- If n
is 0 or 1, return 1 (base case).
- Otherwise,
return n * calculateFactorial(n - 1).
- Call
calculateFactorial with the input number to compute the factorial.
Question 6 (10 points): Implement a Stack Write a
Java program to implement a stack (using an array or a linked list) and perform
push and pop operations.
Algorithm:
- Implement
a stack data structure using an array or a linked list.
- Create
methods push to push an element onto the stack and pop to
pop an element from the stack.
Question 7 (10 points): Find Largest and Smallest
Elements Write a Java program to find the largest and smallest elements in
an array of integers.
Algorithm:
- Read
the array of integers.
- Initialize
variables largest and smallest to the first element of the array.
- Use
a loop to iterate through the elements of the array:
- If
an element is greater than largest, update largest.
- If
an element is smaller than smallest, update smallest.
- largest
and smallest now contain the largest and smallest elements.
Question 8 (10 points): Count Vowels and Consonants in a
String Write a Java program to count the number of vowels and consonants in
a given string.
Algorithm:
- Read
the input string and convert it to lowercase.
- Initialize
variables vowelCount and consonantCount to 0.
- Use
a loop to iterate through each character of the string:
- If
the character is a vowel (a, e, i, o, or u), increment vowelCount.
- If
the character is a letter (not a space, digit, or special character),
increment consonantCount.
- vowelCount
and consonantCount now contain the counts.
Question 9 (10 points): Sum of Digits in an Integer
Write a Java program to calculate the sum of all digits in a given integer.
Algorithm:
- Read
the input integer.
- Initialize
a variable sum to 0.
- Use
a loop to extract each digit from the integer:
- Calculate
the last digit (remainder of the integer divided by 10).
- Add
the digit to sum.
- Update
the integer by removing the last digit (integer division by 10).
- sum
now contains the sum of digits.
Question 10 (10 points): GCD using Euclidean Algorithm
Write a Java program to find the Greatest Common Divisor (GCD) of two numbers
using the Euclidean algorithm.
Algorithm:
- Read
the two input numbers, a and b.
- Use
the Euclidean algorithm to calculate the GCD:
- If
b is 0, the GCD is a.
- Otherwise,
set temp to the remainder of a divided by b, then
set a to b and `b
Comments
Post a Comment