All Exams  >   Software Development  >   Basics of Python  >   All Questions

All questions of Strings for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

What will be the output of the following code?
str = "Python Programming"
print(str.index("Prog"))
  • a)
    7
  • b)
    10
  • c)
    11
  • d)
    Error: substring not found
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The index() method returns the index of the first occurrence of a specified substring within a string. In this case, it returns the index of the substring "Prog" in the string "Python Programming", which is 11.

What will be the output of the following code?
str = "Python"
print(str[1:4])
  • a)
    Pyt
  • b)
    yth
  • c)
    tho
  • d)
    pyt
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The slicing notation str[start:end] is used to extract a substring from a string. In this case, str[1:4] returns the characters at indices 1, 2, and 3, resulting in "yth".

What is the output of the following code snippet?
message = "Welcome to Python"
print(message.split())
  • a)
    ['Welcome', 'to', 'Python']
  • b)
    'Welcome to Python'
  • c)
    Welcome to Python
  • d)
    Welcome, to, Python
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The split() method is used to split a string into a list of substrings based on the specified delimiter. If no delimiter is provided, it splits the string at whitespace characters by default.

What will be the output of the following code?
str1 = "Hello"
str2 = "hello"
print(str1 == str2)
  • a)
    True
  • b)
    False
  • c)
    Error: Case-sensitive comparison
  • d)
    Error: Invalid syntax
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
String comparisons are case-sensitive in Python. In this case, "Hello" and "hello" are different strings, so the comparison str1 == str2 evaluates to False.

What is the output of the following code snippet?
text = "Python is fun"
print(text[7:])
  • a)
    is fun
  • b)
    fun
  • c)
    is
  • d)
    Error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The slice notation [7:] returns a substring starting from index 7 until the end of the string. In this case, the output is "is fun".

What is the output of the following code snippet?
text = "Hello, World!"
print(text[::-1])
  • a)
    Hello, World!
  • b)
    dlroW ,olleH
  • c)
    !dlroW ,olleH
  • d)
    Error
Correct answer is option 'B'. Can you explain this answer?

"The output of the code snippet is missing as the code is incomplete. The variable 'text' is not given any value or assigned any operation, so there is no output to determine."

What is the result of the following expression?
"Python" + "Programming"
  • a)
    PythonProgramming
  • b)
    Python Programming
  • c)
    Python + Programming
  • d)
    Error: Cannot concatenate strings
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The + operator is used for concatenating strings in Python. In this case, it concatenates the strings "Python" and "Programming" to form "PythonProgramming".

What will be the output of the following code?
str = "Python Programming"
print(str.replace("Python", "Java"))
  • a)
    Java Programming
  • b)
    Python Java
  • c)
    Python Programming
  • d)
    Error: Strings are immutable
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The replace() method replaces occurrences of a specified substring with another substring in a string. In this case, it replaces "Python" with "Java" in the string "Python Programming".

What will be the output of the following code?
str = "Python Programming"
print(str.count("P"))
  • a)
    1
  • b)
    2
  • c)
    0
  • d)
    Error: Invalid syntax
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The count() method returns the number of occurrences of a specified substring in a string. In this case, it counts the number of occurrences of the letter "P" in the string "Python Programming", which is 1.

Which of the following methods is used to convert a string to uppercase in Python?
  • a)
    toUpper()
  • b)
    upper()
  • c)
    uppercase()
  • d)
    to_uppercase()
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The upper() method is used to convert a string to uppercase in Python. It returns a new string with all alphabetic characters converted to uppercase.

What will be the output of the following code?
str = "Python"
print(str[-2:])
  • a)
    th
  • b)
    on
  • c)
    Py
  • d)
    thon
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
Negative indexing is used to access characters from the end of a string. str[-2:] returns the last two characters of the string "Python", which are "on".

