Table of contents | |
Multiple Choice Questions (MCQs) | |
Higher Order Thinking Questions (HOTS) | |
Fill in the Blanks | |
True/False | |
Hands-On Questions |
Q.1. Which of the following is NOT a valid method to concatenate two strings in Python?
(a) Using the + operator
(b) Using the join() method
(c) Using the concat() function
(d) Using formatted string literals
Ans. (c)
Q.2. Which of the following methods can be used to check if a string starts with a specific substring in Python?
(a) startswith()
(b) endswith()
(c) contains()
(d) find()
Ans. (a)
Q.3. What will be the output of the following code snippet?
string = "Hello, World!"
print(string[3:8])
(a) Hello
(b) lo, Wo
(c) lo, W
(d) lo, Wor
Ans. (c)
Q.4. Which of the following methods can be used to convert a string to uppercase in Python?
(a) to_upper()
(b) upper()
(c) uppercase()
(d) capitalize()
Ans. (b)
Q.5. What will be the output of the following code snippet?
string = "Hello, World!"
print(string.split(","))
(a) ['Hello', ' World!']
(b) ['Hello,', ' World!']
(c) ['Hello, World!']
(d) ['Hello', 'World!']
Ans. (a)
Q.1. Write a Python program that takes a string as input and counts the number of uppercase letters in it.
def count_uppercase(string):
count = 0
for char in string:
if char.isupper():
count += 1
return count
Q.2. Write a Python function that checks if a given string is a palindrome or not. A palindrome is a string that reads the same forwards and backwards, ignoring spaces, punctuation, and capitalization.
def is_palindrome(string):
cleaned_string = ''.join(char.lower() for char in string if char.isalpha())
return cleaned_string == cleaned_string[::-1]
Q.3. Write a Python program that takes a sentence as input and returns the words in reverse order. For example, if the input sentence is "Hello, how are you?", the program should return "you? are how Hello,".
def reverse_words(sentence):
words = sentence.split()
return ' '.join(words[::-1])
Q.4. Write a Python program to remove all the vowels from a given string.
def remove_vowels(string):
vowels = 'aeiouAEIOU'
return ''.join(char for char in string if char not in vowels)
Q.5. Write a Python program to find the longest word in a given string.
def longest_word(string):
words = string.split()
return max(words, key=len)
Q.1. The method __________ returns the number of occurrences of a substring in a string.
Ans. count()
Q.2. The __________ method is used to remove leading and trailing whitespace from a string.
Ans. strip()
Q.3. The method __________ returns a new string in which all occurrences of a substring in the original string are replaced with another substring.
Ans. replace()
Q.4. The __________ method is used to convert a string to a list of substrings, split by a specified delimiter.
Ans. split()
Q.5. The __________ method can be used to check if a given string contains only numeric characters.
Ans. isdigit()
Q.1. In Python, strings are immutable, meaning they cannot be changed once created.
Ans. True
Q.2. The strip() method removes all occurrences of a specified character from a string.
Ans. False
Q.3. The find() method returns the index of the first occurrence of a specified substring in a string.
Ans. True
Q.4. The isnumeric() method returns True if all characters in a string are numeric.
Ans. False
Q.5. Python uses zero-based indexing for strings, where the first character is at index 1.
Ans. False
Q.1. Write a Python program that takes a string as input and prints the string in reverse order.
def reverse_string(string):
return string[::-1]
Q.2. Write a Python program that takes two strings as input and checks if they are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of another word or phrase.
def is_anagram(string1, string2):
return sorted(string1.lower()) == sorted(string2.lower())
Q.3. Write a Python program that takes a sentence as input and counts the number of words in it.
def count_words(sentence):
words = sentence.split()
return len(words)
Q.4. Write a Python program that takes a string as input and prints the frequency of each character in the string.
def char_frequency(string):
frequency = {}
for char in string:
frequency[char] = frequency.get(char, 0) + 1
return frequency
Q.5. Write a Python program that takes a string as input and checks if it is a valid email address. Assume a valid email address contains a username, followed by the @ symbol, and then a domain name.
import re
def is_valid_email(email):
pattern = r'^[\w\.-]+@\w+\.\w+$'
return bool(re.match(pattern, email))
49 videos|38 docs|18 tests
|
|
Explore Courses for Software Development exam
|