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

All questions of Patterns for Software Development Exam

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

What is the output of the following code?
for i in range(1, 6):
    print(' ' * (5 - i) + '* ' * i)
  • a)
    *
    * *
    * * *
    * * * *
    * * * * *
  • b)
    * *
    * * *
    * * * *
    * * * * *
    * * * * * *
  • c)
    *
    * * *
    * * * *
    * * * * *
  • d)
    * *
    * * *
    * * * *
    * * * * *
Correct answer is option 'A'. Can you explain this answer?

Majid Al Ali answered
Explanation:

The given code is using a loop to print a pattern of asterisks (*). Let's break down the code and analyze the output.

1. Loop:
The code uses a for loop to iterate from 1 to 5 (inclusive).
- The range(1, 6) generates the numbers 1, 2, 3, 4, and 5.

2. Print Statement:
Inside the loop, there is a print statement that generates the pattern of asterisks.
- The pattern consists of spaces and asterisks (*).
- The number of spaces in each line is determined by subtracting the current iteration number from 5 (5 - i).
- The number of asterisks in each line is equal to the iteration number (i).

3. Output:
Let's go through each iteration and see the output:

- Iteration 1:
- Spaces: 5 - 1 = 4
- Asterisks: 1
- Output: " *"

- Iteration 2:
- Spaces: 5 - 2 = 3
- Asterisks: 2
- Output: " * *"

- Iteration 3:
- Spaces: 5 - 3 = 2
- Asterisks: 3
- Output: " * * *"

- Iteration 4:
- Spaces: 5 - 4 = 1
- Asterisks: 4
- Output: " * * * *"

- Iteration 5:
- Spaces: 5 - 5 = 0
- Asterisks: 5
- Output: "* * * * *"

4. Final Output:
The final output is the combination of all the outputs from each iteration.
- " *"
- " * *"
- " * * *"
- " * * * *"
- "* * * * *"

Therefore, the correct answer is option 'A' which represents the pattern:
- *
- * *
- * * *
- * * * *
- * * * * *

What is the output of the following code?
for i in range(5):
    print('*' * i)
  • a)
    *
  • b)
    **
  • c)
    ***
  • d)
    ****
Correct answer is option 'C'. Can you explain this answer?

The output of the code will be:

c) ***

Explanation:
The code uses a for loop to iterate through the range(5), which generates the sequence [0, 1, 2, 3, 4]. For each iteration, the code prints a number of asterisks equal to the current value of i.

Let's go through each iteration to understand how the output is generated:

1. i = 0:
- The value of i is 0.
- The code prints "*" (an asterisk) since i is 0.
- The output so far: "*"

2. i = 1:
- The value of i is 1.
- The code prints "**" since i is 1.
- The output so far: "*" + "**" = "***"

3. i = 2:
- The value of i is 2.
- The code prints "***" since i is 2.
- The output so far: "***" + "***" = "******"

4. i = 3:
- The value of i is 3.
- The code prints "****" since i is 3.
- The output so far: "******" + "****" = "********"

5. i = 4:
- The value of i is 4.
- The code prints "*****" since i is 4.
- The output so far: "********" + "*****" = "*************"

Therefore, the final output of the code is "*****" (option c).

Which pattern can be used to match an email address format?
  • a)
    r"\w+@\w+.\w+"
  • b)
    r"\d{3}-\d{3}-\d{4}"
  • c)
    r"\b[A-Z][a-z]+\b"
  • d)
    r"^http://www\.\w+\.\w+$"
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The pattern "r"\w+@\w+.\w+"" matches an email address in the format "username@domain.extension" where "username", "domain", and "extension" consist of one or more word characters.

Given the following code, what will be the output?
import re
pattern = r"\b[A-Za-z]+\b"
text = "The quick brown fox jumps over the lazy dog."
result = re.findall(pattern, text)
print(len(result))
 
  • a)
    6
  • b)
    8
  • c)
    9
  • d)
    10
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The regular expression pattern "\b[A-Za-z]+\b" matches words in the given text. The findall() function returns a list of all matched words, and len(result) gives the count of matched words.

What will be the output of the following code?
import re
pattern = r"\b\d{3}-\d{3}-\d{4}\b"
text = "Contact me at 123-456-7890"
result = re.search(pattern, text)
print(result.group())
  • a)
    123-456-7890
  • b)
    123
  • c)
    456
  • d)
    Error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The regular expression pattern "\b\d{3}-\d{3}-\d{4}\b" will match a phone number in the format "XXX-XXX-XXXX". The search() function returns the first occurrence that matches the pattern.

What will be the output of the following code?
import re
pattern = r"\d{2}-\d{2}-\d{4}"
text = "DOB: 01-01-2000, SSN: 123-45-6789"
result = re.findall(pattern, text)
print(result)
 
  • a)
    []
  • b)
    ['01-01-2000', '123-45-6789']
  • c)
    ['01', '01', '2000', '123', '45', '6789']
  • d)
    Error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The regular expression pattern "\d{2}-\d{2}-\d{4}" matches two digits, followed by a hyphen, followed by two digits, another hyphen, and four digits. The findall() function returns a list of all matched patterns in the text.

Which regular expression pattern can be used to match a URL starting with "https://"?
  • a)
    r"https://\w+.\w+.\w+"
  • b)
    r"http://\w+.\w+.\w+"
  • c)
    r"\b[A-Z][a-z]+\b"
  • d)
    r"^https://\w+.\w+.\w+$"
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The pattern "r"^https://\w+.\w+.\w+$"" matches a URL starting with "https://" followed by a domain and extension. The caret (^) matches the start of the string, and the dollar sign ($) matches the end of the string.

Chapter doubts & questions for Patterns - 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 Patterns - 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