How can you determine the length of a string in Python?
  • a)
    len(string)
  • b)
    string.length()
  • c)
    string.len()
  • d)
    length(string)
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The len() function returns the length of a string in Python. It can be used to determine the number of characters in a string.

What does the join() method do in Python?
  • a)
    Concatenates two strings
  • b)
    Splits a string into a list of substrings
  • c)
    Reverses a string
  • d)
    Joins the elements of a list into a single string
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The join() method is used to join the elements of a list into a single string. It takes a list as an argument and returns a string where the elements of the list are concatenated with the specified separator.

Which of the following methods can be used to convert a string into a list in Python?
  • a)
    split()
  • b)
    list()
  • c)
    splitlist()
  • d)
    convert()
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The split() method is used to split a string into a list of substrings based on the specified delimiter. It returns a list of substrings.

What is the output of the following code snippet?
name = "John"
print(name[1:3])
  • a)
    J
  • b)
    o
  • c)
    oh
  • d)
    n
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The slice notation name[1:3] returns the characters at index 1 and 2 of the string name, excluding the character at index 3. Therefore, the output is "oh".

What will be the output of the following code?
 
  • a)
    True
  • b)
    False
  • c)
    Error: Invalid syntax
  • d)
    Error: String doesn't support startswith() method
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The startswith() method returns True if a string starts with a specified substring. In this case, "Hello World" starts with "Hello", so the result is True.

Which method is used to determine the length of a string in Python?
  • a)
    length()
  • b)
    count()
  • c)
    size()
  • d)
    len()
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The len() function is used to determine the length of a string. It returns the number of characters in the string.

What is the output of the following code snippet?
text = "Hello,World"
print(text.split(","))
  • a)
    ['Hello', 'World']
  • b)
    ['Hello,World']
  • c)
    'Hello', 'World'
  • d)
    Error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The split() method is used to split a string into a list of substrings based on the specified delimiter. In this case, the string is split at the comma (",") character, resulting in the list ['Hello', 'World'].

Which of the following is the correct way to define a string in Python?
  • a)
    str = "Hello"
  • b)
    str = 'Hello'
  • c)
    str = Hello
  • d)
    str = [H, e, l, l, o]
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
In Python, strings can be defined using single quotes ('') or double quotes (""). Option (b) demonstrates the correct way to define a string.

Which of the following methods is used to remove leading and trailing whitespaces from a string in Python?
  • a)
    remove()
  • b)
    strip()
  • c)
    trim()
  • d)
    clean()
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The strip() method is used to remove leading and trailing whitespaces from a string in Python. It does not modify the original string but returns a new string with the whitespaces removed.

What is the output of the following code snippet?
text = "Python is fun"
print(text.startswith("Java"))
  • a)
    True
  • b)
    False
  • c)
    Error
  • d)
    None
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The startswith() method is used to check if a string starts with a specified prefix. In this case, the output is False because the string "Python is fun" does not start with "Java".

What is the output of the following code snippet?
text = "Python is fun"
print(text.find("is"))
  • a)
    0
  • b)
    2
  • c)
    7
  • d)
    -1
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The find() method returns the index of the first occurrence of a specified substring in a string. If the substring is not found, it returns -1. In this case, the substring "is" is found at index 7.

What will be the output of the following code?
str = "Python Programming"
print(str[7])
  • a)
    P
  • b)
    m
  • c)
    g
  • d)
    R
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
In Python, strings can be accessed using indexing. The index starts from 0, so str[7] refers to the character at index 7, which is "P".

What will be the output of the following code?
str = "Hello World"
print(str.split())
  • a)
    ['Hello', 'World']
  • b)
    Hello World
  • c)
    ['Hello World']
  • d)
    ['Hello', ' ', 'World']
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The split() method splits a string into a list of substrings based on whitespace by default. In this case, it splits the string "Hello World" into the list ['Hello', 'World'].

Chapter doubts & questions for Strings - Basics of Python 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Strings - Basics of Python in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of Python

49 videos|38 docs|18 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev