Skip to content
Question

Q.What possible output(s) are expected to be displayed on screen at the time of execution of the program from the following code? Also specify the minimum values that can be assigned to each of the variables BEGIN and LAST. import random VALUES=[10,20,30,40,50,60,70,80] BEGIN=random.randint(1,3) LAST=random.randint(BEGIN,4) for I in range(BEGIN,LAST+1): print VALUES[I],"-",

(i) 30 - 40 - 50 -
(ii) 10 - 20 - 30 - 40 -
(iii) 30 - 40 - 50 - 60 -
(iv) 30 - 40 - 50 - 60 - 70 -
CBSECBSE Class XII Board 2019Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The code prints a slice of the list VALUES from index BEGIN to index LAST, and the possible outputs depend on the random values chosen for BEGIN and LAST.

Let’s walk through this carefully. The code imports the random module and defines a list called VALUES with eight elements: 10, 20, 30, 40, 50, 60, 70, 80. The variable BEGIN is assigned a random integer between 1 and 3 (inclusive). So BEGIN can be 1, 2, or 3. Then LAST is assigned a random integer between BEGIN and 4 (inclusive). That means if BEGIN is 1, LAST can be 1, 2, 3, or 4. If BEGIN is 2, LAST can be 2, 3, or 4. If BEGIN is 3, LAST can be 3 or 4.

The for loop runs from I = BEGIN to I = LAST (inclusive), and for each value of I, it prints VALUES[I] followed by a hyphen and a space. Remember that Python list indices start at 0, so VALUES[0] is 10, VALUES[1] is 20, VALUES[2] is 30, VALUES[3] is 40, VALUES[4] is 50, VALUES[5] is 60, VALUES[6] is 70, and VALUES[7] is 80.

Now, what are the possible sequences printed? Let’s list all combinations:

  • If BEGIN = 1 and LAST = 1: prints VALUES[1] = 20 → output: 20 -
  • If BEGIN = 1 and LAST = 2: prints VALUES[1] and VALUES[2] → 20 - 30 -
  • If BEGIN = 1 and LAST = 3: prints VALUES[1], VALUES[2], VALUES[3] → 20 - 30 - 40 -
  • If BEGIN = 1 and LAST = 4: prints VALUES[1] through VALUES[4] → 20 - 30 - 40 - 50 -
  • If BEGIN = 2 and LAST = 2: prints VALUES[2] = 30 → 30 -
  • If BEGIN = 2 and LAST = 3: prints VALUES[2] and VALUES[3] → 30 - 40 -
  • If BEGIN = 2 and LAST = 4: prints VALUES[2], VALUES[3], VALUES[4] → 30 - 40 - 50 -
  • If BEGIN = 3 and LAST = 3: prints VALUES[3] = 40 → 40 -
  • If BEGIN = 3 and LAST = 4: prints VALUES[3] and VALUES[4] → 40 - 50 -
Note

The output always ends with a hyphen and a space, and there is no newline between items — the print statement with a comma at the end keeps them on the same line.

Now look at the four options given in the question:

(i) 30 - 40 - 50 - …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.