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:

  1. Initialize variables a, b, and sum to 0, 1, and 0, respectively.
  2. Print the initial values of a and b.
  3. 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.
  4. 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:

  1. Read the input number.
  2. Calculate the number of digits in the input number.
  3. Initialize a variable temp to store a temporary copy of the input number.
  4. Initialize a variable sum to 0.
  5. Use a loop to extract each digit from temp:
    • Calculate the digit^numberOfDigits for each digit.
    • Add the result to sum.
  6. 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:

  1. Read the lower and upper bounds of the range.
  2. Initialize a variable isPrime to true.
  3. 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:

  1. Read the input string.
  2. Initialize an empty string reverseStr.
  3. Use a loop to iterate through the characters of the input string from the end to the beginning:
    • Append each character to reverseStr.
  4. 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:

  1. Read the input number.
  2. Create a recursive function calculateFactorial that takes an integer parameter n.
  3. In the function:
    • If n is 0 or 1, return 1 (base case).
    • Otherwise, return n * calculateFactorial(n - 1).
  4. 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:

  1. Read the array of integers.
  2. Initialize variables largest and smallest to the first element of the array.
  3. 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.
  4. 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:

  1. Read the input string and convert it to lowercase.
  2. Initialize variables vowelCount and consonantCount to 0.
  3. 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.
  4. 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:

  1. Read the input integer.
  2. Initialize a variable sum to 0.
  3. 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).
  4. 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:

  1. Read the two input numbers, a and b.
  2. 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

Top of Form

 

                                                          ----------*****-----------

Comments

Popular posts from this blog

FrontEnd - FAQs - Part 1

Java Script - FAQs

CoreJava - ClassesObjectsMethods - Assignment