arrow
arrow
arrow
What will be the output of the following Python code snippet?
Question



What will be the output of the following Python code snippet?

A.

LLUB

B.

BULL

C.

LULB

D.

UBLL

E.

None of the above

Correct option is A

Let’s analyze the code:
· The for loop iterates through each character in the string Str.
· item.upper() converts the current character to uppercase.
· Each uppercase character is added before the existing value of r.
· This effectively reverses the string while converting it to uppercase.
Step-by-step Execution:
1. Initial r = ""
2. First Iteration (item = 'b'): r = "B" + "" → r = "B"
3. Second Iteration (item = 'u'): r = "U" + "B" → r = "UB"
4. Third Iteration (item = 'l'): r = "L" + "UB" → r = "LUB"
5. Fourth Iteration (item = 'l'): r = "L" + "LUB" → r = "LLUB"
Final Output: LLUB
Important Key Points:
1. Strings in Python are immutable, but concatenation creates new string objects.
2. The upper() method converts characters to uppercase.
3. Adding characters before the existing string reverses the order.
Knowledge Booster:
· lower(): Converts a string to lowercase.
· reversed(): Reverses a sequence but requires explicit joining for strings.
· join(): Combines elements of an iterable into a single string.

Free Tests

Free
Must Attempt
Video Solutions

RBI Assistant Pre 2026 Full Mock Test -01

languageIcon English
  • pdpQsnIcon100 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English
Free
Must Attempt
Video Solutions

RBI Asst Prelims 2026 : Reasoning Section Test 01

languageIcon English
  • pdpQsnIcon35 Questions
  • pdpsheetsIcon35 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt
Video Solutions

RBI Office Attendant 2026 Full Mock Test - 01

languageIcon English
  • pdpQsnIcon120 Questions
  • pdpsheetsIcon120 Marks
  • timerIcon90 Mins
languageIcon English
test-prime-package

Access ‘IBPS RRB SO IT Officer (Scale-II)’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow