0% found this document useful (0 votes)
129 views2 pages

Cognizant Python Technical Questions

Uploaded by

Bhavya Shree
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)
129 views2 pages

Cognizant Python Technical Questions

Uploaded by

Bhavya Shree
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/ 2

Cognizant Technical Assessment - Python Questions

1. Palindrome Check
def is_palindrome(s):
return s == s[::-1]

print(is_palindrome("madam")) # Output: True

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

fibonacci(5) # Output: 0 1 1 2 3

3. Factorial Using Recursion


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

print(factorial(5)) # Output: 120

4. Prime Number Check


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

print(is_prime(7)) # Output: True

5. Count Vowels in a String


def count_vowels(s):
vowels = "aeiouAEIOU"
count = sum(1 for char in s if char in vowels)
return count

print(count_vowels("Cognizant")) # Output: 3
6. Reverse a String
def reverse_string(s):
return s[::-1]

print(reverse_string("python")) # Output: nohtyp

7. Remove Duplicates from List


def remove_duplicates(lst):
return list(set(lst))

print(remove_duplicates([1, 2, 2, 3, 3])) # Output: [1, 2, 3]

8. Min and Max in List


def min_max(lst):
return min(lst), max(lst)

print(min_max([10, 5, 3, 7, 9])) # Output: (3, 10)

9. List Comprehension - Squares of Even Numbers


nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares) # Output: [4, 16]

10. Word Count Using Dictionary


def word_count(text):
words = text.split()
count = {}
for word in words:
count[word] = count.get(word, 0) + 1
return count

print(word_count("hello world hello")) # {'hello': 2, 'world': 1}

You might also like