Java Code for CTS Exam
Priority model questions
1. Sum of cubes of all integers
2. Sum of squares of all integers
3. Sum of product of integers
4. Sum of prime numbers of integers
5. Sum of factorial of integers
6. Sum of all the elements
7. Sum of all the numbers from 1 to N
8. Check prime numbers or not
9. Leap year
10. Find the sum of highest elements
11. Reverse a word
12. Reverse a sentence
13. Sort character alphabetically
14. Find the sum of two elements pair
15. Rotate an array
16. Check palindrome
17. Find the duplicate numbers
18. Find the frequency of a character
19. Merge two sorted array
20. Merge two strings
generate java code simpler version for my cts exam.
Got it — you want simple Java code for these priority questions to prepare for your CTS (Cognizant)
exam.
I’ll write easy-to-understand Java code for each of your 20 points, keeping it beginner-friendly but
correct.
1. Sum of cubes of all integers from 1 to N
java
int sumOfCubes(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i * i * i;
}
return sum;
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/6
2. Sum of squares of all integers from 1 to N
java
int sumOfSquares(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i * i;
}
return sum;
}
3. Sum of product of integers (1*2 + 2*3 + 3*4 + ...)
java
int sumOfProducts(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
sum += i * (i + 1);
}
return sum;
}
4. Sum of prime numbers up to N
java
boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) return false;
}
return true;
}
int sumOfPrimes(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
}
5. Sum of factorials up to N
java
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
int sumOfFactorials(int n) {
int sum = 0;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/6
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
return sum;
}
6. Sum of all elements in an array
java
int sumOfElements(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
7. Sum of all numbers from 1 to N
java
int sumOneToN(int n) {
return n * (n + 1) / 2;
}
8. Check if a number is Prime
java
boolean isPrimeNumber(int num) {
if (num <= 1) return false;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) return false;
}
return true;
}
9. Check Leap Year
java
boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
10. Sum of highest elements (example: sum of two highest numbers)
java
int sumOfTwoHighest(int[] arr) {
[Link](arr);
int n = [Link];
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/6
return arr[n-1] + arr[n-2];
}
11. Reverse a Word
java
String reverseWord(String word) {
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversed += [Link](i);
}
return reversed;
}
12. Reverse a Sentence
java
String reverseSentence(String sentence) {
String[] words = [Link](" ");
String result = "";
for (int i = [Link] - 1; i >= 0; i--) {
result += words[i] + " ";
}
return [Link]();
}
13. Sort Characters Alphabetically
java
String sortCharacters(String str) {
char[] chars = [Link]();
[Link](chars);
return new String(chars);
}
14. Find Two Elements Pair with Given Sum
java
boolean findPairWithSum(int[] arr, int target) {
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] + arr[j] == target) {
return true;
}
}
}
return false;
}
15. Rotate an Array (One Position to Right)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/6
java
void rotateArray(int[] arr) {
int n = [Link];
int last = arr[n-1];
for (int i = n-1; i > 0; i--) {
arr[i] = arr[i-1];
}
arr[0] = last;
}
16. Check Palindrome
java
boolean isPalindrome(String str) {
int start = 0, end = [Link]() - 1;
while (start < end) {
if ([Link](start) != [Link](end)) return false;
start++;
end--;
}
return true;
}
17. Find Duplicate Numbers in Array
java
void findDuplicates(int[] arr) {
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] == arr[j]) {
[Link](arr[i]);
}
}
}
}
18. Find Frequency of Character
java
int characterFrequency(String str, char ch) {
int count = 0;
for (char c : [Link]()) {
if (c == ch) count++;
}
return count;
}
19. Merge Two Sorted Arrays
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/6
java
int[] mergeSortedArrays(int[] arr1, int[] arr2) {
int n = [Link], m = [Link];
int[] result = new int[n + m];
int i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (arr1[i] < arr2[j]) {
result[k++] = arr1[i++];
} else {
result[k++] = arr2[j++];
}
}
while (i < n) result[k++] = arr1[i++];
while (j < m) result[k++] = arr2[j++];
return result;
}
20. Merge Two Strings
java
String mergeStrings(String s1, String s2) {
return s1 + s2;
}
✅ All codes are simple, clean, ready for CTS-type exams.
Would you also like me to send a single file (.java) version combining all the methods into one for
easier revision? 🚀
(Example: a `[Link]` file with `main()` to test all of them.)
Shall I? 🎯
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/6