Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Consider the statements given below and then choose the correct output from the given options:

pride="#G20 Presidency"

print(pride[-2:2:-2])

Options

a. ndsr

b. ceieP0

c. ceieP

d. yndsr

Answer

Answer by student:

b. ceieP0

Detailed answer by teachoo:

To answer this question, we need to know the syntax and usage of strings and slicing in Python.

  • A string is a sequence of characters enclosed in single quotes (‘’) or double quotes (“”). For example, pride = “#G20 Presidency” is a string that assigns the value “#G20 Presidency” to the variable pride.
  • Slicing is a technique that allows us to access a part of a string or a list by specifying the start, stop, and step values. The syntax of slicing is:

string_name[start:stop:step]

    • where string_name is the name of the string, start is the index of the first character to be included, stop is the index of the first character to be excluded, and step is the number of characters to skip. For example, pride[1:4:1] will return “G20” from the string pride.
    • If the start value is omitted , it defaults to 0, which is the beginning of the string. If the stop value is omitted , it defaults to the length of the string, which is the end of the string. If the step value is omitted , it defaults to 1, which means no skipping. For example, pride[:4] is equivalent to pride[0:4:1] and will return “#G20”.
    • If the step value is negative , it means slicing from right to left, or in reverse order. In this case, the start value should be greater than the stop value, otherwise an empty string will be returned. For example, pride[4:1:-1] will return “02G” from the string pride.

In this question, we have a string pride = “#G20 Presidency” and we want to find the output of print(pride[-2:2:-2]).

Let’s go through each of the options and see why they are correct or incorrect:

  • a. ndsr: This is incorrect because it does not match the output of slicing with negative step value. The characters “n”, “d”, “s”, and “r” are not in the string pride at all.
  • b. ceieP0: This is correct because it matches the output of slicing with negative step value. The start value -2 means the second last character of the string, which is “0”. The stop value 2 means the third character of the string, which is “G”. The step value -2 means skipping every other character from right to left. Therefore, slicing from “0” to “G” with step -2 will return “ceieP0”.
  • c. ceieP: This is incorrect because it does not match the output of slicing with negative step value. The last character of the output should be “0”, not “P”.
  • d. yndsr: This is incorrect because it does not match the output of slicing with negative step value. The characters “y”, “n”, “d”, “s”, and “r” are not in the string pride at all.

So, the correct answer is b. ceieP0.

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo