0% found this document useful (0 votes)
32 views4 pages

CGI Coding Questions With Answers

The document contains a series of coding questions along with their solutions in Python and Java. It covers various topics such as checking for palindromes, reversing strings, calculating factorials, finding prime numbers, and more. Each question is presented with both Python and Java code implementations for clarity and comparison.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

CGI Coding Questions With Answers

The document contains a series of coding questions along with their solutions in Python and Java. It covers various topics such as checking for palindromes, reversing strings, calculating factorials, finding prime numbers, and more. Each question is presented with both Python and Java code implementations for clarity and comparison.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CGI Coding Questions with Answers

Q1) Check if a string is a palindrome.


Python:
def is_palindrome(s):
return s == s[::-1]

print(is_palindrome("madam")) # True

Java:
public boolean isPalindrome(String s) {
return s.equals(new StringBuilder(s).reverse().toString());
}

Q2) Reverse a string.


Python:
def reverse_string(s):
return s[::-1]

Java:
public String reverseString(String s) {
return new StringBuilder(s).reverse().toString();
}

Q3) Find the factorial of a number.


Python:
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)

Java:
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

Q4) Check if a number is prime.


Python:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True

Java:
boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
CGI Coding Questions with Answers

if (n % i == 0) return false;
}
return true;
}

Q5) Find duplicate elements in an array.


Python:
def find_duplicates(arr):
seen = set()
duplicates = set(x for x in arr if x in seen or seen.add(x))
return list(duplicates)

Java:
Set<Integer> findDuplicates(int[] arr) {
Set<Integer> seen = new HashSet<>();
Set<Integer> dupes = new HashSet<>();
for (int num : arr) {
if (!seen.add(num)) {
dupes.add(num);
}
}
return dupes;
}

Q6) Print Fibonacci series up to N terms.


Python:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

Java:
void fibonacci(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int temp = a;
a = b;
b = temp + b;
}
}

Q7) Find second largest element in array.


Python:
def second_largest(arr):
unique = list(set(arr))
unique.sort()
CGI Coding Questions with Answers

return unique[-2]

Java:
int secondLargest(int[] arr) {
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
return second;
}

Q8) Reverse a number.


Python:
def reverse_number(n):
return int(str(n)[::-1])

Java:
int reverseNumber(int n) {
int rev = 0;
while (n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}

Q9) Check if number is Armstrong.


Python:
def is_armstrong(n):
num = str(n)
return sum(int(d)**len(num) for d in num) == n

Java:
boolean isArmstrong(int n) {
int original = n, result = 0, digits = String.valueOf(n).length();
while (n > 0) {
int digit = n % 10;
result += Math.pow(digit, digits);
n /= 10;
}
return result == original;
}
CGI Coding Questions with Answers

Q10) Find GCD of two numbers.


Python:
def gcd(a, b):
while b:
a, b = b, a % b
return a

Java:
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

You might also like