0% found this document useful (0 votes)
7 views

Python Interview Questions

The document outlines a Python assignment consisting of 30 tasks that require writing functions to solve various problems, such as finding the second largest element in a list, merging two lists alternately, and checking for unique characters in a string. Each task includes specific input and output requirements, as well as exceptions that need to be handled for invalid inputs. The assignment covers a range of topics including string manipulation, list operations, mathematical calculations, and data structure handling.

Uploaded by

Lohith Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Interview Questions

The document outlines a Python assignment consisting of 30 tasks that require writing functions to solve various problems, such as finding the second largest element in a list, merging two lists alternately, and checking for unique characters in a string. Each task includes specific input and output requirements, as well as exceptions that need to be handled for invalid inputs. The assignment covers a range of topics including string manipulation, list operations, mathematical calculations, and data structure handling.

Uploaded by

Lohith Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Assignment

1. Find the Second Largest Element in a List

 Question: Write a function to find the second largest element in a list of numbers.
 Input: A list of numbers.
 Output: The second largest number.
 Requirements: Handle exceptions for lists with fewer than two elements.

2. Merge Two Lists Alternately

 Question: Write a function that merges two lists by taking elements alternately from
each list.
 Input: Two lists of equal length.
 Output: A merged list with alternating elements.
 Requirements: Handle exceptions if the lists are of different lengths.

3. Check if All Characters are Unique

 Question: Write a function to check if all characters in a given string are unique.
 Input: A string.
 Output: "Unique" or "Not Unique".
 Requirements: Handle exceptions for non-string inputs.

4. Group Anagrams Together

 Question: Write a function to group anagrams from a list of strings.


 Input: A list of strings.
 Output: A list of lists where each sublist contains anagrams.
 Requirements: Handle exceptions for non-string elements.

5. Count the Frequency of Elements in a List

 Question: Write a function to count the frequency of each element in a list.


 Input: A list of elements.
 Output: A dictionary where keys are elements and values are their frequencies.
 Requirements: Handle exceptions for invalid inputs.

6. Find the Missing Number in a Sequence

 Question: Write a function to find the missing number in a sequence of numbers from
1 to n.
 Input: A list of numbers with one missing element.
 Output: The missing number.
 Requirements: Handle exceptions for lists with no missing elements.

7. Sum of Elements in a Tuple

 Question: Write a function to calculate the sum of all elements in a tuple.


 Input: A tuple of numbers.
 Output: The sum of the elements.
 Requirements: Handle exceptions for non-numeric elements.

8. Find the Longest Word in a Sentence

 Question: Write a function to find the longest word in a sentence.


 Input: A sentence.
 Output: The longest word in the sentence.
 Requirements: Handle exceptions for non-string inputs.

9. Find Intersection of Two Lists

 Question: Write a function to find the intersection of two lists.


 Input: Two lists.
 Output: A list containing the common elements.
 Requirements: Handle exceptions for invalid inputs.

10. Check for Substring

 Question: Write a function to check if one string is a substring of another.


 Input: Two strings.
 Output: "Substring" or "Not Substring".

11. String Palindrome Check

 Question: Write a function to check if a given string is a palindrome.


 Input: A string.
 Output: "Palindrome" or "Not Palindrome".
 Requirements: Handle exceptions for non-string inputs.

12. Temperature Conversion

 Question: Write a function to convert temperature between Celsius and Fahrenheit.


 Input: A temperature and the unit (C/F).
 Output: The converted temperature.
 Requirements: Handle invalid unit inputs and non-numeric temperature values.

13. Simple Calculator

 Question: Write a function-based calculator to perform basic arithmetic operations


(+, -, *, /).
 Input: Two numbers and an operator.
 Output: The result of the operation.
 Requirements: Handle division by zero and invalid operator exceptions.

14. Reverse a String

 Question: Write a function to reverse a given string.


 Input: A string.
 Output: The reversed string.
 Requirements: Handle exceptions for non-string inputs.

15. Count Vowels in a String

 Question: Write a function to count the number of vowels in a given string.


 Input: A string.
 Output: The number of vowels.
 Requirements: Handle exceptions for non-string inputs.

16. Check Armstrong Number

 Question: Write a function to check if a number is an Armstrong number.


 Input: An integer.
 Output: "Armstrong" or "Not Armstrong".
 Requirements: Handle exceptions for non-integer inputs.

17. Generate Multiplication Table

 Question: Write a function to generate and print the multiplication table for a given
number.
 Input: An integer.
 Output: The multiplication table for the number.
 Requirements: Handle exceptions for non-integer inputs.

18. Greatest Common Divisor (GCD)

 Question: Write a function to find the GCD of two numbers.


 Input: Two integers.
 Output: The GCD of the two numbers.
 Requirements: Handle exceptions for non-integer inputs.

19. Convert List to Set

 Question: Write a function that converts a list to a set, removing duplicate elements.
 Input: A list.
 Output: A set with unique elements.
 Requirements: Handle exceptions if the input is not a list.

20. Remove Vowels from String

 Question: Write a function to remove all vowels from a given string.


 Input: A string.
 Output: The string without vowels.
 Requirements: Handle exceptions for non-string inputs.

21. Merge Two Dictionaries


 Question: Write a function to merge two dictionaries into one.
 Input: Two dictionaries.
 Output: A merged dictionary.
 Requirements: Handle exceptions for invalid inputs.

22. Find Maximum Element in List

 Question: Write a function to find the maximum element in a list.


 Input: A list of numbers.
 Output: The maximum number.
 Requirements: Handle exceptions for empty lists or non-numeric elements.

23. Calculate Simple Interest

 Question: Write a function to calculate simple interest given the principal, rate of
interest, and time.
 Input: Three numbers representing principal, rate, and time.
 Output: The calculated simple interest.
 Requirements: Handle exceptions for invalid inputs.

24. Count Words in a Sentence

 Question: Write a function to count the number of words in a given sentence.


 Input: A sentence.
 Output: The word count.
 Requirements: Handle exceptions for non-string inputs.

25. Check for Anagram

 Question: Write a function to check if two strings are anagrams of each other.
 Input: Two strings.
 Output: "Anagram" or "Not Anagram".
 Requirements: Handle exceptions for non-string inputs.

26. Count Occurrences of a Character

 Question: Write a function to count the number of times a specific character appears
in a string.
 Input: A string and a character.
 Output: The count of occurrences.
 Requirements: Handle exceptions for non-string inputs.

27. Flatten a Nested List

 Question: Write a function to flatten a nested list into a single list.


 Input: A nested list.
 Output: A flat list.
 Requirements: Handle exceptions for non-list inputs.
28. Find the Unique Elements in a List

 Question: Write a function to find all unique elements in a list, excluding duplicates.
 Input: A list of elements.
 Output: A list of unique elements.
 Requirements: Handle exceptions for invalid inputs (e.g., non-list inputs).

29. Convert a String to Title Case

 Question: Write a function to convert a given string to title case (capitalize the first letter of
each word).
 Input: A string.
 Output: The string in title case.
 Requirements: Handle exceptions for non-string inputs.

30. Calculate Compound Interest

 Question: Write a function to calculate compound interest given the principal, rate of
interest, time, and number of times interest is compounded per year.
 Input: Four numbers representing principal, rate, time, and the number of times interest is
compounded per year.
 Output: The calculated compound interest.
 Requirements: Handle exceptions for invalid inputs (e.g., negative numbers).

You might also like