All Exams  >   EmSAT Achieve  >   Python for EmSAT Achieve  >   All Questions

All questions of Patterns for EmSAT Achieve Exam

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

The output of the code is option 'A': 12 1 23 2 1 2 34 3 2 1 2 3 45 4 3 2 1 2 3 4 5.

Explanation:
- The code uses a for loop to iterate through the range from 1 to 6, with the variable 'i' representing each iteration.
- Inside the loop, there is a print statement that combines multiple join operations.
- The first join operation is `(5 - i).join(str(j) for j in range(i, 0, -1))`. Let's break it down:
- `range(i, 0, -1)` generates a sequence of numbers starting from 'i' and going down to 1 in reverse order.
- `str(j) for j in range(i, 0, -1)` converts each number in the sequence to a string.
- `(5 - i).join(...)` joins the string representation of the numbers using `(5 - i)` as the separator. The value of `(5 - i)` decreases as 'i' increases, creating a decreasing number of separators.
- The second join operation is `join(str(j) for j in range(2, i-1))`. Let's break it down:
- `range(2, i-1)` generates a sequence of numbers starting from 2 and going up to 'i-1'.
- `str(j) for j in range(2, i-1)` converts each number in the sequence to a string.
- `join(...)` joins the string representation of the numbers using an empty string as the separator.
- The result of the two join operations is then printed on each iteration of the loop.

Here's a breakdown of the output for each iteration of the loop:

- i = 1:
- `(5 - i).join(str(j) for j in range(i, 0, -1))` evaluates to '1' (no separator).
- `join(str(j) for j in range(2, i-1))` evaluates to an empty string.
- The print statement outputs '1'.

- i = 2:
- `(5 - i).join(str(j) for j in range(i, 0, -1))` evaluates to '2 1' (separator: ' ').
- `join(str(j) for j in range(2, i-1))` evaluates to an empty string.
- The print statement outputs '2 1'.

- i = 3:
- `(5 - i).join(str(j) for j in range(i, 0, -1))` evaluates to '3 2 1' (separator: ' ').
- `join(str(j) for j in range(2, i-1))` evaluates to '2' (no separator).
- The print statement outputs '3 2 1 2'.

- i = 4:
- `(5 - i).join(str(j) for j in range(i, 0, -1))` evaluates to '4 3 2 1' (separator: ' ').
- `join(str(j) for j in range(2, i-1))` evaluates to '2 3' (separator: ' ').

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 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:
- *
- * *
- * * *
- * * * *
- * * * * *

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.

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.

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"\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.

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

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

Python for EmSAT Achieve

57 videos|39 docs|18 tests

Top Courses EmSAT Achieve