Class XI Computer Science (083)

Minimum Learning Support Material · CBSE 2026-27

Notes · Labs · Run-checked programs · 1423 solved questions · Flashcards · Quiz · Practice Paper Maker

Designed by NareN, PGT (Computer Science), PM SHRI K V Shalimar Bagh, Delhi-88 (Shift-I) ✨ Click Here For Your Valuable Feedback ✨
1

Know the paper first

Theory 70 marks, 3 hours, 37 questions in five sections

SectionQuestionsMarks eachTotalWhat comes
AQ1 to Q21121MCQ and True/False on all units; Q20 and Q21 are Assertion-Reason
BQ22 to Q28214Unit I (Q22), Python explain (Q23), output (Q24, Q27), error correction (Q26), society (Q25, Q28)
CQ29 to Q3139Society situations, program output, number or pattern program
DQ32 to Q35416Conversions and truth table, cyber case study, string program, list or dictionary program
EQ36, Q37510Long Python programs (lists, dictionaries, menus)

Unit-wise marks (theory 70)

Blueprint of the paper

One common blueprint for all twenty practice papers of this kit. Each cell shows the number of questions, with their marks in brackets.

UnitSec A1 mark each1 mSec B2 marks each2 mSec C3 marks each3 mSec D4 marks each4 mSec E5 marks each5 mTotalmarks
Unit IComputer Systems and Organisation4 (4)1 (2)-1 (4)-10
Unit IIComputational Thinking and Programming-113 (13)4 (8)2 (6)2 (8)2 (10)45
Unit IIISociety, Law and Ethics4 (4)2 (4)1 (3)1 (4)-15
Total21 (21)7 (14)3 (9)4 (16)2 (10)70

CBSE holds no board exam for Class XI, so it gives no Class XI sample paper. This design follows the CBSE Class XII Computer Science Sample Question Paper 2026-27 (37 questions, 70 marks, 3 hours), with the Class XI unit marks 10, 45 and 15. Schools may set their own design.

The fixed slots: know them in advance

Q22Number system, memory or logic2 marks
Q26Correct the errors in code2 marks
Q29Society: three situations3 marks
Q30Output of a program3 marks
Q31Number or pattern program3 marks
Q32Conversions + truth table4 marks
Q33Cyber case study4 marks
Q34String program4 marks
Q35List / dictionary program4 marks
Q36, Q37Long programs5 marks each

Internal choice (OR) is given in eight questions: Q23, Q26, Q27, Q28, Q30, Q31, Q34 and Q37.

Practical: 30 marks

Report file: at least 20 Python programs. Lab test: one program, marked on logic, documentation and code quality.

2

Pass plan in three steps

Where the 70 marks come from, easiest first

Python (Unit II) carries 45 of the 70 marks. But Units I and III are the quickest marks. Climb the ladder.

Step 1 · 25 marksUnit I (10) and Unit III (15): number conversions, truth tables, memory, software, and the society tables. Pure practice and learning, no programming needed.
Step 2 · 25 marks morePython basics and output questions: operators and precedence, slicing, list and string methods, dictionary methods. Trace code slowly on paper.
Step 3 · 20 marks moreProgram writing: learn the twelve model programs (factorial, prime, palindrome, Fibonacci, digit sum, patterns, vowel count, word count, linear search, frequency with a dictionary, menu program, records in a dictionary).
Marks milne wali baat
  • In output questions, write every line of the output and keep the spaces and brackets as Python prints them.
  • In programs, use clear variable names and add one comment line.
  • Show working in conversions: division table or place values.
Common mistake
  • input() gives a string; use int() for numbers.
  • // rounds down: -7 // 2 is -4.
  • A slice stops before the end index.
  • L.sort() returns None.
  • Truth table of 3 inputs has 8 rows.
3

Computer system and organisation

CPU, memory, units, software, operating system

A computer is an electronic machine that takes data as input, processes it according to instructions (a program), stores it and gives the result as output.

Picture: the parts of a computer

Central Processing Unit (CPU)Control Unit (CU)fetch · decode · executeALUarithmetic, logicRegistersfastestPrimary memoryRAM · ROM · cacheInput unitkeyboard, mouseOutput unitmonitor, printerSecondary storageHDD · SSD · pen driveData and instructions flow between the unitsdata / instructionscontrol signals from the CU
Block diagram of a computer. The CU controls every unit (dashed lines); data moves along the solid arrows.

In daily life: A kitchen: ingredients come in (input), the cook works on them (CPU), the shelf keeps things handy (memory), and the dish is served (output).

Tap and learnInside the CPU

Tap each part of the Central Processing Unit.

Arithmetic Logic Unit

Does: all calculations (+, -, ×, ÷) and comparisons (>, <, ==).

Example: working out 45 + 12 or checking marks >= 33.

Control Unit (CU)

Does: fetches each instruction, decodes it and tells the other parts what to do.

Example: like a traffic police officer directing the flow of data.

Registers

Are: tiny, fastest storage places inside the CPU.

Hold: the data and instruction being worked on right now.

System bus

Data bus: carries data both ways.

Address bus: carries the memory address, one way from the CPU.

Control bus: carries signals such as read and write.

Microprocessor

Is: the CPU built on a single chip.

Speed: clock speed is measured in GHz; more cores do more work together.

Picture: the machine cycle

Fetchread from memoryDecodeCU understands itExecuteALU does the work
The CPU repeats the fetch, decode, execute cycle for every instruction.

In daily life: Like reading a recipe: read the step (fetch), understand it (decode), do it (execute), then the next step.

Memory

Picture: the memory hierarchy

RegistersCacheRAM (main memory)SSD / Hard diskPen drive, DVD, cloudspeed
Higher in the pyramid: faster, smaller and costlier. Lower: slower, bigger and cheaper.
MemoryVolatile?SpeedUse
RegistersYesfastestdata being processed now
CacheYesvery fastoften-used data, between CPU and RAM
RAMYesfastprograms and data being used
ROMNofast to readboot instructions (firmware)
SSD / HDDNoslowfiles kept for long
Pen drive, DVDNoslowercarrying and backup

Units of memory

bit>nibble = 4 bits>byte = 8 bits>KB>MB>GB>TB>PB

1 KB = 1024 bytes, 1 MB = 1024 KB, 1 GB = 1024 MB, 1 TB = 1024 GB, 1 PB = 1024 TB (then EB, ZB, YB).

Software

TypeWhat it doesExamples
Operating systemmanages hardware and runs other programsWindows, Linux, Android
Device driverlets the OS use a deviceprinter driver
Utility softwaremaintains the computerantivirus, disk cleanup, compression
Language translatorconverts programs to machine codecompiler, interpreter, assembler
Application softwaredoes a task for the userword processor, Tally, browser

Language translators

Source codehigh-level (Python)Translatorcompiler / interpreterMachine code0s and 1s
AssemblerCompilerInterpreter
Convertsassembly to machine codehigh-level to machine code, all at oncehigh-level, line by line
Errors-all listed together after translationstops at the first error
ExampleNASMC, C++Python

Operating system

Functions of an OS

  • Process management: decides which program runs on the CPU
  • Memory management: gives and takes back memory
  • File management: files and folders
  • Device management: drivers, input and output
  • Security: passwords and user accounts
  • User interface: CLI or GUI

User interfaces

  • CLI: commands are typed, e.g. Linux terminal
  • GUI: windows, icons, menus, pointer
  • Touch: phones and tablets
  • Voice: assistants that obey spoken commands

GameSort the device

Tap: input, output or both?

GameSort the software

Tap the type of software.

More in detail

Types of memory chips

MemoryFull formFeature
SRAMStatic RAMvery fast, costly; used as cache
DRAMDynamic RAMneeds refreshing; used as main memory
PROMProgrammable ROMwritten once by the user
EPROMErasable PROMerased with ultraviolet light
EEPROMElectrically Erasable PROMerased electrically; flash memory is a kind of it

Types of operating systems

TypeIdeaExample use
Single-userone user at a timea home PC
Multi-usermany users share one computera server, bank system
Multitaskingmany programs run togethermusic while typing
Real-timeresponds within a fixed timeair-bag, traffic signals, missiles
Distributedmany computers work as onecloud services
Mobilemade for phones and tabletsAndroid, iOS

Hardware, software, firmware

Hardware

  • Physical parts you can touch
  • CPU, RAM, keyboard, monitor

Software

  • Programs and data
  • System, application, utility

Firmware

  • Software stored in ROM inside a device
  • BIOS of a computer, program in a washing machine

Ports and connections

PortUsed for
USBpen drive, mouse, printer, phone
HDMImonitor, projector, TV (video + audio)
Ethernet (RJ45)wired network
Audio jackheadphones, microphone
Remember

Speed order to remember: Registers > Cache > RAM > SSD > HDD. Cost per GB follows the same order.

Common mistake

Do not call the hard disk "memory" in answers; it is secondary storage. Memory means primary memory (RAM, ROM, cache).

Open its questions
4

Number system and encoding

Binary, octal, decimal, hexadecimal; ASCII, ISCII, Unicode

A number system is a way of writing numbers using a set of digits. The number of digits is its base (radix).

SystemBaseDigitsExample
Binary20, 1(1011)2
Octal80 to 7(752)8
Decimal100 to 9(945)10
Hexadecimal160 to 9, A to F (A = 10 ... F = 15)(3AF)16

Try it Number converter

Type a number and choose its base. See it in all four systems, with the division steps.

Tap and learnThe four kinds of conversion

Tap a method to see a worked example.

Decimal to base b: divide again and again

Rule: divide by the base, write the remainder, divide the quotient again until it is 0; read the remainders from the bottom.

Example: 25 ÷ 2: remainders 1, 0, 0, 1, 1 → read up: (11001)₂.

Base b to decimal: place values

Rule: multiply every digit by base raised to its position (from 0 at the right) and add.

Example: (1A)₁₆ = 1×16 + 10×1 = 26.

Groups of three bits

Rule: from the right, make groups of 3 bits; each group is one octal digit. Back: each octal digit becomes 3 bits.

Example: (101 110)₂ = (56)₈.

Groups of four bits

Rule: from the right, make groups of 4 bits; each group is one hex digit. Back: each hex digit becomes 4 bits.

Example: (1011 0111)₂ = (B7)₁₆.

Go through binary

Rule: octal → binary (3 bits each) → regroup in 4 bits → hex.

Example: (57)₈ = 101 111 = 0010 1111 = (2F)₁₆.

Place values

Position3210
Binary8421
Octal5126481
Hexadecimal4096256161

Encoding: how text is stored

Encoding gives a unique number (code) to every character, so that text can be stored as binary.

SchemeBits / bytesCharactersRemember
ASCII7 bits128A = 65, a = 97, 0 = 48, space = 32
ISCII8 bits256Indian scripts (Devanagari, Tamil ...)
Unicode UTF-81 to 4 bytesall scriptsEnglish letters take 1 byte; used on the web
UTF-162 or 4 bytesall scripts
UTF-324 bytes fixedall scriptssimple but uses more space

Try it Character code lab

Type any text, even Hindi. See the code of each character, its binary, and the bytes it takes in UTF-8.

print(ord('A'), chr(97))   # 65 a

More in detail

Worked example: decimal 75 to all bases

BaseWorkingAnswer
Binary75 → 37 r1, 18 r1, 9 r0, 4 r1, 2 r0, 1 r0, 0 r1(1001011)2
Octal75 → 9 r3, 1 r1, 0 r1(113)8
Hexadecimal75 → 4 r11 (B), 0 r4(4B)16

Worked example: (2F)₁₆ to decimal, octal and binary

(2F)16 = 2 x 16^1 + 15 x 16^0 = 32 + 15 = 47
(2F)16 = 0010 1111 (each hex digit is 4 bits)
       = 101111 (drop the leading zeros)
        = 101 111 (groups of 3)
        = (57)8

Binary table of 0 to 15 (learn by heart)

DecBinOctHexDecBinOctHex
000000081000108
100011191001119
200102210101012A
300113311101113B
401004412110014C
501015513110115D
601106614111016E
701117715111117F

ASCII codes to remember

CharacterCodeCharacterCode
space320 to 948 to 57
A to Z65 to 90a to z97 to 122
a - A32chr(ord('a') - 32)'A'
Remember

Check your conversion: convert the answer back to decimal. If you get the starting number, it is right.

Common mistake

Always write the base as a subscript, like (101)2. Without it, 101 could be decimal.

Open its questions
5

Boolean logic

Gates, truth tables, laws, De Morgan

Boolean logic works with only two values: 1 (True) and 0 (False). The basic operations are NOT, AND and OR.

The six gates: symbol, expression and truth table

AND gate
ABY

Y = A · B

ABY
000
010
100
111

Y is 1 only when both inputs are 1.

OR gate
ABY

Y = A + B

ABY
000
011
101
111

Y is 0 only when both inputs are 0.

NOT gate
AY

Y = Ā

AY
01
10

Y is the opposite of A.

NAND gate
ABY

Y = NOT(A · B)

ABY
001
011
101
110

Opposite of AND: 0 only when both are 1.

NOR gate
ABY

Y = NOT(A + B)

ABY
001
010
100
110

Opposite of OR: 1 only when both are 0.

XOR gate
ABY

Y = A ⊕ B

ABY
000
011
101
110

Y is 1 when the inputs are different.

ABA AND BA OR BA NAND BA NOR BA XOR B
0000110
0101101
1001101
1111000

Try it Truth table maker

Choose an expression. Rows where the output is 1 are coloured.

Logic circuit

ABY

Picture: from expression to logic circuit

Draw the gates in the order Python would work it out: NOT first, then AND, then OR. Each gate output becomes an input of the next gate.

ABCY
ABCY = (A AND B) OR (NOT C)
0001
0010
0101
0110
1001
1010
1101
1111

In daily life: A room light (Y) is on if both switches A and B are on, or if the night sensor C is off.

Picture: a circuit with three gates

ABCY
ABCY = NOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110

Laws of Boolean algebra

LawAND formOR form
IdentityA · 1 = AA + 0 = A
NullA · 0 = 0A + 1 = 1
IdempotentA · A = AA + A = A
ComplementA · Ā = 0A + Ā = 1
InvolutionNOT (NOT A) = A
De MorganNOT(A · B) = Ā + B̄NOT(A + B) = Ā · B̄

Prove De Morgan's law with a truth table

ABA·BNOT(A·B)ĀB̄Ā + B̄
0001111
0101101
1001011
1110000

The columns NOT(A·B) and Ā + B̄ are the same in every row, so the law is proved.

Remember

Number of rows in a truth table = 2n for n inputs: 2 inputs → 4 rows, 3 inputs → 8 rows.

Common mistake

Work out NOT first, then AND, then OR, unless brackets say otherwise.

GameWhich gate?

Read the rule and tap the gate.

More in detail

All the laws of Boolean algebra

LawAND formOR form
CommutativeA · B = B · AA + B = B + A
Associative(A · B) · C = A · (B · C)(A + B) + C = A + (B + C)
DistributiveA · (B + C) = A·B + A·CA + B·C = (A + B) · (A + C)
AbsorptionA · (A + B) = AA + A·B = A

NAND as a universal gate

To makeUsing NAND only
NOT AA NAND A
A AND B(A NAND B) NAND (A NAND B)
A OR B(A NAND A) NAND (B NAND B)

More circuits to practise

(A OR B) AND (NOT C)

ABCY
ABCY = (A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110

(A AND NOT B) OR (B AND C)

ABBCY
ABCY = (A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111

(A AND B) OR (NOT A AND NOT B)

ABABY
ABY = (A AND B) OR (NOT A AND NOT B)
001
010
100
111
Remember

Steps to draw a circuit: (1) find the innermost brackets, (2) put NOT gates first, (3) then AND gates, (4) the OR gate last gives Y.

Common mistake

A bubble (small circle) at the output means NOT. NAND = AND + bubble; NOR = OR + bubble.

Open its questions
6

Problem solving

Algorithm, flowchart, pseudocode, decomposition

Before writing code, we plan. Problem solving has four steps.

Analyse the problem>Develop an algorithm>Code>Test and debug

A good algorithm is

  • Finite: it ends after some steps
  • Definite: every step is clear
  • Input and output are defined
  • Effective: each step can be done
  • Correct and efficient

Three building blocks

  • Sequence: steps one after another
  • Selection: choose a path (if)
  • Iteration: repeat (loops)

Picture: a flowchart

StartInput NN % 2 == 0 ?Print 'Even'Print 'Odd'StopYesNo
Oval: start/stop. Parallelogram: input/output. Diamond: decision. Rectangle: process. Arrows: flow.
SymbolNameUse
OvalTerminalStart / Stop
ParallelogramInput / outputread or print
RectangleProcesscalculation, assignment
DiamondDecisionyes / no question
ArrowFlow lineorder of steps

The same problem as pseudocode

INPUT N
IF N MOD 2 = 0 THEN
    PRINT 'Even'
ELSE
    PRINT 'Odd'
END IF

Tap and learnKey ideas of problem solving

Tap a word.

Decomposition

Meaning: breaking a big problem into small parts.

Example: a library system: add books, issue, return, fine.

Verifying an algorithm

Meaning: dry-run the steps with sample input to check the output.

Example: try N = 7 and N = 10 in the even-odd algorithm.

Debugging

Meaning: finding and removing errors (bugs).

Tip: print the values of variables inside the loop.

Comparing algorithms

Meaning: choosing the one that uses less time and memory.

Example: adding 1 to n with a loop vs the formula n(n+1)/2.

GamePut the steps of problem solving in order

Tap from first to last.

More in detail

Worked algorithm: largest of three numbers

Step 1: Start
Step 2: Input A, B, C
Step 3: If A >= B and A >= C, then Big = A
Step 4: Else if B >= C, then Big = B
Step 5: Else Big = C
Step 6: Print Big
Step 7: Stop

Worked algorithm: sum of digits

INPUT N
SET S = 0
WHILE N > 0
    D = N MOD 10
    S = S + D
    N = N DIV 10
END WHILE
PRINT S

Dry run (verification) of the sum of digits for N = 352

ND = N MOD 10SN DIV 10
3522235
35573
33100

Algorithm and program

AlgorithmProgram
Written inplain language, stepsa programming language
Runs on a computer?NoYes
Rulesno strict syntaxstrict syntax

GameOrder the algorithm: largest of three

Tap the steps in order.

Open its questions
7

Python fundamentals

Tokens, data types, operators, precedence, input, errors

Python is a free, open-source, high-level, interpreted language. It is easy to read, case sensitive, and runs on every platform.

Two modes

  • Interactive: type at >>>, see the answer at once, not saved
  • Script: write a .py file, save it, run it all (F5 in IDLE)

Tokens: smallest units

  • Keywords: if, else, for, while, True, None, and ...
  • Identifiers: names: marks, total_1
  • Literals: 10, 3.5, "KV", True, None
  • Operators: + - * / // % ** == and in is
  • Punctuators: ( ) [ ] { } , : . ; = #

Picture: data types

Data typesNumbersint, float,complex, boolSequencesstr, list,tupleMappingdictNoneNoneTypeImmutable: int, float, bool,complex, str, tupleMutable: list, dict
Immutable types cannot be changed after creation; mutable ones can.

Operators

KindOperatorsExampleResult
Arithmetic+ - * / // % **17 // 5, 17 % 5, 2 ** 53, 2, 32
Relational== != > < >= <=5 != 3True
Logicalnot and or5 > 3 and 2 > 4False
Assignment= += -= *= /= //= %= **=x = 5; x += 2x is 7
Identityis, is nota is bsame object?
Membershipin, not in'a' in 'cat'True

Precedence: highest first

( )>**>+x -x>* / // %>+ ->== != < >>not>and>or

** goes right to left: 2 ** 3 ** 2 = 2 ** 9 = 512. Others of equal level go left to right.

print(17 + 5 * 2 // 3 - 2 ** 2)
print(-17 // 5, -17 % 5)
print(7 / 2, 7 // 2, 7.0 // 2)
print(True + True, 5 > 3 > 1)

Output

16
-4 3
3.5 3 3.0
2 True

Input, output and type conversion

name = input('Name: ')
age = int(input('Age: '))
print('Hello', name, 'next year you will be', age + 1)
print(name, age, sep=' | ', end='!\n')

Output

Name: Asha
Age: 16
Hello Asha next year you will be 17
Asha | 16!
FunctionDoesExample
int()to integerint('25') → 25, int(7.9) → 7
float()to floatfloat('2.5') → 2.5
str()to stringstr(10) → "10"
type()gives the typetype(5.0) → <class 'float'>
id()identity (address)id(x)

Errors

ErrorWhenExample
Syntax errorgrammar is wrong; found before runningif x > 5 print(x) (no colon)
Runtime error (exception)while running5 / 0 ZeroDivisionError; int("abc") ValueError
Logical errorruns but gives a wrong answeraverage = a + b / 2 (brackets missing)

GameMutable or immutable?

Tap the right box.

GameWhich error is it?

Tap the kind of error.

More in detail

Multiple assignment and swapping

a, b, c = 10, 20, 30
x = y = 5
a, b = b, a
print(a, b, c, x, y)

Output

20 10 30 5 5

Mutable and immutable: watch the id()

x = 10
print(id(x) == id(10))
x = x + 1          # a new object is made
print(x)
L = [1, 2]
old = id(L)
L.append(3)        # the same list changes
print(L, id(L) == old)

Output

True
11
[1, 2, 3] True

Escape sequences

SequenceMeaning
\nnew line
\ttab
\'single quote
\\backslash

Keywords of Python (35)

False None True and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield

Implicit and explicit conversion

a = 5
b = 2.5
print(a + b, type(a + b))      # implicit: int to float
print(int('7') + 3, float(4))   # explicit

Output

7.5 <class 'float'>
10 4.0

Common runtime errors

ErrorExample
ZeroDivisionError5 / 0
ValueErrorint('abc')
TypeError'5' + 5
NameErrorprint(total) before total is made
IndexError[1, 2][5]
KeyError{'a': 1}['b']
Open its questions
8

Flow of control

if-elif-else, for, while, range, break, continue, nested loops

Statements run in sequence unless we use selection (if) or iteration (loops). Python marks a block by indentation (4 spaces).

if, if-else, if-elif-else

if marks >= 90:
    grade = 'A'
elif marks >= 60:
    grade = 'B'
else:
    grade = 'C'

for and while

for i in range(1, 6):
    print(i, end=' ')

i = 1
while i <= 5:
    print(i, end=' ')
    i += 1

Try it range() explorer

Change start, stop and step. See the values the loop gets.

StatementEffectExample output
breakleaves the loop at oncefor i in range(1, 10): if i == 4: break → 1 2 3
continueskips to the next roundif i == 4: continue → 1 2 3 5 6 7 8 9
passdoes nothing; a placeholderif x > 0: pass
loop elseruns if no break happeneduseful for "not found"

Nested loops: a pattern

for i in range(1, 5):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()

Output

1 
1 2 
1 2 3 
1 2 3 4 

Trace a loop: sum of digits of 4728

nn % 10sn // 10
472888472
47221047
477174
44210
Remember

Model programs with loops: factorial, prime check, Fibonacci, sum of digits, reverse, palindrome, Armstrong, GCD, patterns, series sums. All are in the question bank (C3) with sample runs.

Common mistake

A while loop needs a change inside it (like i += 1), or it never ends.

More in detail

Loop with else: prime check

n = 29
for i in range(2, n):
    if n % i == 0:
        print(n, 'is not prime')
        break
else:
    print(n, 'is prime')

Output

29 is prime

Trace a while loop: reverse of 1234

nn % 10rev = rev*10 + dn // 10
123444123
12334312
1224321
1143210

Twelve model programs (all in C3, D3, D4 with sample runs)

ProgramKey idea
Factorialf = f * i in a loop
Prime checkfor-else with break
Fibonaccia, b = b, a + b
Sum of digitsn % 10 and n // 10
Palindrome numberreverse and compare
Armstrongsum of cubes of digits
GCD and LCMx, y = y, x % y
Patternsnested loops, print(end=" ")
Series sumterm in a loop
Vowel countif ch in "aeiou"
Linear searchfor-else
FrequencyD[x] = D.get(x, 0) + 1
Remember

Number of times a loop runs: range(a, b) runs b - a times; range(a, b, s) runs about (b - a) / s times, rounded up.

Common mistake

Do not put a semicolon or brackets after for or if; only a colon.

Open its questions
9

Strings

Indexing, slicing, operators, methods, traversal

A string is a sequence of characters in quotes. Strings are immutable: they cannot be changed in place.

Picture: indexing

0P-6
1Y-5
2T-4
3H-3
4O-2
5N-1

Top: positive index. Bottom: negative index.

Try it String slicing lab

Change the string and the start, stop and step. The chosen characters glow, numbered in order.

OperationExampleResult
Concatenation'KV' + 'S''KVS'
Replication'Ha' * 3'HaHaHa'
Membership'dr' in 'Kendriya'True
Slicing'Kendriya'[2:5]'ndr'
Reverse'Kendriya'[::-1]'ayirdneK'
Comparison'apple' < 'banana'True (by character codes)

String methods

MethodExample (s = "hello World")Result
len(s)len(s)11
upper(), lower()s.upper()'HELLO WORLD'
title(), capitalize()s.title()'Hello World'
swapcase()s.swapcase()'HELLO wORLD'
count(x)s.count('o')2
find(x)s.find('o'), s.find('z')4, -1
index(x)s.index('W')6 (error if not found)
startswith(), endswith()s.endswith('ld')True
isalpha(), isdigit(), isalnum()'abc12'.isalnum()True
islower(), isupper(), isspace()' '.isspace()True
strip(), lstrip(), rstrip()' hi '.strip()'hi'
replace(a, b)s.replace('l', 'L')'heLLo WorLd'
split()s.split()['hello', 'World']
partition(x)s.partition(' ')('hello', ' ', 'World')
join()'-'.join(['a', 'b'])'a-b'
ord(), chr()ord('A'), chr(98)65, 'b'

Traversing a string

s = 'Hello World 26'
u = l = d = 0
for ch in s:
    if ch.isupper():
        u += 1
    elif ch.islower():
        l += 1
    elif ch.isdigit():
        d += 1
print(u, l, d)

Output

2 8 2

GameWhat does it return?

Tap the type of value returned.

More in detail

String comparison

print('apple' < 'banana', 'Zoo' < 'ant', 'abc' == 'ABC')
print(ord('Z'), ord('a'))

Output

True True False
90 97

Building a new string (strings cannot change)

s = 'computer'
new = ''
for ch in s:
    if ch in 'aeiou':
        new = new + ch.upper()
    else:
        new = new + ch
print(new)

Output

cOmpUtEr

More methods with examples

MethodExampleResult
center(w, ch)'KV'.center(6, '*')'**KV**'
zfill(w)'7'.zfill(3)'007'
count with start'banana'.count('a', 2)2
find from end'banana'.rfind('a')5
split with limit'a,b,c'.split(',', 1)['a', 'b,c']
isalnum on space'KV 88'.isalnum()False
Remember

Slice trick: s[::-1] reverses; s[::2] takes every second character; s[-3:] takes the last three.

Common mistake

A slice never gives an IndexError, but s[20] on a short string does.

Open its questions
10

Lists

Indexing, slicing, methods, nested lists, linear search

A list is an ordered, mutable collection written in [ ]. It can hold values of different types: [10, 'KV', 3.5, [1, 2]].

Try it List method lab

Type a value and tap a method. The list changes as Python would change it; the code is shown below.

Method / functionDoesReturns
L.append(x)adds x at the endNone
L.extend(L2)adds every element of L2None
L.insert(i, x)puts x at index iNone
L.remove(x)deletes the first x (error if absent)None
L.pop(i)deletes the element at i (last if no i)the deleted element
del L[i]statement: deletes by index or slice-
L.clear()empties the listNone
L.index(x)position of the first xint
L.count(x)how many times x occursint
L.sort(), L.sort(reverse=True)sorts L itselfNone
sorted(L)sorted copy, L unchangednew list
L.reverse()reverses L itselfNone
len, max, min, sumfunctions on a listvalue

Copying a list

A = [1, 2, 3]
B = A          # same list, two names
C = A.copy()   # a new list
A.append(4)
print(B)
print(C)

Output

[1, 2, 3, 4]
[1, 2, 3]

Linear search

L = [12, 45, 7, 30]
x = 7
for i in range(len(L)):
    if L[i] == x:
        print('Found at index', i)
        break
else:
    print('Not found')

Output

Found at index 2

GameWhat happens?

L = [5, 2, 8]. Tap what the statement gives.

More in detail

Nested list: rows and columns

M = [[1, 2, 3], [4, 5, 6]]
print(M[1][2], len(M), len(M[0]))
for row in M:
    print(sum(row), end=' ')

Output

6 2 3
6 15 

Slicing changes a list

L = [10, 20, 30, 40, 50]
L[1:3] = [7, 8, 9]
print(L)
del L[::2]
print(L)

Output

[10, 7, 8, 9, 40, 50]
[7, 9, 50]

Common list programs and the line that does the work

TaskKey line
Sum, max, minsum(L), max(L), min(L)
Count evensif x % 2 == 0: c += 1
Remove duplicatesif x not in new: new.append(x)
Reverse without reverse()L[::-1]
Second largestsorted(set(L))[-2]
Swap first and lastL[0], L[-1] = L[-1], L[0]
Remember

Use L.copy() or L[:] to make a real copy. B = A only gives another name.

Common mistake

L.remove(x) removes only the first x. Use a loop to remove all.

Open its questions
11

Tuples and dictionaries

Immutable sequences, key-value pairs, methods, counting

Tuples

A tuple is an ordered, immutable sequence written in ( ). A one-element tuple needs a comma: (5,).

T = (10, 20, 30, 20)
print(T[1:3], T.count(20), T.index(30))
a, b, c, d = T        # unpacking
print(a + d)
T2 = T + (40,)        # a new tuple
print(len(T2), max(T2))

Output

(20, 30) 2 2
30
5 40

Dictionaries

A dictionary stores key: value pairs in { }. Keys are unique and immutable; values can be anything. It is mutable.

Try it Dictionary lab

D = {'Amit': 85, 'Bela': 92}. Add, change, get or delete a key.

MethodDoesExample (D = {"a": 1, "b": 2})
D[k]value of k (KeyError if absent)D["a"] → 1
D[k] = vadds or changesD["c"] = 3
D.get(k, d)value or d (None) if absentD.get("z", 0) → 0
D.keys(), D.values(), D.items()views of keys, values, pairslist(D.items()) → [("a", 1), ("b", 2)]
D.update(E)adds or changes the pairs of ED.update({"b": 5})
D.pop(k)removes k, returns its valueD.pop("a") → 1
D.popitem()removes the last pair("b", 2)
del D[k]deletes a keydel D["a"]
D.setdefault(k, v)adds k with v only if absentreturns the value
dict.fromkeys(L, v)new dict from keys in Ldict.fromkeys("ab", 0) → {"a": 0, "b": 0}
D.copy(), D.clear()copy; empty
len, sorted, max, minwork on the keyssorted(D) → ["a", "b"]

Counting with a dictionary

text = 'banana'
F = {}
for ch in text:
    F[ch] = F.get(ch, 0) + 1
print(F)

Output

{'b': 1, 'a': 3, 'n': 2}

List, tuple, dictionary, string

StringListTupleDictionary
Brackets' '[ ]( ){ }
Mutable?NoYesNoYes
Access byindexindexindexkey
Duplicatesyesyesyeskeys no, values yes

More in detail

Tuple functions

T = (8, 3, 9, 3)
print(len(T), max(T), min(T), sum(T))
print(sorted(T), tuple(sorted(T)))
print(T.count(3), T.index(9))

Output

4 9 3 23
[3, 3, 8, 9] (3, 3, 8, 9)
2 2

Traversing a dictionary in three ways

D = {'Amit': 85, 'Bela': 92}
for k in D:
    print(k, D[k])
for v in D.values():
    print(v, end=' ')
print()
for k, v in D.items():
    print(k, '->', v)

Output

Amit 85
Bela 92
85 92 
Amit -> 85
Bela -> 92

Nested dictionary

S = {'Amit': {'Eng': 80, 'CS': 95}, 'Bela': {'Eng': 88, 'CS': 91}}
print(S['Bela']['CS'])
for name in S:
    print(name, sum(S[name].values()))

Output

91
Amit 175
Bela 179
Remember

To count anything (letters, words, marks), use D[x] = D.get(x, 0) + 1.

Common mistake

D[k] on a missing key gives KeyError; D.get(k) gives None.

Open its questions
12

Python modules

math, random, statistics

A module is a file of ready Python functions. Import it, then use its functions.

import math                 # use as math.sqrt(16)
from math import sqrt, pi   # use as sqrt(16)
import statistics as st     # use as st.mean(L)
ModuleFunctionExampleResult
mathpi, emath.pi3.141592653589793
mathsqrt(x)math.sqrt(49)7.0
mathceil(x), floor(x)math.ceil(4.2), math.floor(-4.2)5, -5
mathpow(x, y)math.pow(2, 3)8.0
mathfabs(x)math.fabs(-6)6.0
mathsin, cos, tan (radians)math.sin(0)0.0
randomrandom()random.random()0.0 ≤ value < 1.0
randomrandint(a, b)random.randint(1, 6)1 to 6, both included
randomrandrange(a, b, s)random.randrange(0, 10, 2)0, 2, 4, 6 or 8
statisticsmean(data)mean([2, 4, 9])5
statisticsmedian(data)median([7, 1, 5])5
statisticsmode(data)mode([2, 3, 3, 8])3

Possible output questions with random

To find possible outputs, write the range each call can give. random.randint(1, 3) gives 1, 2 or 3; random.randrange(1, 3) gives only 1 or 2.

import random
L = ['red', 'blue', 'green', 'pink']
k = random.randint(1, 2)
for i in range(k, 4):
    print(L[i], end='#')

k can be 1 or 2. So the possible outputs are blue#green#pink# and green#pink#. An output that starts with red is not possible.

More in detail

Four ways to import

StatementCall as
import mathmath.sqrt(9)
import math as mm.sqrt(9)
from math import sqrtsqrt(9)
from math import *sqrt(9), pi (all names)

Statistics on one list

import statistics as st
marks = [72, 85, 85, 90, 64]
print(st.mean(marks), st.median(marks), st.mode(marks))

Output

79.2 85 85

random: possible outputs, worked

import random
AR = [20, 30, 40, 50, 60, 70]
lo = random.randint(1, 3)
hi = random.randint(2, 4)
for k in range(lo, hi + 1):
    print(AR[k], end='#')

lo can be 1, 2 or 3; hi can be 2, 3 or 4. The printing starts at index lo and ends at index hi. So 30#40#50# (lo = 1, hi = 3) is possible, but anything starting with 20 is not, because index 0 is never chosen.

Open its questions
13

Society, law and ethics

Digital footprint, IPR, licences, cyber crime, IT Act, e-waste

Technology changes how we live. This unit is about using it safely, fairly and lawfully.

Tap and learnDigital footprint

Tap to see each kind.

Active digital footprint

Is: data we leave knowingly.

Examples: posts, comments, e-mails, forms filled, photos uploaded.

Passive digital footprint

Is: data collected without our knowledge.

Examples: browsing history, IP address, location, cookies.

Managing it

Do: think before posting, keep accounts private, delete old accounts, use private browsing.

Why: colleges and employers may see it; it is hard to erase.

Intellectual property rights (IPR)

RightProtectsExample
Copyrightcreative works: books, music, software, arta novel, an app
Patentnew inventionsa new engine design
Trademarkbrand names, logos, symbolsthe logo of a company

Violations of IPR

  • Plagiarism: using others’ work as your own
  • Copyright infringement: using without permission
  • Piracy: illegal copying or selling of software, movies, music

Licences

  • Proprietary: owned, code closed, paid (MS Office)
  • FOSS: free and open source (Linux, LibreOffice, Python)
  • GPL: copyleft; changed versions also stay open
  • Creative Commons: for creative works (BY, NC, SA, ND)
  • Apache, MIT, BSD: permissive open source

Cyber crime and cyber safety

Crime / threatWhat it is
Hackingunauthorised access; ethical hacking is done with permission
Phishingfake messages or sites to steal passwords and bank details
Ransomwarelocks files and demands money
Cyber bullyingharassing a person online
Cyber trollingposting provocative comments to upset people
Identity theftusing another person’s identity to cheat
Virus / worm / Trojanneeds a host / spreads by itself / disguised as useful
Spyware / adware / keyloggerspies / shows ads / records keys

Stay safe

  • Strong, different passwords and two-factor authentication
  • https sites, updated antivirus and OS, a firewall
  • Do not click unknown links or open unknown attachments
  • Log out on shared computers; use private browsing
  • Report cyber crime: cybercrime.gov.in, helpline 1930

IT Act, 2000

  • Legal recognition of electronic records and digital signatures
  • Punishes hacking, identity theft, cyber terrorism, offensive messages
  • CERT-In handles cyber security incidents

E-waste, health, gender and disability

E-waste

  • Old phones, computers, batteries
  • Has lead, mercury, cadmium: harmful to soil, water and health
  • Reduce, reuse, recycle; give to authorised recyclers

Technology and society

  • Gender issues: fewer girls in computing, unequal access; encourage and include
  • Disability: screen readers, Braille keyboards, captions, inclusive design
  • Health: eye strain, bad posture, addiction; take breaks

GameName the threat

Read the case and tap the threat.

GameWhich IPR?

Tap the right that protects it.

More in detail

IT Act, 2000: important sections

SectionOffence
43damage to a computer or data without permission (penalty)
66computer-related offences such as hacking
66Cidentity theft (misusing a password or signature)
66Dcheating by personation using a computer
66Eviolation of privacy (capturing private images)
67publishing obscene material electronically

Malware at a glance

MalwareSpreads byHarm
Virusinfected files, needs a hostdamages files
Wormnetwork, by itselfslows networks
Trojanpretends to be usefulsteals or opens a back door
Ransomwareattachments, linkslocks files for money
Spywarefree apps, downloadssteals data
Adwarefree softwareunwanted ads

Creative Commons licence parts

CodeMeaning
BYgive credit to the creator
SAshare changed versions under the same licence
NCno commercial use
NDno changes (no derivatives)

Safe password checklist

Remember

Answer case studies in this order: name the problem, why it is wrong or harmful, what to do.

Common mistake

Free software is about freedom, not price; freeware is free of cost but closed.

Open its questions
14

Question bank: every type, with answers

1423 questions in 21 types, in the order of the paper

Tap a type to open it. In Section A, tap an option to check yourself. Every output, sample run, conversion and truth table shown in the answers was produced by running the code or computing it.

Section A · 1 mark each

A1Unit I: computer system, numbers, logic1 mark · 160 questions
A1.1Practice1 mark

A program that translates the whole high-level program at once into machine code is a

Answer

(d) compiler A compiler translates the whole program at once and reports all errors together.

A1.2Practice1 mark

The decimal equivalent of (101)₈ is

Answer

(a) 65 Multiply each digit by its place value (powers of 8) and add: 1×8^2 + 0×8^1 + 1×8^0 = 65.

A1.3Practice1 mark

What is the value of NOT A OR (B AND C) when A = 0, B = 0, C = 1?

Answer

(b) 1 Work out NOT first, then AND, then OR.

A1.4Practice1 mark

Small, very fast storage locations inside the CPU are called

Answer

(d) registers Registers are tiny storage places inside the CPU itself, so they are the fastest memory; they hold the data being worked on right now.

A1.5Practice1 mark

By De Morgan's law, NOT (A AND B) equals

Answer

(a) (NOT A) OR (NOT B) De Morgan: break the bar and change the sign. NOT (A AND B) = (NOT A) OR (NOT B). Check with A = 1, B = 0: left side NOT 0 = 1, right side 0 OR 1 = 1.

A1.6Practice1 mark

The hexadecimal equivalent of (10001101)₂ is

Answer

(d) 8D Make groups of 4 bits from the right and write each group as one hexadecimal digit: 1000 = 8, 1101 = D.

A1.7Practice1 mark

The binary equivalent of (303)₈ is

Answer

(a) 11000011 Write each octal digit as 3 bits: 3 = 011, 0 = 000, 3 = 011.

A1.8Practice1 mark

Which of these is an input device?

Answer

(b) Scanner A barcode reader sends the product code into the computer, so it is an input device.

A1.9Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'D'?

Answer

(b) 68 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.10Practice1 mark

Which digit is NOT valid in the octal number system?

Answer

(a) 8 Octal uses the digits 0 to 7.

A1.11Practice1 mark

What is the value of (NOT A) OR B when A = 1, B = 0?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.12Practice1 mark

Which of these is the fastest memory?

Answer

(c) Register Speed order: registers > cache > RAM > secondary storage.

A1.13Practice1 mark

How many KB are there in 4 MBs?

Answer

(b) 4096 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.14Practice1 mark

The hexadecimal equivalent of (162)₁₀ is

Answer

(c) A2 Divide by 16 again and again and read the remainders from bottom to top: 162 ÷ 16 = 10 r 2; 10 ÷ 16 = 0 r A.

A1.15Practice1 mark

The octal equivalent of (10001101)₂ is

Answer

(d) 215 Make groups of 3 bits from the right and write each group as one octal digit: 010 = 2, 001 = 1, 101 = 5.

A1.16Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'A'?

Answer

(d) 65 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.17Practice1 mark

An assembler converts

Answer

(c) assembly language into machine language An assembler translates assembly language (mnemonics such as ADD, MOV) into machine language (0s and 1s).

A1.18Practice1 mark

What is the value of (A AND B) OR (NOT A AND NOT B) when A = 0, B = 0?

Answer

(b) 1 Work out NOT first, then AND, then OR.

A1.19Practice1 mark

NAND and NOR are called universal gates because

Answer

(c) any other gate can be made from them Using only NAND gates (or only NOR gates) we can build NOT, AND, OR and every other gate.

A1.20Practice1 mark

What is the value of (NOT A) AND (NOT B) when A = 0, B = 0?

Answer

(b) 1 Work out NOT first, then AND, then OR.

A1.21Practice1 mark

4 bits make one

Answer

(a) nibble 4 bits = 1 nibble, 8 bits = 1 byte.

A1.22Practice1 mark

The octal equivalent of (89)₁₀ is

Answer

(d) 131 Divide by 8 again and again and read the remainders from bottom to top: 89 ÷ 8 = 11 r 1; 11 ÷ 8 = 1 r 3; 1 ÷ 8 = 0 r 1.

A1.23Practice1 mark

The decimal equivalent of (10100110)₂ is

Answer

(c) 166 Multiply each digit by its place value (powers of 2) and add: 1×2^7 + 0×2^6 + 1×2^5 + 0×2^4 + 0×2^3 + 1×2^2 + 1×2^1 + 0×2^0 = 166.

A1.24Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'B'?

Answer

(b) 66 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.25Practice1 mark

The hexadecimal equivalent of (211)₁₀ is

Answer

(a) D3 Divide by 16 again and again and read the remainders from bottom to top: 211 ÷ 16 = 13 r 3; 13 ÷ 16 = 0 r D.

A1.26Practice1 mark

What is the value of (NOT A) OR B when A = 0, B = 0?

Answer

(b) 1 Work out NOT first, then AND, then OR.

A1.27Practice1 mark

What is the value of NOT A OR (B AND C) when A = 1, B = 0, C = 1?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.28Practice1 mark

The octal equivalent of (68)₁₆ is

Answer

(a) 150 Go through binary: 68 = 0110 1000; regroup in 3 bits from the right: 001 = 1, 101 = 5, 000 = 0.

A1.29Practice1 mark

What is the value of (A AND NOT B) OR (B AND C) when A = 0, B = 1, C = 1?

Answer

(b) 1 Work out NOT first, then AND, then OR.

A1.30Practice1 mark

Which unit of the CPU performs arithmetic and logical operations?

Answer

(d) ALU ALU stands for Arithmetic Logic Unit.

A1.31Practice1 mark

The binary equivalent of (3A)₁₆ is

Answer

(c) 111010 Write each hexadecimal digit as 4 bits: 3 = 0011, A = 1010.

A1.32Practice1 mark

The decimal equivalent of (203)₈ is

Answer

(b) 131 Multiply each digit by its place value (powers of 8) and add: 2×8^2 + 0×8^1 + 3×8^0 = 131.

A1.33Practice1 mark

State True or False: Cache memory is faster than RAM.

Answer

(a) True Cache is made of fast SRAM and sits close to the CPU, so it is faster than RAM.

A1.34Practice1 mark

How many different characters can 7-bit ASCII represent?

Answer

(a) 128 2 to the power 7 = 128.

A1.35Practice1 mark

How many MB are there in 7 GBs?

Answer

(b) 7168 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.36Practice1 mark

The decimal equivalent of (360)₈ is

Answer

(c) 240 Multiply each digit by its place value (powers of 8) and add: 3×8^2 + 6×8^1 + 0×8^0 = 240.

A1.37Practice1 mark

What is the value of A AND (B OR C) when A = 1, B = 1, C = 0?

Answer

(b) 1 Work out NOT first, then AND, then OR.

A1.38Practice1 mark

In UTF-32, every character takes

Answer

(c) 4 bytes UTF-32 is a fixed-length encoding: every character uses 32 bits = 4 bytes.

A1.39Practice1 mark

The hexadecimal equivalent of (114)₁₀ is

Answer

(b) 72 Divide by 16 again and again and read the remainders from bottom to top: 114 ÷ 16 = 7 r 2; 7 ÷ 16 = 0 r 7.

A1.40Practice1 mark

In UTF-8, an English letter such as 'A' takes

Answer

(b) 1 byte UTF-8 uses 1 byte for the English (ASCII) characters and 2 to 4 bytes for other scripts.

A1.41Practice1 mark

The ASCII code of 'a' is 97. What is the ASCII code of 'z'?

Answer

(a) 122 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.42Practice1 mark

Which gate gives 1 when its two inputs are different?

Answer

(b) XOR XOR (exclusive OR) gives 1 for inputs 0,1 and 1,0, and 0 when both inputs are the same.

A1.43Practice1 mark

The ASCII code of 'a' is 97. What is the ASCII code of 'w'?

Answer

(d) 119 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.44Practice1 mark

The hexadecimal equivalent of (11010100)₂ is

Answer

(d) D4 Make groups of 4 bits from the right and write each group as one hexadecimal digit: 1101 = D, 0100 = 4.

A1.45Practice1 mark

The decimal equivalent of (334)₈ is

Answer

(b) 220 Multiply each digit by its place value (powers of 8) and add: 3×8^2 + 3×8^1 + 4×8^0 = 220.

A1.46Practice1 mark

The ASCII code of '0' is 48. What is the ASCII code of '5'?

Answer

(c) 53 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.47Practice1 mark

What is the value of A AND (B OR C) when A = 0, B = 1, C = 0?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.48Practice1 mark

The hexadecimal equivalent of (124)₈ is

Answer

(d) 54 Go through binary: 124 = 001 010 100; regroup in 4 bits from the right: 0101 = 5, 0100 = 4.

A1.49Practice1 mark

The hexadecimal equivalent of (1000011)₂ is

Answer

(a) 43 Make groups of 4 bits from the right and write each group as one hexadecimal digit: 0100 = 4, 0011 = 3.

A1.50Practice1 mark

The binary equivalent of (331)₈ is

Answer

(b) 11011001 Write each octal digit as 3 bits: 3 = 011, 3 = 011, 1 = 001.

A1.51Practice1 mark

A user interface where commands are typed is called

Answer

(b) command line interface (CLI) In a CLI the user types commands (as in the DOS or Linux terminal) instead of clicking icons.

A1.52Practice1 mark

Which gate has only one input?

Answer

(c) NOT NOT (inverter) has one input and gives its opposite: NOT 0 = 1, NOT 1 = 0.

A1.53Practice1 mark

The decimal equivalent of (336)₈ is

Answer

(c) 222 Multiply each digit by its place value (powers of 8) and add: 3×8^2 + 3×8^1 + 6×8^0 = 222.

A1.54Practice1 mark

What is the value of NOT A OR (B AND C) when A = 1, B = 0, C = 0?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.55Practice1 mark

Which of these is an output device?

Answer

(c) Plotter A projector shows the computer's output on a big screen.

A1.56Practice1 mark

State True or False: UTF-8 and ASCII codes are the same for English letters.

Answer

(a) True UTF-8 keeps the 128 ASCII codes unchanged.

A1.57Practice1 mark

The decimal equivalent of (307)₈ is

Answer

(d) 199 Multiply each digit by its place value (powers of 8) and add: 3×8^2 + 0×8^1 + 7×8^0 = 199.

A1.58Practice1 mark

The decimal equivalent of (1001111)₂ is

Answer

(a) 79 Multiply each digit by its place value (powers of 2) and add: 1×2^6 + 0×2^5 + 0×2^4 + 1×2^3 + 1×2^2 + 1×2^1 + 1×2^0 = 79.

A1.59Practice1 mark

The hexadecimal equivalent of (243)₈ is

Answer

(a) A3 Go through binary: 243 = 010 100 011; regroup in 4 bits from the right: 1010 = A, 0011 = 3.

A1.60Practice1 mark

Which memory is volatile?

Answer

(b) RAM RAM loses its data when the power is switched off.

A1.61Practice1 mark

The hexadecimal equivalent of (73)₁₀ is

Answer

(a) 49 Divide by 16 again and again and read the remainders from bottom to top: 73 ÷ 16 = 4 r 9; 4 ÷ 16 = 0 r 4.

A1.62Practice1 mark

The ASCII code of 'a' is 97. What is the ASCII code of 'b'?

Answer

(c) 98 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.63Practice1 mark

Which of these is system software?

Answer

(a) Operating system A device driver helps the operating system use a device, so it is system software.

A1.64Practice1 mark

The decimal equivalent of (74)₈ is

Answer

(a) 60 Multiply each digit by its place value (powers of 8) and add: 7×8^1 + 4×8^0 = 60.

A1.65Practice1 mark

The ASCII code of 'a' is 97. What is the ASCII code of 'n'?

Answer

(b) 110 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.66Practice1 mark

What is the value of NOT (A OR B) AND C when A = 1, B = 1, C = 0?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.67Practice1 mark

The hexadecimal equivalent of (11101100)₂ is

Answer

(b) EC Make groups of 4 bits from the right and write each group as one hexadecimal digit: 1110 = E, 1100 = C.

A1.68Practice1 mark

The hexadecimal equivalent of (132)₈ is

Answer

(b) 5A Go through binary: 132 = 001 011 010; regroup in 4 bits from the right: 0101 = 5, 1010 = A.

A1.69Practice1 mark

State True or False: ROM is a volatile memory.

Answer

(b) False ROM keeps its contents without power.

A1.70Practice1 mark

Windows, macOS and Ubuntu provide a

Answer

(d) graphical user interface (GUI) They show windows, icons, menus and a pointer, which is a graphical user interface.

A1.71Practice1 mark

How many bytes are there in 7 KBs?

Answer

(c) 7168 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.72Practice1 mark

A language made of 0s and 1s, which the computer understands directly, is

Answer

(a) machine language Machine language is binary code that the CPU can run without any translation.

A1.73Practice1 mark

What is the value of A AND B AND NOT C when A = 0, B = 0, C = 1?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.74Practice1 mark

The decimal equivalent of (342)₈ is

Answer

(d) 226 Multiply each digit by its place value (powers of 8) and add: 3×8^2 + 4×8^1 + 2×8^0 = 226.

A1.75Practice1 mark

Software that lets the operating system talk to a printer is a

Answer

(a) device driver A device driver is system software that tells the OS how to control a particular device.

A1.76Practice1 mark

How many GB are there in 4 TBs?

Answer

(a) 4096 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.77Practice1 mark

How many bits are there in 3 bytes?

Answer

(c) 24 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.78Practice1 mark

A truth table for 3 inputs has

Answer

(c) 8 rows 2 to the power 3 = 8.

A1.79Practice1 mark

The binary equivalent of (119)₁₀ is

Answer

(d) 1110111 Divide by 2 again and again and read the remainders from bottom to top: 119 ÷ 2 = 59 r 1; 59 ÷ 2 = 29 r 1; 29 ÷ 2 = 14 r 1; 14 ÷ 2 = 7 r 0; 7 ÷ 2 = 3 r 1; 3 ÷ 2 = 1 r 1 ….

A1.80Practice1 mark

The base of the hexadecimal number system is

Answer

(d) 16 Hexadecimal uses 16 symbols: 0 to 9 and A to F, so its base is 16.

A1.81Practice1 mark

The octal equivalent of (11011110)₂ is

Answer

(a) 336 Make groups of 3 bits from the right and write each group as one octal digit: 011 = 3, 011 = 3, 110 = 6.

A1.82Practice1 mark

The decimal equivalent of (1001000)₂ is

Answer

(a) 72 Multiply each digit by its place value (powers of 2) and add: 1×2^6 + 0×2^5 + 0×2^4 + 1×2^3 + 0×2^2 + 0×2^1 + 0×2^0 = 72.

A1.83Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'S'?

Answer

(a) 83 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.84Practice1 mark

The binary equivalent of (364)₈ is

Answer

(d) 11110100 Write each octal digit as 3 bits: 3 = 011, 6 = 110, 4 = 100.

A1.85Practice1 mark

How many bits are there in 7 nibbles?

Answer

(b) 28 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.86Practice1 mark

Python uses which language translator?

Answer

(d) Interpreter Python code is run line by line by the Python interpreter.

A1.87Practice1 mark

The decimal equivalent of (277)₈ is

Answer

(c) 191 Multiply each digit by its place value (powers of 8) and add: 2×8^2 + 7×8^1 + 7×8^0 = 191.

A1.88Practice1 mark

The octal equivalent of (10000111)₂ is

Answer

(a) 207 Make groups of 3 bits from the right and write each group as one octal digit: 010 = 2, 000 = 0, 111 = 7.

A1.89Practice1 mark

The octal equivalent of (162)₁₀ is

Answer

(c) 242 Divide by 8 again and again and read the remainders from bottom to top: 162 ÷ 8 = 20 r 2; 20 ÷ 8 = 2 r 4; 2 ÷ 8 = 0 r 2.

A1.90Practice1 mark

The binary equivalent of (EF)₁₆ is

Answer

(c) 11101111 Write each hexadecimal digit as 4 bits: E = 1110, F = 1111.

A1.91Practice1 mark

The output of an AND gate is 1 only when

Answer

(b) all its inputs are 1 AND is like multiplication: 1 AND 1 = 1, and any 0 input gives 0.

A1.92Practice1 mark

The decimal equivalent of (31)₈ is

Answer

(d) 25 Multiply each digit by its place value (powers of 8) and add: 3×8^1 + 1×8^0 = 25.

A1.93Practice1 mark

In hexadecimal, the letter F stands for

Answer

(b) 15 A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.

A1.94Practice1 mark

Which of these is secondary memory?

Answer

(d) Solid State Drive An SSD stores data permanently outside the CPU; registers, cache and RAM are primary memory.

A1.95Practice1 mark

The decimal equivalent of (1101111)₂ is

Answer

(d) 111 Multiply each digit by its place value (powers of 2) and add: 1×2^6 + 1×2^5 + 0×2^4 + 1×2^3 + 1×2^2 + 1×2^1 + 1×2^0 = 111.

A1.96Practice1 mark

Software whose source code is available to all for use and change is called

Answer

(b) open source software Open source software shares its source code, so anyone can study, change and share it (e.g. Linux, Python).

A1.97Practice1 mark

The hexadecimal equivalent of (10011110)₂ is

Answer

(c) 9E Make groups of 4 bits from the right and write each group as one hexadecimal digit: 1001 = 9, 1110 = E.

A1.98Practice1 mark

The output of an OR gate is 0 only when

Answer

(d) all its inputs are 0 OR gives 1 if any input is 1, so it gives 0 only when every input is 0.

A1.99Practice1 mark

How many bytes are there in 5 KBs?

Answer

(a) 5120 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.100Practice1 mark

The decimal equivalent of (367)₈ is

Answer

(a) 247 Multiply each digit by its place value (powers of 8) and add: 3×8^2 + 6×8^1 + 7×8^0 = 247.

A1.101Practice1 mark

The binary equivalent of (111)₁₀ is

Answer

(d) 1101111 Divide by 2 again and again and read the remainders from bottom to top: 111 ÷ 2 = 55 r 1; 55 ÷ 2 = 27 r 1; 27 ÷ 2 = 13 r 1; 13 ÷ 2 = 6 r 1; 6 ÷ 2 = 3 r 0; 3 ÷ 2 = 1 r 1 ….

A1.102Practice1 mark

The octal equivalent of (1101111)₂ is

Answer

(d) 157 Make groups of 3 bits from the right and write each group as one octal digit: 001 = 1, 101 = 5, 111 = 7.

A1.103Practice1 mark

The hexadecimal equivalent of (55)₈ is

Answer

(c) 2D Go through binary: 55 = 101 101; regroup in 4 bits from the right: 0010 = 2, 1101 = D.

A1.104Practice1 mark

Which part of the CPU directs and controls the working of all other parts?

Answer

(c) Control Unit The Control Unit fetches and decodes instructions and sends control signals to all the units.

A1.105Practice1 mark

The decimal equivalent of (76)₈ is

Answer

(c) 62 Multiply each digit by its place value (powers of 8) and add: 7×8^1 + 6×8^0 = 62.

A1.106Practice1 mark

The octal equivalent of (22)₁₀ is

Answer

(c) 26 Divide by 8 again and again and read the remainders from bottom to top: 22 ÷ 8 = 2 r 6; 2 ÷ 8 = 0 r 2.

A1.107Practice1 mark

The ASCII code of 'a' is 97. What is the ASCII code of 'm'?

Answer

(c) 109 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.108Practice1 mark

The instructions needed to start a computer (boot) are stored in

Answer

(d) ROM The start-up (BIOS / boot) program is kept in ROM because ROM does not lose data when the power is off.

A1.109Practice1 mark

How many bits are there in 2 nibbles?

Answer

(b) 8 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.110Practice1 mark

The hexadecimal equivalent of (273)₈ is

Answer

(b) BB Go through binary: 273 = 010 111 011; regroup in 4 bits from the right: 1011 = B, 1011 = B.

A1.111Practice1 mark

The smallest unit of data in a computer is

Answer

(d) bit A bit (binary digit) is a single 0 or 1, the smallest unit; 8 bits make a byte.

A1.112Practice1 mark

The hexadecimal equivalent of (56)₈ is

Answer

(c) 2E Go through binary: 56 = 101 110; regroup in 4 bits from the right: 0010 = 2, 1110 = E.

A1.113Practice1 mark

One binary digit is called a

Answer

(d) bit bit is short for binary digit: it is either 0 or 1.

A1.114Practice1 mark

What is the value of (A OR B) AND (NOT C) when A = 0, B = 0, C = 0?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.115Practice1 mark

Which of these is NOT a function of an operating system?

Answer

(d) Writing letters for the user The OS manages processes, memory, files, devices and security; writing letters is the job of application software such as a word processor.

A1.116Practice1 mark

How many bits are there in 5 nibbles?

Answer

(a) 20 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.117Practice1 mark

The place value of the leftmost 1 in (1000)₂ is

Answer

(d) 8 Place values in binary are 8 4 2 1 from the left for 4 digits, so the leftmost 1 is worth 2³ = 8.

A1.118Practice1 mark

The octal equivalent of (201)₁₀ is

Answer

(b) 311 Divide by 8 again and again and read the remainders from bottom to top: 201 ÷ 8 = 25 r 1; 25 ÷ 8 = 3 r 1; 3 ÷ 8 = 0 r 3.

A1.119Practice1 mark

The ASCII code of 'a' is 97. What is the ASCII code of 'r'?

Answer

(b) 114 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.120Practice1 mark

The binary equivalent of (5B)₁₆ is

Answer

(b) 1011011 Write each hexadecimal digit as 4 bits: 5 = 0101, B = 1011.

A1.121Practice1 mark

The hexadecimal equivalent of (204)₁₀ is

Answer

(a) CC Divide by 16 again and again and read the remainders from bottom to top: 204 ÷ 16 = 12 r C; 12 ÷ 16 = 0 r C.

A1.122Practice1 mark

The octal equivalent of (27)₁₆ is

Answer

(c) 47 Go through binary: 27 = 0010 0111; regroup in 3 bits from the right: 100 = 4, 111 = 7.

A1.123Practice1 mark

The hexadecimal equivalent of (167)₈ is

Answer

(b) 77 Go through binary: 167 = 001 110 111; regroup in 4 bits from the right: 0111 = 7, 0111 = 7.

A1.124Practice1 mark

The binary equivalent of (135)₈ is

Answer

(a) 1011101 Write each octal digit as 3 bits: 1 = 001, 3 = 011, 5 = 101.

A1.125Practice1 mark

The decimal equivalent of (105)₈ is

Answer

(a) 69 Multiply each digit by its place value (powers of 8) and add: 1×8^2 + 0×8^1 + 5×8^0 = 69.

A1.126Practice1 mark

State True or False: An interpreter translates and runs a program one line at a time.

Answer

(a) True An interpreter takes one line, translates it and runs it, then moves to the next line.

A1.127Practice1 mark

The octal equivalent of (57)₁₆ is

Answer

(c) 127 Go through binary: 57 = 0101 0111; regroup in 3 bits from the right: 001 = 1, 010 = 2, 111 = 7.

A1.128Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'H'?

Answer

(b) 72 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.129Practice1 mark

The output of a NOR gate is 1 when

Answer

(b) all inputs are 0 NOR = NOT OR. OR gives 0 only when all inputs are 0, so NOR gives 1 only then.

A1.130Practice1 mark

Antivirus, disk cleanup and file compression tools are examples of

Answer

(a) utility software Utility software keeps the computer healthy and safe; it is a kind of system software.

A1.131Practice1 mark

The octal equivalent of (78)₁₆ is

Answer

(c) 170 Go through binary: 78 = 0111 1000; regroup in 3 bits from the right: 001 = 1, 111 = 7, 000 = 0.

A1.132Practice1 mark

Which bus carries the address of the memory location?

Answer

(c) Address bus The address bus carries the address of the memory location; the data bus carries the data and the control bus carries signals.

A1.133Practice1 mark

The hexadecimal equivalent of (214)₈ is

Answer

(c) 8C Go through binary: 214 = 010 001 100; regroup in 4 bits from the right: 1000 = 8, 1100 = C.

A1.134Practice1 mark

The binary equivalent of (27)₁₀ is

Answer

(a) 11011 Divide by 2 again and again and read the remainders from bottom to top: 27 ÷ 2 = 13 r 1; 13 ÷ 2 = 6 r 1; 6 ÷ 2 = 3 r 0; 3 ÷ 2 = 1 r 1; 1 ÷ 2 = 0 r 1.

A1.135Practice1 mark

By De Morgan's law, NOT (A OR B) equals

Answer

(c) (NOT A) AND (NOT B) De Morgan: NOT (A OR B) = (NOT A) AND (NOT B). Check with A = 0, B = 0: left side NOT 0 = 1, right side 1 AND 1 = 1.

A1.136Practice1 mark

ISCII is an encoding scheme made for

Answer

(b) Indian scripts ISCII (Indian Script Code for Information Interchange) was made for Indian scripts such as Devanagari.

A1.137Practice1 mark

Which device works as both input and output?

Answer

(b) Touch screen A touch screen shows output and also takes our touch as input.

A1.138Practice1 mark

The binary equivalent of (40)₈ is

Answer

(c) 100000 Write each octal digit as 3 bits: 4 = 100, 0 = 000.

A1.139Practice1 mark

State True or False: The output of NAND is the opposite (NOT) of the output of AND.

Answer

(a) True NAND = NOT AND, so its output is always the opposite of AND for the same inputs.

A1.140Practice1 mark

What is the value of A AND (NOT B) when A = 0, B = 1?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.141Practice1 mark

The octal equivalent of (6D)₁₆ is

Answer

(b) 155 Go through binary: 6D = 0110 1101; regroup in 3 bits from the right: 001 = 1, 101 = 5, 101 = 5.

A1.142Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'E'?

Answer

(c) 69 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.143Practice1 mark

How many bits are there in 6 nibbles?

Answer

(a) 24 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.144Practice1 mark

What is the value of A AND B AND NOT C when A = 0, B = 0, C = 0?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.145Practice1 mark

How many MB are there in 5 GBs?

Answer

(b) 5120 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.146Practice1 mark

The binary equivalent of (CF)₁₆ is

Answer

(a) 11001111 Write each hexadecimal digit as 4 bits: C = 1100, F = 1111.

A1.147Practice1 mark

UTF-8 uses

Answer

(b) 1 to 4 bytes for a character UTF-8 is variable length: 1 byte for English characters, up to 4 bytes for other scripts and symbols.

A1.148Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'K'?

Answer

(a) 75 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.149Practice1 mark

The decimal equivalent of (E5)₁₆ is

Answer

(c) 229 Multiply each digit by its place value (powers of 16) and add: E×16^1 + 5×16^0 = 229.

A1.150Practice1 mark

What is the value of A XOR B when A = 1, B = 1?

Answer

(a) 0 Work out NOT first, then AND, then OR.

A1.151Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'P'?

Answer

(d) 80 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.152Practice1 mark

How many bytes are there in 3 KBs?

Answer

(d) 3072 1 byte = 8 bits; 1 KB = 1024 bytes; 1 MB = 1024 KB; 1 GB = 1024 MB; 1 TB = 1024 GB.

A1.153Practice1 mark

The hexadecimal equivalent of (11110011)₂ is

Answer

(c) F3 Make groups of 4 bits from the right and write each group as one hexadecimal digit: 1111 = F, 0011 = 3.

A1.154Practice1 mark

The ASCII code of 'a' is 97. What is the ASCII code of 'h'?

Answer

(a) 104 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.155Practice1 mark

Which memory sits between the CPU and RAM to speed up processing?

Answer

(d) Cache memory Cache keeps the data the CPU uses most often, so the CPU need not wait for slower RAM.

A1.156Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'C'?

Answer

(b) 67 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.157Practice1 mark

The binary equivalent of (228)₁₀ is

Answer

(a) 11100100 Divide by 2 again and again and read the remainders from bottom to top: 228 ÷ 2 = 114 r 0; 114 ÷ 2 = 57 r 0; 57 ÷ 2 = 28 r 1; 28 ÷ 2 = 14 r 0; 14 ÷ 2 = 7 r 0; 7 ÷ 2 = 3 r 1 ….

A1.158Practice1 mark

The ASCII code of 'A' is 65. What is the ASCII code of 'G'?

Answer

(d) 71 Letters and digits have codes in order: 'A' = 65, 'a' = 97, '0' = 48.

A1.159Practice1 mark

The hexadecimal equivalent of (176)₈ is

Answer

(c) 7E Go through binary: 176 = 001 111 110; regroup in 4 bits from the right: 0111 = 7, 1110 = E.

A1.160Practice1 mark

Which encoding can represent characters of almost all the languages of the world?

Answer

(a) Unicode Unicode gives a unique code to the characters of almost every script, including Hindi, Tamil and Chinese.

A2Python concepts MCQ and True/False1 mark · 111 questions
A2.1Practice1 mark

Choosing one of two paths depending on a condition is called

Answer

(d) selection Selection (if ... else) picks one path according to a condition; sequence and iteration are the other two.

A2.2Practice1 mark

What does 'abc' * 0 give?

Answer

(d) an empty string '' Repeating a string 0 times gives the empty string ''.

A2.3Practice1 mark

Which statement skips the rest of the current loop round and goes to the next round?

Answer

(d) continue continue jumps to the next round of the loop.

A2.4Practice1 mark

math.floor(-4.1) gives

Answer

(b) -5 floor() gives the largest whole number not greater than the value; for -4.1 that is -5 (it moves down the number line).

A2.5Practice1 mark

Which method adds one element at the end of a list?

Answer

(d) append() L.append(5) adds 5 as the last element.

A2.6Practice1 mark

A list can contain

Answer

(b) elements of different types A list can hold any mix of types, e.g. [1, 'KV', 2.5, True].

A2.7Practice1 mark

What is the length of [1, [2, 3], 4]?

Answer

(d) 3 The inner list counts as one element: 1, [2, 3], 4.

A2.8Practice1 mark

Which statement does nothing and is used as a placeholder?

Answer

(b) pass pass is used where a statement is needed but nothing is to be done.

A2.9Practice1 mark

Which function gives the memory address (identity) of an object?

Answer

(b) id() id(x) gives the identity of the object x.

A2.10Practice1 mark

Which operator checks whether two names refer to the same object?

Answer

(a) is is checks identity (same object); == checks equal values.

A2.11Practice1 mark

A program runs but gives the wrong answer because of a wrong formula. This is a

Answer

(a) logical error A logical error gives no error message; the program runs but the result is wrong.

A2.12Practice1 mark

Which method removes an element by index and returns it?

Answer

(c) pop() L.pop(2) removes and returns the element at index 2.

A2.13Practice1 mark

The smallest individual unit of a Python program is a

Answer

(d) token Tokens: keywords, identifiers, literals, operators and punctuators.

A2.14Practice1 mark

list('KV') gives

Answer

(c) ['K', 'V'] list() makes one element from each character.

A2.15Practice1 mark

The default value of end in print() is

Answer

(b) a new line (\n) After printing, print() goes to a new line unless end is changed.

A2.16Practice1 mark

Which method checks if every character of a string is a digit?

Answer

(a) isdigit() '2026'.isdigit() is True; '20a6'.isdigit() is False.

A2.17Practice1 mark

Which of these is a valid identifier?

Answer

(c) total_marks It has letters and an underscore; it does not start with a digit, has no hyphen and is not a keyword.

A2.18Practice1 mark

Which function returns the median of a list?

Answer

(b) statistics.median() statistics.median([3, 1, 2]) sorts the values and gives the middle one: 2.

A2.19Practice1 mark

Accessing a key that is not in the dictionary with d[key] gives

Answer

(a) KeyError D['x'] for a missing key gives KeyError.

A2.20Practice1 mark

Which of these is immutable?

Answer

(b) tuple A tuple cannot be changed after it is made; lists, dictionaries and sets can be changed.

A2.21Practice1 mark

Which flowchart symbol is used for input and output?

Answer

(c) Parallelogram The parallelogram is used for input (read N) and output (print).

A2.22Practice1 mark

random.random() returns a float

Answer

(a) from 0.0 up to, but not including, 1.0 random.random() gives a float from 0.0 up to but not including 1.0, e.g. 0.3721.

A2.23Practice1 mark

Which of these is a valid string literal?

Answer

(b) '''KV''' Triple quotes ''' ... ''' (or """ ... """) make a valid string, which can also go over many lines.

A2.24Practice1 mark

Which data type is mutable?

Answer

(b) list A list can be changed in place (append, remove, L[0] = 5); strings and tuples cannot.

A2.25Practice1 mark

Which operator gives the remainder?

Answer

(d) % 17 % 5 gives 2, the remainder.

A2.26Practice1 mark

random.choice([3, 6, 9]) returns

Answer

(d) any one element of the list choice() picks any one element of the sequence at random.

A2.27Practice1 mark

s.partition('-') returns

Answer

(b) a tuple of three parts partition() splits at the first '-' and returns a tuple (before, '-', after); e.g. 'a-b-c'.partition('-') = ('a', '-', 'b-c').

A2.28Practice1 mark

The value None belongs to the data type

Answer

(d) NoneType None is the only value of the NoneType type.

A2.29Practice1 mark

Which method removes all the elements of a list?

Answer

(b) clear() L.clear() leaves an empty list [].

A2.30Practice1 mark

Which method adds all the elements of another list at the end?

Answer

(b) extend() L.extend([4, 5]) adds 4 and 5 one by one.

A2.31Practice1 mark

Which of these is a membership operator?

Answer

(a) in in checks whether a value is present: 3 in [1, 3] is True.

A2.32Practice1 mark

Which of these is a keyword in Python?

Answer

(b) elif sum, print and input are built-in functions, not keywords.

A2.33Practice1 mark

input() always returns a value of type

Answer

(c) str input() returns a string; use int() or float() to get a number.

A2.34Practice1 mark

Strings in Python are

Answer

(b) immutable Strings cannot be changed in place: s[0] = 'P' gives TypeError. Methods return new strings.

A2.35Practice1 mark

A comment in Python starts with

Answer

(a) # Everything after # on a line is ignored by Python.

A2.36Practice1 mark

Which statement deletes the elements from index 1 to 2 of L?

Answer

(b) del L[1:3] del L[1:3] deletes index 1 and 2 (the stop index 3 is not included).

A2.37Practice1 mark

The value of 5 > 3 and 3 > 1 is

Answer

(c) True Both conditions are True, so and gives True.

A2.38Practice1 mark

An error in the grammar of the code, such as a missing colon, is a

Answer

(c) syntax error Wrong grammar (syntax) is caught before the program runs.

A2.39Practice1 mark

Which method removes the first occurrence of a given value?

Answer

(a) remove() L.remove(10) deletes the first 10 in the list.

A2.40Practice1 mark

Finding and removing errors from a program is called

Answer

(a) debugging Debugging means finding and correcting errors (bugs).

A2.41Practice1 mark

The default separator (sep) in print() is

Answer

(d) a space print(1, 2) prints 1 2 with a space between.

A2.42Practice1 mark

Which statement ends the loop at once?

Answer

(a) break break stops the loop immediately.

A2.43Practice1 mark

Which method breaks a string into a list of words?

Answer

(b) split() 'I love KV'.split() gives ['I', 'love', 'KV'].

A2.44Practice1 mark

A step-by-step, finite set of instructions to solve a problem is

Answer

(d) an algorithm An algorithm is a finite list of clear steps that solves a problem.

A2.45Practice1 mark

d.update(e)

Answer

(b) adds the pairs of e to d, changing the values of common keys New keys are added and the values of existing keys are replaced.

A2.46Practice1 mark

State True or False: In Python, a variable must be declared with its type before use.

Answer

(b) False Python finds the type from the value given.

A2.47Practice1 mark

Breaking a complex problem into smaller sub-problems is called

Answer

(a) decomposition Decomposition breaks a big problem into small parts that are easier to solve.

A2.48Practice1 mark

The else part of a loop runs when

Answer

(d) the loop ends normally, without break The loop's else runs only if the loop was not stopped by break.

A2.49Practice1 mark

Which method removes spaces from both ends of a string?

Answer

(d) strip() ' KV '.strip() gives 'KV'; lstrip() and rstrip() remove from one side only.

A2.50Practice1 mark

Python program files are saved with the extension

Answer

(a) .py Python scripts are saved as name.py.

A2.51Practice1 mark

Which of these converts 65 into 'A'?

Answer

(b) chr(65) chr() gives the character of a code: chr(65) = 'A'; ord('A') = 65.

A2.52Practice1 mark

Which statement is used to write an empty if block?

Answer

(d) pass An empty block gives an error, so pass is written inside it.

A2.53Practice1 mark

Converting the type of a value by a function such as int() or float() is called

Answer

(c) explicit type conversion The programmer converts it on purpose, so it is explicit conversion.

A2.54Practice1 mark

Searching a list element by element from the start is called

Answer

(a) linear search Checking every element one after another is linear search.

A2.55Practice1 mark

Which method returns the value of a key, and None if the key is not present?

Answer

(d) get() D.get('x') returns None instead of giving KeyError.

A2.56Practice1 mark

A loop that never ends is called

Answer

(c) an infinite loop If the condition never becomes False, the loop runs for ever.

A2.57Practice1 mark

The output of print(10, 20, sep='-') is

Answer

(c) 10-20 sep='-' puts - between the values instead of a space.

A2.58Practice1 mark

Python marks a block of statements by

Answer

(a) indentation Python uses indentation (usually 4 spaces) to show which statements belong to a block.

A2.59Practice1 mark

The keys of a dictionary must be

Answer

(a) immutable and unique Keys must be immutable (numbers, strings, tuples) and unique; values can be anything.

A2.60Practice1 mark

Joining two strings with + is called

Answer

(c) concatenation 'KV' + 'S' = 'KVS': joining strings is concatenation.

A2.61Practice1 mark

Which of these is NOT a token?

Answer

(b) comment Comments are ignored by Python.

A2.62Practice1 mark

L.sort() and sorted(L) differ because

Answer

(a) sort() changes L, sorted() returns a new list L.sort() sorts the same list and returns None; sorted(L) makes a new sorted list and L stays the same.

A2.63Practice1 mark

State True or False: Dictionary keys can be repeated.

Answer

(b) False A new value for an existing key replaces the old value.

A2.64Practice1 mark

What is the result of 9 % 4 ** 2?

Answer

(c) 9 4 ** 2 = 16 first; 9 % 16 = 9.

A2.65Practice1 mark

'Hi' * 3 is an example of

Answer

(d) replication * repeats a string: 'Hi' * 3 = 'HiHiHi'. This is replication.

A2.66Practice1 mark

Which method returns all the key-value pairs of a dictionary?

Answer

(d) items() D.items() gives (key, value) pairs.

A2.67Practice1 mark

State True or False: math.sqrt(16) returns 4.0, a float.

Answer

(a) True math.sqrt() always returns a float, so sqrt(16) is 4.0.

A2.68Practice1 mark

Which method returns the position of a substring, and -1 if it is not found?

Answer

(b) find() 'KVS'.find('V') = 1 and 'KVS'.find('Z') = -1; index() would give an error instead.

A2.69Practice1 mark

bool(0) and bool('') give

Answer

(d) False and False 0 and the empty string are treated as False.

A2.70Practice1 mark

Which flowchart symbol shows a decision?

Answer

(c) Diamond The diamond holds a condition and has two exits, Yes and No.

A2.71Practice1 mark

Packing values into a tuple and taking them out into variables is called

Answer

(d) packing and unpacking T = 1, 2, 3 packs the values; a, b, c = T unpacks them.

A2.72Practice1 mark

s[-1] gives

Answer

(d) the last character of s Negative indexes count from the end: s[-1] is the last character.

A2.73Practice1 mark

index() is different from find() because index()

Answer

(c) gives an error when the substring is not found Both give the position, but index() raises ValueError when the substring is missing, while find() returns -1.

A2.74Practice1 mark

math.ceil(4.1) gives

Answer

(d) 5 ceil() gives the smallest whole number not less than the value, so 4.1 becomes 5.

A2.75Practice1 mark

Python is case sensitive. So Total and total are

Answer

(d) two different names Capital and small letters are different in Python names, so Total and total are two variables.

A2.76Practice1 mark

The value of math.pi to two decimal places is

Answer

(c) 3.14 math.pi = 3.141592653589793, which is 3.14 to two decimal places.

A2.77Practice1 mark

Which function converts the string '25' into the integer 25?

Answer

(b) int() int('25') gives 25; float('25') would give 25.0.

A2.78Practice1 mark

In Python, complex numbers are written as

Answer

(d) 3 + 4j Python uses j for the imaginary part: 3 + 4j.

A2.79Practice1 mark

Which module has the functions mean, median and mode?

Answer

(a) statistics import statistics, then statistics.mean(), statistics.median() and statistics.mode().

A2.80Practice1 mark

Which of these names is NOT valid?

Answer

(c) my-name A hyphen is not allowed in a name; Python reads it as minus.

A2.81Practice1 mark

The first step of problem solving is to

Answer

(b) analyse and understand the problem First understand the problem: what is given, what is wanted and any conditions; only then design the algorithm.

A2.82Practice1 mark

Checking an algorithm by working it out on paper with sample values is called

Answer

(b) dry run In a dry run we follow the steps by hand with sample values to check the result.

A2.83Practice1 mark

A tuple with one element is written as

Answer

(a) (5,) The comma makes it a tuple: (5,) is a tuple, but (5) is just the number 5.

A2.84Practice1 mark

State True or False: The = operator compares two values.

Answer

(b) False = assigns; == compares.

A2.85Practice1 mark

Which method makes a dictionary from a list of keys, with the same value for every key?

Answer

(c) dict.fromkeys() dict.fromkeys(['a', 'b'], 0) gives {'a': 0, 'b': 0}.

A2.86Practice1 mark

In 5 + 2.0, Python converts 5 to a float on its own. This is

Answer

(c) implicit type conversion Python converts it automatically, so it is implicit conversion.

A2.87Practice1 mark

tuple([1, 2]) gives

Answer

(a) (1, 2) tuple() converts the list into a tuple: (1, 2).

A2.88Practice1 mark

Repeating a set of steps again and again in an algorithm is called

Answer

(c) iteration Iteration means repetition, done with loops such as for and while.

A2.89Practice1 mark

Which operator is used for exponent (power)?

Answer

(a) ** 2 ** 3 gives 8; ^ is not power in Python.

A2.90Practice1 mark

A rectangle in a flowchart shows

Answer

(b) a process The rectangle is the process box, used for calculations such as sum = a + b.

A2.91Practice1 mark

Which of these is NOT allowed on a tuple?

Answer

(b) T.append(5) Tuples are immutable, so they have no append(); count(), index() and slicing are allowed.

A2.92Practice1 mark

range(5) gives the numbers

Answer

(c) 0, 1, 2, 3, 4 range(5) starts at 0 and stops before 5.

A2.93Practice1 mark

len({'a': 1, 'b': 2}) is

Answer

(c) 2 There are two key-value pairs.

A2.94Practice1 mark

Dividing a number by zero while the program runs gives a

Answer

(c) runtime error (ZeroDivisionError) The program starts but stops with ZeroDivisionError while running.

A2.95Practice1 mark

How many times does 'for i in range(3, 15, 4)' run?

Answer

(b) 3 i takes 3, 7 and 11.

A2.96Practice1 mark

An algorithm must end after a finite number of steps. This property is called

Answer

(d) finiteness Finiteness: an algorithm must stop after a fixed number of steps; an endless process is not an algorithm.

A2.97Practice1 mark

Which mode of Python runs one statement at a time with the >>> prompt?

Answer

(b) Interactive mode Interactive mode shows the >>> prompt and gives the result of each statement at once.

A2.98Practice1 mark

Python is a

Answer

(b) high-level, interpreted language Python is close to English (high level) and is run by an interpreter.

A2.99Practice1 mark

A while loop is called

Answer

(b) entry-controlled The condition is checked before each round (at the entry).

A2.100Practice1 mark

State True or False: L[1] = 10 is allowed on a list L of 3 elements.

Answer

(a) True Lists are mutable and index 1 exists in a 3-element list.

A2.101Practice1 mark

Which statement imports only the sqrt function from math?

Answer

(d) from math import sqrt from math import sqrt brings in only sqrt, which can then be used without writing math.

A2.102Practice1 mark

Writing the steps of an algorithm in simple English-like statements is called

Answer

(a) pseudocode Pseudocode is written like a program but in simple English, without the strict rules of a language.

A2.103Practice1 mark

State True or False: A while loop may not run even once.

Answer

(a) True If the condition is False at the start, the body is skipped.

A2.104Practice1 mark

Which loop is best when the number of repetitions is known?

Answer

(c) for for i in range(n) runs exactly n times.

A2.105Practice1 mark

'Hello'.replace('l', 'L', 1) gives

Answer

(a) 'HeLlo' The third argument 1 means replace only the first 'l', so 'Hello' becomes 'HeLlo'.

A2.106Practice1 mark

A loop inside another loop is called

Answer

(c) a nested loop The inner loop runs fully for every round of the outer loop.

A2.107Practice1 mark

random.randint(1, 6) can return

Answer

(a) 1, 2, 3, 4, 5 or 6 randint(a, b) includes both ends, so any of 1, 2, 3, 4, 5, 6 can come.

A2.108Practice1 mark

State True or False: s.upper() changes the string s itself.

Answer

(b) False Strings are immutable; upper() returns a new string.

A2.109Practice1 mark

The oval (terminal) symbol in a flowchart shows

Answer

(d) start or stop The oval marks where the flowchart starts and stops.

A2.110Practice1 mark

Which of these can be a dictionary key?

Answer

(a) (1, 2) A key must be immutable: numbers, strings and tuples can be keys; lists and dictionaries cannot.

A2.111Practice1 mark

The index of the first character of a string is

Answer

(a) 0 Indexing starts from 0, so s[0] is the first character.

A3Output: operators and flow of control1 mark · 150 questions
A3.1Practice1 mark

What is the output of the following code?

print(17 + 5 * 4 // 2)
Answer

(b) 27 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.2Practice1 mark

What is the output of the following code?

print(20 / 4, 20 // 4, 20 % 4)
Answer

(a) 5.0 5 0 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.3Practice1 mark

What is the output of the following code?

print(-23 // 4, -23 % 4)
Answer

(b) -6 1 // rounds down towards minus infinity, so -23 // 4 is -6; the remainder takes the sign of the divisor.

A3.4Practice1 mark

What is the output of the following code?

print(0 or 5)
Answer

(c) 5 not is applied first, then and, then or. and/or return one of the operands.

A3.5Practice1 mark

What is the output of the following code?

print(type(float(7)))
Answer

(a) <class 'float'> The type of the result decides the class.

A3.6Practice1 mark

What is the output of the following code?

for i in range(0, 5, 1):
    print(i, end=" ")
Answer

(a) 0 1 2 3 4 range(start, stop, step) starts at start and stops before stop.

A3.7Practice1 mark

What is the output of the following code?

for i in range(0, -11, -2):
    print(i, end=" ")
Answer

(b) 0 -2 -4 -6 -8 -10 range(start, stop, step) starts at start and stops before stop.

A3.8Practice1 mark

What is the output of the following code?

n = 49
count = 0
while n > 7:
    n = n - 4
    count += 1
print(count, n)
Answer

(a) 11 5 Trace the loop: the condition is checked before every round.

A3.9Practice1 mark

What is the output of the following code?

for i in range(4, 13):
    if i % 3 == 0:
        continue
    print(i, end=" ")
Answer

(a) 4 5 7 8 10 11 break leaves the loop; continue skips only the rest of that round.

A3.10Practice1 mark

What is the output of the following code?

x = 9
x *= 2 + 3
print(x)
Answer

(a) 45 In a, b = b, a + b the right side is worked out first, with the old values.

A3.11Practice1 mark

What is the output of the following code?

print(float('9') * 2)
Answer

(d) 18.0 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.12Practice1 mark

What is the output of the following code?

marks = 78
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(d) A Only the first true condition of an if-elif chain runs.

A3.13Practice1 mark

What is the output of the following code?

c = 0
for i in range(1, 6):
    for j in range(i):
        c += j
print(c)
Answer

(c) 20 The inner loop runs fully for every round of the outer loop.

A3.14Practice1 mark

What is the output of the following code?

print(14 - 5 // 2 ** 3)
Answer

(c) 14 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.15Practice1 mark

What is the output of the following code?

print(7 + 5 * 3 % 3)
Answer

(d) 7 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.16Practice1 mark

What is the output of the following code?

print(34 / 6, 34 // 6, 34 % 6)
Answer

(d) 5.666666666666667 5 4 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.17Practice1 mark

What is the output of the following code?

print(not 1)
Answer

(b) False not is applied first, then and, then or. and/or return one of the operands.

A3.18Practice1 mark

What is the output of the following code?

print(type((7)))
Answer

(b) <class 'int'> The type of the result decides the class.

A3.19Practice1 mark

What is the output of the following code?

for i in range(0, 4, 1):
    print(i, end=" ")
Answer

(d) 0 1 2 3 range(start, stop, step) starts at start and stops before stop.

A3.20Practice1 mark

What is the output of the following code?

for i in range(5, 12, 1):
    print(i, end=" ")
Answer

(b) 5 6 7 8 9 10 11 range(start, stop, step) starts at start and stops before stop.

A3.21Practice1 mark

What is the output of the following code?

n = 27
count = 0
while n > 6:
    n = n - 9
    count += 1
print(count, n)
Answer

(a) 3 0 Trace the loop: the condition is checked before every round.

A3.22Practice1 mark

What is the output of the following code?

for i in range(1, 16):
    if i % 3 == 0:
        continue
    print(i, end=" ")
Answer

(c) 1 2 4 5 7 8 10 11 13 14 break leaves the loop; continue skips only the rest of that round.

A3.23Practice1 mark

What is the output of the following code?

x = 3
x -= 2
x **= 2
print(x)
Answer

(c) 1 In a, b = b, a + b the right side is worked out first, with the old values.

A3.24Practice1 mark

What is the output of the following code?

print(str(3) * 2)
Answer

(d) 33 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.25Practice1 mark

What is the output of the following code?

marks = 49
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(b) C Only the first true condition of an if-elif chain runs.

A3.26Practice1 mark

What is the output of the following code?

c = 0
for i in range(4):
    for j in range(4):
        c += 1
print(c)
Answer

(d) 16 The inner loop runs fully for every round of the outer loop.

A3.27Practice1 mark

What is the output of the following code?

print(23 - 5 // 3 ** 2)
Answer

(d) 23 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.28Practice1 mark

What is the output of the following code?

print(7 + 6 // 5 * 3)
Answer

(d) 10 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.29Practice1 mark

What is the output of the following code?

print(22 / 6, 22 // 6, 22 % 6)
Answer

(a) 3.6666666666666665 3 4 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.30Practice1 mark

What is the output of the following code?

print(-21 // 6, -21 % 6)
Answer

(d) -4 3 // rounds down towards minus infinity, so -21 // 6 is -4; the remainder takes the sign of the divisor.

A3.31Practice1 mark

What is the output of the following code?

for i in range(2, 9, 2):
    print(i, end=" ")
Answer

(b) 2 4 6 8 range(start, stop, step) starts at start and stops before stop.

A3.32Practice1 mark

What is the output of the following code?

n = 54
count = 0
while n > 7:
    n = n - 6
    count += 1
print(count, n)
Answer

(b) 8 6 Trace the loop: the condition is checked before every round.

A3.33Practice1 mark

What is the output of the following code?

for i in range(2, 12):
    if i % 6 == 0:
        break
    print(i, end=" ")
Answer

(a) 2 3 4 5 break leaves the loop; continue skips only the rest of that round.

A3.34Practice1 mark

What is the output of the following code?

x = 8
x -= 2
x **= 2
print(x)
Answer

(a) 36 In a, b = b, a + b the right side is worked out first, with the old values.

A3.35Practice1 mark

What is the output of the following code?

print(str(8) * 2)
Answer

(d) 88 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.36Practice1 mark

What is the output of the following code?

marks = 4
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(a) Fail Only the first true condition of an if-elif chain runs.

A3.37Practice1 mark

What is the output of the following code?

c = 0
for i in range(1, 5):
    for j in range(i):
        c += j
print(c)
Answer

(a) 10 The inner loop runs fully for every round of the outer loop.

A3.38Practice1 mark

What is the output of the following code?

print(8 + 2 * 2 % 4)
Answer

(c) 8 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.39Practice1 mark

What is the output of the following code?

print(22 + 6 * 4 ** 2)
Answer

(d) 118 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.40Practice1 mark

What is the output of the following code?

print(-19 // 6, -19 % 6)
Answer

(a) -4 5 // rounds down towards minus infinity, so -19 // 6 is -4; the remainder takes the sign of the divisor.

A3.41Practice1 mark

What is the output of the following code?

print(7 > 5 and 12 < 10 or not 0)
Answer

(d) True not is applied first, then and, then or. and/or return one of the operands.

A3.42Practice1 mark

What is the output of the following code?

print(type('3' + '4'))
Answer

(d) <class 'str'> The type of the result decides the class.

A3.43Practice1 mark

What is the output of the following code?

for i in range(2, 7, 2):
    print(i, end=" ")
Answer

(c) 2 4 6 range(start, stop, step) starts at start and stops before stop.

A3.44Practice1 mark

What is the output of the following code?

for i in range(1, -11, -2):
    print(i, end=" ")
Answer

(a) 1 -1 -3 -5 -7 -9 range(start, stop, step) starts at start and stops before stop.

A3.45Practice1 mark

What is the output of the following code?

n = 35
count = 0
while n > 6:
    n = n - 5
    count += 1
print(count, n)
Answer

(c) 6 5 Trace the loop: the condition is checked before every round.

A3.46Practice1 mark

What is the output of the following code?

for i in range(3, 12):
    if i % 6 == 0:
        break
    print(i, end=" ")
Answer

(c) 3 4 5 break leaves the loop; continue skips only the rest of that round.

A3.47Practice1 mark

What is the output of the following code?

a = 4
b = a * 2
a = b - a
b //= 3
print(a, b)
Answer

(d) 4 2 In a, b = b, a + b the right side is worked out first, with the old values.

A3.48Practice1 mark

What is the output of the following code?

marks = 55
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(b) C Only the first true condition of an if-elif chain runs.

A3.49Practice1 mark

What is the output of the following code?

print(20 - 4 % 3 % 3)
Answer

(d) 19 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.50Practice1 mark

What is the output of the following code?

print(27 - 6 * 2 ** 2)
Answer

(b) 3 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.51Practice1 mark

What is the output of the following code?

print(42 / 3, 42 // 3, 42 % 3)
Answer

(c) 14.0 14 0 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.52Practice1 mark

What is the output of the following code?

print(-19 // 5, -19 % 5)
Answer

(a) -4 1 // rounds down towards minus infinity, so -19 // 5 is -4; the remainder takes the sign of the divisor.

A3.53Practice1 mark

What is the output of the following code?

print(not 5 > 3 or 5 == 3 and 0)
Answer

(a) False not is applied first, then and, then or. and/or return one of the operands.

A3.54Practice1 mark

What is the output of the following code?

print(type(7 / 2))
Answer

(b) <class 'float'> The type of the result decides the class.

A3.55Practice1 mark

What is the output of the following code?

for i in range(5, -7, -2):
    print(i, end=" ")
Answer

(c) 5 3 1 -1 -3 -5 range(start, stop, step) starts at start and stops before stop.

A3.56Practice1 mark

What is the output of the following code?

for i in range(3, -8, -2):
    print(i, end=" ")
Answer

(b) 3 1 -1 -3 -5 -7 range(start, stop, step) starts at start and stops before stop.

A3.57Practice1 mark

What is the output of the following code?

n = 38
count = 0
while n > 1:
    n = n - 9
    count += 1
print(count, n)
Answer

(d) 5 -7 Trace the loop: the condition is checked before every round.

A3.58Practice1 mark

What is the output of the following code?

x = 10
x *= 2 + 3
print(x)
Answer

(b) 50 In a, b = b, a + b the right side is worked out first, with the old values.

A3.59Practice1 mark

What is the output of the following code?

print(str(9) * 2)
Answer

(a) 99 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.60Practice1 mark

What is the output of the following code?

marks = 7
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(c) Fail Only the first true condition of an if-elif chain runs.

A3.61Practice1 mark

What is the output of the following code?

print(16 - 6 % 4 * 3)
Answer

(a) 10 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.62Practice1 mark

What is the output of the following code?

print(10 - 2 // 3 % 3)
Answer

(d) 10 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.63Practice1 mark

What is the output of the following code?

print(56 / 9, 56 // 9, 56 % 9)
Answer

(d) 6.222222222222222 6 2 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.64Practice1 mark

What is the output of the following code?

for i in range(1, 13, 2):
    print(i, end=" ")
Answer

(c) 1 3 5 7 9 11 range(start, stop, step) starts at start and stops before stop.

A3.65Practice1 mark

What is the output of the following code?

for i in range(6, 10, 2):
    print(i, end=" ")
Answer

(a) 6 8 range(start, stop, step) starts at start and stops before stop.

A3.66Practice1 mark

What is the output of the following code?

n = 39
count = 0
while n > 7:
    n = n - 4
    count += 1
print(count, n)
Answer

(a) 8 7 Trace the loop: the condition is checked before every round.

A3.67Practice1 mark

What is the output of the following code?

for i in range(3, 10):
    if i % 2 == 0:
        continue
    print(i, end=" ")
Answer

(c) 3 5 7 9 break leaves the loop; continue skips only the rest of that round.

A3.68Practice1 mark

What is the output of the following code?

print(int(8.5), round(8.5))
Answer

(a) 8 8 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.69Practice1 mark

What is the output of the following code?

marks = 12
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(c) Fail Only the first true condition of an if-elif chain runs.

A3.70Practice1 mark

What is the output of the following code?

print(13 + 6 * 2 ** 2)
Answer

(a) 37 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.71Practice1 mark

What is the output of the following code?

print(28 - 5 // 2 % 2)
Answer

(b) 28 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.72Practice1 mark

What is the output of the following code?

print(28 / 3, 28 // 3, 28 % 3)
Answer

(c) 9.333333333333334 9 1 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.73Practice1 mark

What is the output of the following code?

print(-22 // 4, -22 % 4)
Answer

(d) -6 2 // rounds down towards minus infinity, so -22 // 4 is -6; the remainder takes the sign of the divisor.

A3.74Practice1 mark

What is the output of the following code?

print(6 and 9)
Answer

(a) 9 not is applied first, then and, then or. and/or return one of the operands.

A3.75Practice1 mark

What is the output of the following code?

print(type(7 // 2))
Answer

(b) <class 'int'> The type of the result decides the class.

A3.76Practice1 mark

What is the output of the following code?

for i in range(6, 12, 1):
    print(i, end=" ")
Answer

(a) 6 7 8 9 10 11 range(start, stop, step) starts at start and stops before stop.

A3.77Practice1 mark

What is the output of the following code?

for i in range(2, -10, -2):
    print(i, end=" ")
Answer

(a) 2 0 -2 -4 -6 -8 range(start, stop, step) starts at start and stops before stop.

A3.78Practice1 mark

What is the output of the following code?

n = 43
count = 0
while n > 1:
    n = n - 3
    count += 1
print(count, n)
Answer

(d) 14 1 Trace the loop: the condition is checked before every round.

A3.79Practice1 mark

What is the output of the following code?

for i in range(2, 13):
    if i % 8 == 0:
        break
    print(i, end=" ")
Answer

(a) 2 3 4 5 6 7 break leaves the loop; continue skips only the rest of that round.

A3.80Practice1 mark

What is the output of the following code?

a = 9
b = 8
a += b
b = a - b
a = a - b
print(a, b)
Answer

(c) 8 9 In a, b = b, a + b the right side is worked out first, with the old values.

A3.81Practice1 mark

What is the output of the following code?

print(float('2') * 2)
Answer

(d) 4.0 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.82Practice1 mark

What is the output of the following code?

marks = 14
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(d) Fail Only the first true condition of an if-elif chain runs.

A3.83Practice1 mark

What is the output of the following code?

c = 0
for i in range(2):
    for j in range(4):
        c += 1
print(c)
Answer

(b) 8 The inner loop runs fully for every round of the outer loop.

A3.84Practice1 mark

What is the output of the following code?

print(13 - 3 // 3 % 4)
Answer

(c) 12 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.85Practice1 mark

What is the output of the following code?

print(26 + 2 * 5 * 4)
Answer

(c) 66 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.86Practice1 mark

What is the output of the following code?

print(57 / 9, 57 // 9, 57 % 9)
Answer

(c) 6.333333333333333 6 3 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.87Practice1 mark

What is the output of the following code?

print(not 0)
Answer

(a) True not is applied first, then and, then or. and/or return one of the operands.

A3.88Practice1 mark

What is the output of the following code?

print(type('7' * 2))
Answer

(a) <class 'str'> The type of the result decides the class.

A3.89Practice1 mark

What is the output of the following code?

for i in range(1, 7, 3):
    print(i, end=" ")
Answer

(b) 1 4 range(start, stop, step) starts at start and stops before stop.

A3.90Practice1 mark

What is the output of the following code?

for i in range(1, -8, -2):
    print(i, end=" ")
Answer

(d) 1 -1 -3 -5 -7 range(start, stop, step) starts at start and stops before stop.

A3.91Practice1 mark

What is the output of the following code?

n = 34
count = 0
while n > 7:
    n = n - 6
    count += 1
print(count, n)
Answer

(d) 5 4 Trace the loop: the condition is checked before every round.

A3.92Practice1 mark

What is the output of the following code?

for i in range(1, 14):
    if i % 3 == 0:
        continue
    print(i, end=" ")
Answer

(d) 1 2 4 5 7 8 10 11 13 break leaves the loop; continue skips only the rest of that round.

A3.93Practice1 mark

What is the output of the following code?

a, b = 8, 10
a, b = b, a + b
print(a, b)
Answer

(c) 10 18 In a, b = b, a + b the right side is worked out first, with the old values.

A3.94Practice1 mark

What is the output of the following code?

print(float('8') * 2)
Answer

(d) 16.0 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.95Practice1 mark

What is the output of the following code?

marks = 42
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(a) C Only the first true condition of an if-elif chain runs.

A3.96Practice1 mark

What is the output of the following code?

c = 0
for i in range(2):
    for j in range(3):
        c += 1
print(c)
Answer

(d) 6 The inner loop runs fully for every round of the outer loop.

A3.97Practice1 mark

What is the output of the following code?

print(24 - 5 // 5 % 2)
Answer

(b) 23 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.98Practice1 mark

What is the output of the following code?

print(30 + 4 // 2 ** 2)
Answer

(d) 31 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.99Practice1 mark

What is the output of the following code?

print(20 / 8, 20 // 8, 20 % 8)
Answer

(b) 2.5 2 4 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.100Practice1 mark

What is the output of the following code?

print(type(2 ** -1))
Answer

(b) <class 'float'> The type of the result decides the class.

A3.101Practice1 mark

What is the output of the following code?

for i in range(1, -6, -2):
    print(i, end=" ")
Answer

(c) 1 -1 -3 -5 range(start, stop, step) starts at start and stops before stop.

A3.102Practice1 mark

What is the output of the following code?

for i in range(0, 12, 3):
    print(i, end=" ")
Answer

(d) 0 3 6 9 range(start, stop, step) starts at start and stops before stop.

A3.103Practice1 mark

What is the output of the following code?

n = 34
count = 0
while n > 2:
    n = n - 9
    count += 1
print(count, n)
Answer

(c) 4 -2 Trace the loop: the condition is checked before every round.

A3.104Practice1 mark

What is the output of the following code?

for i in range(2, 14):
    if i % 3 == 0:
        continue
    print(i, end=" ")
Answer

(d) 2 4 5 7 8 10 11 13 break leaves the loop; continue skips only the rest of that round.

A3.105Practice1 mark

What is the output of the following code?

a = 10
b = 8
a += b
b = a - b
a = a - b
print(a, b)
Answer

(c) 8 10 In a, b = b, a + b the right side is worked out first, with the old values.

A3.106Practice1 mark

What is the output of the following code?

print(21 + 4 * 3 * 2)
Answer

(a) 45 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.107Practice1 mark

What is the output of the following code?

print(27 - 3 % 4 ** 3)
Answer

(b) 24 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.108Practice1 mark

What is the output of the following code?

print(47 / 7, 47 // 7, 47 % 7)
Answer

(c) 6.714285714285714 6 5 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.109Practice1 mark

What is the output of the following code?

print(-14 // 4, -14 % 4)
Answer

(d) -4 2 // rounds down towards minus infinity, so -14 // 4 is -4; the remainder takes the sign of the divisor.

A3.110Practice1 mark

What is the output of the following code?

print(9 < 6 < 15)
Answer

(a) False not is applied first, then and, then or. and/or return one of the operands.

A3.111Practice1 mark

What is the output of the following code?

print(type(7 % 2 == 1))
Answer

(c) <class 'bool'> The type of the result decides the class.

A3.112Practice1 mark

What is the output of the following code?

for i in range(0, -8, -1):
    print(i, end=" ")
Answer

(a) 0 -1 -2 -3 -4 -5 -6 -7 range(start, stop, step) starts at start and stops before stop.

A3.113Practice1 mark

What is the output of the following code?

for i in range(5, 12, 3):
    print(i, end=" ")
Answer

(d) 5 8 11 range(start, stop, step) starts at start and stops before stop.

A3.114Practice1 mark

What is the output of the following code?

n = 47
count = 0
while n > 3:
    n = n - 9
    count += 1
print(count, n)
Answer

(c) 5 2 Trace the loop: the condition is checked before every round.

A3.115Practice1 mark

What is the output of the following code?

for i in range(3, 15):
    if i % 7 == 0:
        break
    print(i, end=" ")
Answer

(d) 3 4 5 6 break leaves the loop; continue skips only the rest of that round.

A3.116Practice1 mark

What is the output of the following code?

a = 2
b = 5
a += b
b = a - b
a = a - b
print(a, b)
Answer

(a) 5 2 In a, b = b, a + b the right side is worked out first, with the old values.

A3.117Practice1 mark

What is the output of the following code?

print(int('7') + 7, '7' + '7')
Answer

(c) 14 77 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A3.118Practice1 mark

What is the output of the following code?

marks = 81
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(a) A Only the first true condition of an if-elif chain runs.

A3.119Practice1 mark

What is the output of the following code?

print(7 + 3 % 3 // 4)
Answer

(b) 7 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.120Practice1 mark

What is the output of the following code?

print(8 - 3 % 5 * 2)
Answer

(a) 2 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.121Practice1 mark

What is the output of the following code?

print(16 / 5, 16 // 5, 16 % 5)
Answer

(d) 3.2 3 1 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.122Practice1 mark

What is the output of the following code?

print(-13 // 6, -13 % 6)
Answer

(d) -3 5 // rounds down towards minus infinity, so -13 // 6 is -3; the remainder takes the sign of the divisor.

A3.123Practice1 mark

What is the output of the following code?

print(0 or 9)
Answer

(a) 9 not is applied first, then and, then or. and/or return one of the operands.

A3.124Practice1 mark

What is the output of the following code?

for i in range(1, 11, 2):
    print(i, end=" ")
Answer

(a) 1 3 5 7 9 range(start, stop, step) starts at start and stops before stop.

A3.125Practice1 mark

What is the output of the following code?

for i in range(2, 12, 2):
    print(i, end=" ")
Answer

(c) 2 4 6 8 10 range(start, stop, step) starts at start and stops before stop.

A3.126Practice1 mark

What is the output of the following code?

n = 25
count = 0
while n > 6:
    n = n - 3
    count += 1
print(count, n)
Answer

(c) 7 4 Trace the loop: the condition is checked before every round.

A3.127Practice1 mark

What is the output of the following code?

for i in range(3, 15):
    if i % 6 == 0:
        break
    print(i, end=" ")
Answer

(d) 3 4 5 break leaves the loop; continue skips only the rest of that round.

A3.128Practice1 mark

What is the output of the following code?

x = 6
x -= 2
x **= 2
print(x)
Answer

(c) 16 In a, b = b, a + b the right side is worked out first, with the old values.

A3.129Practice1 mark

What is the output of the following code?

marks = 15
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(d) Fail Only the first true condition of an if-elif chain runs.

A3.130Practice1 mark

What is the output of the following code?

print(30 + 5 * 4 // 4)
Answer

(a) 35 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.131Practice1 mark

What is the output of the following code?

print(20 + 5 % 2 * 3)
Answer

(a) 23 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.132Practice1 mark

What is the output of the following code?

print(29 / 8, 29 // 8, 29 % 8)
Answer

(a) 3.625 3 5 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.133Practice1 mark

What is the output of the following code?

print(-25 // 4, -25 % 4)
Answer

(d) -7 3 // rounds down towards minus infinity, so -25 // 4 is -7; the remainder takes the sign of the divisor.

A3.134Practice1 mark

What is the output of the following code?

print(9 and 8)
Answer

(a) 8 not is applied first, then and, then or. and/or return one of the operands.

A3.135Practice1 mark

What is the output of the following code?

print(type(2 ** 3))
Answer

(d) <class 'int'> The type of the result decides the class.

A3.136Practice1 mark

What is the output of the following code?

for i in range(6, -6, -1):
    print(i, end=" ")
Answer

(d) 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 range(start, stop, step) starts at start and stops before stop.

A3.137Practice1 mark

What is the output of the following code?

for i in range(1, -6, -1):
    print(i, end=" ")
Answer

(a) 1 0 -1 -2 -3 -4 -5 range(start, stop, step) starts at start and stops before stop.

A3.138Practice1 mark

What is the output of the following code?

n = 52
count = 0
while n > 4:
    n = n - 9
    count += 1
print(count, n)
Answer

(b) 6 -2 Trace the loop: the condition is checked before every round.

A3.139Practice1 mark

What is the output of the following code?

for i in range(3, 12):
    if i % 2 == 0:
        continue
    print(i, end=" ")
Answer

(a) 3 5 7 9 11 break leaves the loop; continue skips only the rest of that round.

A3.140Practice1 mark

What is the output of the following code?

a = 7
b = 6
a += b
b = a - b
a = a - b
print(a, b)
Answer

(c) 6 7 In a, b = b, a + b the right side is worked out first, with the old values.

A3.141Practice1 mark

What is the output of the following code?

marks = 74
if marks >= 75:
    print("A")
elif marks >= 60:
    print("B")
elif marks >= 33:
    print("C")
else:
    print("Fail")
Answer

(c) B Only the first true condition of an if-elif chain runs.

A3.142Practice1 mark

What is the output of the following code?

print(8 + 6 % 3 % 3)
Answer

(c) 8 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.143Practice1 mark

What is the output of the following code?

print(13 + 5 % 5 ** 2)
Answer

(c) 18 Python applies ** first, then *, /, //, % from left to right, then + and -.

A3.144Practice1 mark

What is the output of the following code?

print(32 / 3, 32 // 3, 32 % 3)
Answer

(b) 10.666666666666666 10 2 / always gives a float; // gives the quotient rounded down; % gives the remainder.

A3.145Practice1 mark

What is the output of the following code?

for i in range(1, 13, 1):
    print(i, end=" ")
Answer

(c) 1 2 3 4 5 6 7 8 9 10 11 12 range(start, stop, step) starts at start and stops before stop.

A3.146Practice1 mark

What is the output of the following code?

for i in range(6, -2, -1):
    print(i, end=" ")
Answer

(a) 6 5 4 3 2 1 0 -1 range(start, stop, step) starts at start and stops before stop.

A3.147Practice1 mark

What is the output of the following code?

n = 36
count = 0
while n > 5:
    n = n - 5
    count += 1
print(count, n)
Answer

(b) 7 1 Trace the loop: the condition is checked before every round.

A3.148Practice1 mark

What is the output of the following code?

for i in range(2, 12):
    if i % 8 == 0:
        break
    print(i, end=" ")
Answer

(b) 2 3 4 5 6 7 break leaves the loop; continue skips only the rest of that round.

A3.149Practice1 mark

What is the output of the following code?

a, b = 9, 6
a, b = b, a + b
print(a, b)
Answer

(b) 6 15 In a, b = b, a + b the right side is worked out first, with the old values.

A3.150Practice1 mark

What is the output of the following code?

print(int(7.5), round(7.5))
Answer

(b) 7 8 round() rounds a .5 value to the nearest even number; int() cuts off the decimal part.

A4Output: strings, lists, tuples, dictionaries, modules1 mark · 190 questions
A4.1Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(3, 8), end=" ")
Answer

(c) 3 7 6 random.randrange(3, 8) gives a whole number from 3 to 7 (the stop value 8 is left out).

A4.2Practice1 mark

What is the output of the following code?

s = "INFORMATION"
print(s.isupper())
Answer

(c) True See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "INFORMATION"s = 'INFORMATION'
2print(s.isupper())True⏎
A4.3Practice1 mark

What is the output of the following code?

s = "NOTEBOOK"
print(s[:-4:-1])
Answer

(c) KOO A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.4Practice1 mark

What is the output of the following code?

s = "Delhi-88"
print(s.split("i"))
Answer

(c) ['Delh', '-88'] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Delhi-88"s = 'Delhi-88'
2print(s.split("i"))['Delh', '-88']⏎
A4.5Practice1 mark

What is the output of the following code?

s = "Vidyalaya"
print(s.count("O"))
Answer

(c) 0 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Vidyalaya"s = 'Vidyalaya'
2print(s.count("O"))0⏎
A4.6Practice1 mark

What is the output of the following code?

import statistics
data = [5, 3, 6, 3, 4, 3, 3]
print(statistics.median(data))
Answer

(c) 3 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.7Practice1 mark

What is the output of the following code?

L = [6, 7, 37, 29]
L.insert(0, 44)
print(L)
Answer

(a) [44, 6, 7, 37, 29] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [6, 7, 37, 29]L = [6, 7, 37, 29]
2L.insert(0, 44)L = [44, 6, 7, 37, 29]
3print(L)[44, 6, 7, 37, 29]⏎
A4.8Practice1 mark

What is the output of the following code?

L = [38, 33, 27, 32]
print(sum(L) // len(L))
Answer

(a) 32 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [38, 33, 27, 32]L = [38, 33, 27, 32]
2print(sum(L) // len(L))32⏎
A4.9Practice1 mark

What is the output of the following code?

s = "KV Shalimar Bagh"
print(s.count("o"))
Answer

(b) 0 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "KV Shalimar Bagh"s = 'KV Shalimar Bagh'
2print(s.count("o"))0⏎
A4.10Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(2, 5), end=" ")
Answer

(c) 5 2 4 random.randint(2, 5) gives a whole number from 2 to 5.

A4.11Practice1 mark

What is the output of the following code?

s = "Learning"
print(s[0:-5])
Answer

(d) Lea A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.12Practice1 mark

What is the output of the following code?

import math
print(math.gcd(18, 12))
Answer

(c) 6 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.gcd(18, 12))6⏎
A4.13Practice1 mark

What is the output of the following code?

import math
print(math.fabs(25))
Answer

(c) 25.0 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.fabs(25))25.0⏎
A4.14Practice1 mark

What is the output of the following code?

L = [18, 33, 28, 16, 25]
L.extend([57, 58])
print(L)
Answer

(b) [18, 33, 28, 16, 25, 57, 58] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [18, 33, 28, 16, 25]L = [18, 33, 28, 16, 25]
2L.extend([57, 58])L = [18, 33, 28, 16, 25, 57, 58]
3print(L)[18, 33, 28, 16, 25, 57, 58]⏎
A4.15Practice1 mark

What is the output of the following code?

T = (25, 28, 1, 22, 5)
print(T[1:4])
Answer

(d) (28, 1, 22) A tuple is ordered like a list but cannot be changed.

A4.16Practice1 mark

What is the output of the following code?

s = "COMPUTER"
print(s[-8:-4])
Answer

(c) COMP A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.17Practice1 mark

What is the output of the following code?

s = "NOTEBOOK"
print(s.replace("n", "#"))
Answer

(b) NOTEBOOK See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "NOTEBOOK"s = 'NOTEBOOK'
2print(s.replace("n", "#"))NOTEBOOK⏎
A4.18Practice1 mark

What is the output of the following code?

L = [15, 8, 22, 5]
L.sort()
print(L)
Answer

(a) [5, 8, 15, 22] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [15, 8, 22, 5]L = [15, 8, 22, 5]
2L.sort()L = [5, 8, 15, 22]
3print(L)[5, 8, 15, 22]⏎
A4.19Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(3, 5), end=" ")
Answer

(b) 3 3 4 random.randint(3, 5) gives a whole number from 3 to 5.

A4.20Practice1 mark

What is the output of the following code?

L = [12, 20, 3, 10]
print(max(L) - min(L))
Answer

(b) 17 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [12, 20, 3, 10]L = [12, 20, 3, 10]
2print(max(L) - min(L))17⏎
A4.21Practice1 mark

What is the output of the following code?

L = [5, 32, 16, 30]
L[0] = 53
print(L)
Answer

(a) [53, 32, 16, 30] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [5, 32, 16, 30]L = [5, 32, 16, 30]
2L[0] = 53L = [53, 32, 16, 30]
3print(L)[53, 32, 16, 30]⏎
A4.22Practice1 mark

What is the output of the following code?

L = [4, 33, 11, 24, 25]
print(L[::-2])
Answer

(c) [25, 11, 4] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [4, 33, 11, 24, 25]L = [4, 33, 11, 24, 25]
2print(L[::-2])[25, 11, 4]⏎
A4.23Practice1 mark

What is the output of the following code?

import math
print(math.floor(-7.8))
Answer

(c) -8 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.floor(-7.8))-8⏎
A4.24Practice1 mark

What is the output of the following code?

L = [25, 36, 6, 2, 27, 31]
L.sort(reverse=True)
print(L)
Answer

(c) [36, 31, 27, 25, 6, 2] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [25, 36, 6, 2, 27, 31]L = [25, 36, 6, 2, 27, 31]
2L.sort(reverse=True)L = [36, 31, 27, 25, 6, 2]
3print(L)[36, 31, 27, 25, 6, 2]⏎
A4.25Practice1 mark

What is the output of the following code?

s = "Python"
print(s.find("n"))
Answer

(b) 5 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Python"s = 'Python'
2print(s.find("n"))5⏎
A4.26Practice1 mark

What is the output of the following code?

D = {'Esha': 97, 'Bela': 43, 'Dev': 54, 'Gita': 45}
D["Zoya"] = 99
print(len(D))
Answer

(c) 5 A dictionary keeps key: value pairs; keys are unique.

A4.27Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(1, 5), end=" ")
Answer

(c) 4 2 2 random.randrange(1, 5) gives a whole number from 1 to 4 (the stop value 5 is left out).

A4.28Practice1 mark

What is the output of the following code?

D = {'Dev': 78, 'Farid': 86, 'Amit': 74, 'Bela': 50}
D["Dev"] = 99
print(D)
Answer

(a) {'Dev': 99, 'Farid': 86, 'Amit': 74, 'Bela': 50} A dictionary keeps key: value pairs; keys are unique.

A4.29Practice1 mark

What is the output of the following code?

import statistics
data = [2, 8, 1, 8, 8, 2, 8]
print(statistics.median(data))
Answer

(b) 8 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.30Practice1 mark

What is the output of the following code?

s = "keyboard"
print(s[-7:-3:1])
Answer

(c) eybo A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.31Practice1 mark

What is the output of the following code?

T = (6, 26, 3, 18, 4, 16)
t = (6,)
print(type(t))
Answer

(d) <class 'tuple'> A tuple is ordered like a list but cannot be changed.

A4.32Practice1 mark

What is the output of the following code?

import statistics
data = [1, 8, 7, 8, 9, 4, 1]
print(statistics.median(data))
Answer

(d) 7 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.33Practice1 mark

What is the output of the following code?

import statistics
data = [1, 8, 2, 8, 5, 3, 2]
print(statistics.median(data))
Answer

(c) 3 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.34Practice1 mark

What is the output of the following code?

D = {'Esha': 51, 'Dev': 54, 'Amit': 56, 'Charu': 64}
print(list(D.values())[1])
Answer

(a) 54 A dictionary keeps key: value pairs; keys are unique.

A4.35Practice1 mark

What is the output of the following code?

s = "KENDRIYA"
print(s.lstrip("K"))
Answer

(a) ENDRIYA See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "KENDRIYA"s = 'KENDRIYA'
2print(s.lstrip("K"))ENDRIYA⏎
A4.36Practice1 mark

What is the output of the following code?

T = (25, 18, 22, 26)
print(25 in T)
Answer

(d) True A tuple is ordered like a list but cannot be changed.

A4.37Practice1 mark

What is the output of the following code?

T = (27, 20, 23, 3)
print(len(T + (5,)))
Answer

(a) 5 A tuple is ordered like a list but cannot be changed.

A4.38Practice1 mark

What is the output of the following code?

import math
print(math.pow(2, 3))
Answer

(a) 8.0 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.pow(2, 3))8.0⏎
A4.39Practice1 mark

What is the output of the following code?

import math
print(math.sqrt(16))
Answer

(b) 4.0 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.sqrt(16))4.0⏎
A4.40Practice1 mark

What is the output of the following code?

import math
print(math.ceil(2.5), math.floor(2.5))
Answer

(b) 3 2 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.ceil(2.5), math.floor(2.5))3 2⏎
A4.41Practice1 mark

What is the output of the following code?

s = "Delhi-88"
print(s[6:-5:-2])
Answer

(d) 8i A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.42Practice1 mark

What is the output of the following code?

T = (16, 19, 28, 27, 23)
print(T.count(19))
Answer

(d) 1 A tuple is ordered like a list but cannot be changed.

A4.43Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(1, 5), end=" ")
Answer

(b) 4 5 3 random.randint(1, 5) gives a whole number from 1 to 5.

A4.44Practice1 mark

What is the output of the following code?

T = (21, 12, 25, 5)
print(T.index(25))
Answer

(d) 2 A tuple is ordered like a list but cannot be changed.

A4.45Practice1 mark

What is the output of the following code?

import statistics
data = [3, 1, 1, 1, 2, 2, 8]
print(statistics.mean(data))
Answer

(d) 2.5714285714285716 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.46Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(4, 7), end=" ")
Answer

(b) 4 4 4 random.randrange(4, 7) gives a whole number from 4 to 6 (the stop value 7 is left out).

A4.47Practice1 mark

What is the output of the following code?

L = [22, 20, 9, 35, 7, 34]
print(L.pop(), L)
Answer

(d) 34 [22, 20, 9, 35, 7] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [22, 20, 9, 35, 7, 34]L = [22, 20, 9, 35, 7, 34]
2print(L.pop(), L)L = [22, 20, 9, 35, 7]34 [22, 20, 9, 35, 7]⏎
A4.48Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(4, 5), end=" ")
Answer

(b) 4 4 4 random.randrange(4, 5) gives a whole number from 4 to 4 (the stop value 5 is left out).

A4.49Practice1 mark

What is the output of the following code?

L = [8, 19, 20, 23, 16, 10]
print(len(L + [51]))
Answer

(c) 7 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [8, 19, 20, 23, 16, 10]L = [8, 19, 20, 23, 16, 10]
2print(len(L + [51]))7⏎
A4.50Practice1 mark

What is the output of the following code?

D = {'Charu': 78, 'Amit': 56, 'Gita': 45, 'Dev': 48}
print(D.get("Zoya"))
Answer

(a) None A dictionary keeps key: value pairs; keys are unique.

A4.51Practice1 mark

What is the output of the following code?

L = [19, 33, 5, 3, 15]
print(max(L) - min(L))
Answer

(a) 30 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [19, 33, 5, 3, 15]L = [19, 33, 5, 3, 15]
2print(max(L) - min(L))30⏎
A4.52Practice1 mark

What is the output of the following code?

D = {'Dev': 65, 'Gita': 50, 'Bela': 41, 'Esha': 77}
D["Zoya"] = 99
print(len(D))
Answer

(b) 5 A dictionary keeps key: value pairs; keys are unique.

A4.53Practice1 mark

What is the output of the following code?

L = [24, 16, 22, 5, 21]
L.insert(0, 53)
print(L)
Answer

(b) [53, 24, 16, 22, 5, 21] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [24, 16, 22, 5, 21]L = [24, 16, 22, 5, 21]
2L.insert(0, 53)L = [53, 24, 16, 22, 5, 21]
3print(L)[53, 24, 16, 22, 5, 21]⏎
A4.54Practice1 mark

What is the output of the following code?

import math
print(math.fabs(-2.5))
Answer

(a) 2.5 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.fabs(-2.5))2.5⏎
A4.55Practice1 mark

What is the output of the following code?

s = "Python"
print(s[:-2:1])
Answer

(b) Pyth A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.56Practice1 mark

What is the output of the following code?

import math
print(int(math.sqrt(15)) ** 2)
Answer

(a) 9 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(int(math.sqrt(15)) ** 2)9⏎
A4.57Practice1 mark

What is the output of the following code?

D = {'Esha': 66, 'Amit': 76, 'Dev': 61, 'Gita': 75}
print(D.pop("Esha"), len(D))
Answer

(b) 66 3 A dictionary keeps key: value pairs; keys are unique.

A4.58Practice1 mark

What is the output of the following code?

L = [32, 17, 25, 23]
print(sorted(L)[1])
Answer

(b) 23 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [32, 17, 25, 23]L = [32, 17, 25, 23]
2print(sorted(L)[1])23⏎
A4.59Practice1 mark

What is the output of the following code?

s = "Program"
print(s.swapcase())
Answer

(b) pROGRAM See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Program"s = 'Program'
2print(s.swapcase())pROGRAM⏎
A4.60Practice1 mark

What is the output of the following code?

import statistics
data = [7, 5, 2, 5, 2]
print(statistics.mode(data))
Answer

(b) 5 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.61Practice1 mark

What is the output of the following code?

import statistics
data = [7, 8, 9, 8, 4]
print(statistics.median(data))
Answer

(d) 8 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.62Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(1, 8), end=" ")
Answer

(b) 2 3 1 random.randrange(1, 8) gives a whole number from 1 to 7 (the stop value 8 is left out).

A4.63Practice1 mark

What is the output of the following code?

L = [32, 23, 15, 14]
print(sorted(L)[1])
Answer

(a) 15 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [32, 23, 15, 14]L = [32, 23, 15, 14]
2print(sorted(L)[1])15⏎
A4.64Practice1 mark

What is the output of the following code?

D = {'Charu': 84, 'Bela': 96, 'Esha': 88, 'Dev': 92}
print(D.get("Zoya"))
Answer

(d) None A dictionary keeps key: value pairs; keys are unique.

A4.65Practice1 mark

What is the output of the following code?

T = (5, 17, 2, 10)
print(T.count(17))
Answer

(b) 1 A tuple is ordered like a list but cannot be changed.

A4.66Practice1 mark

What is the output of the following code?

import math
print(abs(7.8))
Answer

(b) 7.8 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(abs(7.8))7.8⏎
A4.67Practice1 mark

What is the output of the following code?

D = {'Bela': 74, 'Dev': 77, 'Charu': 94, 'Esha': 84}
print(D.pop("Esha"), len(D))
Answer

(d) 84 3 A dictionary keeps key: value pairs; keys are unique.

A4.68Practice1 mark

What is the output of the following code?

s = "Learning"
print(s[0:2:1])
Answer

(b) Le A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.69Practice1 mark

What is the output of the following code?

L = [25, 32, 27, 22]
L.append(46)
print(L)
Answer

(b) [25, 32, 27, 22, 46] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [25, 32, 27, 22]L = [25, 32, 27, 22]
2L.append(46)L = [25, 32, 27, 22, 46]
3print(L)[25, 32, 27, 22, 46]⏎
A4.70Practice1 mark

What is the output of the following code?

s = "India is Great"
print(s.split())
Answer

(a) ['India', 'is', 'Great'] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "India is Great"s = 'India is Great'
2print(s.split())['India', 'is', 'Great']⏎
A4.71Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(4, 5), end=" ")
Answer

(a) 5 5 4 random.randint(4, 5) gives a whole number from 4 to 5.

A4.72Practice1 mark

What is the output of the following code?

D = {'Esha': 94, 'Charu': 80, 'Dev': 73, 'Amit': 48}
print(list(D.values())[1])
Answer

(d) 80 A dictionary keeps key: value pairs; keys are unique.

A4.73Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(4, 8), end=" ")
Answer

(b) 5 5 7 random.randint(4, 8) gives a whole number from 4 to 8.

A4.74Practice1 mark

What is the output of the following code?

s = "Monitor"
print(s[-1::2])
Answer

(a) r A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.75Practice1 mark

What is the output of the following code?

import statistics
data = [9, 1, 7, 1, 5, 7, 7]
print(statistics.mean(data))
Answer

(a) 5.285714285714286 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.76Practice1 mark

What is the output of the following code?

s = "Save Water Save Life"
print("-".join(s.split()))
Answer

(a) Save-Water-Save-Life See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Save Water Save Life"s = 'Save Water Save Life'
2print("-".join(s.split()))Save-Water-Save-Life⏎
A4.77Practice1 mark

What is the output of the following code?

import math
print(math.floor(-25))
Answer

(d) -25 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.floor(-25))-25⏎
A4.78Practice1 mark

What is the output of the following code?

s = "Algorithm"
print(len(s))
Answer

(d) 9 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Algorithm"s = 'Algorithm'
2print(len(s))9⏎
A4.79Practice1 mark

What is the output of the following code?

s = "INFORMATION"
print(s.lstrip("I"))
Answer

(c) NFORMATION See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "INFORMATION"s = 'INFORMATION'
2print(s.lstrip("I"))NFORMATION⏎
A4.80Practice1 mark

What is the output of the following code?

s = "CBSE Exam 2027"
print(s.upper())
Answer

(a) CBSE EXAM 2027 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "CBSE Exam 2027"s = 'CBSE Exam 2027'
2print(s.upper())CBSE EXAM 2027⏎
A4.81Practice1 mark

What is the output of the following code?

import statistics
data = [6, 2, 8, 2, 9, 1, 7]
print(statistics.mode(data))
Answer

(b) 2 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.82Practice1 mark

What is the output of the following code?

import statistics
data = [3, 9, 6, 9, 9]
print(statistics.mean(data))
Answer

(a) 7.2 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.83Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(2, 8), end=" ")
Answer

(c) 3 8 4 random.randint(2, 8) gives a whole number from 2 to 8.

A4.84Practice1 mark

What is the output of the following code?

import statistics
data = [5, 5, 7, 5, 6]
print(statistics.mode(data))
Answer

(d) 5 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.85Practice1 mark

What is the output of the following code?

s = "Vidyalaya"
print(s[:-6:-1])
Answer

(a) ayala A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.86Practice1 mark

What is the output of the following code?

L = [35, 19, 8, 24]
L.insert(2, 51)
print(L)
Answer

(a) [35, 19, 51, 8, 24] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [35, 19, 8, 24]L = [35, 19, 8, 24]
2L.insert(2, 51)L = [35, 19, 51, 8, 24]
3print(L)[35, 19, 51, 8, 24]⏎
A4.87Practice1 mark

What is the output of the following code?

s = "Algorithm"
print(s.count("I"))
Answer

(d) 0 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Algorithm"s = 'Algorithm'
2print(s.count("I"))0⏎
A4.88Practice1 mark

What is the output of the following code?

L = [32, 11, 15, 1, 29]
print(11 in L, 45 not in L)
Answer

(d) True True See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [32, 11, 15, 1, 29]L = [32, 11, 15, 1, 29]
2print(11 in L, 45 not in L)True True⏎
A4.89Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(3, 6), end=" ")
Answer

(b) 6 5 4 random.randint(3, 6) gives a whole number from 3 to 6.

A4.90Practice1 mark

What is the output of the following code?

D = {'Esha': 55, 'Gita': 96, 'Bela': 51, 'Farid': 56}
D["Bela"] = 99
print(D)
Answer

(d) {'Esha': 55, 'Gita': 96, 'Bela': 99, 'Farid': 56} A dictionary keeps key: value pairs; keys are unique.

A4.91Practice1 mark

What is the output of the following code?

s = "Program"
print(s[-5:4])
Answer

(d) og A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.92Practice1 mark

What is the output of the following code?

s = "Vidyalaya"
print(s[4:])
Answer

(a) alaya A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.93Practice1 mark

What is the output of the following code?

D = {'Charu': 89, 'Esha': 65, 'Farid': 94, 'Dev': 71}
print(len(D))
Answer

(c) 4 A dictionary keeps key: value pairs; keys are unique.

A4.94Practice1 mark

What is the output of the following code?

D = {'Bela': 59, 'Dev': 68, 'Farid': 81, 'Charu': 60}
print(D.get("Zoya", 0))
Answer

(a) 0 A dictionary keeps key: value pairs; keys are unique.

A4.95Practice1 mark

What is the output of the following code?

s = "Save Water Save Life"
print(s.isupper())
Answer

(b) False See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Save Water Save Life"s = 'Save Water Save Life'
2print(s.isupper())False⏎
A4.96Practice1 mark

What is the output of the following code?

s = "Science"
print(s.title())
Answer

(d) Science See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Science"s = 'Science'
2print(s.title())Science⏎
A4.97Practice1 mark

What is the output of the following code?

L = [23, 3, 17, 28, 9]
L.insert(4, 60)
print(L)
Answer

(c) [23, 3, 17, 28, 60, 9] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [23, 3, 17, 28, 9]L = [23, 3, 17, 28, 9]
2L.insert(4, 60)L = [23, 3, 17, 28, 60, 9]
3print(L)[23, 3, 17, 28, 60, 9]⏎
A4.98Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(1, 7), end=" ")
Answer

(b) 2 6 5 random.randint(1, 7) gives a whole number from 1 to 7.

A4.99Practice1 mark

What is the output of the following code?

L = [35, 27, 31, 2]
print(L[1:4])
Answer

(a) [27, 31, 2] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [35, 27, 31, 2]L = [35, 27, 31, 2]
2print(L[1:4])[27, 31, 2]⏎
A4.100Practice1 mark

What is the output of the following code?

D = {'Esha': 72, 'Bela': 80, 'Amit': 85, 'Farid': 74}
print(list(D.values())[1])
Answer

(a) 80 A dictionary keeps key: value pairs; keys are unique.

A4.101Practice1 mark

What is the output of the following code?

L = [12, 1, 26, 6]
print(L.pop(1), L)
Answer

(d) 1 [12, 26, 6] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [12, 1, 26, 6]L = [12, 1, 26, 6]
2print(L.pop(1), L)L = [12, 26, 6]1 [12, 26, 6]⏎
A4.102Practice1 mark

What is the output of the following code?

s = "Network"
print("-".join(s.split()))
Answer

(b) Network See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Network"s = 'Network'
2print("-".join(s.split()))Network⏎
A4.103Practice1 mark

What is the output of the following code?

D = {'Gita': 46, 'Charu': 76, 'Bela': 52, 'Dev': 74}
print(sorted(D))
Answer

(b) ['Bela', 'Charu', 'Dev', 'Gita'] A dictionary keeps key: value pairs; keys are unique.

A4.104Practice1 mark

What is the output of the following code?

s = "keyboard"
print(s[2:7])
Answer

(d) yboar A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.105Practice1 mark

What is the output of the following code?

import math
print(int(math.sqrt(30)) ** 2)
Answer

(b) 25 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(int(math.sqrt(30)) ** 2)25⏎
A4.106Practice1 mark

What is the output of the following code?

D = {'Dev': 53, 'Charu': 90, 'Amit': 72, 'Farid': 97}
print(max(D.values()))
Answer

(a) 97 A dictionary keeps key: value pairs; keys are unique.

A4.107Practice1 mark

What is the output of the following code?

D = {'Dev': 40, 'Charu': 70, 'Amit': 67, 'Farid': 60}
del D["Charu"]
print(D)
Answer

(a) {'Dev': 40, 'Amit': 67, 'Farid': 60} A dictionary keeps key: value pairs; keys are unique.

A4.108Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randint(4, 7), end=" ")
Answer

(a) 4 6 7 random.randint(4, 7) gives a whole number from 4 to 7.

A4.109Practice1 mark

What is the output of the following code?

s = "Hello World 2026"
print(s.title())
Answer

(c) Hello World 2026 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Hello World 2026"s = 'Hello World 2026'
2print(s.title())Hello World 2026⏎
A4.110Practice1 mark

What is the output of the following code?

T = (11, 9, 24, 10, 27, 5)
print(max(T), min(T))
Answer

(b) 27 5 A tuple is ordered like a list but cannot be changed.

A4.111Practice1 mark

What is the output of the following code?

s = "Data and Logic"
print(s.count("t"))
Answer

(c) 1 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Data and Logic"s = 'Data and Logic'
2print(s.count("t"))1⏎
A4.112Practice1 mark

What is the output of the following code?

D = {'Farid': 83, 'Charu': 44, 'Esha': 94, 'Gita': 82}
print(D.get("Zoya", 0))
Answer

(a) 0 A dictionary keeps key: value pairs; keys are unique.

A4.113Practice1 mark

What is the output of the following code?

L = [24, 17, 10, 11, 19]
print(L[1:2])
Answer

(b) [17] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [24, 17, 10, 11, 19]L = [24, 17, 10, 11, 19]
2print(L[1:2])[17]⏎
A4.114Practice1 mark

What is the output of the following code?

D = {'Amit': 87, 'Bela': 49, 'Dev': 66, 'Gita': 63}
print(D["Amit"])
Answer

(d) 87 A dictionary keeps key: value pairs; keys are unique.

A4.115Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(2, 8), end=" ")
Answer

(d) 2 2 7 random.randrange(2, 8) gives a whole number from 2 to 7 (the stop value 8 is left out).

A4.116Practice1 mark

What is the output of the following code?

T = (11, 1, 9, 8)
t = (11)
print(type(t))
Answer

(b) <class 'int'> A tuple is ordered like a list but cannot be changed.

A4.117Practice1 mark

What is the output of the following code?

s = "CBSE Exam 2027"
print(s.find("BS"))
Answer

(c) 1 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "CBSE Exam 2027"s = 'CBSE Exam 2027'
2print(s.find("BS"))1⏎
A4.118Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(2, 5), end=" ")
Answer

(d) 2 2 2 random.randrange(2, 5) gives a whole number from 2 to 4 (the stop value 5 is left out).

A4.119Practice1 mark

What is the output of the following code?

L = [26, 1, 33, 6]
print(sum(L) // len(L))
Answer

(c) 16 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [26, 1, 33, 6]L = [26, 1, 33, 6]
2print(sum(L) // len(L))16⏎
A4.120Practice1 mark

What is the output of the following code?

import statistics
data = [9, 2, 9, 2, 1]
print(statistics.mean(data))
Answer

(b) 4.6 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.121Practice1 mark

What is the output of the following code?

D = {'Gita': 47, 'Esha': 74, 'Bela': 94, 'Amit': 88}
print(D.pop("Bela"), len(D))
Answer

(c) 94 3 A dictionary keeps key: value pairs; keys are unique.

A4.122Practice1 mark

What is the output of the following code?

s = "Vidyalaya"
print(s.split())
Answer

(d) ['Vidyalaya'] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Vidyalaya"s = 'Vidyalaya'
2print(s.split())['Vidyalaya']⏎
A4.123Practice1 mark

What is the output of the following code?

s = "Keep Calm and Code"
print(s.startswith("Ke"))
Answer

(b) True See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Keep Calm and Code"s = 'Keep Calm and Code'
2print(s.startswith("Ke"))True⏎
A4.124Practice1 mark

What is the output of the following code?

import statistics
data = [7, 6, 4, 6, 1, 8, 1]
print(statistics.median(data))
Answer

(c) 6 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.125Practice1 mark

What is the output of the following code?

T = (18, 13, 8, 26, 12)
print(T.index(8))
Answer

(d) 2 A tuple is ordered like a list but cannot be changed.

A4.126Practice1 mark

What is the output of the following code?

D = {'Esha': 60, 'Bela': 69, 'Charu': 94, 'Amit': 42}
print(list(D.items())[0])
Answer

(b) ('Esha', 60) A dictionary keeps key: value pairs; keys are unique.

A4.127Practice1 mark

What is the output of the following code?

import statistics
data = [4, 5, 4, 5, 6, 4, 8]
print(statistics.median(data))
Answer

(c) 5 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.128Practice1 mark

What is the output of the following code?

s = "Python"
print(s.replace("A", "#"))
Answer

(d) Python See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Python"s = 'Python'
2print(s.replace("A", "#"))Python⏎
A4.129Practice1 mark

What is the output of the following code?

D = {'Charu': 80, 'Farid': 49, 'Bela': 62, 'Dev': 68}
print(max(D))
Answer

(c) Farid A dictionary keeps key: value pairs; keys are unique.

A4.130Practice1 mark

What is the output of the following code?

s = "DATABASE"
print(s[1:-3:2])
Answer

(c) AA A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.131Practice1 mark

What is the output of the following code?

D = {'Esha': 48, 'Charu': 52, 'Gita': 76, 'Dev': 43}
print(D.get("Zoya"))
Answer

(d) None A dictionary keeps key: value pairs; keys are unique.

A4.132Practice1 mark

What is the output of the following code?

T = (23, 18, 5, 10, 21, 3)
print(T.index(5))
Answer

(b) 2 A tuple is ordered like a list but cannot be changed.

A4.133Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(3, 5), end=" ")
Answer

(a) 3 4 4 random.randrange(3, 5) gives a whole number from 3 to 4 (the stop value 5 is left out).

A4.134Practice1 mark

What is the output of the following code?

s = "COMPUTER"
print(s.capitalize())
Answer

(a) Computer See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "COMPUTER"s = 'COMPUTER'
2print(s.capitalize())Computer⏎
A4.135Practice1 mark

What is the output of the following code?

D = {'Esha': 83, 'Farid': 44, 'Bela': 86, 'Amit': 55}
D["Farid"] = 99
print(D)
Answer

(b) {'Esha': 83, 'Farid': 99, 'Bela': 86, 'Amit': 55} A dictionary keeps key: value pairs; keys are unique.

A4.136Practice1 mark

What is the output of the following code?

import math
print(math.factorial(4))
Answer

(b) 24 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.factorial(4))24⏎
A4.137Practice1 mark

What is the output of the following code?

import math
print(math.ceil(-2.5), math.floor(-2.5))
Answer

(c) -2 -3 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.ceil(-2.5), math.floor(-2.5))-2 -3⏎
A4.138Practice1 mark

What is the output of the following code?

L = [14, 5, 11, 2, 25]
print(14 in L, 48 not in L)
Answer

(c) True True See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [14, 5, 11, 2, 25]L = [14, 5, 11, 2, 25]
2print(14 in L, 48 not in L)True True⏎
A4.139Practice1 mark

What is the output of the following code?

L = [1, 15, 11, 17, 24, 5]
L.reverse()
print(L)
Answer

(d) [5, 24, 17, 11, 15, 1] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [1, 15, 11, 17, 24, 5]L = [1, 15, 11, 17, 24, 5]
2L.reverse()L = [5, 24, 17, 11, 15, 1]
3print(L)[5, 24, 17, 11, 15, 1]⏎
A4.140Practice1 mark

What is the output of the following code?

T = (15, 24, 10, 3, 13)
print(max(T), min(T))
Answer

(c) 24 3 A tuple is ordered like a list but cannot be changed.

A4.141Practice1 mark

What is the output of the following code?

L = [6, 36, 38, 31, 33, 25]
L.append(47)
print(L)
Answer

(a) [6, 36, 38, 31, 33, 25, 47] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [6, 36, 38, 31, 33, 25]L = [6, 36, 38, 31, 33, 25]
2L.append(47)L = [6, 36, 38, 31, 33, 25, 47]
3print(L)[6, 36, 38, 31, 33, 25, 47]⏎
A4.142Practice1 mark

What is the output of the following code?

s = "NOTEBOOK"
print(s.split())
Answer

(a) ['NOTEBOOK'] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "NOTEBOOK"s = 'NOTEBOOK'
2print(s.split())['NOTEBOOK']⏎
A4.143Practice1 mark

What is the output of the following code?

s = "Data and Logic"
print(s.replace("O", "#"))
Answer

(b) Data and Logic See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Data and Logic"s = 'Data and Logic'
2print(s.replace("O", "#"))Data and Logic⏎
A4.144Practice1 mark

What is the output of the following code?

T = (14, 1, 26, 3, 28)
print(T.index(26))
Answer

(c) 2 A tuple is ordered like a list but cannot be changed.

A4.145Practice1 mark

What is the output of the following code?

s = "Science"
print(s[-4:7])
Answer

(c) ence A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.146Practice1 mark

What is the output of the following code?

L = [18, 24, 33, 19, 7, 20]
print(7 in L, 45 not in L)
Answer

(a) True True See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [18, 24, 33, 19, 7, 20]L = [18, 24, 33, 19, 7, 20]
2print(7 in L, 45 not in L)True True⏎
A4.147Practice1 mark

What is the output of the following code?

s = "Keep Calm and Code"
print(len(s))
Answer

(d) 18 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Keep Calm and Code"s = 'Keep Calm and Code'
2print(len(s))18⏎
A4.148Practice1 mark

What is the output of the following code?

D = {'Esha': 78, 'Dev': 53, 'Bela': 52, 'Gita': 70}
print(D.pop("Bela"), len(D))
Answer

(c) 52 3 A dictionary keeps key: value pairs; keys are unique.

A4.149Practice1 mark

What is the output of the following code?

T = (26, 11, 10, 27, 19, 5)
print(sum(T))
Answer

(b) 98 A tuple is ordered like a list but cannot be changed.

A4.150Practice1 mark

What is the output of the following code?

s = "Keep Calm and Code"
print(s.count("n"))
Answer

(c) 1 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Keep Calm and Code"s = 'Keep Calm and Code'
2print(s.count("n"))1⏎
A4.151Practice1 mark

What is the output of the following code?

T = (5, 24, 15, 6)
print(T.index(15))
Answer

(c) 2 A tuple is ordered like a list but cannot be changed.

A4.152Practice1 mark

What is the output of the following code?

D = {'Farid': 63, 'Amit': 66, 'Dev': 48, 'Gita': 90}
print(sorted(D))
Answer

(b) ['Amit', 'Dev', 'Farid', 'Gita'] A dictionary keeps key: value pairs; keys are unique.

A4.153Practice1 mark

What is the output of the following code?

s = "Keep Calm and Code"
print(s.split("O"))
Answer

(d) ['Keep Calm and Code'] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Keep Calm and Code"s = 'Keep Calm and Code'
2print(s.split("O"))['Keep Calm and Code']⏎
A4.154Practice1 mark

What is the output of the following code?

s = "Monitor"
print(s.capitalize())
Answer

(a) Monitor See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Monitor"s = 'Monitor'
2print(s.capitalize())Monitor⏎
A4.155Practice1 mark

What is the output of the following code?

L = [38, 8, 10, 15, 39, 31]
L.append([60, 61])
print(L)
Answer

(d) [38, 8, 10, 15, 39, 31, [60, 61]] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [38, 8, 10, 15, 39, 31]L = [38, 8, 10, 15, 39, 31]
2L.append([60, 61])L = [38, 8, 10, 15, 39, 31, [60, 61]]
3print(L)[38, 8, 10, 15, 39, 31, [60, 61]]⏎
A4.156Practice1 mark

What is the output of the following code?

s = "KV Shalimar Bagh"
print(s.title())
Answer

(a) Kv Shalimar Bagh See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "KV Shalimar Bagh"s = 'KV Shalimar Bagh'
2print(s.title())Kv Shalimar Bagh⏎
A4.157Practice1 mark

What is the output of the following code?

L = [8, 32, 2, 4, 16]
print(L.pop(3), L)
Answer

(c) 4 [8, 32, 2, 16] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [8, 32, 2, 4, 16]L = [8, 32, 2, 4, 16]
2print(L.pop(3), L)L = [8, 32, 2, 16]4 [8, 32, 2, 16]⏎
A4.158Practice1 mark

What is the output of the following code?

L = [8, 9, 36, 6]
L[0] = 44
print(L)
Answer

(a) [44, 9, 36, 6] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [8, 9, 36, 6]L = [8, 9, 36, 6]
2L[0] = 44L = [44, 9, 36, 6]
3print(L)[44, 9, 36, 6]⏎
A4.159Practice1 mark

What is the output of the following code?

L = [30, 39, 5, 33, 8]
print(len(L + [53]))
Answer

(d) 6 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [30, 39, 5, 33, 8]L = [30, 39, 5, 33, 8]
2print(len(L + [53]))6⏎
A4.160Practice1 mark

What is the output of the following code?

import statistics
data = [1, 3, 2, 3, 2, 3, 4]
print(statistics.median(data))
Answer

(d) 3 mean is the average, median is the middle value after sorting, mode is the most common value.

A4.161Practice1 mark

What is the output of the following code?

s = "Python"
print(s.swapcase())
Answer

(a) pYTHON See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Python"s = 'Python'
2print(s.swapcase())pYTHON⏎
A4.162Practice1 mark

What is the output of the following code?

D = {'Esha': 76, 'Amit': 77, 'Bela': 71, 'Dev': 78}
print(sorted(D))
Answer

(d) ['Amit', 'Bela', 'Dev', 'Esha'] A dictionary keeps key: value pairs; keys are unique.

A4.163Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(1, 7), end=" ")
Answer

(c) 1 3 2 random.randrange(1, 7) gives a whole number from 1 to 6 (the stop value 7 is left out).

A4.164Practice1 mark

What is the output of the following code?

D = {'Esha': 41, 'Charu': 62, 'Bela': 79, 'Gita': 53}
print("Esha" in D, 41 in D)
Answer

(d) True False A dictionary keeps key: value pairs; keys are unique.

A4.165Practice1 mark

What is the output of the following code?

T = (17, 4, 18, 28, 27, 12)
print(17 in T)
Answer

(c) True A tuple is ordered like a list but cannot be changed.

A4.166Practice1 mark

What is the output of the following code?

s = "Science"
print(s[:-5:1])
Answer

(b) Sc A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.167Practice1 mark

What is the output of the following code?

T = (17, 19, 26, 27, 23)
print(len(T + (5,)))
Answer

(b) 6 A tuple is ordered like a list but cannot be changed.

A4.168Practice1 mark

What is the output of the following code?

L = [30, 14, 36, 33, 13, 1]
L.remove(14)
print(L)
Answer

(a) [30, 36, 33, 13, 1] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [30, 14, 36, 33, 13, 1]L = [30, 14, 36, 33, 13, 1]
2L.remove(14)L = [30, 36, 33, 13, 1]
3print(L)[30, 36, 33, 13, 1]⏎
A4.169Practice1 mark

What is the output of the following code?

s = "Vidyalaya"
print(s.lstrip("V"))
Answer

(a) idyalaya See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Vidyalaya"s = 'Vidyalaya'
2print(s.lstrip("V"))idyalaya⏎
A4.170Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(4, 8), end=" ")
Answer

(a) 5 5 4 random.randrange(4, 8) gives a whole number from 4 to 7 (the stop value 8 is left out).

A4.171Practice1 mark

What is the output of the following code?

D = {'Dev': 88, 'Bela': 82, 'Esha': 44, 'Gita': 81}
print(D["Gita"])
Answer

(d) 81 A dictionary keeps key: value pairs; keys are unique.

A4.172Practice1 mark

What is the output of the following code?

D = {'Esha': 77, 'Charu': 47, 'Amit': 84, 'Bela': 70}
print(sorted(D))
Answer

(b) ['Amit', 'Bela', 'Charu', 'Esha'] A dictionary keeps key: value pairs; keys are unique.

A4.173Practice1 mark

What is the output of the following code?

s = "Python"
print(s.upper())
Answer

(c) PYTHON See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Python"s = 'Python'
2print(s.upper())PYTHON⏎
A4.174Practice1 mark

What is the output of the following code?

import math
print(math.factorial(5))
Answer

(b) 120 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.factorial(5))120⏎
A4.175Practice1 mark

What is the output of the following code?

s = "COMPUTER"
print(s[6:4:-2])
Answer

(d) E A slice s[start:stop:step] stops before stop; a negative index counts from the end.

A4.176Practice1 mark

What is the output of the following code?

D = {'Bela': 70, 'Gita': 64, 'Farid': 42, 'Charu': 67}
print(D.get("Zoya", 0))
Answer

(c) 0 A dictionary keeps key: value pairs; keys are unique.

A4.177Practice1 mark

What is the output of the following code?

import math
print(math.gcd(42, 20))
Answer

(d) 2 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
2print(math.gcd(42, 20))2⏎
A4.178Practice1 mark

What is the output of the following code?

L = [20, 6, 16, 10, 22]
print(16 in L, 52 not in L)
Answer

(d) True True See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [20, 6, 16, 10, 22]L = [20, 6, 16, 10, 22]
2print(16 in L, 52 not in L)True True⏎
A4.179Practice1 mark

What is the output of the following code?

L = [30, 4, 31, 35, 23, 37]
del L[3]
print(L)
Answer

(d) [30, 4, 31, 23, 37] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [30, 4, 31, 35, 23, 37]L = [30, 4, 31, 35, 23, 37]
2del L[3]L = [30, 4, 31, 23, 37]
3print(L)[30, 4, 31, 23, 37]⏎
A4.180Practice1 mark

What is the output of the following code?

D = {'Bela': 90, 'Gita': 54, 'Amit': 82, 'Dev': 76}
print("Gita" in D, 54 in D)
Answer

(d) True False A dictionary keeps key: value pairs; keys are unique.

A4.181Practice1 mark

What is the output of the following code?

D = {'Amit': 50, 'Dev': 42, 'Charu': 51, 'Esha': 47}
del D["Dev"]
print(D)
Answer

(a) {'Amit': 50, 'Charu': 51, 'Esha': 47} A dictionary keeps key: value pairs; keys are unique.

A4.182Practice1 mark

What is the output of the following code?

D = {'Gita': 80, 'Charu': 60, 'Farid': 98, 'Esha': 55}
D["Charu"] = 99
print(D)
Answer

(c) {'Gita': 80, 'Charu': 99, 'Farid': 98, 'Esha': 55} A dictionary keeps key: value pairs; keys are unique.

A4.183Practice1 mark

What is the output of the following code?

L = [14, 38, 27, 19, 32]
del L[1]
print(L)
Answer

(a) [14, 27, 19, 32] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [14, 38, 27, 19, 32]L = [14, 38, 27, 19, 32]
2del L[1]L = [14, 27, 19, 32]
3print(L)[14, 27, 19, 32]⏎
A4.184Practice1 mark

What is the output of the following code?

T = (10, 24, 4, 7)
print(10 in T)
Answer

(b) True A tuple is ordered like a list but cannot be changed.

A4.185Practice1 mark

What is the output of the following code?

D = {'Charu': 86, 'Gita': 47, 'Bela': 84, 'Farid': 80}
print(sorted(D))
Answer

(c) ['Bela', 'Charu', 'Farid', 'Gita'] A dictionary keeps key: value pairs; keys are unique.

A4.186Practice1 mark

What is the output of the following code?

L = [13, 1, 9, 29, 25]
L.extend([46, 47])
print(L)
Answer

(b) [13, 1, 9, 29, 25, 46, 47] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [13, 1, 9, 29, 25]L = [13, 1, 9, 29, 25]
2L.extend([46, 47])L = [13, 1, 9, 29, 25, 46, 47]
3print(L)[13, 1, 9, 29, 25, 46, 47]⏎
A4.187Practice1 mark

What is the output of the following code?

L = [31, 29, 18, 24, 6]
print(L * 2)
Answer

(d) [31, 29, 18, 24, 6, 31, 29, 18, 24, 6] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1L = [31, 29, 18, 24, 6]L = [31, 29, 18, 24, 6]
2print(L * 2)[31, 29, 18, 24, 6, 31, 29, 18, 24, 6]⏎
A4.188Practice1 mark

What is the output of the following code?

s = "India is Great"
print(s.find("nd"))
Answer

(c) 1 See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "India is Great"s = 'India is Great'
2print(s.find("nd"))1⏎
A4.189Practice1 mark

Which of these is a possible output of the code?

import random
for i in range(3):
    print(random.randrange(2, 6), end=" ")
Answer

(d) 4 4 2 random.randrange(2, 6) gives a whole number from 2 to 5 (the stop value 6 is left out).

A4.190Practice1 mark

What is the output of the following code?

s = "Program"
print(s.split())
Answer

(c) ['Program'] See the working below.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Program"s = 'Program'
2print(s.split())['Program']⏎
A5Society, law and ethics1 mark · 100 questions
A5.1Practice1 mark

Hacking done with permission, to find and fix the weak points of a system, is

Answer

(b) ethical hacking Ethical (white-hat) hackers test a system with the owner's permission to make it safer.

A5.2Practice1 mark

Spending too much time on screens can lead to

Answer

(b) eye strain and poor sleep Long screen time strains the eyes and disturbs sleep; take breaks (20-20-20 rule).

A5.3Practice1 mark

Which of these is open source software?

Answer

(b) Python Python's source code is free to use, change and share; it is open source.

A5.4Practice1 mark

Which of these reduces e-waste?

Answer

(c) Repairing and using devices longer Repairing and using devices for longer means fewer devices are thrown away.

A5.5Practice1 mark

GPL is a type of

Answer

(d) free and open source software licence The GNU General Public License is a free and open source (copyleft) licence.

A5.6Practice1 mark

Unwanted bulk e-mail sent to many users is

Answer

(b) spam Spam is unwanted e-mail sent in bulk, often advertisements or scams.

A5.7Practice1 mark

FOSS stands for

Answer

(d) Free and Open Source Software FOSS = Free and Open Source Software, e.g. Linux, LibreOffice, Python.

A5.8Practice1 mark

State True or False: The IT Act, 2000 deals with cyber crimes in India.

Answer

(a) True The Information Technology Act, 2000 deals with cyber crimes and electronic records in India.

A5.9Practice1 mark

State True or False: Deleting a post removes every copy of it from the Internet.

Answer

(b) False Copies and screenshots may remain; footprints are lasting.

A5.10Practice1 mark

Data protection means

Answer

(a) keeping personal data safe from misuse Data protection means keeping personal data safe from misuse, theft and loss.

A5.11Practice1 mark

Small text files stored by websites on the user's computer are

Answer

(d) cookies Cookies are small text files that websites save to remember your visit, login and preferences.

A5.12Practice1 mark

Which harmful metal is found in e-waste?

Answer

(b) Lead E-waste contains lead, mercury and cadmium, which harm health when burnt or dumped.

A5.13Practice1 mark

Which of these is a safe practice on a public computer?

Answer

(b) log out and close the browser after use Always log out and close the browser so the next user cannot open your accounts.

A5.14Practice1 mark

OTP stands for

Answer

(d) One-Time Password OTP = One-Time Password, a code that works only once, used for safe login and payments.

A5.15Practice1 mark

Which of these is personal data that should not be shared online?

Answer

(c) Aadhaar number An Aadhaar number identifies you and can be misused, so it must not be shared openly.

A5.16Practice1 mark

Which of these helps a visually challenged person use a computer?

Answer

(a) Screen reader A screen reader reads the text on the screen aloud.

A5.17Practice1 mark

Encryption is used to

Answer

(b) turn data into a secret code so others cannot read it Encryption turns data into a secret code; only a person with the key can read it.

A5.18Practice1 mark

A white-hat hacker is

Answer

(a) an ethical hacker White-hat hackers are ethical hackers who work with permission to improve security.

A5.19Practice1 mark

Which of these is NOT a good netiquette?

Answer

(a) Typing in ALL CAPITALS Typing in ALL CAPITALS looks like shouting, so it is bad netiquette.

A5.20Practice1 mark

Respecting others, not using bad language and not shouting (ALL CAPS) online is part of

Answer

(a) netiquette Netiquette is good manners on the Internet.

A5.21Practice1 mark

Software that secretly records what the user does and sends it to others is

Answer

(b) spyware Spyware spies on the user and sends the information to someone else without permission.

A5.22Practice1 mark

A digital signature is used to

Answer

(a) verify the identity of the sender of an electronic document A digital signature proves who sent an electronic document and that it was not changed.

A5.23Practice1 mark

Apache, MIT and BSD are examples of

Answer

(a) open source licences They are permissive open source licences: the code can be used and changed freely.

A5.24Practice1 mark

Which of these protects a new medicine formula?

Answer

(a) Patent A new invention such as a medicine formula is protected by a patent.

A5.25Practice1 mark

Which of these is NOT a cyber crime?

Answer

(d) Using a licensed copy of software Using a licensed (legally bought) copy of software is legal; the others are crimes.

A5.26Practice1 mark

A program that records keystrokes to steal passwords is a

Answer

(b) keylogger A keylogger secretly records every key pressed, to steal passwords and card numbers.

A5.27Practice1 mark

A virus needs a

Answer

(a) host program to spread A virus attaches itself to a host program or file and spreads when that file is run.

A5.28Practice1 mark

HTTPS in a website address shows that

Answer

(d) the connection is encrypted and secure The S stands for secure: data between the browser and the website is encrypted.

A5.29Practice1 mark

The best way to manage e-waste is to

Answer

(d) reduce, reuse and recycle Reduce (buy less), reuse (repair, donate) and recycle through authorised recyclers.

A5.30Practice1 mark

Fake e-mails that trick users into giving passwords or bank details are

Answer

(a) phishing Phishing uses fake messages that look real to steal passwords and bank details.

A5.31Practice1 mark

Using a copyrighted work without permission is

Answer

(a) copyright infringement Using someone's protected work without permission breaks the copyright law.

A5.32Practice1 mark

Which of these is personal identifiable information?

Answer

(b) Phone number A phone number can identify a person, so it is PII.

A5.33Practice1 mark

Software given free for a trial period, after which it must be bought, is

Answer

(d) shareware Shareware is free only for a trial period; then it must be bought.

A5.34Practice1 mark

Before sharing a photo of a friend online, you should

Answer

(c) take the friend's permission The friend has the right to privacy, so take permission first.

A5.35Practice1 mark

Good manners to follow while communicating online are called

Answer

(b) netiquette Netiquette = net + etiquette (good manners online).

A5.36Practice1 mark

A fake website that looks like a real one is an example of

Answer

(b) spoofing Spoofing makes something fake look like a trusted source.

A5.37Practice1 mark

Which of these is free and open source software?

Answer

(d) LibreOffice LibreOffice is free and its source code is open; the others are proprietary.

A5.38Practice1 mark

Software that is free to use but whose source code is not open is

Answer

(a) freeware Freeware costs nothing but its source code is not shared (e.g. Adobe Reader).

A5.39Practice1 mark

Using a fake identity of another person to cheat is

Answer

(c) identity theft Identity theft is pretending to be someone else using their personal details.

A5.40Practice1 mark

Unauthorised copying of a movie and uploading it online is

Answer

(d) piracy Making and sharing illegal copies of copyrighted content is piracy.

A5.41Practice1 mark

The Indian law that deals with cyber crime and electronic records is the

Answer

(d) IT Act, 2000 The Information Technology Act, 2000 (IT Act) is India's cyber law.

A5.42Practice1 mark

A message saying 'Forward this to 10 people or bad luck will come' is

Answer

(c) a chain message (spam) Chain messages spread fear to make people forward them; they are spam.

A5.43Practice1 mark

A common gender issue in computing is

Answer

(a) fewer girls choosing computer science Fewer girls choose computer science because of stereotypes and less encouragement.

A5.44Practice1 mark

Two-factor authentication means

Answer

(b) checking identity in two ways, such as a password and an OTP Two checks are used, such as a password you know and an OTP on your phone.

A5.45Practice1 mark

A word, logo or symbol that identifies a brand is protected by a

Answer

(b) trademark A trademark protects brand names, logos and symbols.

A5.46Practice1 mark

Keeping the operating system and antivirus updated helps to

Answer

(a) fix security holes Updates fix security holes that attackers could use.

A5.47Practice1 mark

Which of these is a sign of a secure payment page?

Answer

(c) https and a padlock in the address bar https and a padlock show that the connection is encrypted.

A5.48Practice1 mark

Which is the national helpline number for reporting cyber fraud in India?

Answer

(b) 1930 Dial 1930 (or visit cybercrime.gov.in) to report cyber fraud.

A5.49Practice1 mark

Making digital content usable by persons with disabilities is called

Answer

(d) inclusive design (accessibility) Accessibility (inclusive design) makes content usable by everyone, including persons with disabilities.

A5.50Practice1 mark

Software that detects and removes malware is

Answer

(b) antivirus Antivirus software scans for viruses and other malware and removes them.

A5.51Practice1 mark

Old mobile phones should be

Answer

(b) given to an authorised e-waste collector An authorised e-waste collector recycles them safely.

A5.52Practice1 mark

Proprietary software

Answer

(c) is owned by a company and its source code is not shared Proprietary software is owned by a company; users buy a licence but cannot see or change the code.

A5.53Practice1 mark

Footprints left knowingly, such as posts and comments, are

Answer

(a) active digital footprints Active digital footprints are left on purpose by the user.

A5.54Practice1 mark

Sending a fake message that looks like it is from a known contact is

Answer

(a) spoofing Spoofing fakes the sender so the message looks trusted.

A5.55Practice1 mark

Using someone else's work or ideas as your own, without giving credit, is

Answer

(a) plagiarism Plagiarism is presenting another person's work as your own.

A5.56Practice1 mark

State True or False: A Trojan horse copies itself to other computers on its own.

Answer

(b) False That is a worm; a Trojan needs the user to run it.

A5.57Practice1 mark

State True or False: Open source software can be studied and changed by users.

Answer

(a) True Its source code is open, so users can study, change and share it.

A5.58Practice1 mark

Malware that copies itself and spreads through a network on its own is a

Answer

(b) worm A worm spreads by itself through networks; it does not need a host file.

A5.59Practice1 mark

Malware that looks like a useful program but harms the computer is a

Answer

(a) Trojan horse Like the wooden horse of Troy, a Trojan looks useful but hides harmful code.

A5.60Practice1 mark

Public domain works

Answer

(c) can be used freely by anyone Works in the public domain have no copyright, so anyone can use them freely.

A5.61Practice1 mark

State True or False: Screen readers help visually challenged users.

Answer

(a) True Screen readers read the screen aloud for users who cannot see it.

A5.62Practice1 mark

Clicking a link in a message that says 'You won a prize, enter your bank details' can lead to

Answer

(d) phishing fraud Such fake prize messages are phishing; they steal bank details.

A5.63Practice1 mark

Which right lets a creator decide who may copy and share the work?

Answer

(c) Copyright Copyright gives the creator the right to decide who may copy and share the work.

A5.64Practice1 mark

Malware that locks the files of a victim and asks for money is

Answer

(b) ransomware Ransomware encrypts files and demands a ransom to unlock them.

A5.65Practice1 mark

Posting provocative comments to upset people online is

Answer

(d) cyber trolling Cyber trolling means posting provocative or insulting comments to upset people.

A5.66Practice1 mark

Cyber safety means

Answer

(a) using the Internet safely and responsibly Cyber safety is using the Internet safely and responsibly to protect yourself and your data.

A5.67Practice1 mark

The legal right over a new invention is a

Answer

(d) patent A patent gives the inventor the right over the invention for a fixed period.

A5.68Practice1 mark

Sending insulting or threatening messages to harass a person online is

Answer

(c) cyber bullying Cyber bullying is harassing or threatening someone online.

A5.69Practice1 mark

Teaching aids such as screen readers and Braille keyboards are called

Answer

(a) assistive technologies Assistive technologies help persons with disabilities use computers.

A5.70Practice1 mark

The difference in access to computers and the Internet between groups of people is the

Answer

(a) digital divide The digital divide is the gap between people who have access to technology and those who do not.

A5.71Practice1 mark

Discarded electronic devices such as old phones and computers are

Answer

(a) e-waste Thrown-away electronic devices are called e-waste.

A5.72Practice1 mark

Private browsing (incognito mode)

Answer

(b) does not save the history and cookies after the window is closed Incognito mode does not keep history and cookies after the window is closed; websites can still see your visit.

A5.73Practice1 mark

Creative Commons (CC) licences are mostly used for

Answer

(d) creative works such as images, music and articles CC licences let creators share images, music and articles on simple terms.

A5.74Practice1 mark

Data collected without the user knowing, such as the websites visited, forms

Answer

(a) passive digital footprints Passive footprints are collected without the user knowing, e.g. browsing history and location.

A5.75Practice1 mark

A strong password

Answer

(a) mixes capital and small letters, digits and symbols A strong password is long and mixes capital and small letters, digits and symbols.

A5.76Practice1 mark

Children should share personal problems faced online with

Answer

(c) parents or teachers Parents and teachers can guide and protect them.

A5.77Practice1 mark

The illegal copying and selling of software is

Answer

(d) software piracy Software piracy is copying or selling software without a licence.

A5.78Practice1 mark

An online fraud in which the attacker calls and pretends to be from a bank is

Answer

(a) vishing (voice phishing) Vishing = voice phishing: fraud done over a phone call.

A5.79Practice1 mark

A message that pretends to come from a trusted website and redirects to a fake one is

Answer

(c) a phishing link A phishing link takes the user to a fake site to steal details.

A5.80Practice1 mark

Captions on videos mainly help

Answer

(c) people with hearing impairment Captions show speech as text for people who cannot hear.

A5.81Practice1 mark

The trail of data a person leaves while using the Internet is a

Answer

(d) digital footprint Everything you do online leaves a digital footprint.

A5.82Practice1 mark

State True or False: Plagiarism is avoided by giving credit to the original source.

Answer

(a) True Citing the source gives credit to the creator, so it is not plagiarism.

A5.83Practice1 mark

Copyleft licences such as GPL require that

Answer

(d) changed versions are also shared under the same licence Copyleft means changed versions must be shared under the same free licence.

A5.84Practice1 mark

The IT Act, 2000 gives legal recognition to

Answer

(b) electronic records and digital signatures The IT Act makes electronic records and digital signatures legally valid.

A5.85Practice1 mark

The safest way to dispose of an old hard disk is to

Answer

(a) wipe the data and give it to an authorised recycler Wipe the data so it cannot be recovered, then give it to an authorised recycler.

A5.86Practice1 mark

State True or False: A strong password should be shared with close friends.

Answer

(b) False A password must never be shared, even with friends.

A5.87Practice1 mark

The legal right of a creator over a book, song or software is

Answer

(b) copyright Copyright protects creative works such as books, songs and software.

A5.88Practice1 mark

Software that shows unwanted advertisements is

Answer

(d) adware Adware shows unwanted advertisements.

A5.89Practice1 mark

Creations of the mind, such as inventions, books and designs, are called

Answer

(d) intellectual property Intellectual property means creations of the mind; it is protected by copyright, patent and trademark.

A5.90Practice1 mark

Recycling e-waste in an unsafe, informal way harms

Answer

(b) the health of workers and the environment Burning and acid-washing e-waste releases toxic fumes that harm workers and the environment.

A5.91Practice1 mark

Gender bias in technology can be reduced by

Answer

(c) giving equal access and encouragement to girls Equal access, role models and encouragement bring more girls into computing.

A5.92Practice1 mark

Which of these is an example of passive digital footprint?

Answer

(b) The location data saved by an app Location data saved by an app is collected without the user actively posting it.

A5.93Practice1 mark

Which of these is a right of a creator under copyright?

Answer

(b) To decide who may copy the work Copyright lets the creator decide who may copy, share or change the work.

A5.94Practice1 mark

The licence that lets anyone use a work, only with credit to the creator, is

Answer

(d) CC BY CC BY (Attribution) allows any use if the creator is credited.

A5.95Practice1 mark

Which agency in India handles the response to cyber security incidents?

Answer

(a) CERT-In CERT-In (Indian Computer Emergency Response Team) responds to cyber security incidents.

A5.96Practice1 mark

A firewall

Answer

(d) filters the traffic between a computer and the network A firewall checks the traffic between a computer and the network and blocks unsafe connections.

A5.97Practice1 mark

State True or False: E-waste can be safely thrown into ordinary garbage.

Answer

(b) False It contains toxic metals; give it to an authorised recycler.

A5.98Practice1 mark

Digital footprints can be used by employers and colleges to

Answer

(a) learn about a person's online behaviour They may look at your online behaviour, so post responsibly.

A5.99Practice1 mark

Breaking into a computer system without permission is

Answer

(b) hacking Unauthorised access to a computer is hacking (cracking), a cyber crime.

A5.100Practice1 mark

Which section of the IT Act deals with identity theft?

Answer

(c) 66C Section 66C of the IT Act punishes identity theft (misusing passwords, signatures).

A6Assertion and Reason1 mark · 52 questions
A6.1Practice1 mark

Assertion (A): In Python, the expression 7 // 2 gives 3.

Reason (R): // is the floor division operator that gives the quotient rounded down.

Answer

(a) Both A and R are true and R is the correct explanation of A. R is the rule behind A: 7 / 2 = 3.5 and // rounds it down to 3.

A6.2Practice1 mark

Assertion (A): Python is called a dynamically typed language.

Reason (R): The type of a variable is decided by the value given to it while the program runs.

Answer

(a) Both A and R are true and R is the correct explanation of A. R is exactly what dynamic typing means: x = 5 makes x an int; later x = 'hi' makes it a str.

A6.3Practice1 mark

Assertion (A): A = 5 and a = 5 create two different variables.

Reason (R): Python is case sensitive.

Answer

(a) Both A and R are true and R is the correct explanation of A. Because Python is case sensitive (R), A and a are different names (A).

A6.4Practice1 mark

Assertion (A): The statement x = int('12.5') converts the string to the integer 12.

Reason (R): int() can convert any string to an integer.

Answer

(d) A is false but R is true. int('12.5') gives ValueError; a string with a decimal point cannot be passed to int().

A6.5Practice1 mark

Assertion (A): input() returns a string even when a number is typed.

Reason (R): int() is used to change the input to an integer.

Answer

(b) Both A and R are true but R is not the correct explanation of A. Both are true, but R does not explain why input() returns a string.

A6.6Practice1 mark

Assertion (A): The expression 2 3 2 gives 512.

Reason (R): The ** operator is evaluated from right to left.

Answer

(a) Both A and R are true and R is the correct explanation of A. 3 2 = 9 first, then 2 9 = 512.

A6.7Practice1 mark

Assertion (A): A comment is executed by the Python interpreter.

Reason (R): A comment starts with the symbol #.

Answer

(d) A is false but R is true. Comments are ignored by the interpreter.

A6.8Practice1 mark

Assertion (A): 5 + '5' gives an error in Python.

Reason (R): Python cannot add an integer and a string.

Answer

(a) Both A and R are true and R is the correct explanation of A. R explains A: an int and a str cannot be added, so TypeError comes.

A6.9Practice1 mark

Assertion (A): A logical error is reported by the interpreter.

Reason (R): A logical error gives a wrong result without any error message.

Answer

(d) A is false but R is true. A is false: the interpreter does not report logical errors. R is true: the program runs but gives a wrong result.

A6.10Practice1 mark

Assertion (A): The keyword 'True' can be used as a variable name.

Reason (R): Keywords have a special meaning in Python.

Answer

(d) A is false but R is true. Keywords cannot be used as identifiers.

A6.11Practice1 mark

Assertion (A): A for loop with range(1, 10, 2) runs 5 times.

Reason (R): range(1, 10, 2) gives 1, 3, 5, 7, 9.

Answer

(a) Both A and R are true and R is the correct explanation of A. The five values 1, 3, 5, 7, 9 in R are why the loop runs 5 times.

A6.12Practice1 mark

Assertion (A): The else block of a loop runs even after break.

Reason (R): break ends the loop at once.

Answer

(d) A is false but R is true. The loop else is skipped when break runs.

A6.13Practice1 mark

Assertion (A): A while loop is called an entry-controlled loop.

Reason (R): Its condition is checked before every round.

Answer

(a) Both A and R are true and R is the correct explanation of A. The condition is checked before each round (at the entry).

A6.14Practice1 mark

Assertion (A): Indentation is important in Python.

Reason (R): Python uses curly brackets to mark a block.

Answer

(c) A is true but R is false. Python uses indentation, not brackets.

A6.15Practice1 mark

Assertion (A): continue ends the loop at once.

Reason (R): continue skips the rest of the current round.

Answer

(d) A is false but R is true. A is false: break ends the loop. R is true: continue skips the rest of the current round only.

A6.16Practice1 mark

Assertion (A): Strings are immutable in Python.

Reason (R): s[0] = 'P' gives a TypeError.

Answer

(a) Both A and R are true and R is the correct explanation of A. R shows A: changing one character with s[0] = 'P' gives TypeError because strings cannot be changed.

A6.17Practice1 mark

Assertion (A): s.find('z') returns -1 when 'z' is not in s.

Reason (R): find() gives an error when the substring is not found.

Answer

(c) A is true but R is false. It is index() that gives an error.

A6.18Practice1 mark

Assertion (A): 'Hello'[1:4] gives 'ell'.

Reason (R): Slicing stops before the end index.

Answer

(a) Both A and R are true and R is the correct explanation of A. Slicing takes index 1, 2, 3 and stops before 4 (R), so the result is 'ell' (A).

A6.19Practice1 mark

Assertion (A): len('KV 88') is 5.

Reason (R): Spaces are counted as characters.

Answer

(a) Both A and R are true and R is the correct explanation of A. K, V, space, 8, 8 = 5 characters, because the space is counted (R).

A6.20Practice1 mark

Assertion (A): 'Python'.upper() changes the original string.

Reason (R): upper() returns a new string in capital letters.

Answer

(d) A is false but R is true. A is false: strings cannot be changed. R is true: upper() returns a new string.

A6.21Practice1 mark

Assertion (A): Lists are mutable.

Reason (R): Elements of a list can be changed after the list is made.

Answer

(a) Both A and R are true and R is the correct explanation of A. Mutable means changeable; R says list elements can be changed, e.g. L[0] = 9.

A6.22Practice1 mark

Assertion (A): L.append([4, 5]) increases the length of L by 2.

Reason (R): append() adds its argument as one element.

Answer

(d) A is false but R is true. The length increases by 1; extend() would add 2.

A6.23Practice1 mark

Assertion (A): L.sort() returns the sorted list.

Reason (R): sort() arranges the list in place.

Answer

(d) A is false but R is true. sort() returns None.

A6.24Practice1 mark

Assertion (A): L.pop() removes the last element of L.

Reason (R): pop() without an index works on the last position.

Answer

(a) Both A and R are true and R is the correct explanation of A. With no index, pop() works on the last position (R), so it removes the last element (A).

A6.25Practice1 mark

Assertion (A): Two lists can be joined with +.

Reason (R): + is the concatenation operator for lists.

Answer

(a) Both A and R are true and R is the correct explanation of A. + is the concatenation operator (R): [1, 2] + [3] gives [1, 2, 3].

A6.26Practice1 mark

Assertion (A): A tuple cannot be changed after it is created.

Reason (R): Tuples are immutable.

Answer

(a) Both A and R are true and R is the correct explanation of A. Tuples are immutable (R), which is why they cannot be changed (A).

A6.27Practice1 mark

Assertion (A): (5) is a tuple.

Reason (R): A tuple with one element needs a comma after the element.

Answer

(d) A is false but R is true. (5) is just the integer 5.

A6.28Practice1 mark

Assertion (A): A list can be a key of a dictionary.

Reason (R): Dictionary keys must be immutable.

Answer

(d) A is false but R is true. A list is mutable, so it cannot be a key.

A6.29Practice1 mark

Assertion (A): d.get('x') does not give an error when 'x' is not a key.

Reason (R): get() returns None for a missing key.

Answer

(a) Both A and R are true and R is the correct explanation of A. get() returns None for a missing key (R), so no KeyError comes (A).

A6.30Practice1 mark

Assertion (A): A dictionary can have two pairs with the same key.

Reason (R): Values in a dictionary can be repeated.

Answer

(d) A is false but R is true. Keys are unique; values may repeat.

A6.31Practice1 mark

Assertion (A): math.floor(-3.5) gives -4.

Reason (R): floor() gives the largest integer not greater than the number.

Answer

(a) Both A and R are true and R is the correct explanation of A. floor() takes the largest whole number not greater than -3.5 (R), which is -4 (A).

A6.32Practice1 mark

Assertion (A): random.randint(1, 5) can give 5.

Reason (R): randint() includes both the end points.

Answer

(a) Both A and R are true and R is the correct explanation of A. randint() includes both end points (R), so 5 is possible (A).

A6.33Practice1 mark

Assertion (A): statistics.mode([2, 3, 3, 5]) gives 3.

Reason (R): Mode is the value that occurs most often.

Answer

(a) Both A and R are true and R is the correct explanation of A. 3 occurs most often (R), so the mode is 3 (A).

A6.34Practice1 mark

Assertion (A): ASCII can represent Hindi letters.

Reason (R): ASCII has codes only for English letters, digits and symbols.

Answer

(d) A is false but R is true. A is false: ASCII has only 128 codes for English letters, digits and symbols (R is true).

A6.35Practice1 mark

Assertion (A): Unicode is used in web pages of many languages.

Reason (R): Unicode gives a unique code to the characters of almost every script.

Answer

(a) Both A and R are true and R is the correct explanation of A. Unicode gives every character of almost every script a code (R), so pages in many languages use it (A).

A6.36Practice1 mark

Assertion (A): NAND is called a universal gate.

Reason (R): All other gates can be built using only NAND gates.

Answer

(a) Both A and R are true and R is the correct explanation of A. All other gates can be built from NAND alone (R); that is what universal means (A).

A6.37Practice1 mark

Assertion (A): RAM is called volatile memory.

Reason (R): RAM loses its data when the power is switched off.

Answer

(a) Both A and R are true and R is the correct explanation of A. Volatile means it loses data without power (R), which is true of RAM (A).

A6.38Practice1 mark

Assertion (A): Cache memory makes a computer faster.

Reason (R): Cache keeps the data that the CPU uses often.

Answer

(a) Both A and R are true and R is the correct explanation of A. Cache keeps often-used data close to the CPU (R), so the CPU waits less (A).

A6.39Practice1 mark

Assertion (A): Phishing is a cyber crime.

Reason (R): Phishing tricks people into giving passwords and bank details.

Answer

(a) Both A and R are true and R is the correct explanation of A. Phishing tricks people into giving passwords and bank details (R), so it is a crime (A).

A6.40Practice1 mark

Assertion (A): E-waste is harmful when burnt.

Reason (R): Electronic devices contain lead and mercury.

Answer

(a) Both A and R are true and R is the correct explanation of A. Electronic devices contain lead and mercury (R), which give toxic fumes when burnt (A).

A6.41Practice1 mark

Assertion (A): Open source software is always free of cost.

Reason (R): In open source software the source code is available.

Answer

(d) A is false but R is true. Open source is about access to the code; it may still be sold.

A6.42Practice1 mark

Assertion (A): Active digital footprints are left knowingly.

Reason (R): Posts and comments are made by the user on purpose.

Answer

(a) Both A and R are true and R is the correct explanation of A. Posts and comments are made on purpose (R), which is what active footprints are (A).

A6.43Practice1 mark

Assertion (A): L = [1, 2] * 2 gives [1, 2, 1, 2].

Reason (R): * repeats the list.

Answer

(a) Both A and R are true and R is the correct explanation of A. * repeats the list (R), so [1, 2] appears twice (A).

A6.44Practice1 mark

Assertion (A): 'Python'[::-1] gives 'nohtyP'.

Reason (R): A step of -1 reads the string from the end.

Answer

(a) Both A and R are true and R is the correct explanation of A. A step of -1 reads from the last character to the first (R), giving the reversed string (A).

A6.45Practice1 mark

Assertion (A): L[1:3] = [7, 8, 9] is allowed for a list L of 5 elements.

Reason (R): A slice of a list can be replaced by any number of elements.

Answer

(a) Both A and R are true and R is the correct explanation of A. A slice can be replaced by any number of elements (R); the list just becomes longer (6 elements) (A).

A6.46Practice1 mark

Assertion (A): for i in range(5): print(i) prints the numbers 1 to 5.

Reason (R): range(5) starts from 0.

Answer

(d) A is false but R is true. It prints 0 to 4.

A6.47Practice1 mark

Assertion (A): The expression 10 / 2 gives 5.

Reason (R): The / operator always gives a float.

Answer

(d) A is false but R is true. 10 / 2 gives 5.0.

A6.48Practice1 mark

Assertion (A): len({'a': 1, 'b': 2, 'a': 3}) is 2.

Reason (R): A repeated key keeps only its last value.

Answer

(a) Both A and R are true and R is the correct explanation of A. The repeated key 'a' keeps only its last value (R), so only 2 keys remain (A).

A6.49Practice1 mark

Assertion (A): s.split() on 'KV Delhi' gives ['KV', 'Delhi'].

Reason (R): split() without an argument splits at any amount of white space.

Answer

(a) Both A and R are true and R is the correct explanation of A. split() with no argument splits at any amount of white space (R), so the extra space gives no empty string (A).

A6.50Practice1 mark

Assertion (A): math.ceil(-2.3) gives -3.

Reason (R): ceil() gives the smallest integer that is not less than the number.

Answer

(d) A is false but R is true. It gives -2.

A6.51Practice1 mark

Assertion (A): max([3, 'a', 5]) gives an error.

Reason (R): Python cannot compare an int and a str with >.

Answer

(a) Both A and R are true and R is the correct explanation of A. max() compares the elements; int and str cannot be compared (R), so TypeError comes (A).

A6.52Practice1 mark

Assertion (A): 'ab' in 'cabin' is True.

Reason (R): in checks whether one string is a part of another.

Answer

(a) Both A and R are true and R is the correct explanation of A. in checks whether one string is part of another (R); 'cabin' contains 'ab' (A).

Section B · 2 marks each

B1Unit I: explain, convert, truth table2 marks · 53 questions
B1.1Practice2 marks

Why are NAND and NOR called universal gates? Show NOT using NAND.

Answer

Because every other gate (AND, OR, NOT) can be built using only NAND gates, or only NOR gates. NOT A = A NAND A: join both inputs of a NAND gate to A.

B1.2Practice2 marks

Draw the truth table for the Boolean expression (NOT A) OR (NOT B).

Answer
AB(NOT A) OR (NOT B)
001
011
101
110

The output is 1 in 3 of the 4 rows.

B1.3Practice2 marks

Draw the truth table for the Boolean expression NOT (A OR B).

Answer
ABNOT (A OR B)
001
010
100
110

The output is 1 in 1 of the 4 rows.

B1.4Practice2 marks

Draw the truth table for the Boolean expression (A OR B) AND (NOT C).

Answer
ABC(A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110

The output is 1 in 3 of the 8 rows.

B1.5Practice2 marks

Draw the truth table for the Boolean expression (NOT A) AND (NOT B).

Answer
AB(NOT A) AND (NOT B)
001
010
100
110

The output is 1 in 1 of the 4 rows.

B1.6Practice2 marks

Convert: (i) (114)10 to hexadecimal   (ii) (10000110)2 to hexadecimal

Answer

(i)

Divide byNumberRemainder
161142
1677

Read the remainders from bottom to top: (72)16

(ii)

Make groups of 4 bits from the right: 1000  0110

1000 → 8   0110 → 6

So (10000110)2 = (86)16

B1.7Practice2 marks

State De Morgan's laws.

Answer

(i) NOT (A AND B) = (NOT A) OR (NOT B). (ii) NOT (A OR B) = (NOT A) AND (NOT B). In words: the complement of a product is the sum of the complements, and the complement of a sum is the product of the complements.

B1.8Practice2 marks

What is encoding? Name any three encoding schemes.

Answer

Encoding is the way of giving a unique code (number) to every character so that a computer can store it. Schemes: ASCII, ISCII, Unicode (UTF-8, UTF-16, UTF-32).

B1.9Practice2 marks

Convert: (i) (111100)2 to hexadecimal   (ii) (109)10 to hexadecimal

Answer

(i)

Make groups of 4 bits from the right: 0011  1100

0011 → 3   1100 → C

So (111100)2 = (3C)16

(ii)

Divide byNumberRemainder
16109D
1666

Read the remainders from bottom to top: (6D)16

B1.10Practice2 marks

Draw the symbols and write the truth table of the XOR gate.

Answer

XOR gives 1 when the inputs are different. Truth table: 0 0 → 0; 0 1 → 1; 1 0 → 1; 1 1 → 0. Symbol: an OR gate with an extra curved line on the input side.

XOR gate symbol

ABY

Truth table: 0 0 → 0, 0 1 → 1, 1 0 → 1, 1 1 → 0 (output is 1 only when the inputs are different). Expression: Y = A ⊕ B = A·B̄ + Ā·B.

B1.11Practice2 marks

Convert: (i) (11100100)2 to hexadecimal   (ii) (210)8 to binary

Answer

(i)

Make groups of 4 bits from the right: 1110  0100

1110 → E   0100 → 4

So (11100100)2 = (E4)16

(ii)

Write each digit as 3 bits: 2 → 010   1 → 001   0 → 000

So (210)8 = (10001000)2

B1.12Practice2 marks

Convert: (i) (40)8 to decimal   (ii) (D0)16 to decimal

Answer

(i)

(40)8 = 4 × 81 + 0 × 80 = 32 + 0 = 32

(ii)

(D0)16 = D × 161 + 0 × 160 = 208 + 0 = 208

B1.13Practice2 marks

Convert: (i) (111111)2 to decimal   (ii) (11001000)2 to hexadecimal

Answer

(i)

(111111)2 = 1 × 25 + 1 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 1 × 20 = 32 + 16 + 8 + 4 + 2 + 1 = 63

(ii)

Make groups of 4 bits from the right: 1100  1000

1100 → C   1000 → 8

So (11001000)2 = (C8)16

B1.14Practice2 marks

Convert: (i) (210)8 to decimal   (ii) (11111111)2 to hexadecimal

Answer

(i)

(210)8 = 2 × 82 + 1 × 81 + 0 × 80 = 128 + 8 + 0 = 136

(ii)

Make groups of 4 bits from the right: 1111  1111

1111 → F   1111 → F

So (11111111)2 = (FF)16

B1.15Practice2 marks

Convert: (i) (153)8 to hexadecimal   (ii) (120)10 to hexadecimal

Answer

(i)

Write each digit as 3 bits: 1 → 001   5 → 101   3 → 011

So (153)8 = (1101011)2

Make groups of 4 bits from the right: 0110  1011

0110 → 6   1011 → B

So (1101011)2 = (6B)16

(ii)

Divide byNumberRemainder
161208
1677

Read the remainders from bottom to top: (78)16

B1.16Practice2 marks

Name the three types of buses and what each carries.

Answer

Data bus: carries data. Address bus: carries the address of the memory location. Control bus: carries control signals such as read and write.

B1.17Practice2 marks

Convert: (i) (107)8 to hexadecimal   (ii) (11100011)2 to octal

Answer

(i)

Write each digit as 3 bits: 1 → 001   0 → 000   7 → 111

So (107)8 = (1000111)2

Make groups of 4 bits from the right: 0100  0111

0100 → 4   0111 → 7

So (1000111)2 = (47)16

(ii)

Make groups of 3 bits from the right: 011  100  011

011 → 3   100 → 4   011 → 3

So (11100011)2 = (343)8

B1.18Practice2 marks

Draw the truth table for the Boolean expression (A AND NOT B) OR (B AND C).

Answer
ABC(A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111

The output is 1 in 4 of the 8 rows.

B1.19Practice2 marks

Convert: (i) (131)10 to hexadecimal   (ii) (A4)16 to octal

Answer

(i)

Divide byNumberRemainder
161313
1688

Read the remainders from bottom to top: (83)16

(ii)

Write each digit as 4 bits: A → 1010   4 → 0100

So (A4)16 = (10100100)2

Make groups of 3 bits from the right: 010  100  100

010 → 2   100 → 4   100 → 4

So (10100100)2 = (244)8

B1.20Practice2 marks

Convert: (i) (266)8 to decimal   (ii) (180)10 to octal

Answer

(i)

(266)8 = 2 × 82 + 6 × 81 + 6 × 80 = 128 + 48 + 6 = 182

(ii)

Divide byNumberRemainder
81804
8226
822

Read the remainders from bottom to top: (264)8

B1.21Practice2 marks

Convert: (i) (246)8 to decimal   (ii) (147)10 to hexadecimal

Answer

(i)

(246)8 = 2 × 82 + 4 × 81 + 6 × 80 = 128 + 32 + 6 = 166

(ii)

Divide byNumberRemainder
161473
1699

Read the remainders from bottom to top: (93)16

B1.22Practice2 marks

Draw the truth table for the Boolean expression NOT (A OR B) AND C.

Answer
ABCNOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110

The output is 1 in 1 of the 8 rows.

B1.23Practice2 marks

Convert: (i) (43)8 to binary   (ii) (316)8 to decimal

Answer

(i)

Write each digit as 3 bits: 4 → 100   3 → 011

So (43)8 = (100011)2

(ii)

(316)8 = 3 × 82 + 1 × 81 + 6 × 80 = 192 + 8 + 6 = 206

B1.24Practice2 marks

Differentiate between UTF-8 and UTF-32.

Answer

UTF-8 is variable length: 1 to 4 bytes per character; English letters take 1 byte, so it saves space. UTF-32 is fixed length: every character takes 4 bytes.

B1.25Practice2 marks

Draw the truth table for the Boolean expression A AND B AND NOT C.

Answer
ABCA AND B AND NOT C
0000
0010
0100
0110
1000
1010
1101
1110

The output is 1 in 1 of the 8 rows.

B1.26Practice2 marks

What is an operating system? Write any two of its functions.

Answer

The operating system is system software that manages the hardware and gives a platform for other programs. Functions: process management, memory management, file management, device management, user interface. (Any two.)

B1.27Practice2 marks

Draw the truth table for the Boolean expression NOT (A AND B).

Answer
ABNOT (A AND B)
001
011
101
110

The output is 1 in 3 of the 4 rows.

B1.28Practice2 marks

Draw the truth table for the Boolean expression (NOT A) OR B.

Answer
AB(NOT A) OR B
001
011
100
111

The output is 1 in 3 of the 4 rows.

B1.29Practice2 marks

Convert: (i) (233)10 to hexadecimal   (ii) (365)8 to binary

Answer

(i)

Divide byNumberRemainder
162339
1614E

Read the remainders from bottom to top: (E9)16

(ii)

Write each digit as 3 bits: 3 → 011   6 → 110   5 → 101

So (365)8 = (11110101)2

B1.30Practice2 marks

Convert: (i) (216)8 to binary   (ii) (142)10 to hexadecimal

Answer

(i)

Write each digit as 3 bits: 2 → 010   1 → 001   6 → 110

So (216)8 = (10001110)2

(ii)

Divide byNumberRemainder
16142E
1688

Read the remainders from bottom to top: (8E)16

B1.31Practice2 marks

Differentiate between ASCII and Unicode.

Answer

ASCII uses 7 bits and has 128 codes, only for English letters, digits and symbols. Unicode can represent the characters of almost all the scripts of the world (over a million code points) using UTF-8, UTF-16 or UTF-32.

B1.32Practice2 marks

Draw the truth table for the Boolean expression A AND (B OR C).

Answer
ABCA AND (B OR C)
0000
0010
0100
0110
1000
1011
1101
1111

The output is 1 in 3 of the 8 rows.

B1.33Practice2 marks

Convert: (i) (45)10 to hexadecimal   (ii) (1001100)2 to octal

Answer

(i)

Divide byNumberRemainder
1645D
1622

Read the remainders from bottom to top: (2D)16

(ii)

Make groups of 3 bits from the right: 001  001  100

001 → 1   001 → 1   100 → 4

So (1001100)2 = (114)8

B1.34Practice2 marks

Differentiate between system software and application software with one example each.

Answer

System software runs and manages the computer: operating system, device drivers, utilities (e.g. Windows). Application software does a task for the user: word processing, billing, games (e.g. LibreOffice Writer).

B1.35Practice2 marks

Convert: (i) (198)10 to octal   (ii) (A1)16 to octal

Answer

(i)

Divide byNumberRemainder
81986
8240
833

Read the remainders from bottom to top: (306)8

(ii)

Write each digit as 4 bits: A → 1010   1 → 0001

So (A1)16 = (10100001)2

Make groups of 3 bits from the right: 010  100  001

010 → 2   100 → 4   001 → 1

So (10100001)2 = (241)8

B1.36Practice2 marks

Draw the truth table for the Boolean expression A AND (NOT B).

Answer
ABA AND (NOT B)
000
010
101
110

The output is 1 in 1 of the 4 rows.

B1.37Practice2 marks

Convert: (i) (69)16 to octal   (ii) (119)10 to binary

Answer

(i)

Write each digit as 4 bits: 6 → 0110   9 → 1001

So (69)16 = (1101001)2

Make groups of 3 bits from the right: 001  101  001

001 → 1   101 → 5   001 → 1

So (1101001)2 = (151)8

(ii)

Divide byNumberRemainder
21191
2591
2291
2140
271
231
211

Read the remainders from bottom to top: (1110111)2

B1.38Practice2 marks

Convert: (i) (189)10 to octal   (ii) (166)8 to decimal

Answer

(i)

Divide byNumberRemainder
81895
8237
822

Read the remainders from bottom to top: (275)8

(ii)

(166)8 = 1 × 82 + 6 × 81 + 6 × 80 = 64 + 48 + 6 = 118

B1.39Practice2 marks

Differentiate between a compiler and an interpreter.

Answer

A compiler translates the whole program at once and lists all the errors together; the machine code can be run again without translating. An interpreter translates and runs one line at a time and stops at the first error. Python uses an interpreter.

B1.40Practice2 marks

What is cache memory? Why is it used?

Answer

Cache is a small, very fast memory between the CPU and RAM. It keeps the data and instructions the CPU uses often, so the CPU waits less and the computer works faster.

B1.41Practice2 marks

Convert: (i) (1010001)2 to decimal   (ii) (1E)16 to binary

Answer

(i)

(1010001)2 = 1 × 26 + 0 × 25 + 1 × 24 + 0 × 23 + 0 × 22 + 0 × 21 + 1 × 20 = 64 + 0 + 16 + 0 + 0 + 0 + 1 = 81

(ii)

Write each digit as 4 bits: 1 → 0001   E → 1110

So (1E)16 = (11110)2

B1.42Practice2 marks

Convert: (i) (234)8 to decimal   (ii) (350)8 to hexadecimal

Answer

(i)

(234)8 = 2 × 82 + 3 × 81 + 4 × 80 = 128 + 24 + 4 = 156

(ii)

Write each digit as 3 bits: 3 → 011   5 → 101   0 → 000

So (350)8 = (11101000)2

Make groups of 4 bits from the right: 1110  1000

1110 → E   1000 → 8

So (11101000)2 = (E8)16

B1.43Practice2 marks

Convert: (i) (133)10 to binary   (ii) (206)10 to octal

Answer

(i)

Divide byNumberRemainder
21331
2660
2331
2160
280
240
220
211

Read the remainders from bottom to top: (10000101)2

(ii)

Divide byNumberRemainder
82066
8251
833

Read the remainders from bottom to top: (316)8

B1.44Practice2 marks

Convert: (i) (103)8 to binary   (ii) (60)16 to octal

Answer

(i)

Write each digit as 3 bits: 1 → 001   0 → 000   3 → 011

So (103)8 = (1000011)2

(ii)

Write each digit as 4 bits: 6 → 0110   0 → 0000

So (60)16 = (1100000)2

Make groups of 3 bits from the right: 001  100  000

001 → 1   100 → 4   000 → 0

So (1100000)2 = (140)8

B1.45Practice2 marks

Convert: (i) (175)8 to decimal   (ii) (37)10 to octal

Answer

(i)

(175)8 = 1 × 82 + 7 × 81 + 5 × 80 = 64 + 56 + 5 = 125

(ii)

Divide byNumberRemainder
8375
844

Read the remainders from bottom to top: (45)8

B1.46Practice2 marks

Draw the truth table for the Boolean expression (A OR NOT B) AND (B OR C).

Answer
ABC(A OR NOT B) AND (B OR C)
0000
0011
0100
0110
1000
1011
1101
1111

The output is 1 in 4 of the 8 rows.

B1.47Practice2 marks

Convert: (i) (83)10 to hexadecimal   (ii) (11101010)2 to hexadecimal

Answer

(i)

Divide byNumberRemainder
16833
1655

Read the remainders from bottom to top: (53)16

(ii)

Make groups of 4 bits from the right: 1110  1010

1110 → E   1010 → A

So (11101010)2 = (EA)16

B1.48Practice2 marks

Differentiate between RAM and ROM (any two points).

Answer

RAM is volatile: it loses data when the power is off; it can be read and written; it holds the programs being run. ROM is non-volatile; it can only be read; it holds the start-up (boot) instructions.

B1.49Practice2 marks

Draw the truth table for the Boolean expression NOT A OR (B AND C).

Answer
ABCNOT A OR (B AND C)
0001
0011
0101
0111
1000
1010
1100
1111

The output is 1 in 5 of the 8 rows.

B1.50Practice2 marks

Arrange in increasing order of speed: RAM, hard disk, registers, cache. Which of these are volatile?

Answer

Hard disk < RAM < cache < registers. RAM, cache and registers are volatile; the hard disk is not.

B1.51Practice2 marks

Convert: (i) (200)10 to hexadecimal   (ii) (CF)16 to octal

Answer

(i)

Divide byNumberRemainder
162008
1612C

Read the remainders from bottom to top: (C8)16

(ii)

Write each digit as 4 bits: C → 1100   F → 1111

So (CF)16 = (11001111)2

Make groups of 3 bits from the right: 011  001  111

011 → 3   001 → 1   111 → 7

So (11001111)2 = (317)8

B1.52Practice2 marks

Convert: (i) (55)8 to hexadecimal   (ii) (10001011)2 to decimal

Answer

(i)

Write each digit as 3 bits: 5 → 101   5 → 101

So (55)8 = (101101)2

Make groups of 4 bits from the right: 0010  1101

0010 → 2   1101 → D

So (101101)2 = (2D)16

(ii)

(10001011)2 = 1 × 27 + 0 × 26 + 0 × 25 + 0 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 = 128 + 0 + 0 + 0 + 8 + 0 + 2 + 1 = 139

B1.53Practice2 marks

Convert: (i) (111000)2 to decimal   (ii) (253)8 to hexadecimal

Answer

(i)

(111000)2 = 1 × 25 + 1 × 24 + 1 × 23 + 0 × 22 + 0 × 21 + 0 × 20 = 32 + 16 + 8 + 0 + 0 + 0 = 56

(ii)

Write each digit as 3 bits: 2 → 010   5 → 101   3 → 011

So (253)8 = (10101011)2

Make groups of 4 bits from the right: 1010  1011

1010 → A   1011 → B

So (10101011)2 = (AB)16

B2Python: explain and differentiate2 marks · 45 questions
B2.1Practice2 marks

What is an algorithm? Write two characteristics of a good algorithm.

Answer

An algorithm is a finite, step-by-step set of instructions to solve a problem. A good algorithm has: clear and unambiguous steps, defined input and output, a finite number of steps, and it is correct and efficient. (Any two.)

B2.2Practice2 marks

Differentiate between a flowchart and pseudocode.

Answer

A flowchart shows the steps of an algorithm as a picture with standard symbols joined by arrows. Pseudocode writes the steps in simple English-like statements, close to a program but without exact syntax.

B2.3Practice2 marks

Write an algorithm to find the larger of two numbers.

Answer

Step 1: Start. Step 2: Input A and B. Step 3: If A > B then print A, else print B. Step 4: Stop.

B2.4Practice2 marks

Write pseudocode to print the sum of the first N natural numbers.

Answer

INPUT N; SET sum = 0; FOR i = 1 TO N: sum = sum + i; END FOR; PRINT sum.

B2.5Practice2 marks

What is decomposition? Give an example.

Answer

Decomposition is breaking a big problem into smaller parts that are easier to solve. Example: a school result system is split into entering marks, calculating totals, finding grades and printing report cards.

B2.6Practice2 marks

Draw a flowchart to check whether a number is even or odd. (Describe the boxes.)

Answer

Oval Start → Parallelogram: input N → Diamond: N % 2 == 0? → Yes: parallelogram print 'Even'; No: parallelogram print 'Odd' → Oval Stop.

Flowchart: even or odd

StartInput NN % 2 == 0 ?YesNoPrint 'Even'Print 'Odd'Stop
B2.7Practice2 marks

What is verification of an algorithm? Why is it needed?

Answer

Verification is checking that the algorithm gives the correct output for all valid inputs, by a dry run with sample values. It is needed to catch errors before the program is written.

B2.8Practice2 marks

Differentiate between interactive mode and script mode of Python.

Answer

Interactive mode: statements are typed one at a time at the >>> prompt and the result is shown at once; the work is not saved. Script mode: the program is written in a .py file, saved, and run as a whole.

B2.9Practice2 marks

What are tokens? Name the types of tokens in Python.

Answer

Tokens are the smallest units of a program. Types: keywords, identifiers, literals, operators and punctuators (delimiters).

B2.10Practice2 marks

Write any four rules for naming an identifier in Python.

Answer

It can have letters, digits and the underscore; it cannot start with a digit; it cannot be a keyword; it cannot have spaces or special symbols such as @, $, -; it is case sensitive.

B2.11Practice2 marks

Differentiate between mutable and immutable data types with examples.

Answer

Mutable objects can be changed in place after creation: list, dictionary. Immutable objects cannot be changed; a change creates a new object: int, float, str, tuple.

B2.12Practice2 marks

Differentiate between = and == in Python.

Answer

= is the assignment operator; it gives a value to a variable: x = 5. == is a relational operator; it compares two values and gives True or False: x == 5.

B2.13Practice2 marks

Differentiate between / and // with examples.

Answer

/ is true division and always gives a float: 7 / 2 = 3.5. // is floor division and gives the quotient rounded down: 7 // 2 = 3, -7 // 2 = -4.

B2.14Practice2 marks

What is type conversion? Explain its two kinds with an example each.

Answer

Changing a value from one data type to another. Implicit: done by Python on its own, e.g. 3 + 2.5 gives 5.5 (int to float). Explicit: done by the programmer using functions, e.g. int('25') gives 25.

B2.15Practice2 marks

Name the three types of errors with one example each.

Answer

Syntax error: wrong grammar, e.g. a missing colon after if. Runtime error: occurs while running, e.g. 5 / 0 gives ZeroDivisionError. Logical error: wrong output, e.g. using + in place of * in a formula.

B2.16Practice2 marks

What is the difference between a keyword and an identifier?

Answer

A keyword is a reserved word with a fixed meaning in Python (if, for, while, True). An identifier is a name given by the programmer to a variable, function, etc. (marks, total_1). A keyword cannot be used as an identifier.

B2.17Practice2 marks

What is a literal? Name any three kinds of literals.

Answer

A literal is a fixed data value written in the program. Kinds: numeric literals (10, 3.5), string literals ('KV'), Boolean literals (True, False), the special literal None, and collection literals ([1, 2]).

B2.18Practice2 marks

Explain the use of id() and type() functions with an example.

Answer

id(x) gives the identity (memory address) of the object x. type(x) gives the data type of x. Example: x = 5; type(x) gives <class 'int'>.

B2.19Practice2 marks

What is the difference between the is and == operators?

Answer

== checks whether two values are equal. is checks whether two names refer to the same object in memory. a = [1]; b = [1]: a == b is True but a is b is False.

B2.20Practice2 marks

Write the precedence of these operators from highest to lowest: +, **, not, *, and, <

Answer

** , then * , then + , then < , then not , then and.

B2.21Practice2 marks

Differentiate between break and continue.

Answer

break ends the loop at once and control goes to the statement after the loop. continue skips the rest of the current round and the loop goes to its next round.

B2.22Practice2 marks

Differentiate between a for loop and a while loop.

Answer

A for loop runs over a sequence or range; it is used when the number of rounds is known. A while loop runs as long as a condition is True; it is used when the number of rounds is not known in advance.

B2.23Practice2 marks

What is an infinite loop? Give an example.

Answer

A loop whose condition never becomes False, so it never ends. Example: i = 1; while i > 0: i = i + 1

B2.24Practice2 marks

Explain range() with its three parameters, using an example.

Answer

range(start, stop, step) gives numbers from start up to stop - 1, jumping by step. Default start is 0 and step is 1. range(2, 11, 3) gives 2, 5, 8.

B2.25Practice2 marks

What is the use of the else clause with a loop?

Answer

The else block of a loop runs after the loop ends normally, that is, when no break was executed. It is useful in searching: print 'not found' in the else block.

B2.26Practice2 marks

Rewrite using a while loop:

for i in range(10, 0, -2):
    print(i)
Answer
i = 10
while i > 0:
    print(i)
    i = i - 2
B2.27Practice2 marks

Rewrite using a for loop:

n = 1
while n <= 15:
    print(n * n)
    n += 3
Answer
for n in range(1, 16, 3):
    print(n * n)
B2.28Practice2 marks

Differentiate between find() and index() methods of a string.

Answer

Both return the lowest index where the substring is found. If it is not found, find() returns -1, but index() raises ValueError.

B2.29Practice2 marks

Differentiate between split() and partition().

Answer

split() breaks the string at every separator and returns a list of any length. partition() breaks it only at the first separator and returns a tuple of exactly three parts: before, separator, after.

B2.30Practice2 marks

What is meant by 'strings are immutable'? Give an example.

Answer

A string cannot be changed after it is created. s = 'Kite'; s[0] = 'B' gives TypeError. To change it, make a new string: s = 'B' + s[1:].

B2.31Practice2 marks

Explain any two of: isalnum(), isspace(), istitle(), endswith().

Answer

isalnum(): True if all characters are letters or digits. isspace(): True if all characters are white space. istitle(): True if every word starts with a capital letter. endswith(x): True if the string ends with x.

B2.32Practice2 marks

Differentiate between append() and extend().

Answer

append(x) adds x as one single element at the end; the length increases by 1. extend(L2) adds each element of L2 at the end; the length increases by len(L2).

B2.33Practice2 marks

Differentiate between remove() and pop().

Answer

remove(x) deletes the first element equal to x and returns nothing. pop(i) deletes the element at index i (the last one if no index is given) and returns it.

B2.34Practice2 marks

Differentiate between sort() and sorted().

Answer

L.sort() arranges L itself and returns None; it works only on lists. sorted(seq) returns a new sorted list and leaves the original unchanged; it works on any sequence.

B2.35Practice2 marks

Write the difference between a list and a string.

Answer

A list is mutable and can hold elements of any type. A string is immutable and holds only characters. Both support indexing, slicing, + and *.

B2.36Practice2 marks

What is a nested list? How is an element of it accessed?

Answer

A list inside another list. L = [[1, 2], [3, 4]]: L[1][0] gives 3 (row 1, item 0).

B2.37Practice2 marks

Differentiate between a list and a tuple.

Answer

A list is mutable, written in [ ], has methods like append and remove. A tuple is immutable, written in ( ), has only count and index. Tuples are faster and can be dictionary keys.

B2.38Practice2 marks

Differentiate between a list and a dictionary.

Answer

A list is an ordered sequence accessed by an integer index. A dictionary stores key: value pairs and is accessed by keys, which must be unique and immutable.

B2.39Practice2 marks

Differentiate between pop() and popitem() of a dictionary.

Answer

pop(key) removes the given key and returns its value. popitem() removes and returns the last inserted (key, value) pair.

B2.40Practice2 marks

What is tuple packing and unpacking? Give an example.

Answer

Packing: T = 10, 20, 30 puts the values in one tuple. Unpacking: a, b, c = T takes them out into three variables.

B2.41Practice2 marks

Explain keys(), values() and items() with an example.

Answer

D = {'a': 1, 'b': 2}. D.keys() gives dict_keys(['a', 'b']); D.values() gives dict_values([1, 2]); D.items() gives dict_items([('a', 1), ('b', 2)]).

B2.42Practice2 marks

Differentiate between import math and from math import sqrt.

Answer

import math loads the whole module; functions are called as math.sqrt(16). from math import sqrt brings only sqrt; it is called directly as sqrt(16).

B2.43Practice2 marks

Differentiate between random.randint() and random.randrange().

Answer

randint(a, b) returns a random integer from a to b, both included. randrange(a, b, step) returns a random value from range(a, b, step), so b is not included.

B2.44Practice2 marks

What is a module? Name any three built-in modules of Python with one function each.

Answer

A module is a file of Python code with functions and variables that can be imported and used. math: sqrt(); random: randint(); statistics: mean().

B2.45Practice2 marks

Write the use of: math.ceil(), math.fabs(), statistics.median().

Answer

ceil(x): smallest integer not less than x, ceil(4.2) = 5. fabs(x): absolute value as a float, fabs(-3) = 3.0. median(data): middle value of the sorted data.

B3Python: write the output2 marks · 110 questions
B3.1Practice2 marks

Write the output of the following code.

for i in range(1, 5):
    for j in range(i):
        print(i, end="")
    print()
Answer

Output:

1
22
333
4444

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(1, 5):i = 1
2for j in range(i):j = 0
3print(i, end="")1
2for j in range(i):loop ends (no more values)
4print()⏎
1for i in range(1, 5):i = 2
2for j in range(i):loop ends (no more values)
3print(i, end="")2
2for j in range(i):j = 1
3print(i, end="")2
2for j in range(i):loop ends (no more values)
4print()⏎
1for i in range(1, 5):i = 3
2for j in range(i):j = 0
3print(i, end="")3
2for j in range(i):j = 1

… the same steps continue for the remaining values.

B3.2Practice2 marks

Write the output of the following code.

for i in range(1, 4):
    for j in range(1, i + 1):
        print(j * i, end=" ")
    print()
Answer

Output:

1 
2 4 
3 6 9 

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(1, 4):i = 1
2for j in range(1, i + 1):j = 1
3print(j * i, end=" ")1
2for j in range(1, i + 1):loop ends (no more values)
4print()⏎
1for i in range(1, 4):i = 2
2for j in range(1, i + 1):loop ends (no more values)
3print(j * i, end=" ")2
2for j in range(1, i + 1):j = 2
3print(j * i, end=" ")4
2for j in range(1, i + 1):loop ends (no more values)
4print()⏎
1for i in range(1, 4):i = 3
2for j in range(1, i + 1):j = 1
3print(j * i, end=" ")3
2for j in range(1, i + 1):j = 2

… the same steps continue for the remaining values.

B3.3Practice2 marks

Write the output of the following code.

for i in range(1, 5):
    for j in range(1, i + 1):
        print(j * i, end=" ")
    print()
Answer

Output:

1 
2 4 
3 6 9 
4 8 12 16 

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(1, 5):i = 1
2for j in range(1, i + 1):j = 1
3print(j * i, end=" ")1
2for j in range(1, i + 1):loop ends (no more values)
4print()⏎
1for i in range(1, 5):i = 2
2for j in range(1, i + 1):loop ends (no more values)
3print(j * i, end=" ")2
2for j in range(1, i + 1):j = 2
3print(j * i, end=" ")4
2for j in range(1, i + 1):loop ends (no more values)
4print()⏎
1for i in range(1, 5):i = 3
2for j in range(1, i + 1):j = 1
3print(j * i, end=" ")3
2for j in range(1, i + 1):j = 2

… the same steps continue for the remaining values.

B3.4Practice2 marks

Write the output of the following code.

D = {'b': 4, 'a': 18, 'e': 3, 'c': 12}
total = 0
for k in D:
    if D[k] > 4:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

a c 30

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'b': 4, 'a': 18, 'e': 3, 'c': 12}D = {'b': 4, 'a': 18, 'e': 3, 'c': 12}
2total = 0total = 0
3for k in D:k = 'b'
4if D[k] > 4:condition is False
3for k in D:k = 'a'
4if D[k] > 4:condition is True
5total += D[k]total = 18
6print(k, end=" ")a
3for k in D:k = 'e'
4if D[k] > 4:condition is False
3for k in D:k = 'c'
4if D[k] > 4:condition is True
5total += D[k]total = 30
6print(k, end=" ")c
3for k in D:loop ends (no more values)
7print(total)30⏎
B3.5Practice2 marks

Write the output of the following code.

s = "learn code daily"
W = s.split()
W.reverse()
print(" ".join(W).title())
Answer

Output:

Daily Code Learn

Working (step by step)

LineStatementValues after this stepPrinted
1s = "learn code daily"s = 'learn code daily'
2W = s.split()W = ['learn', 'code', 'daily']
3W.reverse()W = ['daily', 'code', 'learn']
4print(" ".join(W).title())Daily Code Learn⏎
B3.6Practice2 marks

Write the output of the following code.

for i in range(1, 4):
    for j in range(i):
        print(i, end="")
    print()
Answer

Output:

1
22
333

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(1, 4):i = 1
2for j in range(i):j = 0
3print(i, end="")1
2for j in range(i):loop ends (no more values)
4print()⏎
1for i in range(1, 4):i = 2
2for j in range(i):loop ends (no more values)
3print(i, end="")2
2for j in range(i):j = 1
3print(i, end="")2
2for j in range(i):loop ends (no more values)
4print()⏎
1for i in range(1, 4):i = 3
2for j in range(i):j = 0
3print(i, end="")3
2for j in range(i):j = 1

… the same steps continue for the remaining values.

B3.7Practice2 marks

Write the output of the following code.

n = 75259
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

95257

Working (step by step)

LineStatementValues after this stepPrinted
1n = 75259n = 75259
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 9
5s = s * 10 + ds = 9
6n = n // 10n = 7525
3while n > 0:condition is True
4d = n % 10d = 5
5s = s * 10 + ds = 95
6n = n // 10n = 752
3while n > 0:condition is True
4d = n % 10d = 2
5s = s * 10 + ds = 952
6n = n // 10n = 75
3while n > 0:condition is True
4d = n % 10d = 5

… the same steps continue for the remaining values.

B3.8Practice2 marks

Write the output of the following code.

s = "Learn Code Daily"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("*", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

lEARN*cODE*dAILY

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Learn Code Daily"s = 'Learn Code Daily'
2for ch in s:ch = 'L'
3if ch.isupper():condition is True
4print(ch.lower(), end="")l
2for ch in s:ch = 'e'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")E
2for ch in s:ch = 'a'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")A
2for ch in s:ch = 'r'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")R

… the same steps continue for the remaining values.

B3.9Practice2 marks

Write the output of the following code.

D = {'c': 7, 'b': 6, 'e': 4, 'a': 1}
total = 0
for k in D:
    if D[k] > 10:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

0

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'c': 7, 'b': 6, 'e': 4, 'a': 1}D = {'c': 7, 'b': 6, 'e': 4, 'a': 1}
2total = 0total = 0
3for k in D:k = 'c'
4if D[k] > 10:condition is False
3for k in D:k = 'b'
4if D[k] > 10:condition is False
3for k in D:k = 'e'
4if D[k] > 10:condition is False
3for k in D:k = 'a'
4if D[k] > 10:condition is False
3for k in D:loop ends (no more values)
7print(total)0⏎
B3.10Practice2 marks

Write the output of the following code.

D = {'b': 18, 'c': 19, 'd': 14, 'a': 15}
total = 0
for k in D:
    if D[k] > 12:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

b c d a 66

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'b': 18, 'c': 19, 'd': 14, 'a': 15}D = {'b': 18, 'c': 19, 'd': 14, 'a': 15}
2total = 0total = 0
3for k in D:k = 'b'
4if D[k] > 12:condition is True
5total += D[k]total = 18
6print(k, end=" ")b
3for k in D:k = 'c'
4if D[k] > 12:condition is True
5total += D[k]total = 37
6print(k, end=" ")c
3for k in D:k = 'd'
4if D[k] > 12:condition is True
5total += D[k]total = 51
6print(k, end=" ")d
3for k in D:k = 'a'
4if D[k] > 12:condition is True

… the same steps continue for the remaining values.

B3.11Practice2 marks

Write the output of the following code.

x, y = 7, 11
while x < y:
    x = x * 2
    y = y - 1
print(x, y)
Answer

Output:

14 10

Working (step by step)

LineStatementValues after this stepPrinted
1x, y = 7, 11x = 7, y = 11
2while x < y:condition is True
3x = x * 2x = 14
4y = y - 1y = 10
2while x < y:condition is False
5print(x, y)14 10⏎
B3.12Practice2 marks

Write the output of the following code.

L = [2, 8, 3, 3, 4, 2]
S = []
for x in L:
    if x not in S:
        S.append(x)
print(S, len(L) - len(S))
Answer

Output:

[2, 8, 3, 4] 2

Working (step by step)

LineStatementValues after this stepPrinted
1L = [2, 8, 3, 3, 4, 2]L = [2, 8, 3, 3, 4, 2]
2S = []S = []
3for x in L:x = 2
4if x not in S:condition is True
5S.append(x)S = [2]
3for x in L:x = 8
4if x not in S:condition is True
5S.append(x)S = [2, 8]
3for x in L:x = 3
4if x not in S:condition is True
5S.append(x)S = [2, 8, 3]
3for x in L:loop ends (no more values)
4if x not in S:condition is False
3for x in L:x = 4
4if x not in S:condition is True
5S.append(x)S = [2, 8, 3, 4]

… the same steps continue for the remaining values.

B3.13Practice2 marks

Write the output of the following code.

s = "KENDRIYA"
t = ""
for i in range(len(s)):
    if i % 2 == 0:
        t = t + s[i]
    else:
        t = s[i] + t
print(t)
Answer

Output:

AIDEKNRY

Working (step by step)

LineStatementValues after this stepPrinted
1s = "KENDRIYA"s = 'KENDRIYA'
2t = ""t = ''
3for i in range(len(s)):i = 0
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'K'
3for i in range(len(s)):i = 1
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'EK'
3for i in range(len(s)):i = 2
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'EKN'
3for i in range(len(s)):i = 3
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'DEKN'
3for i in range(len(s)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

B3.14Practice2 marks

Write the output of the following code.

n = 78519
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

91587

Working (step by step)

LineStatementValues after this stepPrinted
1n = 78519n = 78519
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 9
5s = s * 10 + ds = 9
6n = n // 10n = 7851
3while n > 0:condition is True
4d = n % 10d = 1
5s = s * 10 + ds = 91
6n = n // 10n = 785
3while n > 0:condition is True
4d = n % 10d = 5
5s = s * 10 + ds = 915
6n = n // 10n = 78
3while n > 0:condition is True
4d = n % 10d = 8

… the same steps continue for the remaining values.

B3.15Practice2 marks

Write the output of the following code.

D = {'d': 6, 'e': 8, 'c': 10, 'a': 13}
total = 0
for k in D:
    if D[k] > 9:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

c a 23

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'d': 6, 'e': 8, 'c': 10, 'a': 13}D = {'d': 6, 'e': 8, 'c': 10, 'a': 13}
2total = 0total = 0
3for k in D:k = 'd'
4if D[k] > 9:condition is False
3for k in D:k = 'e'
4if D[k] > 9:condition is False
3for k in D:k = 'c'
4if D[k] > 9:condition is True
5total += D[k]total = 10
6print(k, end=" ")c
3for k in D:k = 'a'
4if D[k] > 9:condition is True
5total += D[k]total = 23
6print(k, end=" ")a
3for k in D:loop ends (no more values)
7print(total)23⏎
B3.16Practice2 marks

Write the output of the following code.

x, y = 8, 21
while x < y:
    x = x * 2
    y = y - 1
print(x, y)
Answer

Output:

32 19

Working (step by step)

LineStatementValues after this stepPrinted
1x, y = 8, 21x = 8, y = 21
2while x < y:condition is True
3x = x * 2x = 16
4y = y - 1y = 20
2while x < y:condition is True
3x = x * 2x = 32
4y = y - 1y = 19
2while x < y:condition is False
5print(x, y)32 19⏎
B3.17Practice2 marks

Write the output of the following code.

for i in range(1, 5):
    print(" " * (4 - i) + "#" * i)
Answer

Output:

   #
  ##
 ###
####

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(1, 5):i = 1
2print(" " * (4 - i) + "#" * i) #⏎
1for i in range(1, 5):i = 2
2print(" " * (4 - i) + "#" * i) ##⏎
1for i in range(1, 5):i = 3
2print(" " * (4 - i) + "#" * i) ###⏎
1for i in range(1, 5):i = 4
2print(" " * (4 - i) + "#" * i)####⏎
1for i in range(1, 5):loop ends (no more values)
B3.18Practice2 marks

Write the output of the following code.

T = (29, 39, 5, 9, 38)
L = list(T)
L.append(T[1] + T[2])
L.sort()
print(L[:3], len(L))
Answer

Output:

[5, 9, 29] 6

Working (step by step)

LineStatementValues after this stepPrinted
1T = (29, 39, 5, 9, 38)T = (29, 39, 5, 9, 38)
2L = list(T)L = [29, 39, 5, 9, 38]
3L.append(T[1] + T[2])L = [29, 39, 5, 9, 38, 44]
4L.sort()L = [5, 9, 29, 38, 39, 44]
5print(L[:3], len(L))[5, 9, 29] 6⏎
B3.19Practice2 marks

Write the output of the following code.

a = 3
b = 11
if a % 2 == 0 and b % 2 == 0:
    print(a + b)
elif a % 2 == 0 or b > 20:
    print(a * b)
else:
    print(b - a)
Answer

Output:

8

Working (step by step)

LineStatementValues after this stepPrinted
1a = 3a = 3
2b = 11b = 11
3if a % 2 == 0 and b % 2 == 0:condition is False
5elif a % 2 == 0 or b > 20:condition is False
8print(b - a)8⏎
B3.20Practice2 marks

Write the output of the following code.

s = "India is Great"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("#", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

iNDIA#IS#gREAT

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "India is Great"s = 'India is Great'
2for ch in s:ch = 'I'
3if ch.isupper():condition is True
4print(ch.lower(), end="")i
2for ch in s:ch = 'n'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")N
2for ch in s:ch = 'd'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")D
2for ch in s:ch = 'i'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")I

… the same steps continue for the remaining values.

B3.21Practice2 marks

Write the output of the following code.

s = "Data and Logic"
W = s.split()
for w in W:
    print(w[0] + str(len(w)), end=" ")
Answer

Output:

D4 a3 L5 

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Data and Logic"s = 'Data and Logic'
2W = s.split()W = ['Data', 'and', 'Logic']
3for w in W:w = 'Data'
4print(w[0] + str(len(w)), end=" ")D4
3for w in W:w = 'and'
4print(w[0] + str(len(w)), end=" ")a3
3for w in W:w = 'Logic'
4print(w[0] + str(len(w)), end=" ")L5
3for w in W:loop ends (no more values)
B3.22Practice2 marks

Write the output of the following code.

s = "cbse exam 2027"
W = s.split()
W.reverse()
print(" ".join(W).title())
Answer

Output:

2027 Exam Cbse

Working (step by step)

LineStatementValues after this stepPrinted
1s = "cbse exam 2027"s = 'cbse exam 2027'
2W = s.split()W = ['cbse', 'exam', '2027']
3W.reverse()W = ['2027', 'exam', 'cbse']
4print(" ".join(W).title())2027 Exam Cbse⏎
B3.23Practice2 marks

Write the output of the following code.

L = [16, 9, 28, 24, 21, 17]
for i in range(len(L)):
    if L[i] % 2 == 0:
        L[i] = L[i] // 3
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[5, 18, 9, 8, 42, 34]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [16, 9, 28, 24, 21, 17]L = [16, 9, 28, 24, 21, 17]
2for i in range(len(L)):i = 0
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [5, 9, 28, 24, 21, 17]
2for i in range(len(L)):i = 1
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [5, 18, 28, 24, 21, 17]
2for i in range(len(L)):i = 2
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [5, 18, 9, 24, 21, 17]
2for i in range(len(L)):i = 3
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [5, 18, 9, 8, 21, 17]
2for i in range(len(L)):i = 4
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [5, 18, 9, 8, 42, 17]

… the same steps continue for the remaining values.

B3.24Practice2 marks

Write the output of the following code.

L = [20, 14, 12, 27, 10, 19]
for i in range(len(L)):
    if L[i] % 2 == 0:
        L[i] = L[i] // 3
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[6, 4, 4, 54, 3, 38]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [20, 14, 12, 27, 10, 19]L = [20, 14, 12, 27, 10, 19]
2for i in range(len(L)):i = 0
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [6, 14, 12, 27, 10, 19]
2for i in range(len(L)):i = 1
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [6, 4, 12, 27, 10, 19]
2for i in range(len(L)):i = 2
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [6, 4, 4, 27, 10, 19]
2for i in range(len(L)):i = 3
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [6, 4, 4, 54, 10, 19]
2for i in range(len(L)):i = 4
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [6, 4, 4, 54, 3, 19]

… the same steps continue for the remaining values.

B3.25Practice2 marks

Write the output of the following code.

x, y = 9, 30
while x < y:
    x = x * 2
    y = y - 1
print(x, y)
Answer

Output:

36 28

Working (step by step)

LineStatementValues after this stepPrinted
1x, y = 9, 30x = 9, y = 30
2while x < y:condition is True
3x = x * 2x = 18
4y = y - 1y = 29
2while x < y:condition is True
3x = x * 2x = 36
4y = y - 1y = 28
2while x < y:condition is False
5print(x, y)36 28⏎
B3.26Practice2 marks

Write the output of the following code.

n = 2287
s = 0
while n > 0:
    d = n % 10
    if d % 2 == 0:
        s = s + d
    n = n // 10
print(s)
Answer

Output:

12

Working (step by step)

LineStatementValues after this stepPrinted
1n = 2287n = 2287
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 7
5if d % 2 == 0:condition is False
7n = n // 10n = 228
3while n > 0:condition is True
4d = n % 10d = 8
5if d % 2 == 0:condition is True
6s = s + ds = 8
7n = n // 10n = 22
3while n > 0:condition is True
4d = n % 10d = 2
5if d % 2 == 0:condition is True
6s = s + ds = 10
7n = n // 10n = 2

… the same steps continue for the remaining values.

B3.27Practice2 marks

Write the output of the following code.

L = [26, 10, 27, 19, 23, 17]
for i in range(len(L)):
    if L[i] % 5 == 0:
        L[i] = L[i] // 3
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[52, 3, 54, 38, 46, 34]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [26, 10, 27, 19, 23, 17]L = [26, 10, 27, 19, 23, 17]
2for i in range(len(L)):i = 0
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 10, 27, 19, 23, 17]
2for i in range(len(L)):i = 1
3if L[i] % 5 == 0:condition is True
4L[i] = L[i] // 3L = [52, 3, 27, 19, 23, 17]
2for i in range(len(L)):i = 2
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 3, 54, 19, 23, 17]
2for i in range(len(L)):i = 3
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 3, 54, 38, 23, 17]
2for i in range(len(L)):i = 4
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 3, 54, 38, 46, 17]

… the same steps continue for the remaining values.

B3.28Practice2 marks

Write the output of the following code.

D = {'b': 10, 'e': 18, 'a': 19, 'd': 12}
total = 0
for k in D:
    if D[k] > 7:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

b e a d 59

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'b': 10, 'e': 18, 'a': 19, 'd': 12}D = {'b': 10, 'e': 18, 'a': 19, 'd': 12}
2total = 0total = 0
3for k in D:k = 'b'
4if D[k] > 7:condition is True
5total += D[k]total = 10
6print(k, end=" ")b
3for k in D:k = 'e'
4if D[k] > 7:condition is True
5total += D[k]total = 28
6print(k, end=" ")e
3for k in D:k = 'a'
4if D[k] > 7:condition is True
5total += D[k]total = 47
6print(k, end=" ")a
3for k in D:k = 'd'
4if D[k] > 7:condition is True

… the same steps continue for the remaining values.

B3.29Practice2 marks

Write the output of the following code.

s = "COMPUTER"
print(s.count("M"), s.find("ER"), s[2:6].upper())
Answer

Output:

1 6 MPUT

Working (step by step)

LineStatementValues after this stepPrinted
1s = "COMPUTER"s = 'COMPUTER'
2print(s.count("M"), s.find("ER"), s[2:6].upper())1 6 MPUT⏎
B3.30Practice2 marks

Write the output of the following code.

s = "Program"
t = ""
for i in range(len(s)):
    if i % 2 == 0:
        t = t + s[i]
    else:
        t = s[i] + t
print(t)
Answer

Output:

agrPorm

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Program"s = 'Program'
2t = ""t = ''
3for i in range(len(s)):i = 0
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'P'
3for i in range(len(s)):i = 1
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'rP'
3for i in range(len(s)):i = 2
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'rPo'
3for i in range(len(s)):i = 3
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'grPo'
3for i in range(len(s)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

B3.31Practice2 marks

Write the output of the following code.

s = "Monitor"
print(s[1:5], s[-3:], s[::3])
Answer

Output:

onit tor Mir

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Monitor"s = 'Monitor'
2print(s[1:5], s[-3:], s[::3])onit tor Mir⏎
B3.32Practice2 marks

Write the output of the following code.

L = [3, 9, 3, 1, 9, 9]
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print(F)
Answer

Output:

{3: 2, 9: 3, 1: 1}

Working (step by step)

LineStatementValues after this stepPrinted
1L = [3, 9, 3, 1, 9, 9]L = [3, 9, 3, 1, 9, 9]
2F = {}F = {}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {3: 1}
3for x in L:x = 9
4F[x] = F.get(x, 0) + 1F = {3: 1, 9: 1}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {3: 2, 9: 1}
3for x in L:x = 1
4F[x] = F.get(x, 0) + 1F = {3: 2, 9: 1, 1: 1}
3for x in L:x = 9
4F[x] = F.get(x, 0) + 1F = {3: 2, 9: 2, 1: 1}
3for x in L:loop ends (no more values)
4F[x] = F.get(x, 0) + 1F = {3: 2, 9: 3, 1: 1}
3for x in L:loop ends (no more values)
5print(F){3: 2, 9: 3, 1: 1}⏎
B3.33Practice2 marks

Write the output of the following code.

s = "COMPUTER"
t = ""
for i in range(len(s)):
    if i % 2 == 0:
        t = t + s[i]
    else:
        t = s[i] + t
print(t)
Answer

Output:

RTPOCMUE

Working (step by step)

LineStatementValues after this stepPrinted
1s = "COMPUTER"s = 'COMPUTER'
2t = ""t = ''
3for i in range(len(s)):i = 0
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'C'
3for i in range(len(s)):i = 1
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'OC'
3for i in range(len(s)):i = 2
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'OCM'
3for i in range(len(s)):i = 3
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'POCM'
3for i in range(len(s)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

B3.34Practice2 marks

Write the output of the following code.

x, y = 4, 25
while x < y:
    x = x * 2
    y = y - 1
print(x, y)
Answer

Output:

32 22

Working (step by step)

LineStatementValues after this stepPrinted
1x, y = 4, 25x = 4, y = 25
2while x < y:condition is True
3x = x * 2x = 8
4y = y - 1y = 24
2while x < y:condition is True
3x = x * 2x = 16
4y = y - 1y = 23
2while x < y:condition is True
3x = x * 2x = 32
4y = y - 1y = 22
2while x < y:condition is False
5print(x, y)32 22⏎
B3.35Practice2 marks

Write the output of the following code.

L = [3, 21, 19, 10, 15, 27]
for i in range(len(L)):
    if L[i] % 2 == 0:
        L[i] = L[i] // 3
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[6, 42, 38, 3, 30, 54]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [3, 21, 19, 10, 15, 27]L = [3, 21, 19, 10, 15, 27]
2for i in range(len(L)):i = 0
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [6, 21, 19, 10, 15, 27]
2for i in range(len(L)):i = 1
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [6, 42, 19, 10, 15, 27]
2for i in range(len(L)):i = 2
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [6, 42, 38, 10, 15, 27]
2for i in range(len(L)):i = 3
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [6, 42, 38, 3, 15, 27]
2for i in range(len(L)):i = 4
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [6, 42, 38, 3, 30, 27]

… the same steps continue for the remaining values.

B3.36Practice2 marks

Write the output of the following code.

s = "CBSE Exam 2027"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("-", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

cbse-eXAM-2027

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "CBSE Exam 2027"s = 'CBSE Exam 2027'
2for ch in s:ch = 'C'
3if ch.isupper():condition is True
4print(ch.lower(), end="")c
2for ch in s:ch = 'B'
3if ch.isupper():condition is True
4print(ch.lower(), end="")b
2for ch in s:ch = 'S'
3if ch.isupper():condition is True
4print(ch.lower(), end="")s
2for ch in s:ch = 'E'
3if ch.isupper():condition is True
4print(ch.lower(), end="")e
2for ch in s:ch = ' '
3if ch.isupper():condition is False
5elif ch == " ":condition is True

… the same steps continue for the remaining values.

B3.37Practice2 marks

Write the output of the following code.

T = (25, 34, 10, 2, 27)
print(T[1:3] + T[-1:], T.index(2))
Answer

Output:

(34, 10, 27) 3

Working (step by step)

LineStatementValues after this stepPrinted
1T = (25, 34, 10, 2, 27)T = (25, 34, 10, 2, 27)
2print(T[1:3] + T[-1:], T.index(2))(34, 10, 27) 3⏎
B3.38Practice2 marks

Write the output of the following code.

D = {'c': 13, 'd': 15, 'e': 14, 'a': 19}
total = 0
for k in D:
    if D[k] > 5:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

c d e a 61

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'c': 13, 'd': 15, 'e': 14, 'a': 19}D = {'c': 13, 'd': 15, 'e': 14, 'a': 19}
2total = 0total = 0
3for k in D:k = 'c'
4if D[k] > 5:condition is True
5total += D[k]total = 13
6print(k, end=" ")c
3for k in D:k = 'd'
4if D[k] > 5:condition is True
5total += D[k]total = 28
6print(k, end=" ")d
3for k in D:k = 'e'
4if D[k] > 5:condition is True
5total += D[k]total = 42
6print(k, end=" ")e
3for k in D:k = 'a'
4if D[k] > 5:condition is True

… the same steps continue for the remaining values.

B3.39Practice2 marks

Write the output of the following code.

n = 17629
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

92671

Working (step by step)

LineStatementValues after this stepPrinted
1n = 17629n = 17629
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 9
5s = s * 10 + ds = 9
6n = n // 10n = 1762
3while n > 0:condition is True
4d = n % 10d = 2
5s = s * 10 + ds = 92
6n = n // 10n = 176
3while n > 0:condition is True
4d = n % 10d = 6
5s = s * 10 + ds = 926
6n = n // 10n = 17
3while n > 0:condition is True
4d = n % 10d = 7

… the same steps continue for the remaining values.

B3.40Practice2 marks

Write the output of the following code.

n = 66407
s = 0
while n > 0:
    d = n % 10
    if d % 2 == 0:
        s = s + d
    n = n // 10
print(s)
Answer

Output:

16

Working (step by step)

LineStatementValues after this stepPrinted
1n = 66407n = 66407
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 7
5if d % 2 == 0:condition is False
7n = n // 10n = 6640
3while n > 0:condition is True
4d = n % 10d = 0
5if d % 2 == 0:condition is True
7n = n // 10n = 664
3while n > 0:condition is True
4d = n % 10d = 4
5if d % 2 == 0:condition is True
6s = s + ds = 4
7n = n // 10n = 66
3while n > 0:condition is True

… the same steps continue for the remaining values.

B3.41Practice2 marks

Write the output of the following code.

D = {'e': 9, 'd': 11, 'a': 16, 'c': 15}
total = 0
for k in D:
    if D[k] > 7:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

e d a c 51

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'e': 9, 'd': 11, 'a': 16, 'c': 15}D = {'e': 9, 'd': 11, 'a': 16, 'c': 15}
2total = 0total = 0
3for k in D:k = 'e'
4if D[k] > 7:condition is True
5total += D[k]total = 9
6print(k, end=" ")e
3for k in D:k = 'd'
4if D[k] > 7:condition is True
5total += D[k]total = 20
6print(k, end=" ")d
3for k in D:k = 'a'
4if D[k] > 7:condition is True
5total += D[k]total = 36
6print(k, end=" ")a
3for k in D:k = 'c'
4if D[k] > 7:condition is True

… the same steps continue for the remaining values.

B3.42Practice2 marks

Write the output of the following code.

T = (47, 44, 37, 43, 33)
L = list(T)
L.append(T[1] + T[2])
L.sort()
print(L[:3], len(L))
Answer

Output:

[33, 37, 43] 6

Working (step by step)

LineStatementValues after this stepPrinted
1T = (47, 44, 37, 43, 33)T = (47, 44, 37, 43, 33)
2L = list(T)L = [47, 44, 37, 43, 33]
3L.append(T[1] + T[2])L = [47, 44, 37, 43, 33, 81]
4L.sort()L = [33, 37, 43, 44, 47, 81]
5print(L[:3], len(L))[33, 37, 43] 6⏎
B3.43Practice2 marks

Write the output of the following code.

D = {'d': 16, 'b': 12, 'a': 4, 'e': 9}
total = 0
for k in D:
    if D[k] > 5:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

d b e 37

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'d': 16, 'b': 12, 'a': 4, 'e': 9}D = {'d': 16, 'b': 12, 'a': 4, 'e': 9}
2total = 0total = 0
3for k in D:k = 'd'
4if D[k] > 5:condition is True
5total += D[k]total = 16
6print(k, end=" ")d
3for k in D:k = 'b'
4if D[k] > 5:condition is True
5total += D[k]total = 28
6print(k, end=" ")b
3for k in D:k = 'a'
4if D[k] > 5:condition is False
3for k in D:k = 'e'
4if D[k] > 5:condition is True
5total += D[k]total = 37
6print(k, end=" ")e
3for k in D:loop ends (no more values)
7print(total)37⏎
B3.44Practice2 marks

Write the output of the following code.

T = (47, 20, 32, 7, 4)
print(T[1:3] + T[-1:], T.index(7))
Answer

Output:

(20, 32, 4) 3

Working (step by step)

LineStatementValues after this stepPrinted
1T = (47, 20, 32, 7, 4)T = (47, 20, 32, 7, 4)
2print(T[1:3] + T[-1:], T.index(7))(20, 32, 4) 3⏎
B3.45Practice2 marks

Write the output of the following code.

for i in range(3, 0, -1):
    print("*" * i)
Answer

Output:

***
**
*

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(3, 0, -1):i = 3
2print("*" * i)***⏎
1for i in range(3, 0, -1):i = 2
2print("*" * i)**⏎
1for i in range(3, 0, -1):i = 1
2print("*" * i)*⏎
1for i in range(3, 0, -1):loop ends (no more values)
B3.46Practice2 marks

Write the output of the following code.

a = 4
b = 18
if a % 2 == 0 and b % 2 == 0:
    print(a + b)
elif a % 2 == 0 or b > 20:
    print(a * b)
else:
    print(b - a)
Answer

Output:

22

Working (step by step)

LineStatementValues after this stepPrinted
1a = 4a = 4
2b = 18b = 18
3if a % 2 == 0 and b % 2 == 0:condition is True
4print(a + b)22⏎
B3.47Practice2 marks

Write the output of the following code.

n = 76794
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

49767

Working (step by step)

LineStatementValues after this stepPrinted
1n = 76794n = 76794
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 4
5s = s * 10 + ds = 4
6n = n // 10n = 7679
3while n > 0:condition is True
4d = n % 10d = 9
5s = s * 10 + ds = 49
6n = n // 10n = 767
3while n > 0:condition is True
4d = n % 10d = 7
5s = s * 10 + ds = 497
6n = n // 10n = 76
3while n > 0:condition is True
4d = n % 10d = 6

… the same steps continue for the remaining values.

B3.48Practice2 marks

Write the output of the following code.

T = (11, 36, 34, 33, 43)
L = list(T)
L.append(T[1] + T[2])
L.sort()
print(L[:3], len(L))
Answer

Output:

[11, 33, 34] 6

Working (step by step)

LineStatementValues after this stepPrinted
1T = (11, 36, 34, 33, 43)T = (11, 36, 34, 33, 43)
2L = list(T)L = [11, 36, 34, 33, 43]
3L.append(T[1] + T[2])L = [11, 36, 34, 33, 43, 70]
4L.sort()L = [11, 33, 34, 36, 43, 70]
5print(L[:3], len(L))[11, 33, 34] 6⏎
B3.49Practice2 marks

Write the output of the following code.

a = 9
b = 10
if a % 2 == 0 and b % 2 == 0:
    print(a + b)
elif a % 2 == 0 or b > 20:
    print(a * b)
else:
    print(b - a)
Answer

Output:

1

Working (step by step)

LineStatementValues after this stepPrinted
1a = 9a = 9
2b = 10b = 10
3if a % 2 == 0 and b % 2 == 0:condition is False
5elif a % 2 == 0 or b > 20:condition is False
8print(b - a)1⏎
B3.50Practice2 marks

Write the output of the following code.

s = "Science"
t = ""
for i in range(len(s)):
    if i % 2 == 0:
        t = t + s[i]
    else:
        t = s[i] + t
print(t)
Answer

Output:

cecSine

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Science"s = 'Science'
2t = ""t = ''
3for i in range(len(s)):i = 0
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'S'
3for i in range(len(s)):i = 1
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'cS'
3for i in range(len(s)):i = 2
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'cSi'
3for i in range(len(s)):i = 3
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'ecSi'
3for i in range(len(s)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

B3.51Practice2 marks

Write the output of the following code.

n = 22202
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

20222

Working (step by step)

LineStatementValues after this stepPrinted
1n = 22202n = 22202
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 2
5s = s * 10 + ds = 2
6n = n // 10n = 2220
3while n > 0:condition is True
4d = n % 10d = 0
5s = s * 10 + ds = 20
6n = n // 10n = 222
3while n > 0:condition is True
4d = n % 10d = 2
5s = s * 10 + ds = 202
6n = n // 10n = 22
3while n > 0:condition is True
5s = s * 10 + ds = 2022

… the same steps continue for the remaining values.

B3.52Practice2 marks

Write the output of the following code.

L = [2, 7, 3, 2, 7, 9]
S = []
for x in L:
    if x not in S:
        S.append(x)
print(S, len(L) - len(S))
Answer

Output:

[2, 7, 3, 9] 2

Working (step by step)

LineStatementValues after this stepPrinted
1L = [2, 7, 3, 2, 7, 9]L = [2, 7, 3, 2, 7, 9]
2S = []S = []
3for x in L:x = 2
4if x not in S:condition is True
5S.append(x)S = [2]
3for x in L:x = 7
4if x not in S:condition is True
5S.append(x)S = [2, 7]
3for x in L:x = 3
4if x not in S:condition is True
5S.append(x)S = [2, 7, 3]
3for x in L:x = 2
4if x not in S:condition is False
3for x in L:x = 7
4if x not in S:condition is False
3for x in L:x = 9

… the same steps continue for the remaining values.

B3.53Practice2 marks

Write the output of the following code.

n = 73711
s = 0
while n > 0:
    d = n % 10
    if d % 2 == 0:
        s = s + d
    n = n // 10
print(s)
Answer

Output:

0

Working (step by step)

LineStatementValues after this stepPrinted
1n = 73711n = 73711
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 1
5if d % 2 == 0:condition is False
7n = n // 10n = 7371
3while n > 0:condition is True
5if d % 2 == 0:condition is False
7n = n // 10n = 737
3while n > 0:condition is True
4d = n % 10d = 7
5if d % 2 == 0:condition is False
7n = n // 10n = 73
3while n > 0:condition is True
4d = n % 10d = 3
5if d % 2 == 0:condition is False

… the same steps continue for the remaining values.

B3.54Practice2 marks

Write the output of the following code.

n = 60528
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

82506

Working (step by step)

LineStatementValues after this stepPrinted
1n = 60528n = 60528
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 8
5s = s * 10 + ds = 8
6n = n // 10n = 6052
3while n > 0:condition is True
4d = n % 10d = 2
5s = s * 10 + ds = 82
6n = n // 10n = 605
3while n > 0:condition is True
4d = n % 10d = 5
5s = s * 10 + ds = 825
6n = n // 10n = 60
3while n > 0:condition is True
4d = n % 10d = 0

… the same steps continue for the remaining values.

B3.55Practice2 marks

Write the output of the following code.

n = 27491
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

19472

Working (step by step)

LineStatementValues after this stepPrinted
1n = 27491n = 27491
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 1
5s = s * 10 + ds = 1
6n = n // 10n = 2749
3while n > 0:condition is True
4d = n % 10d = 9
5s = s * 10 + ds = 19
6n = n // 10n = 274
3while n > 0:condition is True
4d = n % 10d = 4
5s = s * 10 + ds = 194
6n = n // 10n = 27
3while n > 0:condition is True
4d = n % 10d = 7

… the same steps continue for the remaining values.

B3.56Practice2 marks

Write the output of the following code.

for i in range(1, 6):
    for j in range(i):
        print(i, end="")
    print()
Answer

Output:

1
22
333
4444
55555

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(1, 6):i = 1
2for j in range(i):j = 0
3print(i, end="")1
2for j in range(i):loop ends (no more values)
4print()⏎
1for i in range(1, 6):i = 2
2for j in range(i):loop ends (no more values)
3print(i, end="")2
2for j in range(i):j = 1
3print(i, end="")2
2for j in range(i):loop ends (no more values)
4print()⏎
1for i in range(1, 6):i = 3
2for j in range(i):j = 0
3print(i, end="")3
2for j in range(i):j = 1

… the same steps continue for the remaining values.

B3.57Practice2 marks

Write the output of the following code.

L = [25, 2, 8, 12, 4, 13]
for i in range(len(L)):
    if L[i] % 2 == 0:
        L[i] = L[i] // 2
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[50, 1, 4, 6, 2, 26]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [25, 2, 8, 12, 4, 13]L = [25, 2, 8, 12, 4, 13]
2for i in range(len(L)):i = 0
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [50, 2, 8, 12, 4, 13]
2for i in range(len(L)):i = 1
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 2L = [50, 1, 8, 12, 4, 13]
2for i in range(len(L)):i = 2
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 2L = [50, 1, 4, 12, 4, 13]
2for i in range(len(L)):i = 3
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 2L = [50, 1, 4, 6, 4, 13]
2for i in range(len(L)):i = 4
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 2L = [50, 1, 4, 6, 2, 13]

… the same steps continue for the remaining values.

B3.58Practice2 marks

Write the output of the following code.

s = "Monitor"
print(s.count("n"), s.find("or"), s[2:6].upper())
Answer

Output:

1 5 NITO

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Monitor"s = 'Monitor'
2print(s.count("n"), s.find("or"), s[2:6].upper())1 5 NITO⏎
B3.59Practice2 marks

Write the output of the following code.

s = "KV Shalimar Bagh"
W = s.split()
for w in W:
    print(w[0] + str(len(w)), end=" ")
Answer

Output:

K2 S8 B4 

Working (step by step)

LineStatementValues after this stepPrinted
1s = "KV Shalimar Bagh"s = 'KV Shalimar Bagh'
2W = s.split()W = ['KV', 'Shalimar', 'Bagh']
3for w in W:w = 'KV'
4print(w[0] + str(len(w)), end=" ")K2
3for w in W:w = 'Shalimar'
4print(w[0] + str(len(w)), end=" ")S8
3for w in W:w = 'Bagh'
4print(w[0] + str(len(w)), end=" ")B4
3for w in W:loop ends (no more values)
B3.60Practice2 marks

Write the output of the following code.

s = "india is great"
W = s.split()
W.reverse()
print(" ".join(W).title())
Answer

Output:

Great Is India

Working (step by step)

LineStatementValues after this stepPrinted
1s = "india is great"s = 'india is great'
2W = s.split()W = ['india', 'is', 'great']
3W.reverse()W = ['great', 'is', 'india']
4print(" ".join(W).title())Great Is India⏎
B3.61Practice2 marks

Write the output of the following code.

L = [9, 5, 3, 4, 3, 7]
S = []
for x in L:
    if x not in S:
        S.append(x)
print(S, len(L) - len(S))
Answer

Output:

[9, 5, 3, 4, 7] 1

Working (step by step)

LineStatementValues after this stepPrinted
1L = [9, 5, 3, 4, 3, 7]L = [9, 5, 3, 4, 3, 7]
2S = []S = []
3for x in L:x = 9
4if x not in S:condition is True
5S.append(x)S = [9]
3for x in L:x = 5
4if x not in S:condition is True
5S.append(x)S = [9, 5]
3for x in L:x = 3
4if x not in S:condition is True
5S.append(x)S = [9, 5, 3]
3for x in L:x = 4
4if x not in S:condition is True
5S.append(x)S = [9, 5, 3, 4]
3for x in L:x = 3
4if x not in S:condition is False

… the same steps continue for the remaining values.

B3.62Practice2 marks

Write the output of the following code.

L = [21, 7, 16, 20, 14, 18]
for i in range(len(L)):
    if L[i] % 2 == 0:
        L[i] = L[i] // 3
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[42, 14, 5, 6, 4, 6]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [21, 7, 16, 20, 14, 18]L = [21, 7, 16, 20, 14, 18]
2for i in range(len(L)):i = 0
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [42, 7, 16, 20, 14, 18]
2for i in range(len(L)):i = 1
3if L[i] % 2 == 0:condition is False
6L[i] = L[i] * 2L = [42, 14, 16, 20, 14, 18]
2for i in range(len(L)):i = 2
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [42, 14, 5, 20, 14, 18]
2for i in range(len(L)):i = 3
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [42, 14, 5, 6, 14, 18]
2for i in range(len(L)):i = 4
3if L[i] % 2 == 0:condition is True
4L[i] = L[i] // 3L = [42, 14, 5, 6, 4, 18]

… the same steps continue for the remaining values.

B3.63Practice2 marks

Write the output of the following code.

n = 2194
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

4912

Working (step by step)

LineStatementValues after this stepPrinted
1n = 2194n = 2194
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 4
5s = s * 10 + ds = 4
6n = n // 10n = 219
3while n > 0:condition is True
4d = n % 10d = 9
5s = s * 10 + ds = 49
6n = n // 10n = 21
3while n > 0:condition is True
4d = n % 10d = 1
5s = s * 10 + ds = 491
6n = n // 10n = 2
3while n > 0:condition is True
4d = n % 10d = 2

… the same steps continue for the remaining values.

B3.64Practice2 marks

Write the output of the following code.

s = "NOTEBOOK"
print(s.count("T"), s.find("OK"), s[2:6].upper())
Answer

Output:

1 6 TEBO

Working (step by step)

LineStatementValues after this stepPrinted
1s = "NOTEBOOK"s = 'NOTEBOOK'
2print(s.count("T"), s.find("OK"), s[2:6].upper())1 6 TEBO⏎
B3.65Practice2 marks

Write the output of the following code.

T = (7, 12, 4, 40, 1)
L = list(T)
L.append(T[1] + T[2])
L.sort()
print(L[:3], len(L))
Answer

Output:

[1, 4, 7] 6

Working (step by step)

LineStatementValues after this stepPrinted
1T = (7, 12, 4, 40, 1)T = (7, 12, 4, 40, 1)
2L = list(T)L = [7, 12, 4, 40, 1]
3L.append(T[1] + T[2])L = [7, 12, 4, 40, 1, 16]
4L.sort()L = [1, 4, 7, 12, 16, 40]
5print(L[:3], len(L))[1, 4, 7] 6⏎
B3.66Practice2 marks

Write the output of the following code.

D = {'a': 13, 'c': 2, 'e': 11, 'd': 18}
total = 0
for k in D:
    if D[k] > 12:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

a d 31

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'a': 13, 'c': 2, 'e': 11, 'd': 18}D = {'a': 13, 'c': 2, 'e': 11, 'd': 18}
2total = 0total = 0
3for k in D:k = 'a'
4if D[k] > 12:condition is True
5total += D[k]total = 13
6print(k, end=" ")a
3for k in D:k = 'c'
4if D[k] > 12:condition is False
3for k in D:k = 'e'
4if D[k] > 12:condition is False
3for k in D:k = 'd'
4if D[k] > 12:condition is True
5total += D[k]total = 31
6print(k, end=" ")d
3for k in D:loop ends (no more values)
7print(total)31⏎
B3.67Practice2 marks

Write the output of the following code.

s = "Python is Easy"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("#", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

pYTHON#IS#eASY

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Python is Easy"s = 'Python is Easy'
2for ch in s:ch = 'P'
3if ch.isupper():condition is True
4print(ch.lower(), end="")p
2for ch in s:ch = 'y'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")Y
2for ch in s:ch = 't'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")T
2for ch in s:ch = 'h'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")H

… the same steps continue for the remaining values.

B3.68Practice2 marks

Write the output of the following code.

L = [6, 3, 3, 8, 2, 3]
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print(F)
Answer

Output:

{6: 1, 3: 3, 8: 1, 2: 1}

Working (step by step)

LineStatementValues after this stepPrinted
1L = [6, 3, 3, 8, 2, 3]L = [6, 3, 3, 8, 2, 3]
2F = {}F = {}
3for x in L:x = 6
4F[x] = F.get(x, 0) + 1F = {6: 1}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {6: 1, 3: 1}
3for x in L:loop ends (no more values)
4F[x] = F.get(x, 0) + 1F = {6: 1, 3: 2}
3for x in L:x = 8
4F[x] = F.get(x, 0) + 1F = {6: 1, 3: 2, 8: 1}
3for x in L:x = 2
4F[x] = F.get(x, 0) + 1F = {6: 1, 3: 2, 8: 1, 2: 1}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {6: 1, 3: 3, 8: 1, 2: 1}
3for x in L:loop ends (no more values)
5print(F){6: 1, 3: 3, 8: 1, 2: 1}⏎
B3.69Practice2 marks

Write the output of the following code.

s = "India is Great"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("@", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

iNDIA@IS@gREAT

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "India is Great"s = 'India is Great'
2for ch in s:ch = 'I'
3if ch.isupper():condition is True
4print(ch.lower(), end="")i
2for ch in s:ch = 'n'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")N
2for ch in s:ch = 'd'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")D
2for ch in s:ch = 'i'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")I

… the same steps continue for the remaining values.

B3.70Practice2 marks

Write the output of the following code.

s = "Python"
t = ""
for i in range(len(s)):
    if i % 2 == 0:
        t = t + s[i]
    else:
        t = s[i] + t
print(t)
Answer

Output:

nhyPto

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Python"s = 'Python'
2t = ""t = ''
3for i in range(len(s)):i = 0
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'P'
3for i in range(len(s)):i = 1
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'yP'
3for i in range(len(s)):i = 2
4if i % 2 == 0:condition is True
5t = t + s[i]t = 'yPt'
3for i in range(len(s)):i = 3
4if i % 2 == 0:condition is False
7t = s[i] + tt = 'hyPt'
3for i in range(len(s)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

B3.71Practice2 marks

Write the output of the following code.

s = "India is Great"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("*", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

iNDIA*IS*gREAT

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "India is Great"s = 'India is Great'
2for ch in s:ch = 'I'
3if ch.isupper():condition is True
4print(ch.lower(), end="")i
2for ch in s:ch = 'n'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")N
2for ch in s:ch = 'd'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")D
2for ch in s:ch = 'i'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")I

… the same steps continue for the remaining values.

B3.72Practice2 marks

Write the output of the following code.

s = "keep calm and code"
W = s.split()
W.reverse()
print(" ".join(W).title())
Answer

Output:

Code And Calm Keep

Working (step by step)

LineStatementValues after this stepPrinted
1s = "keep calm and code"s = 'keep calm and code'
2W = s.split()W = ['keep', 'calm', 'and', 'code']
3W.reverse()W = ['code', 'and', 'calm', 'keep']
4print(" ".join(W).title())Code And Calm Keep⏎
B3.73Practice2 marks

Write the output of the following code.

s = "CBSE Exam 2027"
W = s.split()
for w in W:
    print(w[0] + str(len(w)), end=" ")
Answer

Output:

C4 E4 24 

Working (step by step)

LineStatementValues after this stepPrinted
1s = "CBSE Exam 2027"s = 'CBSE Exam 2027'
2W = s.split()W = ['CBSE', 'Exam', '2027']
3for w in W:w = 'CBSE'
4print(w[0] + str(len(w)), end=" ")C4
3for w in W:w = 'Exam'
4print(w[0] + str(len(w)), end=" ")E4
3for w in W:w = '2027'
4print(w[0] + str(len(w)), end=" ")24
3for w in W:loop ends (no more values)
B3.74Practice2 marks

Write the output of the following code.

L = [6, 6, 4, 3, 7, 8]
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print(F)
Answer

Output:

{6: 2, 4: 1, 3: 1, 7: 1, 8: 1}

Working (step by step)

LineStatementValues after this stepPrinted
1L = [6, 6, 4, 3, 7, 8]L = [6, 6, 4, 3, 7, 8]
2F = {}F = {}
3for x in L:x = 6
4F[x] = F.get(x, 0) + 1F = {6: 1}
3for x in L:loop ends (no more values)
4F[x] = F.get(x, 0) + 1F = {6: 2}
3for x in L:x = 4
4F[x] = F.get(x, 0) + 1F = {6: 2, 4: 1}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {6: 2, 4: 1, 3: 1}
3for x in L:x = 7
4F[x] = F.get(x, 0) + 1F = {6: 2, 4: 1, 3: 1, 7: 1}
3for x in L:x = 8
4F[x] = F.get(x, 0) + 1F = {6: 2, 4: 1, 3: 1, 7: 1, 8: 1}
3for x in L:loop ends (no more values)
5print(F){6: 2, 4: 1, 3: 1, 7: 1, 8: 1}⏎
B3.75Practice2 marks

Write the output of the following code.

s = "Learn Code Daily"
c = 0
for w in s.split():
    if w[0] in "AEIOUaeiou":
        c += 1
print(c, len(s.split()))
Answer

Output:

0 3

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Learn Code Daily"s = 'Learn Code Daily'
2c = 0c = 0
3for w in s.split():w = 'Learn'
4if w[0] in "AEIOUaeiou":condition is False
3for w in s.split():w = 'Code'
4if w[0] in "AEIOUaeiou":condition is False
3for w in s.split():w = 'Daily'
4if w[0] in "AEIOUaeiou":condition is False
3for w in s.split():loop ends (no more values)
6print(c, len(s.split()))0 3⏎
B3.76Practice2 marks

Write the output of the following code.

s = "Keep Calm and Code"
c = 0
for w in s.split():
    if w[0] in "AEIOUaeiou":
        c += 1
print(c, len(s.split()))
Answer

Output:

1 4

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Keep Calm and Code"s = 'Keep Calm and Code'
2c = 0c = 0
3for w in s.split():w = 'Keep'
4if w[0] in "AEIOUaeiou":condition is False
3for w in s.split():w = 'Calm'
4if w[0] in "AEIOUaeiou":condition is False
3for w in s.split():w = 'and'
4if w[0] in "AEIOUaeiou":condition is True
5c += 1c = 1
3for w in s.split():w = 'Code'
4if w[0] in "AEIOUaeiou":condition is False
3for w in s.split():loop ends (no more values)
6print(c, len(s.split()))1 4⏎
B3.77Practice2 marks

Write the output of the following code.

T = (45, 27, 44, 25, 29)
L = list(T)
L.append(T[1] + T[2])
L.sort()
print(L[:3], len(L))
Answer

Output:

[25, 27, 29] 6

Working (step by step)

LineStatementValues after this stepPrinted
1T = (45, 27, 44, 25, 29)T = (45, 27, 44, 25, 29)
2L = list(T)L = [45, 27, 44, 25, 29]
3L.append(T[1] + T[2])L = [45, 27, 44, 25, 29, 71]
4L.sort()L = [25, 27, 29, 44, 45, 71]
5print(L[:3], len(L))[25, 27, 29] 6⏎
B3.78Practice2 marks

Write the output of the following code.

s = "KV Shalimar Bagh"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("-", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

kv-sHALIMAR-bAGH

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "KV Shalimar Bagh"s = 'KV Shalimar Bagh'
2for ch in s:ch = 'K'
3if ch.isupper():condition is True
4print(ch.lower(), end="")k
2for ch in s:ch = 'V'
3if ch.isupper():condition is True
4print(ch.lower(), end="")v
2for ch in s:ch = ' '
3if ch.isupper():condition is False
5elif ch == " ":condition is True
6print("-", end="")-
2for ch in s:ch = 'S'
3if ch.isupper():condition is True
4print(ch.lower(), end="")s
2for ch in s:ch = 'h'
3if ch.isupper():condition is False

… the same steps continue for the remaining values.

B3.79Practice2 marks

Write the output of the following code.

s = "keyboard"
print(s.count("y"), s.find("rd"), s[2:6].upper())
Answer

Output:

1 6 YBOA

Working (step by step)

LineStatementValues after this stepPrinted
1s = "keyboard"s = 'keyboard'
2print(s.count("y"), s.find("rd"), s[2:6].upper())1 6 YBOA⏎
B3.80Practice2 marks

Write the output of the following code.

x, y = 6, 18
while x < y:
    x = x * 2
    y = y - 1
print(x, y)
Answer

Output:

24 16

Working (step by step)

LineStatementValues after this stepPrinted
1x, y = 6, 18x = 6, y = 18
2while x < y:condition is True
3x = x * 2x = 12
4y = y - 1y = 17
2while x < y:condition is True
3x = x * 2x = 24
4y = y - 1y = 16
2while x < y:condition is False
5print(x, y)24 16⏎
B3.81Practice2 marks

Write the output of the following code.

L = [8, 7, 1, 2, 4, 7]
S = []
for x in L:
    if x not in S:
        S.append(x)
print(S, len(L) - len(S))
Answer

Output:

[8, 7, 1, 2, 4] 1

Working (step by step)

LineStatementValues after this stepPrinted
1L = [8, 7, 1, 2, 4, 7]L = [8, 7, 1, 2, 4, 7]
2S = []S = []
3for x in L:x = 8
4if x not in S:condition is True
5S.append(x)S = [8]
3for x in L:x = 7
4if x not in S:condition is True
5S.append(x)S = [8, 7]
3for x in L:x = 1
4if x not in S:condition is True
5S.append(x)S = [8, 7, 1]
3for x in L:x = 2
4if x not in S:condition is True
5S.append(x)S = [8, 7, 1, 2]
3for x in L:x = 4
4if x not in S:condition is True

… the same steps continue for the remaining values.

B3.82Practice2 marks

Write the output of the following code.

L = [3, 5, 6, 3, 1, 2]
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print(F)
Answer

Output:

{3: 2, 5: 1, 6: 1, 1: 1, 2: 1}

Working (step by step)

LineStatementValues after this stepPrinted
1L = [3, 5, 6, 3, 1, 2]L = [3, 5, 6, 3, 1, 2]
2F = {}F = {}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {3: 1}
3for x in L:x = 5
4F[x] = F.get(x, 0) + 1F = {3: 1, 5: 1}
3for x in L:x = 6
4F[x] = F.get(x, 0) + 1F = {3: 1, 5: 1, 6: 1}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {3: 2, 5: 1, 6: 1}
3for x in L:x = 1
4F[x] = F.get(x, 0) + 1F = {3: 2, 5: 1, 6: 1, 1: 1}
3for x in L:x = 2
4F[x] = F.get(x, 0) + 1F = {3: 2, 5: 1, 6: 1, 1: 1, 2: 1}
3for x in L:loop ends (no more values)
5print(F){3: 2, 5: 1, 6: 1, 1: 1, 2: 1}⏎
B3.83Practice2 marks

Write the output of the following code.

T = (48, 20, 26, 27, 31)
L = list(T)
L.append(T[1] + T[2])
L.sort()
print(L[:3], len(L))
Answer

Output:

[20, 26, 27] 6

Working (step by step)

LineStatementValues after this stepPrinted
1T = (48, 20, 26, 27, 31)T = (48, 20, 26, 27, 31)
2L = list(T)L = [48, 20, 26, 27, 31]
3L.append(T[1] + T[2])L = [48, 20, 26, 27, 31, 46]
4L.sort()L = [20, 26, 27, 31, 46, 48]
5print(L[:3], len(L))[20, 26, 27] 6⏎
B3.84Practice2 marks

Write the output of the following code.

for i in range(4, 0, -1):
    print("*" * i)
Answer

Output:

****
***
**
*

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(4, 0, -1):i = 4
2print("*" * i)****⏎
1for i in range(4, 0, -1):i = 3
2print("*" * i)***⏎
1for i in range(4, 0, -1):i = 2
2print("*" * i)**⏎
1for i in range(4, 0, -1):i = 1
2print("*" * i)*⏎
1for i in range(4, 0, -1):loop ends (no more values)
B3.85Practice2 marks

Write the output of the following code.

s = "Network"
print(s.count("t"), s.find("rk"), s[2:6].upper())
Answer

Output:

1 5 TWOR

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Network"s = 'Network'
2print(s.count("t"), s.find("rk"), s[2:6].upper())1 5 TWOR⏎
B3.86Practice2 marks

Write the output of the following code.

L = [5, 6, 2, 3, 5, 1]
S = []
for x in L:
    if x not in S:
        S.append(x)
print(S, len(L) - len(S))
Answer

Output:

[5, 6, 2, 3, 1] 1

Working (step by step)

LineStatementValues after this stepPrinted
1L = [5, 6, 2, 3, 5, 1]L = [5, 6, 2, 3, 5, 1]
2S = []S = []
3for x in L:x = 5
4if x not in S:condition is True
5S.append(x)S = [5]
3for x in L:x = 6
4if x not in S:condition is True
5S.append(x)S = [5, 6]
3for x in L:x = 2
4if x not in S:condition is True
5S.append(x)S = [5, 6, 2]
3for x in L:x = 3
4if x not in S:condition is True
5S.append(x)S = [5, 6, 2, 3]
3for x in L:x = 5
4if x not in S:condition is False

… the same steps continue for the remaining values.

B3.87Practice2 marks

Write the output of the following code.

T = (9, 39, 4, 46, 19)
L = list(T)
L.append(T[1] + T[2])
L.sort()
print(L[:3], len(L))
Answer

Output:

[4, 9, 19] 6

Working (step by step)

LineStatementValues after this stepPrinted
1T = (9, 39, 4, 46, 19)T = (9, 39, 4, 46, 19)
2L = list(T)L = [9, 39, 4, 46, 19]
3L.append(T[1] + T[2])L = [9, 39, 4, 46, 19, 43]
4L.sort()L = [4, 9, 19, 39, 43, 46]
5print(L[:3], len(L))[4, 9, 19] 6⏎
B3.88Practice2 marks

Write the output of the following code.

L = [26, 15, 22, 18, 24, 12]
for i in range(len(L)):
    if L[i] % 5 == 0:
        L[i] = L[i] // 2
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[52, 7, 44, 36, 48, 24]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [26, 15, 22, 18, 24, 12]L = [26, 15, 22, 18, 24, 12]
2for i in range(len(L)):i = 0
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 15, 22, 18, 24, 12]
2for i in range(len(L)):i = 1
3if L[i] % 5 == 0:condition is True
4L[i] = L[i] // 2L = [52, 7, 22, 18, 24, 12]
2for i in range(len(L)):i = 2
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 7, 44, 18, 24, 12]
2for i in range(len(L)):i = 3
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 7, 44, 36, 24, 12]
2for i in range(len(L)):i = 4
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 7, 44, 36, 48, 12]

… the same steps continue for the remaining values.

B3.89Practice2 marks

Write the output of the following code.

D = {'b': 15, 'd': 12, 'c': 9, 'a': 7}
total = 0
for k in D:
    if D[k] > 5:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

b d c a 43

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'b': 15, 'd': 12, 'c': 9, 'a': 7}D = {'b': 15, 'd': 12, 'c': 9, 'a': 7}
2total = 0total = 0
3for k in D:k = 'b'
4if D[k] > 5:condition is True
5total += D[k]total = 15
6print(k, end=" ")b
3for k in D:k = 'd'
4if D[k] > 5:condition is True
5total += D[k]total = 27
6print(k, end=" ")d
3for k in D:k = 'c'
4if D[k] > 5:condition is True
5total += D[k]total = 36
6print(k, end=" ")c
3for k in D:k = 'a'
4if D[k] > 5:condition is True

… the same steps continue for the remaining values.

B3.90Practice2 marks

Write the output of the following code.

L = [9, 8, 8, 8, 7, 9]
S = []
for x in L:
    if x not in S:
        S.append(x)
print(S, len(L) - len(S))
Answer

Output:

[9, 8, 7] 3

Working (step by step)

LineStatementValues after this stepPrinted
1L = [9, 8, 8, 8, 7, 9]L = [9, 8, 8, 8, 7, 9]
2S = []S = []
3for x in L:x = 9
4if x not in S:condition is True
5S.append(x)S = [9]
3for x in L:x = 8
4if x not in S:condition is True
5S.append(x)S = [9, 8]
3for x in L:loop ends (no more values)
4if x not in S:condition is False
3for x in L:loop ends (no more values)
4if x not in S:condition is False
3for x in L:x = 7
4if x not in S:condition is True
5S.append(x)S = [9, 8, 7]
3for x in L:x = 9

… the same steps continue for the remaining values.

B3.91Practice2 marks

Write the output of the following code.

L = [26, 2, 6, 25, 8, 12]
for i in range(len(L)):
    if L[i] % 5 == 0:
        L[i] = L[i] // 3
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[52, 4, 12, 8, 16, 24]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [26, 2, 6, 25, 8, 12]L = [26, 2, 6, 25, 8, 12]
2for i in range(len(L)):i = 0
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 2, 6, 25, 8, 12]
2for i in range(len(L)):i = 1
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 4, 6, 25, 8, 12]
2for i in range(len(L)):i = 2
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 4, 12, 25, 8, 12]
2for i in range(len(L)):i = 3
3if L[i] % 5 == 0:condition is True
4L[i] = L[i] // 3L = [52, 4, 12, 8, 8, 12]
2for i in range(len(L)):i = 4
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [52, 4, 12, 8, 16, 12]

… the same steps continue for the remaining values.

B3.92Practice2 marks

Write the output of the following code.

for i in range(1, 6):
    print(" " * (5 - i) + "#" * i)
Answer

Output:

    #
   ##
  ###
 ####
#####

Working (step by step)

LineStatementValues after this stepPrinted
1for i in range(1, 6):i = 1
2print(" " * (5 - i) + "#" * i) #⏎
1for i in range(1, 6):i = 2
2print(" " * (5 - i) + "#" * i) ##⏎
1for i in range(1, 6):i = 3
2print(" " * (5 - i) + "#" * i) ###⏎
1for i in range(1, 6):i = 4
2print(" " * (5 - i) + "#" * i) ####⏎
1for i in range(1, 6):i = 5
2print(" " * (5 - i) + "#" * i)#####⏎
1for i in range(1, 6):loop ends (no more values)
B3.93Practice2 marks

Write the output of the following code.

D = {'b': 4, 'e': 3, 'd': 7, 'c': 13}
total = 0
for k in D:
    if D[k] > 8:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

c 13

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'b': 4, 'e': 3, 'd': 7, 'c': 13}D = {'b': 4, 'e': 3, 'd': 7, 'c': 13}
2total = 0total = 0
3for k in D:k = 'b'
4if D[k] > 8:condition is False
3for k in D:k = 'e'
4if D[k] > 8:condition is False
3for k in D:k = 'd'
4if D[k] > 8:condition is False
3for k in D:k = 'c'
4if D[k] > 8:condition is True
5total += D[k]total = 13
6print(k, end=" ")c
3for k in D:loop ends (no more values)
7print(total)13⏎
B3.94Practice2 marks

Write the output of the following code.

s = "Delhi-88"
print(s[1:5], s[-3:], s[::3])
Answer

Output:

elhi -88 Dh8

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Delhi-88"s = 'Delhi-88'
2print(s[1:5], s[-3:], s[::3])elhi -88 Dh8⏎
B3.95Practice2 marks

Write the output of the following code.

L = [5, 1, 2, 5, 7, 6]
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print(F)
Answer

Output:

{5: 2, 1: 1, 2: 1, 7: 1, 6: 1}

Working (step by step)

LineStatementValues after this stepPrinted
1L = [5, 1, 2, 5, 7, 6]L = [5, 1, 2, 5, 7, 6]
2F = {}F = {}
3for x in L:x = 5
4F[x] = F.get(x, 0) + 1F = {5: 1}
3for x in L:x = 1
4F[x] = F.get(x, 0) + 1F = {5: 1, 1: 1}
3for x in L:x = 2
4F[x] = F.get(x, 0) + 1F = {5: 1, 1: 1, 2: 1}
3for x in L:x = 5
4F[x] = F.get(x, 0) + 1F = {5: 2, 1: 1, 2: 1}
3for x in L:x = 7
4F[x] = F.get(x, 0) + 1F = {5: 2, 1: 1, 2: 1, 7: 1}
3for x in L:x = 6
4F[x] = F.get(x, 0) + 1F = {5: 2, 1: 1, 2: 1, 7: 1, 6: 1}
3for x in L:loop ends (no more values)
5print(F){5: 2, 1: 1, 2: 1, 7: 1, 6: 1}⏎
B3.96Practice2 marks

Write the output of the following code.

n = 79101
s = 0
while n > 0:
    d = n % 10
    if d % 2 == 0:
        s = s + d
    n = n // 10
print(s)
Answer

Output:

0

Working (step by step)

LineStatementValues after this stepPrinted
1n = 79101n = 79101
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 1
5if d % 2 == 0:condition is False
7n = n // 10n = 7910
3while n > 0:condition is True
4d = n % 10d = 0
5if d % 2 == 0:condition is True
7n = n // 10n = 791
3while n > 0:condition is True
4d = n % 10d = 1
5if d % 2 == 0:condition is False
7n = n // 10n = 79
3while n > 0:condition is True
4d = n % 10d = 9

… the same steps continue for the remaining values.

B3.97Practice2 marks

Write the output of the following code.

L = [1, 9, 9, 8, 3, 6]
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print(F)
Answer

Output:

{1: 1, 9: 2, 8: 1, 3: 1, 6: 1}

Working (step by step)

LineStatementValues after this stepPrinted
1L = [1, 9, 9, 8, 3, 6]L = [1, 9, 9, 8, 3, 6]
2F = {}F = {}
3for x in L:x = 1
4F[x] = F.get(x, 0) + 1F = {1: 1}
3for x in L:x = 9
4F[x] = F.get(x, 0) + 1F = {1: 1, 9: 1}
3for x in L:loop ends (no more values)
4F[x] = F.get(x, 0) + 1F = {1: 1, 9: 2}
3for x in L:x = 8
4F[x] = F.get(x, 0) + 1F = {1: 1, 9: 2, 8: 1}
3for x in L:x = 3
4F[x] = F.get(x, 0) + 1F = {1: 1, 9: 2, 8: 1, 3: 1}
3for x in L:x = 6
4F[x] = F.get(x, 0) + 1F = {1: 1, 9: 2, 8: 1, 3: 1, 6: 1}
3for x in L:loop ends (no more values)
5print(F){1: 1, 9: 2, 8: 1, 3: 1, 6: 1}⏎
B3.98Practice2 marks

Write the output of the following code.

a = 2
b = 22
if a % 2 == 0 and b % 2 == 0:
    print(a + b)
elif a % 2 == 0 or b > 20:
    print(a * b)
else:
    print(b - a)
Answer

Output:

24

Working (step by step)

LineStatementValues after this stepPrinted
1a = 2a = 2
2b = 22b = 22
3if a % 2 == 0 and b % 2 == 0:condition is True
4print(a + b)24⏎
B3.99Practice2 marks

Write the output of the following code.

s = "Save Water Save Life"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("*", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

sAVE*wATER*sAVE*lIFE

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Save Water Save Life"s = 'Save Water Save Life'
2for ch in s:ch = 'S'
3if ch.isupper():condition is True
4print(ch.lower(), end="")s
2for ch in s:ch = 'a'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")A
2for ch in s:ch = 'v'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")V
2for ch in s:ch = 'e'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")E

… the same steps continue for the remaining values.

B3.100Practice2 marks

Write the output of the following code.

L = [5, 2, 8, 8, 8, 7]
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print(F)
Answer

Output:

{5: 1, 2: 1, 8: 3, 7: 1}

Working (step by step)

LineStatementValues after this stepPrinted
1L = [5, 2, 8, 8, 8, 7]L = [5, 2, 8, 8, 8, 7]
2F = {}F = {}
3for x in L:x = 5
4F[x] = F.get(x, 0) + 1F = {5: 1}
3for x in L:x = 2
4F[x] = F.get(x, 0) + 1F = {5: 1, 2: 1}
3for x in L:x = 8
4F[x] = F.get(x, 0) + 1F = {5: 1, 2: 1, 8: 1}
3for x in L:loop ends (no more values)
4F[x] = F.get(x, 0) + 1F = {5: 1, 2: 1, 8: 2}
3for x in L:loop ends (no more values)
4F[x] = F.get(x, 0) + 1F = {5: 1, 2: 1, 8: 3}
3for x in L:x = 7
4F[x] = F.get(x, 0) + 1F = {5: 1, 2: 1, 8: 3, 7: 1}
3for x in L:loop ends (no more values)
5print(F){5: 1, 2: 1, 8: 3, 7: 1}⏎
B3.101Practice2 marks

Write the output of the following code.

s = "Algorithm"
print(s[1:5], s[-3:], s[::3])
Answer

Output:

lgor thm Aot

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Algorithm"s = 'Algorithm'
2print(s[1:5], s[-3:], s[::3])lgor thm Aot⏎
B3.102Practice2 marks

Write the output of the following code.

D = {'e': 13, 'a': 18, 'c': 9, 'b': 2}
total = 0
for k in D:
    if D[k] > 6:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

e a c 40

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'e': 13, 'a': 18, 'c': 9, 'b': 2}D = {'e': 13, 'a': 18, 'c': 9, 'b': 2}
2total = 0total = 0
3for k in D:k = 'e'
4if D[k] > 6:condition is True
5total += D[k]total = 13
6print(k, end=" ")e
3for k in D:k = 'a'
4if D[k] > 6:condition is True
5total += D[k]total = 31
6print(k, end=" ")a
3for k in D:k = 'c'
4if D[k] > 6:condition is True
5total += D[k]total = 40
6print(k, end=" ")c
3for k in D:k = 'b'
4if D[k] > 6:condition is False
3for k in D:loop ends (no more values)
7print(total)40⏎
B3.103Practice2 marks

Write the output of the following code.

x, y = 7, 10
while x < y:
    x = x * 2
    y = y - 1
print(x, y)
Answer

Output:

14 9

Working (step by step)

LineStatementValues after this stepPrinted
1x, y = 7, 10x = 7, y = 10
2while x < y:condition is True
3x = x * 2x = 14
4y = y - 1y = 9
2while x < y:condition is False
5print(x, y)14 9⏎
B3.104Practice2 marks

Write the output of the following code.

n = 49478
s = 0
while n > 0:
    d = n % 10
    s = s * 10 + d
    n = n // 10
print(s)
Answer

Output:

87494

Working (step by step)

LineStatementValues after this stepPrinted
1n = 49478n = 49478
2s = 0s = 0
3while n > 0:condition is True
4d = n % 10d = 8
5s = s * 10 + ds = 8
6n = n // 10n = 4947
3while n > 0:condition is True
4d = n % 10d = 7
5s = s * 10 + ds = 87
6n = n // 10n = 494
3while n > 0:condition is True
4d = n % 10d = 4
5s = s * 10 + ds = 874
6n = n // 10n = 49
3while n > 0:condition is True
4d = n % 10d = 9

… the same steps continue for the remaining values.

B3.105Practice2 marks

Write the output of the following code.

s = "CBSE Exam 2027"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("#", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

cbse#eXAM#2027

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "CBSE Exam 2027"s = 'CBSE Exam 2027'
2for ch in s:ch = 'C'
3if ch.isupper():condition is True
4print(ch.lower(), end="")c
2for ch in s:ch = 'B'
3if ch.isupper():condition is True
4print(ch.lower(), end="")b
2for ch in s:ch = 'S'
3if ch.isupper():condition is True
4print(ch.lower(), end="")s
2for ch in s:ch = 'E'
3if ch.isupper():condition is True
4print(ch.lower(), end="")e
2for ch in s:ch = ' '
3if ch.isupper():condition is False
5elif ch == " ":condition is True

… the same steps continue for the remaining values.

B3.106Practice2 marks

Write the output of the following code.

D = {'b': 7, 'a': 12, 'c': 1, 'd': 2}
total = 0
for k in D:
    if D[k] > 8:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

a 12

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'b': 7, 'a': 12, 'c': 1, 'd': 2}D = {'b': 7, 'a': 12, 'c': 1, 'd': 2}
2total = 0total = 0
3for k in D:k = 'b'
4if D[k] > 8:condition is False
3for k in D:k = 'a'
4if D[k] > 8:condition is True
5total += D[k]total = 12
6print(k, end=" ")a
3for k in D:k = 'c'
4if D[k] > 8:condition is False
3for k in D:k = 'd'
4if D[k] > 8:condition is False
3for k in D:loop ends (no more values)
7print(total)12⏎
B3.107Practice2 marks

Write the output of the following code.

D = {'b': 7, 'c': 13, 'e': 15, 'a': 16}
total = 0
for k in D:
    if D[k] > 9:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

c e a 44

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'b': 7, 'c': 13, 'e': 15, 'a': 16}D = {'b': 7, 'c': 13, 'e': 15, 'a': 16}
2total = 0total = 0
3for k in D:k = 'b'
4if D[k] > 9:condition is False
3for k in D:k = 'c'
4if D[k] > 9:condition is True
5total += D[k]total = 13
6print(k, end=" ")c
3for k in D:k = 'e'
4if D[k] > 9:condition is True
5total += D[k]total = 28
6print(k, end=" ")e
3for k in D:k = 'a'
4if D[k] > 9:condition is True
5total += D[k]total = 44
6print(k, end=" ")a
3for k in D:loop ends (no more values)
7print(total)44⏎
B3.108Practice2 marks

Write the output of the following code.

s = "Data and Logic"
for ch in s:
    if ch.isupper():
        print(ch.lower(), end="")
    elif ch == " ":
        print("@", end="")
    else:
        print(ch.upper(), end="")
Answer

Output:

dATA@AND@lOGIC

Each upper-case letter becomes lower case, each space becomes the symbol, and every other character becomes upper case.

Working (step by step)

LineStatementValues after this stepPrinted
1s = "Data and Logic"s = 'Data and Logic'
2for ch in s:ch = 'D'
3if ch.isupper():condition is True
4print(ch.lower(), end="")d
2for ch in s:ch = 'a'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")A
2for ch in s:ch = 't'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")T
2for ch in s:ch = 'a'
3if ch.isupper():condition is False
5elif ch == " ":condition is False
8print(ch.upper(), end="")A

… the same steps continue for the remaining values.

B3.109Practice2 marks

Write the output of the following code.

L = [29, 25, 22, 14, 27, 7]
for i in range(len(L)):
    if L[i] % 5 == 0:
        L[i] = L[i] // 3
    else:
        L[i] = L[i] * 2
print(L)
Answer

Output:

[58, 8, 44, 28, 54, 14]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [29, 25, 22, 14, 27, 7]L = [29, 25, 22, 14, 27, 7]
2for i in range(len(L)):i = 0
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [58, 25, 22, 14, 27, 7]
2for i in range(len(L)):i = 1
3if L[i] % 5 == 0:condition is True
4L[i] = L[i] // 3L = [58, 8, 22, 14, 27, 7]
2for i in range(len(L)):i = 2
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [58, 8, 44, 14, 27, 7]
2for i in range(len(L)):i = 3
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [58, 8, 44, 28, 27, 7]
2for i in range(len(L)):i = 4
3if L[i] % 5 == 0:condition is False
6L[i] = L[i] * 2L = [58, 8, 44, 28, 54, 7]

… the same steps continue for the remaining values.

B3.110Practice2 marks

Write the output of the following code.

D = {'c': 16, 'b': 12, 'e': 14, 'd': 2}
total = 0
for k in D:
    if D[k] > 8:
        total += D[k]
        print(k, end=" ")
print(total)
Answer

Output:

c b e 42

Working (step by step)

LineStatementValues after this stepPrinted
1D = {'c': 16, 'b': 12, 'e': 14, 'd': 2}D = {'c': 16, 'b': 12, 'e': 14, 'd': 2}
2total = 0total = 0
3for k in D:k = 'c'
4if D[k] > 8:condition is True
5total += D[k]total = 16
6print(k, end=" ")c
3for k in D:k = 'b'
4if D[k] > 8:condition is True
5total += D[k]total = 28
6print(k, end=" ")b
3for k in D:k = 'e'
4if D[k] > 8:condition is True
5total += D[k]total = 42
6print(k, end=" ")e
3for k in D:k = 'd'
4if D[k] > 8:condition is False
3for k in D:loop ends (no more values)
7print(total)42⏎
B4Python: correct the errors2 marks · 40 questions
B4.1Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

x = input('Enter a number')
if x % 2 = 0
    print('Even')
else
    print('Odd')
Answer
x = int(input('Enter a number'))
if x % 2 == 0:
    print('Even')
else:
    print('Odd')

input() gives a string: use int(); use == to compare; colons are needed after if and else.

B4.2Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

for i in range(1, 6)
    sum = sum + i
print(Sum)
Answer
sum = 0
for i in range(1, 6):
    sum = sum + i
print(sum)

sum must be given a starting value; colon after for; Python is case sensitive (Sum).

B4.3Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

n = 5
while n > 0
print(n)
n = n - 1
Answer
n = 5
while n > 0:
    print(n)
    n = n - 1

Colon after while; the body must be indented.

B4.4Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

s = 'Python'
s[0] = 'J'
print(s.Upper())
Answer
s = 'Python'
s = 'J' + s[1:]
print(s.upper())

Strings are immutable; make a new string. The method name is upper().

B4.5Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

L = [10, 20, 30]
L.add(40)
print(L[4])
Answer
L = [10, 20, 30]
L.append(40)
print(L[3])

Lists have append(), not add(); after adding, the last index is 3.

B4.6Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

T = (1, 2, 3)
T[1] = 5
T.append(4)
print(T)
Answer
T = (1, 2, 3)
L = list(T)
L[1] = 5
L.append(4)
T = tuple(L)
print(T)

Tuples are immutable; change a list copy and convert back.

B4.7Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

D = {'a': 1, 'b': 2]
for k in D
    print(k, D(k))
Answer
D = {'a': 1, 'b': 2}
for k in D:
    print(k, D[k])

Close the dictionary with }; colon after for; values are accessed with D[k].

B4.8Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

a = 10
b = '20'
c = a + b
print 'Sum =', c
Answer
a = 10
b = '20'
c = a + int(b)
print('Sum =', c)

Convert b to int before adding; print is a function and needs brackets.

B4.9Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

num = int(input('N: '))
if num > 0:
print('Positive')
elif num = 0:
    print('Zero')
else if num < 0:
    print('Negative')
Answer
num = int(input('N: '))
if num > 0:
    print('Positive')
elif num == 0:
    print('Zero')
else:
    print('Negative')

Indent the body of if; use == ; Python uses elif or else, not 'else if'.

B4.10Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

name = 'Kendriya'
for ch in Name:
    if ch in 'aeiou'
        count += 1
print(count)
Answer
name = 'Kendriya'
count = 0
for ch in name:
    if ch in 'aeiou':
        count += 1
print(count)

Name is not defined (case); colon after if; count needs a starting value.

B4.11Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

L = [5, 3, 8]
L = L.sort()
print(L[0])
Answer
L = [5, 3, 8]
L.sort()
print(L[0])

sort() returns None; it sorts L in place, so do not assign its result.

B4.12Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

import Math
x = math.sqrt(25)
print(x.floor)
Answer
import math
x = math.sqrt(25)
print(math.floor(x))

The module name is math (small m); floor is a function of math.

B4.13Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

for i in range(5):
    if i == 3:
        Break
    print(i)
else
    print('done')
Answer
for i in range(5):
    if i == 3:
        break
    print(i)
else:
    print('done')

break is in small letters; colon after else.

B4.14Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

Marks = 90
if Marks >= 90 and <= 100:
    print('A1')
Answer
Marks = 90
if Marks >= 90 and Marks <= 100:
    print('A1')

Each side of and must be a full condition (or write 90 <= Marks <= 100).

B4.15Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

L = [1, 2, 3]
for i in range(len(L))
    L[i] = L[i] * 2
print(l)
Answer
L = [1, 2, 3]
for i in range(len(L)):
    L[i] = L[i] * 2
print(L)

Colon after for; print L, not l.

B4.16Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

s = "Hello World'
w = s.split(' ')
print(len w)
Answer
s = "Hello World"
w = s.split(' ')
print(len(w))

The string must open and close with the same quote; len() needs brackets.

B4.17Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

D = dict()
D[1] = 'one'
D.[2] = 'two'
print(D.key())
Answer
D = dict()
D[1] = 'one'
D[2] = 'two'
print(D.keys())

Use D[2] to add; the method is keys().

B4.18Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

x = 5
y = 2
print(x ^ y, x ** y, x // y, x % y))
Answer
x = 5
y = 2
print(x ** y, x // y, x % y)

^ is not power in Python (it is bitwise XOR); one closing bracket too many.

B4.19Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

i = 1
while i < 10:
    if i % 2 = 0:
        continue
    print(i)
    i =+ 1
Answer
i = 1
while i < 10:
    if i % 2 == 0:
        i += 1
        continue
    print(i)
    i += 1

Use == to compare; =+ means 'assign +1', write += 1; i must change before continue, or the loop never ends.

B4.20Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

num = [4, 5, 6]
num.insert(10)
num.remove(1)
print(num.length)
Answer
num = [4, 5, 6]
num.insert(0, 10)
num.remove(4)
print(len(num))

insert() needs an index and a value; remove() takes a value present in the list; length is found by len().

B4.21Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

s = 'abc'
print(s * 2 + 3)
Answer
s = 'abc'
print(s * 2 + '3')

A string cannot be added to an integer; use '3'.

B4.22Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

student = {'name' = 'Asha', 'age' = 16}
print(student[name])
Answer
student = {'name': 'Asha', 'age': 16}
print(student['name'])

Use : between key and value; the key is the string 'name'.

B4.23Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

N = 4
for i in range(1, N+1):
  for j in range(i)
    print('*', end=' ')
  print()
Answer
N = 4
for i in range(1, N+1):
    for j in range(i):
        print('*', end=' ')
    print()

Colon after the inner for; keep the indentation consistent.

B4.24Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

from random import *
r = random.randint(1, 6)
print(r)
Answer
import random
r = random.randint(1, 6)
print(r)

With 'from random import *' the name random is not available; import the module itself (or call randint directly).

B4.25Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

1st_num = 10
second num = 20
print(1st_num + second num)
Answer
num1 = 10
second_num = 20
print(num1 + second_num)

An identifier cannot start with a digit or have a space.

B4.26Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

L = [1, 2, 3]
L[3] = 4
print(L)
Answer
L = [1, 2, 3]
L.append(4)
print(L)

Index 3 does not exist (IndexError); add a new element with append().

B4.27Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

s = 'KV'
if s.isupper:
    print(s.lower)
Answer
s = 'KV'
if s.isupper():
    print(s.lower())

Methods must be called with brackets.

B4.28Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

t = (10, 20, 30)
a, b = t
print(a + b)
Answer
t = (10, 20, 30)
a, b, c = t
print(a + b)

The number of variables must match the number of values when unpacking.

B4.29Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

def = 5
for i in range(def):
    print(i)
Answer
d = 5
for i in range(d):
    print(i)

def is a keyword; it cannot be a variable name.

B4.30Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

a = int(input('A: ')
b = float(input('B: '))
print('Total' + a + b)
Answer
a = int(input('A: '))
b = float(input('B: '))
print('Total', a + b)

Missing closing bracket; a string cannot be joined to numbers with +, use a comma.

B4.31Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

marks = [70, 80, 90]
total = 0
for m in range(marks):
    total += m
print(total / len[marks])
Answer
marks = [70, 80, 90]
total = 0
for m in marks:
    total += m
print(total / len(marks))

Loop directly over the list (range needs a number); len() uses round brackets.

B4.32Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

word = input('Word: ')
if word == word.reverse():
    print('Palindrome')
Answer
word = input('Word: ')
if word == word[::-1]:
    print('Palindrome')

Strings have no reverse() method; use the slice [::-1].

B4.33Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

s = input('Enter: ')
for i in range(len(s))
    if s[i].isupper:
        print(s[i], end='')
Answer
s = input('Enter: ')
for i in range(len(s)):
    if s[i].isupper():
        print(s[i], end='')

Colon after for; isupper() must be called with brackets.

B4.34Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

L = [3, 6, 9]
L.insert(1)
print(L.sort())
Answer
L = [3, 6, 9]
L.insert(1, 4)
L.sort()
print(L)

insert() needs an index and a value; sort() returns None, so sort first and then print L.

B4.35Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

D = {[1, 2]: 'a', 'b': 2}
print(D.items)
Answer
D = {(1, 2): 'a', 'b': 2}
print(D.items())

A list cannot be a key (it is mutable); use a tuple. items() needs brackets.

B4.36Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

count = 0
for i in range(10):
    if i % 3 == 0:
    count = count + 1
print('Count:' count)
Answer
count = 0
for i in range(10):
    if i % 3 == 0:
        count = count + 1
print('Count:', count)

The body of if must be indented; a comma is needed between the items in print().

B4.37Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

x = 10
y = 0
if y != 0:
    print(x / y)
Else:
    print('Cannot divide')
Answer
x = 10
y = 0
if y != 0:
    print(x / y)
else:
    print('Cannot divide')

Python is case sensitive: the keyword is else.

B4.38Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

s = 'Data'
n = len(s)
for i in range(n, 0):
    print(s[i])
Answer
s = 'Data'
n = len(s)
for i in range(n - 1, -1, -1):
    print(s[i])

s[n] is out of range and range(n, 0) needs a negative step; go from n - 1 down to 0.

B4.39Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

import math
print(math.squareroot(81))
print(math.PI)
Answer
import math
print(math.sqrt(81))
print(math.pi)

The function is sqrt() and the constant is pi in small letters.

B4.40Practice2 marks

Rewrite the code after removing the errors. Underline each correction.

T = 1, 2, 3
T += 4
print(T)
Answer
T = 1, 2, 3
T += (4,)
print(T)

A tuple can be joined only with another tuple; write (4,).

B5Society, law and ethics2 marks · 60 questions
B5.1Practice2 marks

What is a digital footprint? Differentiate between its two types.

Answer

The trail of data left by a person while using the Internet. Active: left knowingly, such as posts, comments, e-mails. Passive: collected without the user knowing, such as browsing history, location, cookies.

B5.2Practice2 marks

Write any four points of netiquette.

Answer

Be polite and respectful; do not use bad language or ALL CAPS (shouting); do not share others' personal data; respect copyright and give credit; do not spam; check facts before forwarding. (Any four.)

B5.3Practice2 marks

What is plagiarism? How can it be avoided?

Answer

Presenting someone else's work or ideas as one's own. Avoid it by giving credit (citation) to the source, using quotation marks for exact words, and writing in your own words.

B5.4Practice2 marks

Differentiate between copyright and patent.

Answer

Copyright protects creative works such as books, music, software and art; it is automatic on creation. Patent protects a new invention; it must be applied for and granted, for a limited period.

B5.5Practice2 marks

What is intellectual property? Name the three ways it is protected.

Answer

Creations of the mind such as inventions, books, music, designs and brand names. It is protected by copyright, patent and trademark.

B5.6Practice2 marks

Differentiate between free software and open source software.

Answer

Free software stresses the user's freedom to run, study, change and share the software. Open source software stresses that the source code is available and can be improved by anyone. Both are together called FOSS. Free software need not be free of cost.

B5.7Practice2 marks

Differentiate between proprietary software and FOSS with one example each.

Answer

Proprietary software is owned by a company; the code is secret and a licence must be bought: MS Office. FOSS has open code and can be used and shared freely: LibreOffice, Linux.

B5.8Practice2 marks

Differentiate between GPL and Creative Commons licences.

Answer

GPL is a copyleft licence for software: changed versions must also be shared under GPL. Creative Commons licences are for creative works (images, music, text); the creator chooses conditions such as attribution or no commercial use.

B5.9Practice2 marks

What is hacking? Differentiate between ethical and unethical hacking.

Answer

Getting unauthorised access to a computer or network. Ethical hacking is done with permission to find and fix weaknesses (white hat). Unethical hacking is done to steal or damage (black hat) and is a crime.

B5.10Practice2 marks

What is phishing? Write two ways to protect yourself from it.

Answer

A fraud in which fake e-mails, messages or websites trick people into giving passwords or bank details. Protection: do not click unknown links; check the website address (https and spelling); never share OTP or passwords; report the message.

B5.11Practice2 marks

What is ransomware? Write one way to stay safe from it.

Answer

Malware that locks or encrypts a victim's files and asks for money (ransom) to unlock them. Stay safe by keeping regular offline backups, updating the system and not opening unknown attachments.

B5.12Practice2 marks

Differentiate between a virus and a worm.

Answer

A virus attaches itself to a host program and spreads when that program is run. A worm is a standalone program that copies itself and spreads through networks on its own.

B5.13Practice2 marks

What is a Trojan horse? How is it different from a virus?

Answer

Malware that looks like a useful program but does harm, such as stealing data. Unlike a virus, it does not copy itself or infect other files.

B5.14Practice2 marks

Differentiate between adware and spyware.

Answer

Adware shows unwanted advertisements, often pop-ups. Spyware secretly collects information about the user, such as passwords and browsing, and sends it to others.

B5.15Practice2 marks

What is cyber bullying? Write two things a student should do if bullied online.

Answer

Using digital media to harass, threaten or insult a person. Do: not reply to the bully, block and report the account, keep screenshots as proof, and tell parents or teachers.

B5.16Practice2 marks

Write any four safety measures while using the Internet.

Answer

Use strong, different passwords; do not share personal details; use antivirus and keep software updated; use secure (https) sites; log out on shared computers; think before clicking links. (Any four.)

B5.17Practice2 marks

What is identity theft? Give an example.

Answer

Stealing someone's personal information and using it to pretend to be that person, usually to cheat. Example: using another person's Aadhaar and phone number to take a loan.

B5.18Practice2 marks

What is the IT Act, 2000? Write two of its provisions.

Answer

The Indian law on electronic transactions and cyber crime. It gives legal recognition to electronic records and digital signatures, and punishes offences such as hacking, identity theft and sending offensive messages.

B5.19Practice2 marks

What is e-waste? Why is it harmful?

Answer

Discarded electronic devices such as old phones, computers and batteries. It contains toxic metals like lead, mercury and cadmium which pollute soil and water and harm health when dumped or burnt.

B5.20Practice2 marks

Write any four ways to manage e-waste.

Answer

Reduce buying new devices; reuse or donate working devices; repair instead of replacing; recycle through authorised e-waste collectors; return to the company under take-back schemes. (Any four.)

B5.21Practice2 marks

What are cookies? Are they harmful?

Answer

Small text files that websites save on the user's computer to remember choices and login. They are not harmful by themselves, but third-party cookies can track the user and invade privacy.

B5.22Practice2 marks

What is the digital divide? Give two ways to reduce it.

Answer

The gap between people who have access to computers and the Internet and those who do not. Reduce it by setting up public computer centres, cheap Internet, digital literacy programmes, and content in local languages.

B5.23Practice2 marks

Write two gender issues related to the use of computers and their solutions.

Answer

Fewer girls choose computer science; girls get less access to devices at home. Solutions: encourage girls through role models and clubs, equal access to labs and devices, and safe online spaces.

B5.24Practice2 marks

How can technology help persons with disabilities? Give two examples.

Answer

Assistive technologies make computers usable for them: screen readers and Braille keyboards for the visually challenged, speech-to-text for those who cannot type, captions for the hearing impaired.

B5.25Practice2 marks

What is data protection? Write two ways to protect your personal data.

Answer

Keeping personal data safe from misuse and unauthorised access. Ways: share data only on trusted sites, use strong passwords and two-factor authentication, check app permissions, and do not post personal details publicly.

B5.26Practice2 marks

Differentiate between freeware and shareware.

Answer

Freeware is free to use for any time, but its code is closed. Shareware is free only for a trial period or with limited features; after that it must be bought.

B5.27Practice2 marks

What is a firewall? Why is it used?

Answer

A hardware or software system that checks the data coming into and going out of a computer or network, and blocks the traffic that breaks the security rules. It protects against hackers and malware.

B5.28Practice2 marks

What is software piracy? Why is it wrong?

Answer

Copying, using or selling software without a licence. It is wrong because it breaks copyright law, causes loss to the creators and may carry malware.

B5.29Practice2 marks

Differentiate between cyber trolling and cyber bullying.

Answer

Trolling is posting provocative or offensive comments to upset people and start arguments, often to anyone. Bullying targets a particular person again and again to harass or threaten.

B5.30Practice2 marks

Why should we think before posting something online? Give two reasons.

Answer

Because digital footprints are lasting: posts can be copied and found later, and may affect college admission or jobs. Posts can also hurt others or break the law.

B5.31Practice2 marks

Write any two differences between active and passive digital footprints with an example of each.

Answer

Active footprints are left on purpose (a post, a comment, an e-mail); passive ones are collected without our knowledge (IP address, location, browsing history). Active ones can be controlled directly; passive ones need privacy settings and care.

B5.32Practice2 marks

Why is it important to manage your digital footprint? Give two tips.

Answer

Because it is lasting and can be seen by colleges, employers and strangers, and can be misused. Tips: think before posting; set accounts to private; delete old accounts; do not overshare personal details.

B5.33Practice2 marks

What is a trademark? Give two examples.

Answer

A word, name, logo or symbol that identifies the goods or services of a company and separates them from others. Examples: the logos of Tata, Amul, Apple.

B5.34Practice2 marks

What is copyright infringement? Give two examples.

Answer

Using a copyrighted work without the owner's permission. Examples: downloading a movie from a pirate site; copying a textbook and selling it; using a paid font without a licence.

B5.35Practice2 marks

What is a software licence? Why is it needed?

Answer

A legal agreement that says how a software may be used, copied or shared. It protects the rights of the creator and tells the user what is allowed.

B5.36Practice2 marks

Write two advantages of open source software.

Answer

It is usually free of cost; its code can be studied and changed; bugs are fixed quickly by a large community; no dependence on one company. (Any two.)

B5.37Practice2 marks

What is the public domain? Give an example.

Answer

Works whose copyright has ended or was given up, so anyone can use them freely. Example: the works of Rabindranath Tagore and Shakespeare.

B5.38Practice2 marks

Differentiate between a virus and a Trojan horse.

Answer

A virus attaches to a file and spreads when that file is run; it copies itself. A Trojan horse looks like useful software, does harm, but does not copy itself.

B5.39Practice2 marks

What is spam? How can it be controlled?

Answer

Unwanted bulk messages, mostly advertisements, sent to many people. Control it by using spam filters, not sharing your e-mail publicly, and not replying to or clicking in spam.

B5.40Practice2 marks

Write two ways in which cyber crimes are different from ordinary crimes.

Answer

They are done through computers and networks; the criminal can be far away or in another country; evidence is digital and can be hidden; many people can be hit at once.

B5.41Practice2 marks

What is two-factor authentication? Why is it safer?

Answer

Logging in with two different checks, such as a password and an OTP sent to the phone. Even if the password is stolen, the account cannot be opened without the second factor.

B5.42Practice2 marks

Name any four types of malware.

Answer

Virus, worm, Trojan horse, ransomware, spyware, adware, keylogger. (Any four.)

B5.43Practice2 marks

What is a keylogger? How can it harm you?

Answer

A program or device that records every key pressed. It can steal passwords, card numbers and messages.

B5.44Practice2 marks

Write any two ways to identify a phishing e-mail.

Answer

It creates urgency or offers prizes; the sender address or link looks odd or misspelt; it asks for passwords, OTP or bank details; it has spelling mistakes; the link is not https.

B5.45Practice2 marks

Write two health hazards of excessive use of technology.

Answer

Eye strain; neck and back pain due to wrong posture; lack of sleep; Internet addiction and stress. (Any two.)

B5.46Practice2 marks

What is the role of CERT-In?

Answer

The Indian Computer Emergency Response Team is the national agency that responds to cyber security incidents, issues alerts and guides people and organisations to stay safe.

B5.47Practice2 marks

How does recycling of e-waste help the environment? Give two points.

Answer

It stops toxic metals from entering soil and water, and recovers useful metals like copper and gold, so less mining is needed.

B5.48Practice2 marks

What is privacy? Why should we protect it online?

Answer

The right to keep our personal information to ourselves. Online data can be misused for fraud, stalking, identity theft or unwanted ads, so it must be protected.

B5.49Practice2 marks

Explain any two points to be kept in mind while using social media.

Answer

Keep the account private and accept only known people; do not share location or personal details; think before posting; report abusive content; respect others' privacy.

B5.50Practice2 marks

Why should we not forward a message without checking it?

Answer

It may be fake news or a rumour that can cause panic, hurt people or break the law. Check it from a trusted source first.

B5.51Practice2 marks

What is ethical use of technology? Give two examples.

Answer

Using technology in a fair, legal and responsible way. Examples: using licensed software; giving credit to sources; respecting others' privacy online.

B5.52Practice2 marks

Explain 'think before you click' with an example.

Answer

Before opening a link or attachment, check who sent it and whether it is safe. Example: a message 'Claim your free recharge' may lead to a phishing site.

B5.53Practice2 marks

Differentiate between hacking and cracking.

Answer

Hacking in general means getting into a system, and may be ethical with permission. Cracking is breaking security for a bad purpose, such as stealing data or removing software protection; it is always illegal.

B5.54Practice2 marks

What is cyber stalking?

Answer

Following or watching a person online again and again, sending unwanted messages and collecting information about them, which makes them afraid. It is a crime.

B5.55Practice2 marks

Give two ways schools can make computer labs friendly for students with disabilities.

Answer

Install screen readers and magnifiers; provide adjustable desks and wheelchair access; use large keyboards or Braille keyboards; give captions on videos.

B5.56Practice2 marks

What does 'copyleft' mean?

Answer

A licence condition that lets anyone use, change and share the work, but requires that changed versions are shared under the same free terms. GPL is a copyleft licence.

B5.57Practice2 marks

Why is a digital signature used? Name the Act that recognises it in India.

Answer

To prove who sent an electronic document and that it was not changed on the way. The IT Act, 2000 gives it legal recognition.

B5.58Practice2 marks

Write two precautions to take while shopping online.

Answer

Use trusted sites with https; do not save card details on shared computers; use cash on delivery or secure payment options; check reviews and seller details.

B5.59Practice2 marks

What is a cyber cell? When should we contact it?

Answer

A police unit that investigates cyber crimes. Contact it (or cybercrime.gov.in, helpline 1930) when we face fraud, hacking, bullying or identity theft.

B5.60Practice2 marks

Why is password sharing unsafe even with friends?

Answer

The friend may misuse it knowingly or by mistake, or the password may leak from their device. You remain responsible for all actions done from your account.

B6Logic circuits: draw and read (practice only)2 marks · 32 questions
B6.1Practice2 marks

Draw the logic circuit for the Boolean expression A AND (NOT B). Also write its truth table.

Answer
ABY
ABY = A AND (NOT B)
000
010
101
110
B6.2Practice2 marks

Draw the logic circuit for the Boolean expression (NOT A) OR B. Also write its truth table.

Answer
ABY
ABY = (NOT A) OR B
001
011
100
111
B6.3Practice2 marks

Draw the logic circuit for the Boolean expression NOT (A AND B). Also write its truth table.

Answer
ABY
ABY = NOT (A AND B)
001
011
101
110
B6.4Practice2 marks

Draw the logic circuit for the Boolean expression NOT (A OR B). Also write its truth table.

Answer
ABY
ABY = NOT (A OR B)
001
010
100
110
B6.5Practice2 marks

Draw the logic circuit for the Boolean expression (NOT A) AND (NOT B). Also write its truth table.

Answer
ABY
ABY = (NOT A) AND (NOT B)
001
010
100
110
B6.6Practice2 marks

Draw the logic circuit for the Boolean expression (NOT A) OR (NOT B). Also write its truth table.

Answer
ABY
ABY = (NOT A) OR (NOT B)
001
011
101
110
B6.7Practice2 marks

Draw the logic circuit for the Boolean expression A XOR B. Also write its truth table.

Answer
ABY
ABXY = A XOR B
0000
0010
0101
0111
1001
1011
1100
1110
B6.8Practice2 marks

Draw the logic circuit for the Boolean expression (A AND B) OR (NOT A AND NOT B). Also write its truth table.

Answer
ABABY
ABY = (A AND B) OR (NOT A AND NOT B)
001
010
100
111
B6.9Practice2 marks

Draw the logic circuit for the Boolean expression (A AND B) OR C. Also write its truth table.

Answer
ABCY
ABCY = (A AND B) OR C
0000
0011
0100
0111
1000
1011
1101
1111
B6.10Practice2 marks

Draw the logic circuit for the Boolean expression A AND (B OR C). Also write its truth table.

Answer
ABCY
ABCY = A AND (B OR C)
0000
0010
0100
0110
1000
1011
1101
1111
B6.11Practice2 marks

Draw the logic circuit for the Boolean expression (A OR B) AND (NOT C). Also write its truth table.

Answer
ABCY
ABCY = (A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110
B6.12Practice2 marks

Draw the logic circuit for the Boolean expression NOT A OR (B AND C). Also write its truth table.

Answer
ABCY
ABCY = NOT A OR (B AND C)
0001
0011
0101
0111
1000
1010
1100
1111
B6.13Practice2 marks

Draw the logic circuit for the Boolean expression (A AND NOT B) OR (B AND C). Also write its truth table.

Answer
ABBCY
ABCY = (A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111
B6.14Practice2 marks

Draw the logic circuit for the Boolean expression NOT (A OR B) AND C. Also write its truth table.

Answer
ABCY
ABCY = NOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110
B6.15Practice2 marks

Draw the logic circuit for the Boolean expression (A OR NOT B) AND (B OR C). Also write its truth table.

Answer
ABBCY
ABCY = (A OR NOT B) AND (B OR C)
0000
0011
0100
0110
1000
1011
1101
1111
B6.16Practice2 marks

Draw the logic circuit for the Boolean expression A AND B AND NOT C. Also write its truth table.

Answer
ABCY
ABCY = A AND B AND NOT C
0000
0010
0100
0110
1000
1010
1101
1110
B6.17Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABY
Answer

Y = A AND (NOT B)

ABY = A AND (NOT B)
000
010
101
110
B6.18Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABY
Answer

Y = (NOT A) OR B

ABY = (NOT A) OR B
001
011
100
111
B6.19Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABY
Answer

Y = NOT (A AND B)

ABY = NOT (A AND B)
001
011
101
110
B6.20Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABY
Answer

Y = NOT (A OR B)

ABY = NOT (A OR B)
001
010
100
110
B6.21Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABY
Answer

Y = (NOT A) AND (NOT B)

ABY = (NOT A) AND (NOT B)
001
010
100
110
B6.22Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABY
Answer

Y = (NOT A) OR (NOT B)

ABY = (NOT A) OR (NOT B)
001
011
101
110
B6.23Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABY
Answer

Y = A XOR B

ABXY = A XOR B
0000
0010
0101
0111
1001
1011
1100
1110
B6.24Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABABY
Answer

Y = (A AND B) OR (NOT A AND NOT B)

ABY = (A AND B) OR (NOT A AND NOT B)
001
010
100
111
B6.25Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABCY
Answer

Y = (A AND B) OR C

ABCY = (A AND B) OR C
0000
0011
0100
0111
1000
1011
1101
1111
B6.26Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABCY
Answer

Y = A AND (B OR C)

ABCY = A AND (B OR C)
0000
0010
0100
0110
1000
1011
1101
1111
B6.27Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABCY
Answer

Y = (A OR B) AND (NOT C)

ABCY = (A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110
B6.28Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABCY
Answer

Y = NOT A OR (B AND C)

ABCY = NOT A OR (B AND C)
0001
0011
0101
0111
1000
1010
1100
1111
B6.29Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABBCY
Answer

Y = (A AND NOT B) OR (B AND C)

ABCY = (A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111
B6.30Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABCY
Answer

Y = NOT (A OR B) AND C

ABCY = NOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110
B6.31Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABBCY
Answer

Y = (A OR NOT B) AND (B OR C)

ABCY = (A OR NOT B) AND (B OR C)
0000
0011
0100
0110
1000
1011
1101
1111
B6.32Practice2 marks

Write the Boolean expression for the output Y of this logic circuit.

ABCY
Answer

Y = A AND B AND NOT C

ABCY = A AND B AND NOT C
0000
0010
0100
0110
1000
1010
1101
1110

Section C · 3 marks each

C1Society: three situations3 marks · 20 questions
C1.1Practice3 marks

Name the cyber crime in each case:

  1. Riya gets a message, 'Your bank account is blocked, click here and enter your PIN.'
  2. Someone makes a fake profile in Aman's name and posts rude messages.
  3. A student is sent insulting messages every day in a class group.
Answer
  1. Phishing
  2. Identity theft (impersonation)
  3. Cyber bullying
C1.2Practice3 marks

Name the type of malware:

  1. It shows pop-up advertisements again and again.
  2. It locks the files and asks for Bitcoin to unlock them.
  3. It spreads through the network by copying itself without any user action.
Answer
  1. Adware
  2. Ransomware
  3. Worm
C1.3Practice3 marks

Which licence or software type is it?

  1. Software free to use for 30 days, then it must be bought.
  2. Software whose code can be changed, but changed copies must be shared under the same terms.
  3. Software free of cost but whose code is closed.
Answer
  1. Shareware
  2. GPL (copyleft, FOSS)
  3. Freeware
C1.4Practice3 marks

Which IPR protects it?

  1. The logo of a sports brand.
  2. A new design of an electric engine.
  3. A novel written by an author.
Answer
  1. Trademark
  2. Patent
  3. Copyright
C1.5Practice3 marks

Active or passive digital footprint?

  1. A comment you post on a video.
  2. The location saved by a map app in the background.
  3. An online form you fill and submit.
Answer
  1. Active
  2. Passive
  3. Active
C1.6Practice3 marks

What should be done?

  1. You find an old mobile phone that no longer works.
  2. You used a picture from the Internet in your project.
  3. You used the school computer to check your e-mail.
Answer
  1. Give it to an authorised e-waste collector or a take-back scheme
  2. Give credit to the source and check its licence
  3. Log out and close the browser
C1.7Practice3 marks

Ethical or unethical?

  1. A company hires an expert to break into its own network and report the weak points.
  2. A student copies a classmate's program and submits it as his own.
  3. Sharing a friend's photo without permission.
Answer
  1. Ethical hacking: ethical
  2. Plagiarism: unethical
  3. Breach of privacy: unethical
C1.8Practice3 marks

Name the term:

  1. The gap between people with and without access to the Internet.
  2. Tools like screen readers and Braille keyboards.
  3. Good manners on the Internet.
Answer
  1. Digital divide
  2. Assistive technology
  3. Netiquette
C1.9Practice3 marks

Identify the threat:

  1. A program records every key pressed to steal passwords.
  2. A free game is downloaded which secretly deletes files.
  3. Thousands of unwanted advertising e-mails fill the inbox.
Answer
  1. Keylogger (spyware)
  2. Trojan horse
  3. Spam
C1.10Practice3 marks

Suggest one safety measure for each:

  1. A weak password like 123456.
  2. Logging in on unknown public Wi-Fi.
  3. A link from an unknown sender.
Answer
  1. Use a strong password with letters, digits and symbols, and two-factor authentication
  2. Avoid banking on public Wi-Fi; use only https sites or mobile data
  3. Do not click it; delete and report the message
C1.11Practice3 marks

Which law or rule applies?

  1. Legal recognition of digital signatures in India.
  2. Copying a song and selling it without permission.
  3. Hacking into a bank server.
Answer
  1. IT Act, 2000
  2. Copyright infringement (piracy)
  3. Punishable cyber crime under the IT Act, 2000
C1.12Practice3 marks

Give one harmful effect of each:

  1. Burning e-waste in the open.
  2. Sharing your home address on social media.
  3. Using pirated software.
Answer
  1. Toxic fumes of lead and mercury harm health and air
  2. Strangers can find and harm you; privacy is lost
  3. It may contain malware, and it is illegal
C1.13Practice3 marks

Identify the crime:

  1. A caller pretends to be a bank officer and asks for an OTP.
  2. A website made to look like a popular shopping site collects card details.
  3. Abusive comments are posted on a stranger's post just to start a fight.
Answer
  1. Vishing (voice phishing)
  2. Phishing using a fake (spoofed) website
  3. Cyber trolling
C1.14Practice3 marks

Free/open source or proprietary?

  1. Linux (Ubuntu).
  2. MS Windows.
  3. Mozilla Firefox.
Answer
  1. FOSS
  2. Proprietary
  3. FOSS
C1.15Practice3 marks

Which is the right action?

  1. Your friend asks for your password.
  2. You receive an e-mail with an unknown .exe attachment.
  3. You want to use a CC-BY image in a poster.
Answer
  1. Do not share it
  2. Do not open it; scan or delete it
  3. Use it and give credit to the creator
C1.16Practice3 marks

Name the e-waste step:

  1. Using an old laptop for a younger sibling.
  2. Buying fewer gadgets.
  3. Sending broken devices to an authorised unit to take out metals.
Answer
  1. Reuse
  2. Reduce
  3. Recycle
C1.17Practice3 marks

Name the term:

  1. Small text files saved by a website.
  2. A system that blocks unsafe network traffic.
  3. Software that removes malware.
Answer
  1. Cookies
  2. Firewall
  3. Antivirus
C1.18Practice3 marks

Gender or disability issue, and one solution:

  1. Very few girls take part in the school coding club.
  2. A website has no captions for its videos.
  3. A blind student cannot read the school website.
Answer
  1. Gender issue: encourage girls with role models and equal access
  2. Disability issue: add captions for the hearing impaired
  3. Disability issue: make the site screen-reader friendly
C1.19Practice3 marks

Name the type of software:

  1. It runs the computer and manages hardware.
  2. It helps the OS use a new printer.
  3. It compresses files.
Answer
  1. Operating system (system software)
  2. Device driver
  3. Utility software
C1.20Practice3 marks

Identify the act:

  1. Using a photo with credit as the licence asks.
  2. Writing a project report by copying text from a website without credit.
  3. Selling copies of a movie.
Answer
  1. Fair, legal use (respecting the licence)
  2. Plagiarism
  3. Piracy (copyright infringement)
C2Python: output of a program3 marks · 80 questions
C2.1Practice3 marks

Write the output of the following code. Show your working in short.

msg = "KV Shalimar Bagh"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "#"
print(new)
print(len(new), new.count("#"))
Answer

Output:

kv#sHALIMAR#bAGH
16 2

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "KV Shalimar Bagh"msg = 'KV Shalimar Bagh'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'K'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'k'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'V'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'kv'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = ' '
5if ch.isupper():condition is False
7elif ch.islower():condition is False
9elif ch.isdigit():condition is False
12new = new + "#"new = 'kv#'

… the same steps continue for the remaining values.

C2.2Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Python is Easy"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "*"
print(new)
print(len(new), new.count("*"))
Answer

Output:

pYTHON*IS*eASY
14 2

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Python is Easy"msg = 'Python is Easy'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'P'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'p'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'y'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'pY'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = 't'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'pYT'

… the same steps continue for the remaining values.

C2.3Practice3 marks

Write the output of the following code. Show your working in short.

T = (33, 25, 28, 26, 12, 10)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(10, 26, 25, 66, 56, 24)
3 10

Working (step by step)

LineStatementValues after this stepPrinted
1T = (33, 25, 28, 26, 12, 10)T = (33, 25, 28, 26, 12, 10)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [66]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [25, 66]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [25, 66, 56]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [26, 25, 66, 56]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.4Practice3 marks

Write the output of the following code. Show your working in short.

msg = "India is Great"
new = ""
for i in range(len(msg)):
    if i % 2 == 0:
        new = new + msg[i].upper()
    elif msg[i] in "aeiouAEIOU":
        new = new + "*"
    else:
        new = new + msg[i - 1]
print(new)
Answer

Output:

IID*AaIi  R*Aa

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "India is Great"msg = 'India is Great'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'I'
3for i in range(len(msg)):i = 1
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'II'
3for i in range(len(msg)):i = 2
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'IID'
3for i in range(len(msg)):i = 3
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is True
7new = new + "*"new = 'IID*'

… the same steps continue for the remaining values.

C2.5Practice3 marks

Write the output of the following code. Show your working in short.

T = (12, 3, 30, 32, 16, 20)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(20, 32, 3, 24, 60, 32)
4 3

Working (step by step)

LineStatementValues after this stepPrinted
1T = (12, 3, 30, 32, 16, 20)T = (12, 3, 30, 32, 16, 20)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [24]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [3, 24]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [3, 24, 60]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [32, 3, 24, 60]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.6Practice3 marks

Write the output of the following code. Show your working in short.

n = 4
for i in range(n):
    s = ""
    for j in range(i + 1):
        s = s + chr(65 + i + j)
    print(s)
Answer

Output:

A
BC
CDE
DEFG

Working (step by step)

LineStatementValues after this stepPrinted
1n = 4n = 4
2for i in range(n):i = 0
3s = ""s = ''
4for j in range(i + 1):j = 0
5s = s + chr(65 + i + j)s = 'A'
4for j in range(i + 1):loop ends (no more values)
6print(s)A⏎
2for i in range(n):i = 1
3s = ""s = ''
4for j in range(i + 1):loop ends (no more values)
5s = s + chr(65 + i + j)s = 'B'
4for j in range(i + 1):j = 1
5s = s + chr(65 + i + j)s = 'BC'
4for j in range(i + 1):loop ends (no more values)
6print(s)BC⏎
2for i in range(n):i = 2

… the same steps continue for the remaining values.

C2.7Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Learn Code Daily"
new = ""
for i in range(len(msg)):
    if i % 2 == 0:
        new = new + msg[i].upper()
    elif msg[i] in "aeiouAEIOU":
        new = new + "*"
    else:
        new = new + msg[i - 1]
print(new)
Answer

Output:

L*AaNnC*D*  A*Ll

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Learn Code Daily"msg = 'Learn Code Daily'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'L'
3for i in range(len(msg)):i = 1
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is True
7new = new + "*"new = 'L*'
3for i in range(len(msg)):i = 2
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'L*A'
3for i in range(len(msg)):i = 3
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'L*Aa'

… the same steps continue for the remaining values.

C2.8Practice3 marks

Write the output of the following code. Show your working in short.

text = "CBSE Exam 2027"
D = {}
for ch in text.lower():
    if ch.isalpha():
        D[ch] = D.get(ch, 0) + 1
for k in sorted(D):
    if D[k] > 1:
        print(k, D[k])
Answer

Output:

e 2

Working (step by step)

LineStatementValues after this stepPrinted
1text = "CBSE Exam 2027"text = 'CBSE Exam 2027'
2D = {}D = {}
3for ch in text.lower():ch = 'c'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'c': 1}
3for ch in text.lower():ch = 'b'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'c': 1, 'b': 1}
3for ch in text.lower():ch = 's'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'c': 1, 'b': 1, 's': 1}
3for ch in text.lower():ch = 'e'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'c': 1, 'b': 1, 's': 1, 'e': 1}
3for ch in text.lower():ch = ' '
4if ch.isalpha():condition is False

… the same steps continue for the remaining values.

C2.9Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Bag': 20, 'Map': 80, 'Pen': 15, 'Ink': 10}
for item in stock:
    if stock[item] < 51:
        stock[item] += 10
stock["Glue"] = 7
del stock['Bag']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Map': 80, 'Pen': 25, 'Ink': 20, 'Glue': 7}
4 132

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Bag': 20, 'Map': 80, 'Pen': 15, 'Ink': 10}stock = {'Bag': 20, 'Map': 80, 'Pen': 15, 'Ink': 10}
2for item in stock:item = 'Bag'
3if stock[item] < 51:condition is True
4stock[item] += 10stock = {'Bag': 30, 'Map': 80, 'Pen': 15, 'Ink': 10}
2for item in stock:item = 'Map'
3if stock[item] < 51:condition is False
2for item in stock:item = 'Pen'
3if stock[item] < 51:condition is True
4stock[item] += 10stock = {'Bag': 30, 'Map': 80, 'Pen': 25, 'Ink': 10}
2for item in stock:item = 'Ink'
3if stock[item] < 51:condition is True
4stock[item] += 10stock = {'Bag': 30, 'Map': 80, 'Pen': 25, 'Ink': 20}
2for item in stock:loop ends (no more values)
5stock["Glue"] = 7stock = {'Bag': 30, 'Map': 80, 'Pen': 25, 'Ink': 20, 'Glue': 7}
6del stock['Bag']stock = {'Map': 80, 'Pen': 25, 'Ink': 20, 'Glue': 7}
7print(stock){'Map': 80, 'Pen': 25, 'Ink': 20, 'Glue': 7}⏎
8print(len(stock), sum(stock.values()))4 132⏎
C2.10Practice3 marks

Write the output of the following code. Show your working in short.

L = [19, 58, 39, 27, 22, 11]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::4], max(L) % 9)
Answer

Output:

[77, 97, 66, 49, 33, 11]
[77, 33] 7

Working (step by step)

LineStatementValues after this stepPrinted
1L = [19, 58, 39, 27, 22, 11]L = [19, 58, 39, 27, 22, 11]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [77, 58, 39, 27, 22, 11]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [77, 97, 39, 27, 22, 11]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [77, 97, 66, 27, 22, 11]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [77, 97, 66, 49, 22, 11]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [77, 97, 66, 49, 33, 11]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[77, 97, 66, 49, 33, 11]⏎
5print(L[::4], max(L) % 9)[77, 33] 7⏎
C2.11Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Vidyalaya"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "@"
print(new)
print(len(new), new.count("@"))
Answer

Output:

vIDYALAYA
9 0

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Vidyalaya"msg = 'Vidyalaya'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'V'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'v'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'i'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'vI'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = 'd'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'vID'

… the same steps continue for the remaining values.

C2.12Practice3 marks

Write the output of the following code. Show your working in short.

T = (32, 2, 28, 39, 30, 18)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(18, 39, 2, 64, 56, 60)
3 2

Working (step by step)

LineStatementValues after this stepPrinted
1T = (32, 2, 28, 39, 30, 18)T = (32, 2, 28, 39, 30, 18)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [64]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [2, 64]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [2, 64, 56]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [39, 2, 64, 56]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.13Practice3 marks

Write the output of the following code. Show your working in short.

n = 5
for i in range(1, n + 1):
    print(" " * (n - i), end="")
    for j in range(1, i + 1):
        print(j, end="")
    for j in range(i - 1, 0, -1):
        print(j, end="")
    print()
Answer

Output:

    1
   121
  12321
 1234321
123454321

Working (step by step)

LineStatementValues after this stepPrinted
1n = 5n = 5
2for i in range(1, n + 1):i = 1
3print(" " * (n - i), end="")
4for j in range(1, i + 1):j = 1
5print(j, end="")1
4for j in range(1, i + 1):loop ends (no more values)
6for j in range(i - 1, 0, -1):loop ends (no more values)
8print()⏎
2for i in range(1, n + 1):i = 2
3print(" " * (n - i), end="")
4for j in range(1, i + 1):loop ends (no more values)
5print(j, end="")1
4for j in range(1, i + 1):j = 2
5print(j, end="")2
4for j in range(1, i + 1):loop ends (no more values)
6for j in range(i - 1, 0, -1):j = 1

… the same steps continue for the remaining values.

C2.14Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Data and Logic"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "*"
print(new)
print(len(new), new.count("*"))
Answer

Output:

dATA*AND*lOGIC
14 2

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Data and Logic"msg = 'Data and Logic'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'D'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'd'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'a'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'dA'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = 't'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'dAT'

… the same steps continue for the remaining values.

C2.15Practice3 marks

Write the output of the following code. Show your working in short.

L = [25, 56, 36, 49, 39, 47]
A, B = [], []
for x in L:
    if x % 4 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[56, 36]
[47, 39, 49, 25]
[56, 36, 47, 39]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [25, 56, 36, 49, 39, 47]L = [25, 56, 36, 49, 39, 47]
2A, B = [], []A = [], B = []
3for x in L:x = 25
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [25]
3for x in L:x = 56
4if x % 4 == 0:condition is True
5A.append(x)A = [56]
3for x in L:x = 36
4if x % 4 == 0:condition is True
5A.append(x)A = [56, 36]
3for x in L:x = 49
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [49, 25]
3for x in L:x = 39
4if x % 4 == 0:condition is False

… the same steps continue for the remaining values.

C2.16Practice3 marks

Write the output of the following code. Show your working in short.

a, b = 0, 1
for i in range(7):
    print(a, end=" ")
    a, b = b, a + b
print()
print(a + b)
Answer

Output:

0 1 1 2 3 5 8 
34

Working (step by step)

LineStatementValues after this stepPrinted
1a, b = 0, 1a = 0, b = 1
2for i in range(7):i = 0
3print(a, end=" ")0
4a, b = b, a + ba = 1
2for i in range(7):i = 1
3print(a, end=" ")1
4a, b = b, a + bb = 2
2for i in range(7):i = 2
3print(a, end=" ")1
4a, b = b, a + ba = 2, b = 3
2for i in range(7):i = 3
3print(a, end=" ")2
4a, b = b, a + ba = 3, b = 5
2for i in range(7):i = 4
3print(a, end=" ")3
4a, b = b, a + ba = 5, b = 8

… the same steps continue for the remaining values.

C2.17Practice3 marks

Write the output of the following code. Show your working in short.

L = [43, 5, 44, 49, 9, 37]
A, B = [], []
for x in L:
    if x % 2 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[44]
[37, 9, 49, 5, 43]
[44, 37, 9]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [43, 5, 44, 49, 9, 37]L = [43, 5, 44, 49, 9, 37]
2A, B = [], []A = [], B = []
3for x in L:x = 43
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [43]
3for x in L:x = 5
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [5, 43]
3for x in L:x = 44
4if x % 2 == 0:condition is True
5A.append(x)A = [44]
3for x in L:x = 49
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [49, 5, 43]
3for x in L:x = 9
4if x % 2 == 0:condition is False

… the same steps continue for the remaining values.

C2.18Practice3 marks

Write the output of the following code. Show your working in short.

L = [12, 22, 29, 35, 46, 32]
A, B = [], []
for x in L:
    if x % 4 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[12, 32]
[46, 35, 29, 22]
[12, 32, 46, 35]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [12, 22, 29, 35, 46, 32]L = [12, 22, 29, 35, 46, 32]
2A, B = [], []A = [], B = []
3for x in L:x = 12
4if x % 4 == 0:condition is True
5A.append(x)A = [12]
3for x in L:x = 22
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [22]
3for x in L:x = 29
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [29, 22]
3for x in L:x = 35
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [35, 29, 22]
3for x in L:x = 46
4if x % 4 == 0:condition is False

… the same steps continue for the remaining values.

C2.19Practice3 marks

Write the output of the following code. Show your working in short.

msg = "DATABASE"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "#"
print(new)
print(len(new), new.count("#"))
Answer

Output:

database
8 0

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "DATABASE"msg = 'DATABASE'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'D'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'd'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'A'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'da'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = 'T'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'dat'
3for i in range(len(msg)):i = 3
4ch = msg[i]ch = 'A'

… the same steps continue for the remaining values.

C2.20Practice3 marks

Write the output of the following code. Show your working in short.

text = "Keep Calm and Code"
D = {}
for ch in text.lower():
    if ch.isalpha():
        D[ch] = D.get(ch, 0) + 1
for k in sorted(D):
    if D[k] > 1:
        print(k, D[k])
Answer

Output:

a 2
c 2
d 2
e 3

Working (step by step)

LineStatementValues after this stepPrinted
1text = "Keep Calm and Code"text = 'Keep Calm and Code'
2D = {}D = {}
3for ch in text.lower():ch = 'k'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'k': 1}
3for ch in text.lower():ch = 'e'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'k': 1, 'e': 1}
3for ch in text.lower():loop ends (no more values)
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'k': 1, 'e': 2}
3for ch in text.lower():ch = 'p'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'k': 1, 'e': 2, 'p': 1}
3for ch in text.lower():ch = ' '
4if ch.isalpha():condition is False

… the same steps continue for the remaining values.

C2.21Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Map': 75, 'Bag': 85, 'Ink': 65, 'Book': 70}
for item in stock:
    if stock[item] < 37:
        stock[item] += 10
stock["Glue"] = 17
del stock['Bag']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Map': 75, 'Ink': 65, 'Book': 70, 'Glue': 17}
4 227

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Map': 75, 'Bag': 85, 'Ink': 65, 'Book': 70}stock = {'Map': 75, 'Bag': 85, 'Ink': 65, 'Book': 70}
2for item in stock:item = 'Map'
3if stock[item] < 37:condition is False
2for item in stock:item = 'Bag'
3if stock[item] < 37:condition is False
2for item in stock:item = 'Ink'
3if stock[item] < 37:condition is False
2for item in stock:item = 'Book'
3if stock[item] < 37:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 17stock = {'Map': 75, 'Bag': 85, 'Ink': 65, 'Book': 70, 'Glue': 17}
6del stock['Bag']stock = {'Map': 75, 'Ink': 65, 'Book': 70, 'Glue': 17}
7print(stock){'Map': 75, 'Ink': 65, 'Book': 70, 'Glue': 17}⏎
8print(len(stock), sum(stock.values()))4 227⏎
C2.22Practice3 marks

Write the output of the following code. Show your working in short.

a, b = 0, 1
for i in range(8):
    print(a, end=" ")
    a, b = b, a + b
print()
print(a + b)
Answer

Output:

0 1 1 2 3 5 8 13 
55

Working (step by step)

LineStatementValues after this stepPrinted
1a, b = 0, 1a = 0, b = 1
2for i in range(8):i = 0
3print(a, end=" ")0
4a, b = b, a + ba = 1
2for i in range(8):i = 1
3print(a, end=" ")1
4a, b = b, a + bb = 2
2for i in range(8):i = 2
3print(a, end=" ")1
4a, b = b, a + ba = 2, b = 3
2for i in range(8):i = 3
3print(a, end=" ")2
4a, b = b, a + ba = 3, b = 5
2for i in range(8):i = 4
3print(a, end=" ")3
4a, b = b, a + ba = 5, b = 8

… the same steps continue for the remaining values.

C2.23Practice3 marks

Write the output of the following code. Show your working in short.

T = (11, 37, 8, 35, 27, 31)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(31, 35, 37, 22, 16, 54)
5 16

Working (step by step)

LineStatementValues after this stepPrinted
1T = (11, 37, 8, 35, 27, 31)T = (11, 37, 8, 35, 27, 31)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [22]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [37, 22]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [37, 22, 16]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [35, 37, 22, 16]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.24Practice3 marks

Write the output of the following code. Show your working in short.

L = [10, 18, 16, 12, 41, 6]
A, B = [], []
for x in L:
    if x % 2 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[10, 18, 16, 12, 6]
[41]
[10, 18, 16, 12, 6, 41]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [10, 18, 16, 12, 41, 6]L = [10, 18, 16, 12, 41, 6]
2A, B = [], []A = [], B = []
3for x in L:x = 10
4if x % 2 == 0:condition is True
5A.append(x)A = [10]
3for x in L:x = 18
4if x % 2 == 0:condition is True
5A.append(x)A = [10, 18]
3for x in L:x = 16
4if x % 2 == 0:condition is True
5A.append(x)A = [10, 18, 16]
3for x in L:x = 12
4if x % 2 == 0:condition is True
5A.append(x)A = [10, 18, 16, 12]
3for x in L:x = 41
4if x % 2 == 0:condition is False

… the same steps continue for the remaining values.

C2.25Practice3 marks

Write the output of the following code. Show your working in short.

msg = "KV Shalimar Bagh"
new = ""
for i in range(len(msg)):
    if i % 2 == 0:
        new = new + msg[i].upper()
    elif msg[i] in "aeiouAEIOU":
        new = new + "*"
    else:
        new = new + msg[i - 1]
print(new)
Answer

Output:

KK  H*L*M*RrB*Gg

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "KV Shalimar Bagh"msg = 'KV Shalimar Bagh'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'K'
3for i in range(len(msg)):i = 1
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'KK'
3for i in range(len(msg)):i = 2
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'KK '
3for i in range(len(msg)):i = 3
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'KK '

… the same steps continue for the remaining values.

C2.26Practice3 marks

Write the output of the following code. Show your working in short.

L = [51, 9, 54, 48, 38, 44]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::3], max(L) % 9)
Answer

Output:

[60, 63, 102, 86, 82, 44]
[60, 86] 3

Working (step by step)

LineStatementValues after this stepPrinted
1L = [51, 9, 54, 48, 38, 44]L = [51, 9, 54, 48, 38, 44]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [60, 9, 54, 48, 38, 44]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [60, 63, 54, 48, 38, 44]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [60, 63, 102, 48, 38, 44]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [60, 63, 102, 86, 38, 44]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [60, 63, 102, 86, 82, 44]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[60, 63, 102, 86, 82, 44]⏎
5print(L[::3], max(L) % 9)[60, 86] 3⏎
C2.27Practice3 marks

Write the output of the following code. Show your working in short.

text = "Learn Code Daily"
D = {}
for ch in text.lower():
    if ch.isalpha():
        D[ch] = D.get(ch, 0) + 1
for k in sorted(D):
    if D[k] > 1:
        print(k, D[k])
Answer

Output:

a 2
d 2
e 2
l 2

Working (step by step)

LineStatementValues after this stepPrinted
1text = "Learn Code Daily"text = 'Learn Code Daily'
2D = {}D = {}
3for ch in text.lower():ch = 'l'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'l': 1}
3for ch in text.lower():ch = 'e'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'l': 1, 'e': 1}
3for ch in text.lower():ch = 'a'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'l': 1, 'e': 1, 'a': 1}
3for ch in text.lower():ch = 'r'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'l': 1, 'e': 1, 'a': 1, 'r': 1}
3for ch in text.lower():ch = 'n'
4if ch.isalpha():condition is True

… the same steps continue for the remaining values.

C2.28Practice3 marks

Write the output of the following code. Show your working in short.

L = [14, 50, 45, 26, 29, 56]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::2], max(L) % 7)
Answer

Output:

[64, 95, 71, 55, 85, 56]
[64, 71, 85] 4

Working (step by step)

LineStatementValues after this stepPrinted
1L = [14, 50, 45, 26, 29, 56]L = [14, 50, 45, 26, 29, 56]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [64, 50, 45, 26, 29, 56]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [64, 95, 45, 26, 29, 56]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [64, 95, 71, 26, 29, 56]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [64, 95, 71, 55, 29, 56]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [64, 95, 71, 55, 85, 56]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[64, 95, 71, 55, 85, 56]⏎
5print(L[::2], max(L) % 7)[64, 71, 85] 4⏎
C2.29Practice3 marks

Write the output of the following code. Show your working in short.

text = "India is Great"
D = {}
for ch in text.lower():
    if ch.isalpha():
        D[ch] = D.get(ch, 0) + 1
for k in sorted(D):
    if D[k] > 1:
        print(k, D[k])
Answer

Output:

a 2
i 3

Working (step by step)

LineStatementValues after this stepPrinted
1text = "India is Great"text = 'India is Great'
2D = {}D = {}
3for ch in text.lower():ch = 'i'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'i': 1}
3for ch in text.lower():ch = 'n'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'i': 1, 'n': 1}
3for ch in text.lower():ch = 'd'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'i': 1, 'n': 1, 'd': 1}
3for ch in text.lower():ch = 'i'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'i': 2, 'n': 1, 'd': 1}
3for ch in text.lower():ch = 'a'
4if ch.isalpha():condition is True

… the same steps continue for the remaining values.

C2.30Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Function"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "#"
print(new)
print(len(new), new.count("#"))
Answer

Output:

fUNCTION
8 0

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Function"msg = 'Function'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'F'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'f'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'u'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'fU'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = 'n'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'fUN'

… the same steps continue for the remaining values.

C2.31Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Learning"
new = ""
for i in range(len(msg)):
    if i % 2 == 0:
        new = new + msg[i].upper()
    elif msg[i] in "aeiouAEIOU":
        new = new + "*"
    else:
        new = new + msg[i - 1]
print(new)
Answer

Output:

L*AaN*Nn

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Learning"msg = 'Learning'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'L'
3for i in range(len(msg)):i = 1
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is True
7new = new + "*"new = 'L*'
3for i in range(len(msg)):i = 2
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'L*A'
3for i in range(len(msg)):i = 3
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'L*Aa'

… the same steps continue for the remaining values.

C2.32Practice3 marks

Write the output of the following code. Show your working in short.

text = "Save Water Save Life"
D = {}
for ch in text.lower():
    if ch.isalpha():
        D[ch] = D.get(ch, 0) + 1
for k in sorted(D):
    if D[k] > 1:
        print(k, D[k])
Answer

Output:

a 3
e 4
s 2
v 2

Working (step by step)

LineStatementValues after this stepPrinted
1text = "Save Water Save Life"text = 'Save Water Save Life'
2D = {}D = {}
3for ch in text.lower():ch = 's'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'s': 1}
3for ch in text.lower():ch = 'a'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'s': 1, 'a': 1}
3for ch in text.lower():ch = 'v'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'s': 1, 'a': 1, 'v': 1}
3for ch in text.lower():ch = 'e'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'s': 1, 'a': 1, 'v': 1, 'e': 1}
3for ch in text.lower():ch = ' '
4if ch.isalpha():condition is False

… the same steps continue for the remaining values.

C2.33Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Python is Easy"
new = ""
for i in range(len(msg)):
    if i % 2 == 0:
        new = new + msg[i].upper()
    elif msg[i] in "aeiouAEIOU":
        new = new + "#"
    else:
        new = new + msg[i - 1]
print(new)
Answer

Output:

PPTtOo #SsE#Ss

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Python is Easy"msg = 'Python is Easy'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'P'
3for i in range(len(msg)):i = 1
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'PP'
3for i in range(len(msg)):i = 2
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'PPT'
3for i in range(len(msg)):i = 3
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'PPTt'

… the same steps continue for the remaining values.

C2.34Practice3 marks

Write the output of the following code. Show your working in short.

T = (30, 7, 29, 33, 39, 14)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(14, 33, 7, 60, 58, 78)
5 7

Working (step by step)

LineStatementValues after this stepPrinted
1T = (30, 7, 29, 33, 39, 14)T = (30, 7, 29, 33, 39, 14)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [60]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [7, 60]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [7, 60, 58]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [33, 7, 60, 58]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.35Practice3 marks

Write the output of the following code. Show your working in short.

L = [37, 22, 7, 16, 58, 34]
A, B = [], []
for x in L:
    if x % 4 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[16]
[34, 58, 7, 22, 37]
[16, 34, 58]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [37, 22, 7, 16, 58, 34]L = [37, 22, 7, 16, 58, 34]
2A, B = [], []A = [], B = []
3for x in L:x = 37
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [37]
3for x in L:x = 22
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [22, 37]
3for x in L:x = 7
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [7, 22, 37]
3for x in L:x = 16
4if x % 4 == 0:condition is True
5A.append(x)A = [16]
3for x in L:x = 58
4if x % 4 == 0:condition is False

… the same steps continue for the remaining values.

C2.36Practice3 marks

Write the output of the following code. Show your working in short.

L = [25, 59, 44, 17, 27, 57]
A, B = [], []
for x in L:
    if x % 2 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[44]
[57, 27, 17, 59, 25]
[44, 57, 27]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [25, 59, 44, 17, 27, 57]L = [25, 59, 44, 17, 27, 57]
2A, B = [], []A = [], B = []
3for x in L:x = 25
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [25]
3for x in L:x = 59
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [59, 25]
3for x in L:x = 44
4if x % 2 == 0:condition is True
5A.append(x)A = [44]
3for x in L:x = 17
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [17, 59, 25]
3for x in L:x = 27
4if x % 2 == 0:condition is False

… the same steps continue for the remaining values.

C2.37Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Map': 45, 'Ruler': 30, 'Ink': 55, 'Pen': 35}
for item in stock:
    if stock[item] < 49:
        stock[item] += 10
stock["Glue"] = 13
del stock['Map']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Ruler': 40, 'Ink': 55, 'Pen': 45, 'Glue': 13}
4 153

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Map': 45, 'Ruler': 30, 'Ink': 55, 'Pen': 35}stock = {'Map': 45, 'Ruler': 30, 'Ink': 55, 'Pen': 35}
2for item in stock:item = 'Map'
3if stock[item] < 49:condition is True
4stock[item] += 10stock = {'Map': 55, 'Ruler': 30, 'Ink': 55, 'Pen': 35}
2for item in stock:item = 'Ruler'
3if stock[item] < 49:condition is True
4stock[item] += 10stock = {'Map': 55, 'Ruler': 40, 'Ink': 55, 'Pen': 35}
2for item in stock:item = 'Ink'
3if stock[item] < 49:condition is False
2for item in stock:item = 'Pen'
3if stock[item] < 49:condition is True
4stock[item] += 10stock = {'Map': 55, 'Ruler': 40, 'Ink': 55, 'Pen': 45}
2for item in stock:loop ends (no more values)
5stock["Glue"] = 13stock = {'Map': 55, 'Ruler': 40, 'Ink': 55, 'Pen': 45, 'Glue': 13}
6del stock['Map']stock = {'Ruler': 40, 'Ink': 55, 'Pen': 45, 'Glue': 13}
7print(stock){'Ruler': 40, 'Ink': 55, 'Pen': 45, 'Glue': 13}⏎
8print(len(stock), sum(stock.values()))4 153⏎
C2.38Practice3 marks

Write the output of the following code. Show your working in short.

T = (37, 5, 20, 39, 22, 21)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(21, 39, 5, 74, 40, 44)
3 5

Working (step by step)

LineStatementValues after this stepPrinted
1T = (37, 5, 20, 39, 22, 21)T = (37, 5, 20, 39, 22, 21)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [74]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [5, 74]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [5, 74, 40]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [39, 5, 74, 40]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.39Practice3 marks

Write the output of the following code. Show your working in short.

T = (2, 22, 31, 28, 1, 19)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(19, 28, 22, 4, 62, 2)
4 2

Working (step by step)

LineStatementValues after this stepPrinted
1T = (2, 22, 31, 28, 1, 19)T = (2, 22, 31, 28, 1, 19)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [4]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [22, 4]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [22, 4, 62]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [28, 22, 4, 62]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.40Practice3 marks

Write the output of the following code. Show your working in short.

T = (6, 16, 2, 30, 18, 8)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(8, 30, 16, 12, 4, 36)
5 4

Working (step by step)

LineStatementValues after this stepPrinted
1T = (6, 16, 2, 30, 18, 8)T = (6, 16, 2, 30, 18, 8)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [12]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [16, 12]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [16, 12, 4]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [30, 16, 12, 4]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.41Practice3 marks

Write the output of the following code. Show your working in short.

T = (30, 33, 9, 36, 17, 19)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(19, 36, 33, 60, 18, 34)
3 18

Working (step by step)

LineStatementValues after this stepPrinted
1T = (30, 33, 9, 36, 17, 19)T = (30, 33, 9, 36, 17, 19)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [60]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [33, 60]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [33, 60, 18]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [36, 33, 60, 18]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.42Practice3 marks

Write the output of the following code. Show your working in short.

T = (25, 39, 29, 12, 33, 26)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(26, 12, 39, 50, 58, 66)
5 12

Working (step by step)

LineStatementValues after this stepPrinted
1T = (25, 39, 29, 12, 33, 26)T = (25, 39, 29, 12, 33, 26)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [50]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [39, 50]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [39, 50, 58]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [12, 39, 50, 58]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.43Practice3 marks

Write the output of the following code. Show your working in short.

msg = "DATABASE"
new = ""
for i in range(len(msg)):
    if i % 2 == 0:
        new = new + msg[i].upper()
    elif msg[i] in "aeiouAEIOU":
        new = new + "@"
    else:
        new = new + msg[i - 1]
print(new)
Answer

Output:

D@T@B@S@

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "DATABASE"msg = 'DATABASE'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'D'
3for i in range(len(msg)):i = 1
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is True
7new = new + "@"new = 'D@'
3for i in range(len(msg)):i = 2
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'D@T'
3for i in range(len(msg)):i = 3
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is True
7new = new + "@"new = 'D@T@'

… the same steps continue for the remaining values.

C2.44Practice3 marks

Write the output of the following code. Show your working in short.

text = "Practice makes Perfect"
D = {}
for ch in text.lower():
    if ch.isalpha():
        D[ch] = D.get(ch, 0) + 1
for k in sorted(D):
    if D[k] > 1:
        print(k, D[k])
Answer

Output:

a 2
c 3
e 4
p 2
r 2
t 2

Working (step by step)

LineStatementValues after this stepPrinted
1text = "Practice makes Perfect"text = 'Practice makes Perfect'
2D = {}D = {}
3for ch in text.lower():ch = 'p'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'p': 1}
3for ch in text.lower():ch = 'r'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'p': 1, 'r': 1}
3for ch in text.lower():ch = 'a'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'p': 1, 'r': 1, 'a': 1}
3for ch in text.lower():ch = 'c'
4if ch.isalpha():condition is True
5D[ch] = D.get(ch, 0) + 1D = {'p': 1, 'r': 1, 'a': 1, 'c': 1}
3for ch in text.lower():ch = 't'
4if ch.isalpha():condition is True

… the same steps continue for the remaining values.

C2.45Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Bag': 50, 'Ruler': 15, 'Map': 80, 'Ink': 65}
for item in stock:
    if stock[item] < 38:
        stock[item] += 10
stock["Glue"] = 12
del stock['Ink']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Bag': 50, 'Ruler': 25, 'Map': 80, 'Glue': 12}
4 167

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Bag': 50, 'Ruler': 15, 'Map': 80, 'Ink': 65}stock = {'Bag': 50, 'Ruler': 15, 'Map': 80, 'Ink': 65}
2for item in stock:item = 'Bag'
3if stock[item] < 38:condition is False
2for item in stock:item = 'Ruler'
3if stock[item] < 38:condition is True
4stock[item] += 10stock = {'Bag': 50, 'Ruler': 25, 'Map': 80, 'Ink': 65}
2for item in stock:item = 'Map'
3if stock[item] < 38:condition is False
2for item in stock:item = 'Ink'
3if stock[item] < 38:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 12stock = {'Bag': 50, 'Ruler': 25, 'Map': 80, 'Ink': 65, 'Glue': 12}
6del stock['Ink']stock = {'Bag': 50, 'Ruler': 25, 'Map': 80, 'Glue': 12}
7print(stock){'Bag': 50, 'Ruler': 25, 'Map': 80, 'Glue': 12}⏎
8print(len(stock), sum(stock.values()))4 167⏎
C2.46Practice3 marks

Write the output of the following code. Show your working in short.

L = [37, 40, 12, 8, 50, 44]
A, B = [], []
for x in L:
    if x % 4 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[40, 12, 8, 44]
[50, 37]
[40, 12, 8, 44, 50, 37]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [37, 40, 12, 8, 50, 44]L = [37, 40, 12, 8, 50, 44]
2A, B = [], []A = [], B = []
3for x in L:x = 37
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [37]
3for x in L:x = 40
4if x % 4 == 0:condition is True
5A.append(x)A = [40]
3for x in L:x = 12
4if x % 4 == 0:condition is True
5A.append(x)A = [40, 12]
3for x in L:x = 8
4if x % 4 == 0:condition is True
5A.append(x)A = [40, 12, 8]
3for x in L:x = 50
4if x % 4 == 0:condition is False

… the same steps continue for the remaining values.

C2.47Practice3 marks

Write the output of the following code. Show your working in short.

L = [44, 8, 57, 59, 10, 29]
A, B = [], []
for x in L:
    if x % 3 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[57]
[29, 10, 59, 8, 44]
[57, 29, 10]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [44, 8, 57, 59, 10, 29]L = [44, 8, 57, 59, 10, 29]
2A, B = [], []A = [], B = []
3for x in L:x = 44
4if x % 3 == 0:condition is False
7B.insert(0, x)B = [44]
3for x in L:x = 8
4if x % 3 == 0:condition is False
7B.insert(0, x)B = [8, 44]
3for x in L:x = 57
4if x % 3 == 0:condition is True
5A.append(x)A = [57]
3for x in L:x = 59
4if x % 3 == 0:condition is False
7B.insert(0, x)B = [59, 8, 44]
3for x in L:x = 10
4if x % 3 == 0:condition is False

… the same steps continue for the remaining values.

C2.48Practice3 marks

Write the output of the following code. Show your working in short.

T = (20, 29, 30, 19, 39, 32)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(32, 19, 29, 40, 60, 78)
5 19

Working (step by step)

LineStatementValues after this stepPrinted
1T = (20, 29, 30, 19, 39, 32)T = (20, 29, 30, 19, 39, 32)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [40]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [29, 40]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [29, 40, 60]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [19, 29, 40, 60]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.49Practice3 marks

Write the output of the following code. Show your working in short.

L = [19, 34, 21, 38, 58, 15]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::2], max(L) % 5)
Answer

Output:

[53, 55, 59, 96, 73, 15]
[53, 59, 73] 1

Working (step by step)

LineStatementValues after this stepPrinted
1L = [19, 34, 21, 38, 58, 15]L = [19, 34, 21, 38, 58, 15]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [53, 34, 21, 38, 58, 15]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [53, 55, 21, 38, 58, 15]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [53, 55, 59, 38, 58, 15]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [53, 55, 59, 96, 58, 15]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [53, 55, 59, 96, 73, 15]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[53, 55, 59, 96, 73, 15]⏎
5print(L[::2], max(L) % 5)[53, 59, 73] 1⏎
C2.50Practice3 marks

Write the output of the following code. Show your working in short.

T = (17, 18, 35, 3, 31, 23)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(23, 3, 18, 34, 70, 62)
4 3

Working (step by step)

LineStatementValues after this stepPrinted
1T = (17, 18, 35, 3, 31, 23)T = (17, 18, 35, 3, 31, 23)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [34]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [18, 34]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [18, 34, 70]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [3, 18, 34, 70]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.51Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Keep Calm and Code"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "@"
print(new)
print(len(new), new.count("@"))
Answer

Output:

kEEP@cALM@AND@cODE
18 3

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Keep Calm and Code"msg = 'Keep Calm and Code'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'K'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'k'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'e'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'kE'
3for i in range(len(msg)):i = 2
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'kEE'
3for i in range(len(msg)):i = 3

… the same steps continue for the remaining values.

C2.52Practice3 marks

Write the output of the following code. Show your working in short.

T = (16, 33, 9, 35, 14, 2)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(2, 35, 33, 32, 18, 28)
1 2

Working (step by step)

LineStatementValues after this stepPrinted
1T = (16, 33, 9, 35, 14, 2)T = (16, 33, 9, 35, 14, 2)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [32]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [33, 32]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [33, 32, 18]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [35, 33, 32, 18]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.53Practice3 marks

Write the output of the following code. Show your working in short.

L = [8, 45, 30, 23, 46, 42]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::4], max(L) % 5)
Answer

Output:

[53, 75, 53, 69, 88, 42]
[53, 88] 3

Working (step by step)

LineStatementValues after this stepPrinted
1L = [8, 45, 30, 23, 46, 42]L = [8, 45, 30, 23, 46, 42]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [53, 45, 30, 23, 46, 42]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [53, 75, 30, 23, 46, 42]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [53, 75, 53, 23, 46, 42]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [53, 75, 53, 69, 46, 42]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [53, 75, 53, 69, 88, 42]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[53, 75, 53, 69, 88, 42]⏎
5print(L[::4], max(L) % 5)[53, 88] 3⏎
C2.54Practice3 marks

Write the output of the following code. Show your working in short.

msg = "keyboard"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "@"
print(new)
print(len(new), new.count("@"))
Answer

Output:

KEYBOARD
8 0

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "keyboard"msg = 'keyboard'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'k'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'K'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'e'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'KE'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = 'y'
5if ch.isupper():condition is False
7elif ch.islower():condition is True

… the same steps continue for the remaining values.

C2.55Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Pen': 50, 'Bag': 10, 'Ruler': 55, 'Map': 20}
for item in stock:
    if stock[item] < 32:
        stock[item] += 10
stock["Glue"] = 15
del stock['Ruler']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Pen': 50, 'Bag': 20, 'Map': 30, 'Glue': 15}
4 115

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Pen': 50, 'Bag': 10, 'Ruler': 55, 'Map': 20}stock = {'Pen': 50, 'Bag': 10, 'Ruler': 55, 'Map': 20}
2for item in stock:item = 'Pen'
3if stock[item] < 32:condition is False
2for item in stock:item = 'Bag'
3if stock[item] < 32:condition is True
4stock[item] += 10stock = {'Pen': 50, 'Bag': 20, 'Ruler': 55, 'Map': 20}
2for item in stock:item = 'Ruler'
3if stock[item] < 32:condition is False
2for item in stock:item = 'Map'
3if stock[item] < 32:condition is True
4stock[item] += 10stock = {'Pen': 50, 'Bag': 20, 'Ruler': 55, 'Map': 30}
2for item in stock:loop ends (no more values)
5stock["Glue"] = 15stock = {'Pen': 50, 'Bag': 20, 'Ruler': 55, 'Map': 30, 'Glue': 15}
6del stock['Ruler']stock = {'Pen': 50, 'Bag': 20, 'Map': 30, 'Glue': 15}
7print(stock){'Pen': 50, 'Bag': 20, 'Map': 30, 'Glue': 15}⏎
8print(len(stock), sum(stock.values()))4 115⏎
C2.56Practice3 marks

Write the output of the following code. Show your working in short.

L = [36, 16, 59, 19, 38, 10]
A, B = [], []
for x in L:
    if x % 2 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[36, 16, 38, 10]
[19, 59]
[36, 16, 38, 10, 19, 59]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [36, 16, 59, 19, 38, 10]L = [36, 16, 59, 19, 38, 10]
2A, B = [], []A = [], B = []
3for x in L:x = 36
4if x % 2 == 0:condition is True
5A.append(x)A = [36]
3for x in L:x = 16
4if x % 2 == 0:condition is True
5A.append(x)A = [36, 16]
3for x in L:x = 59
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [59]
3for x in L:x = 19
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [19, 59]
3for x in L:x = 38
4if x % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.57Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Map': 65, 'Bag': 70, 'Ink': 50, 'Book': 20}
for item in stock:
    if stock[item] < 53:
        stock[item] += 10
stock["Glue"] = 14
del stock['Book']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Map': 65, 'Bag': 70, 'Ink': 60, 'Glue': 14}
4 209

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Map': 65, 'Bag': 70, 'Ink': 50, 'Book': 20}stock = {'Map': 65, 'Bag': 70, 'Ink': 50, 'Book': 20}
2for item in stock:item = 'Map'
3if stock[item] < 53:condition is False
2for item in stock:item = 'Bag'
3if stock[item] < 53:condition is False
2for item in stock:item = 'Ink'
3if stock[item] < 53:condition is True
4stock[item] += 10stock = {'Map': 65, 'Bag': 70, 'Ink': 60, 'Book': 20}
2for item in stock:item = 'Book'
3if stock[item] < 53:condition is True
4stock[item] += 10stock = {'Map': 65, 'Bag': 70, 'Ink': 60, 'Book': 30}
2for item in stock:loop ends (no more values)
5stock["Glue"] = 14stock = {'Map': 65, 'Bag': 70, 'Ink': 60, 'Book': 30, 'Glue': 14}
6del stock['Book']stock = {'Map': 65, 'Bag': 70, 'Ink': 60, 'Glue': 14}
7print(stock){'Map': 65, 'Bag': 70, 'Ink': 60, 'Glue': 14}⏎
8print(len(stock), sum(stock.values()))4 209⏎
C2.58Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Network"
new = ""
for i in range(len(msg)):
    if i % 2 == 0:
        new = new + msg[i].upper()
    elif msg[i] in "aeiouAEIOU":
        new = new + "*"
    else:
        new = new + msg[i - 1]
print(new)
Answer

Output:

N*TtOoK

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Network"msg = 'Network'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'N'
3for i in range(len(msg)):i = 1
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is True
7new = new + "*"new = 'N*'
3for i in range(len(msg)):i = 2
4if i % 2 == 0:condition is True
5new = new + msg[i].upper()new = 'N*T'
3for i in range(len(msg)):i = 3
4if i % 2 == 0:condition is False
6elif msg[i] in "aeiouAEIOU":condition is False
9new = new + msg[i - 1]new = 'N*Tt'

… the same steps continue for the remaining values.

C2.59Practice3 marks

Write the output of the following code. Show your working in short.

L = [32, 50, 7, 23, 56, 31]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::3], max(L) % 8)
Answer

Output:

[82, 57, 30, 79, 87, 31]
[82, 79] 7

Working (step by step)

LineStatementValues after this stepPrinted
1L = [32, 50, 7, 23, 56, 31]L = [32, 50, 7, 23, 56, 31]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [82, 50, 7, 23, 56, 31]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [82, 57, 7, 23, 56, 31]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [82, 57, 30, 23, 56, 31]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [82, 57, 30, 79, 56, 31]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [82, 57, 30, 79, 87, 31]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[82, 57, 30, 79, 87, 31]⏎
5print(L[::3], max(L) % 8)[82, 79] 7⏎
C2.60Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Ink': 85, 'Pen': 60, 'Map': 45, 'Book': 80}
for item in stock:
    if stock[item] < 38:
        stock[item] += 10
stock["Glue"] = 9
del stock['Ink']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Pen': 60, 'Map': 45, 'Book': 80, 'Glue': 9}
4 194

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Ink': 85, 'Pen': 60, 'Map': 45, 'Book': 80}stock = {'Ink': 85, 'Pen': 60, 'Map': 45, 'Book': 80}
2for item in stock:item = 'Ink'
3if stock[item] < 38:condition is False
2for item in stock:item = 'Pen'
3if stock[item] < 38:condition is False
2for item in stock:item = 'Map'
3if stock[item] < 38:condition is False
2for item in stock:item = 'Book'
3if stock[item] < 38:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 9stock = {'Ink': 85, 'Pen': 60, 'Map': 45, 'Book': 80, 'Glue': 9}
6del stock['Ink']stock = {'Pen': 60, 'Map': 45, 'Book': 80, 'Glue': 9}
7print(stock){'Pen': 60, 'Map': 45, 'Book': 80, 'Glue': 9}⏎
8print(len(stock), sum(stock.values()))4 194⏎
C2.61Practice3 marks

Write the output of the following code. Show your working in short.

n = 3
for i in range(n):
    s = ""
    for j in range(i + 1):
        s = s + chr(65 + i + j)
    print(s)
Answer

Output:

A
BC
CDE

Working (step by step)

LineStatementValues after this stepPrinted
1n = 3n = 3
2for i in range(n):i = 0
3s = ""s = ''
4for j in range(i + 1):j = 0
5s = s + chr(65 + i + j)s = 'A'
4for j in range(i + 1):loop ends (no more values)
6print(s)A⏎
2for i in range(n):i = 1
3s = ""s = ''
4for j in range(i + 1):loop ends (no more values)
5s = s + chr(65 + i + j)s = 'B'
4for j in range(i + 1):j = 1
5s = s + chr(65 + i + j)s = 'BC'
4for j in range(i + 1):loop ends (no more values)
6print(s)BC⏎
2for i in range(n):i = 2

… the same steps continue for the remaining values.

C2.62Practice3 marks

Write the output of the following code. Show your working in short.

T = (35, 3, 16, 19, 10, 1)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(1, 19, 3, 70, 32, 20)
3 1

Working (step by step)

LineStatementValues after this stepPrinted
1T = (35, 3, 16, 19, 10, 1)T = (35, 3, 16, 19, 10, 1)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [70]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [3, 70]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [3, 70, 32]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [19, 3, 70, 32]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.63Practice3 marks

Write the output of the following code. Show your working in short.

L = [49, 5, 21, 16, 31, 55]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::4], max(L) % 8)
Answer

Output:

[54, 26, 37, 47, 86, 55]
[54, 86] 6

Working (step by step)

LineStatementValues after this stepPrinted
1L = [49, 5, 21, 16, 31, 55]L = [49, 5, 21, 16, 31, 55]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [54, 5, 21, 16, 31, 55]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [54, 26, 21, 16, 31, 55]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [54, 26, 37, 16, 31, 55]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [54, 26, 37, 47, 31, 55]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [54, 26, 37, 47, 86, 55]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[54, 26, 37, 47, 86, 55]⏎
5print(L[::4], max(L) % 8)[54, 86] 6⏎
C2.64Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Pen': 80, 'Map': 70, 'Ink': 45, 'Book': 60}
for item in stock:
    if stock[item] < 50:
        stock[item] += 10
stock["Glue"] = 9
del stock['Map']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Pen': 80, 'Ink': 55, 'Book': 60, 'Glue': 9}
4 204

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Pen': 80, 'Map': 70, 'Ink': 45, 'Book': 60}stock = {'Pen': 80, 'Map': 70, 'Ink': 45, 'Book': 60}
2for item in stock:item = 'Pen'
3if stock[item] < 50:condition is False
2for item in stock:item = 'Map'
3if stock[item] < 50:condition is False
2for item in stock:item = 'Ink'
3if stock[item] < 50:condition is True
4stock[item] += 10stock = {'Pen': 80, 'Map': 70, 'Ink': 55, 'Book': 60}
2for item in stock:item = 'Book'
3if stock[item] < 50:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 9stock = {'Pen': 80, 'Map': 70, 'Ink': 55, 'Book': 60, 'Glue': 9}
6del stock['Map']stock = {'Pen': 80, 'Ink': 55, 'Book': 60, 'Glue': 9}
7print(stock){'Pen': 80, 'Ink': 55, 'Book': 60, 'Glue': 9}⏎
8print(len(stock), sum(stock.values()))4 204⏎
C2.65Practice3 marks

Write the output of the following code. Show your working in short.

T = (20, 34, 33, 2, 15, 19)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(19, 2, 34, 40, 66, 30)
4 2

Working (step by step)

LineStatementValues after this stepPrinted
1T = (20, 34, 33, 2, 15, 19)T = (20, 34, 33, 2, 15, 19)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [40]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [34, 40]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [34, 40, 66]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [2, 34, 40, 66]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.66Practice3 marks

Write the output of the following code. Show your working in short.

L = [28, 43, 39, 44, 14, 47]
for i in range(1, len(L)):
    L[i - 1] = L[i - 1] + L[i]
print(L)
print(L[::2], max(L) % 9)
Answer

Output:

[71, 82, 83, 58, 61, 47]
[71, 83, 61] 2

Working (step by step)

LineStatementValues after this stepPrinted
1L = [28, 43, 39, 44, 14, 47]L = [28, 43, 39, 44, 14, 47]
2for i in range(1, len(L)):i = 1
3L[i - 1] = L[i - 1] + L[i]L = [71, 43, 39, 44, 14, 47]
2for i in range(1, len(L)):i = 2
3L[i - 1] = L[i - 1] + L[i]L = [71, 82, 39, 44, 14, 47]
2for i in range(1, len(L)):i = 3
3L[i - 1] = L[i - 1] + L[i]L = [71, 82, 83, 44, 14, 47]
2for i in range(1, len(L)):i = 4
3L[i - 1] = L[i - 1] + L[i]L = [71, 82, 83, 58, 14, 47]
2for i in range(1, len(L)):i = 5
3L[i - 1] = L[i - 1] + L[i]L = [71, 82, 83, 58, 61, 47]
2for i in range(1, len(L)):loop ends (no more values)
4print(L)[71, 82, 83, 58, 61, 47]⏎
5print(L[::2], max(L) % 9)[71, 83, 61] 2⏎
C2.67Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Ink': 75, 'Book': 55, 'Bag': 45, 'Map': 10}
for item in stock:
    if stock[item] < 52:
        stock[item] += 10
stock["Glue"] = 12
del stock['Book']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Ink': 75, 'Bag': 55, 'Map': 20, 'Glue': 12}
4 162

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Ink': 75, 'Book': 55, 'Bag': 45, 'Map': 10}stock = {'Ink': 75, 'Book': 55, 'Bag': 45, 'Map': 10}
2for item in stock:item = 'Ink'
3if stock[item] < 52:condition is False
2for item in stock:item = 'Book'
3if stock[item] < 52:condition is False
2for item in stock:item = 'Bag'
3if stock[item] < 52:condition is True
4stock[item] += 10stock = {'Ink': 75, 'Book': 55, 'Bag': 55, 'Map': 10}
2for item in stock:item = 'Map'
3if stock[item] < 52:condition is True
4stock[item] += 10stock = {'Ink': 75, 'Book': 55, 'Bag': 55, 'Map': 20}
2for item in stock:loop ends (no more values)
5stock["Glue"] = 12stock = {'Ink': 75, 'Book': 55, 'Bag': 55, 'Map': 20, 'Glue': 12}
6del stock['Book']stock = {'Ink': 75, 'Bag': 55, 'Map': 20, 'Glue': 12}
7print(stock){'Ink': 75, 'Bag': 55, 'Map': 20, 'Glue': 12}⏎
8print(len(stock), sum(stock.values()))4 162⏎
C2.68Practice3 marks

Write the output of the following code. Show your working in short.

msg = "Keep Calm and Code"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "*"
print(new)
print(len(new), new.count("*"))
Answer

Output:

kEEP*cALM*AND*cODE
18 3

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "Keep Calm and Code"msg = 'Keep Calm and Code'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'K'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'k'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'e'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'kE'
3for i in range(len(msg)):i = 2
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'kEE'
3for i in range(len(msg)):i = 3

… the same steps continue for the remaining values.

C2.69Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Bag': 60, 'Pen': 45, 'Map': 40, 'Ink': 70}
for item in stock:
    if stock[item] < 33:
        stock[item] += 10
stock["Glue"] = 13
del stock['Ink']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Bag': 60, 'Pen': 45, 'Map': 40, 'Glue': 13}
4 158

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Bag': 60, 'Pen': 45, 'Map': 40, 'Ink': 70}stock = {'Bag': 60, 'Pen': 45, 'Map': 40, 'Ink': 70}
2for item in stock:item = 'Bag'
3if stock[item] < 33:condition is False
2for item in stock:item = 'Pen'
3if stock[item] < 33:condition is False
2for item in stock:item = 'Map'
3if stock[item] < 33:condition is False
2for item in stock:item = 'Ink'
3if stock[item] < 33:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 13stock = {'Bag': 60, 'Pen': 45, 'Map': 40, 'Ink': 70, 'Glue': 13}
6del stock['Ink']stock = {'Bag': 60, 'Pen': 45, 'Map': 40, 'Glue': 13}
7print(stock){'Bag': 60, 'Pen': 45, 'Map': 40, 'Glue': 13}⏎
8print(len(stock), sum(stock.values()))4 158⏎
C2.70Practice3 marks

Write the output of the following code. Show your working in short.

T = (8, 15, 18, 9, 33, 28)
L = []
for i in range(len(T)):
    if i % 2 == 0:
        L.append(T[i] * 2)
    else:
        L.insert(0, T[i])
T2 = tuple(L)
print(T2)
print(T2.index(max(T2)), min(T2))
Answer

Output:

(28, 9, 15, 16, 36, 66)
5 9

Working (step by step)

LineStatementValues after this stepPrinted
1T = (8, 15, 18, 9, 33, 28)T = (8, 15, 18, 9, 33, 28)
2L = []L = []
3for i in range(len(T)):i = 0
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [16]
3for i in range(len(T)):i = 1
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [15, 16]
3for i in range(len(T)):i = 2
4if i % 2 == 0:condition is True
5L.append(T[i] * 2)L = [15, 16, 36]
3for i in range(len(T)):i = 3
4if i % 2 == 0:condition is False
7L.insert(0, T[i])L = [9, 15, 16, 36]
3for i in range(len(T)):i = 4
4if i % 2 == 0:condition is True

… the same steps continue for the remaining values.

C2.71Practice3 marks

Write the output of the following code. Show your working in short.

L = [51, 34, 6, 37, 15, 33]
A, B = [], []
for x in L:
    if x % 2 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[34, 6]
[33, 15, 37, 51]
[34, 6, 33, 15]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [51, 34, 6, 37, 15, 33]L = [51, 34, 6, 37, 15, 33]
2A, B = [], []A = [], B = []
3for x in L:x = 51
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [51]
3for x in L:x = 34
4if x % 2 == 0:condition is True
5A.append(x)A = [34]
3for x in L:x = 6
4if x % 2 == 0:condition is True
5A.append(x)A = [34, 6]
3for x in L:x = 37
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [37, 51]
3for x in L:x = 15
4if x % 2 == 0:condition is False

… the same steps continue for the remaining values.

C2.72Practice3 marks

Write the output of the following code. Show your working in short.

L = [19, 27, 43, 15, 8, 46]
A, B = [], []
for x in L:
    if x % 3 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[27, 15]
[46, 8, 43, 19]
[27, 15, 46, 8]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [19, 27, 43, 15, 8, 46]L = [19, 27, 43, 15, 8, 46]
2A, B = [], []A = [], B = []
3for x in L:x = 19
4if x % 3 == 0:condition is False
7B.insert(0, x)B = [19]
3for x in L:x = 27
4if x % 3 == 0:condition is True
5A.append(x)A = [27]
3for x in L:x = 43
4if x % 3 == 0:condition is False
7B.insert(0, x)B = [43, 19]
3for x in L:x = 15
4if x % 3 == 0:condition is True
5A.append(x)A = [27, 15]
3for x in L:x = 8
4if x % 3 == 0:condition is False

… the same steps continue for the remaining values.

C2.73Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Bag': 35, 'Map': 50, 'Ruler': 40, 'Ink': 80}
for item in stock:
    if stock[item] < 31:
        stock[item] += 10
stock["Glue"] = 12
del stock['Ruler']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Bag': 35, 'Map': 50, 'Ink': 80, 'Glue': 12}
4 177

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Bag': 35, 'Map': 50, 'Ruler': 40, 'Ink': 80}stock = {'Bag': 35, 'Map': 50, 'Ruler': 40, 'Ink': 80}
2for item in stock:item = 'Bag'
3if stock[item] < 31:condition is False
2for item in stock:item = 'Map'
3if stock[item] < 31:condition is False
2for item in stock:item = 'Ruler'
3if stock[item] < 31:condition is False
2for item in stock:item = 'Ink'
3if stock[item] < 31:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 12stock = {'Bag': 35, 'Map': 50, 'Ruler': 40, 'Ink': 80, 'Glue': 12}
6del stock['Ruler']stock = {'Bag': 35, 'Map': 50, 'Ink': 80, 'Glue': 12}
7print(stock){'Bag': 35, 'Map': 50, 'Ink': 80, 'Glue': 12}⏎
8print(len(stock), sum(stock.values()))4 177⏎
C2.74Practice3 marks

Write the output of the following code. Show your working in short.

n = 4
for i in range(1, n + 1):
    print(" " * (n - i), end="")
    for j in range(1, i + 1):
        print(j, end="")
    for j in range(i - 1, 0, -1):
        print(j, end="")
    print()
Answer

Output:

   1
  121
 12321
1234321

Working (step by step)

LineStatementValues after this stepPrinted
1n = 4n = 4
2for i in range(1, n + 1):i = 1
3print(" " * (n - i), end="")
4for j in range(1, i + 1):j = 1
5print(j, end="")1
4for j in range(1, i + 1):loop ends (no more values)
6for j in range(i - 1, 0, -1):loop ends (no more values)
8print()⏎
2for i in range(1, n + 1):i = 2
3print(" " * (n - i), end="")
4for j in range(1, i + 1):loop ends (no more values)
5print(j, end="")1
4for j in range(1, i + 1):j = 2
5print(j, end="")2
4for j in range(1, i + 1):loop ends (no more values)
6for j in range(i - 1, 0, -1):j = 1

… the same steps continue for the remaining values.

C2.75Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Bag': 85, 'Ruler': 30, 'Map': 25, 'Pen': 70}
for item in stock:
    if stock[item] < 42:
        stock[item] += 10
stock["Glue"] = 6
del stock['Ruler']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Bag': 85, 'Map': 35, 'Pen': 70, 'Glue': 6}
4 196

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Bag': 85, 'Ruler': 30, 'Map': 25, 'Pen': 70}stock = {'Bag': 85, 'Ruler': 30, 'Map': 25, 'Pen': 70}
2for item in stock:item = 'Bag'
3if stock[item] < 42:condition is False
2for item in stock:item = 'Ruler'
3if stock[item] < 42:condition is True
4stock[item] += 10stock = {'Bag': 85, 'Ruler': 40, 'Map': 25, 'Pen': 70}
2for item in stock:item = 'Map'
3if stock[item] < 42:condition is True
4stock[item] += 10stock = {'Bag': 85, 'Ruler': 40, 'Map': 35, 'Pen': 70}
2for item in stock:item = 'Pen'
3if stock[item] < 42:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 6stock = {'Bag': 85, 'Ruler': 40, 'Map': 35, 'Pen': 70, 'Glue': 6}
6del stock['Ruler']stock = {'Bag': 85, 'Map': 35, 'Pen': 70, 'Glue': 6}
7print(stock){'Bag': 85, 'Map': 35, 'Pen': 70, 'Glue': 6}⏎
8print(len(stock), sum(stock.values()))4 196⏎
C2.76Practice3 marks

Write the output of the following code. Show your working in short.

n = 5
for i in range(n):
    s = ""
    for j in range(i + 1):
        s = s + chr(65 + i + j)
    print(s)
Answer

Output:

A
BC
CDE
DEFG
EFGHI

Working (step by step)

LineStatementValues after this stepPrinted
1n = 5n = 5
2for i in range(n):i = 0
3s = ""s = ''
4for j in range(i + 1):j = 0
5s = s + chr(65 + i + j)s = 'A'
4for j in range(i + 1):loop ends (no more values)
6print(s)A⏎
2for i in range(n):i = 1
3s = ""s = ''
4for j in range(i + 1):loop ends (no more values)
5s = s + chr(65 + i + j)s = 'B'
4for j in range(i + 1):j = 1
5s = s + chr(65 + i + j)s = 'BC'
4for j in range(i + 1):loop ends (no more values)
6print(s)BC⏎
2for i in range(n):i = 2

… the same steps continue for the remaining values.

C2.77Practice3 marks

Write the output of the following code. Show your working in short.

L = [39, 19, 40, 10, 56, 12]
A, B = [], []
for x in L:
    if x % 4 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[40, 56, 12]
[10, 19, 39]
[40, 56, 12, 10, 19]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [39, 19, 40, 10, 56, 12]L = [39, 19, 40, 10, 56, 12]
2A, B = [], []A = [], B = []
3for x in L:x = 39
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [39]
3for x in L:x = 19
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [19, 39]
3for x in L:x = 40
4if x % 4 == 0:condition is True
5A.append(x)A = [40]
3for x in L:x = 10
4if x % 4 == 0:condition is False
7B.insert(0, x)B = [10, 19, 39]
3for x in L:x = 56
4if x % 4 == 0:condition is True

… the same steps continue for the remaining values.

C2.78Practice3 marks

Write the output of the following code. Show your working in short.

stock = {'Bag': 30, 'Pen': 25, 'Ruler': 50, 'Book': 55}
for item in stock:
    if stock[item] < 30:
        stock[item] += 10
stock["Glue"] = 16
del stock['Ruler']
print(stock)
print(len(stock), sum(stock.values()))
Answer

Output:

{'Bag': 30, 'Pen': 35, 'Book': 55, 'Glue': 16}
4 136

Working (step by step)

LineStatementValues after this stepPrinted
1stock = {'Bag': 30, 'Pen': 25, 'Ruler': 50, 'Book': 55}stock = {'Bag': 30, 'Pen': 25, 'Ruler': 50, 'Book': 55}
2for item in stock:item = 'Bag'
3if stock[item] < 30:condition is False
2for item in stock:item = 'Pen'
3if stock[item] < 30:condition is True
4stock[item] += 10stock = {'Bag': 30, 'Pen': 35, 'Ruler': 50, 'Book': 55}
2for item in stock:item = 'Ruler'
3if stock[item] < 30:condition is False
2for item in stock:item = 'Book'
3if stock[item] < 30:condition is False
2for item in stock:loop ends (no more values)
5stock["Glue"] = 16stock = {'Bag': 30, 'Pen': 35, 'Ruler': 50, 'Book': 55, 'Glue': 16}
6del stock['Ruler']stock = {'Bag': 30, 'Pen': 35, 'Book': 55, 'Glue': 16}
7print(stock){'Bag': 30, 'Pen': 35, 'Book': 55, 'Glue': 16}⏎
8print(len(stock), sum(stock.values()))4 136⏎
C2.79Practice3 marks

Write the output of the following code. Show your working in short.

msg = "India is Great"
new = ""
for i in range(len(msg)):
    ch = msg[i]
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    elif ch.isdigit():
        new = new + str(int(ch) + 1)
    else:
        new = new + "*"
print(new)
print(len(new), new.count("*"))
Answer

Output:

iNDIA*IS*gREAT
14 2

Working (step by step)

LineStatementValues after this stepPrinted
1msg = "India is Great"msg = 'India is Great'
2new = ""new = ''
3for i in range(len(msg)):i = 0
4ch = msg[i]ch = 'I'
5if ch.isupper():condition is True
6new = new + ch.lower()new = 'i'
3for i in range(len(msg)):i = 1
4ch = msg[i]ch = 'n'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'iN'
3for i in range(len(msg)):i = 2
4ch = msg[i]ch = 'd'
5if ch.isupper():condition is False
7elif ch.islower():condition is True
8new = new + ch.upper()new = 'iND'

… the same steps continue for the remaining values.

C2.80Practice3 marks

Write the output of the following code. Show your working in short.

L = [46, 33, 29, 10, 44, 36]
A, B = [], []
for x in L:
    if x % 2 == 0:
        A.append(x)
    else:
        B.insert(0, x)
print(A)
print(B)
print(A + B[:2])
Answer

Output:

[46, 10, 44, 36]
[29, 33]
[46, 10, 44, 36, 29, 33]

Working (step by step)

LineStatementValues after this stepPrinted
1L = [46, 33, 29, 10, 44, 36]L = [46, 33, 29, 10, 44, 36]
2A, B = [], []A = [], B = []
3for x in L:x = 46
4if x % 2 == 0:condition is True
5A.append(x)A = [46]
3for x in L:x = 33
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [33]
3for x in L:x = 29
4if x % 2 == 0:condition is False
7B.insert(0, x)B = [29, 33]
3for x in L:x = 10
4if x % 2 == 0:condition is True
5A.append(x)A = [46, 10]
3for x in L:x = 44
4if x % 2 == 0:condition is True

… the same steps continue for the remaining values.

C3Programs: numbers, patterns, series3 marks · 40 questions
C3.1Practice3 marks

Write a program to input a number and print its factorial.

Answer
n = int(input('Enter a number: '))
f = 1
for i in range(1, n + 1):
    f = f * i
print('Factorial =', f)

Sample run

Enter a number: 6
Factorial = 720
C3.2Practice3 marks

Write a program to input a number and find the sum of its digits.

Answer
n = int(input('Enter a number: '))
s = 0
while n > 0:
    s = s + n % 10
    n = n // 10
print('Sum of digits =', s)

Sample run

Enter a number: 5274
Sum of digits = 18
C3.3Practice3 marks

Write a program to input a number and check whether it is a palindrome.

Answer
n = int(input('Enter a number: '))
temp = n
rev = 0
while temp > 0:
    rev = rev * 10 + temp % 10
    temp = temp // 10
if rev == n:
    print(n, 'is a palindrome')
else:
    print(n, 'is not a palindrome')

Sample run

Enter a number: 12321
12321 is a palindrome
C3.4Practice3 marks

Write a program to input a number and check whether it is prime.

Answer
n = int(input('Enter a number: '))
if n < 2:
    print(n, 'is not prime')
else:
    for i in range(2, n):
        if n % i == 0:
            print(n, 'is not prime')
            break
    else:
        print(n, 'is prime')

Sample run

Enter a number: 29
29 is prime
C3.5Practice3 marks

Write a program to check whether a three-digit number is an Armstrong number.

Answer
n = int(input('Enter a 3-digit number: '))
temp = n
s = 0
while temp > 0:
    d = temp % 10
    s = s + d ** 3
    temp = temp // 10
if s == n:
    print(n, 'is an Armstrong number')
else:
    print(n, 'is not an Armstrong number')

Sample run

Enter a 3-digit number: 153
153 is an Armstrong number
C3.6Practice3 marks

Write a program to input two numbers and find their GCD (HCF) and LCM.

Answer
a = int(input('First number: '))
b = int(input('Second number: '))
x, y = a, b
while y != 0:
    x, y = y, x % y
print('GCD =', x)
print('LCM =', a * b // x)

Sample run

First number: 12
Second number: 18
GCD = 6
LCM = 36
C3.7Practice3 marks

Write a program to print the first n terms of the Fibonacci series.

Answer
n = int(input('How many terms? '))
a, b = 0, 1
for i in range(n):
    print(a, end=' ')
    a, b = b, a + b

Sample run

How many terms? 8
0 1 1 2 3 5 8 13 
C3.8Practice3 marks

Write a program to input a number and print its multiplication table up to 10.

Answer
n = int(input('Enter a number: '))
for i in range(1, 11):
    print(n, 'x', i, '=', n * i)

Sample run

Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
C3.9Practice3 marks

Write a program to input a year and check whether it is a leap year.

Answer
y = int(input('Enter a year: '))
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
    print(y, 'is a leap year')
else:
    print(y, 'is not a leap year')

Sample run

Enter a year: 2024
2024 is a leap year
C3.10Practice3 marks

Write a program to input three numbers and print the largest.

Answer
a = int(input('A: '))
b = int(input('B: '))
c = int(input('C: '))
if a >= b and a >= c:
    big = a
elif b >= c:
    big = b
else:
    big = c
print('Largest =', big)

Sample run

A: 45
B: 72
C: 19
Largest = 72
C3.11Practice3 marks

Write a program to input marks out of 100 and print the grade: A (90 and above), B (75-89), C (50-74), D (33-49), E (below 33).

Answer
m = int(input('Marks: '))
if m >= 90:
    g = 'A'
elif m >= 75:
    g = 'B'
elif m >= 50:
    g = 'C'
elif m >= 33:
    g = 'D'
else:
    g = 'E'
print('Grade', g)

Sample run

Marks: 78
Grade B
C3.12Practice3 marks

Write a program to check whether a number is a perfect number (equal to the sum of its factors other than itself).

Answer
n = int(input('Enter a number: '))
s = 0
for i in range(1, n):
    if n % i == 0:
        s = s + i
if s == n:
    print(n, 'is a perfect number')
else:
    print(n, 'is not a perfect number')

Sample run

Enter a number: 28
28 is a perfect number
C3.13Practice3 marks

Write a program to input a number and count its digits.

Answer
n = int(input('Enter a number: '))
count = 0
while n > 0:
    count += 1
    n = n // 10
print('Digits =', count)

Sample run

Enter a number: 904512
Digits = 6
C3.14Practice3 marks

Write a program to print all prime numbers between 1 and n.

Answer
n = int(input('Up to: '))
for num in range(2, n + 1):
    for i in range(2, num):
        if num % i == 0:
            break
    else:
        print(num, end=' ')

Sample run

Up to: 30
2 3 5 7 11 13 17 19 23 29 
C3.15Practice3 marks

Write a program to find the sum of even numbers and the sum of odd numbers from 1 to n.

Answer
n = int(input('Enter n: '))
even = odd = 0
for i in range(1, n + 1):
    if i % 2 == 0:
        even += i
    else:
        odd += i
print('Sum of even =', even)
print('Sum of odd =', odd)

Sample run

Enter n: 10
Sum of even = 30
Sum of odd = 25
C3.16Practice3 marks

Write a program to input a number and print its reverse.

Answer
n = int(input('Enter a number: '))
rev = 0
while n > 0:
    rev = rev * 10 + n % 10
    n = n // 10
print('Reverse =', rev)

Sample run

Enter a number: 37081
Reverse = 18073
C3.17Practice3 marks

Write a program to input the principal, rate and time and print the simple interest and compound interest.

Answer
p = float(input('Principal: '))
r = float(input('Rate: '))
t = float(input('Time: '))
si = p * r * t / 100
ci = p * (1 + r / 100) ** t - p
print('Simple interest =', round(si, 2))
print('Compound interest =', round(ci, 2))

Sample run

Principal: 10000
Rate: 8
Time: 2
Simple interest = 1600.0
Compound interest = 1664.0
C3.18Practice3 marks

Write a program to convert a temperature in Celsius to Fahrenheit and Kelvin.

Answer
c = float(input('Celsius: '))
f = c * 9 / 5 + 32
k = c + 273.15
print('Fahrenheit =', f)
print('Kelvin =', k)

Sample run

Celsius: 37
Fahrenheit = 98.6
Kelvin = 310.15
C3.19Practice3 marks

Write a program to input the three sides of a triangle and check whether it is equilateral, isosceles or scalene.

Answer
a = int(input('Side 1: '))
b = int(input('Side 2: '))
c = int(input('Side 3: '))
if a == b == c:
    print('Equilateral')
elif a == b or b == c or a == c:
    print('Isosceles')
else:
    print('Scalene')

Sample run

Side 1: 5
Side 2: 7
Side 3: 5
Isosceles
C3.20Practice3 marks

Write a program to find the sum of the squares of the first n natural numbers.

Answer
n = int(input('Enter n: '))
s = 0
for i in range(1, n + 1):
    s = s + i * i
print('Sum of squares =', s)

Sample run

Enter n: 6
Sum of squares = 91
C3.21Practice3 marks

Write a program to input n numbers and print their average, the largest and the smallest.

Answer
n = int(input('How many numbers? '))
total = 0
for i in range(n):
    x = int(input('Number: '))
    if i == 0:
        big = small = x
    big = max(big, x)
    small = min(small, x)
    total += x
print('Average =', total / n)
print('Largest =', big, 'Smallest =', small)

Sample run

How many numbers? 4
Number: 15
Number: 42
Number: 8
Number: 23
Average = 22.0
Largest = 42 Smallest = 8
C3.22Practice3 marks

Write a program to print all the factors of a number and their count.

Answer
n = int(input('Enter a number: '))
count = 0
for i in range(1, n + 1):
    if n % i == 0:
        print(i, end=' ')
        count += 1
print()
print('Number of factors =', count)

Sample run

Enter a number: 36
1 2 3 4 6 9 12 18 36 
Number of factors = 9
C3.23Practice3 marks

Write a program to check whether a number is a 'special number': the sum of the factorials of its digits equals the number (e.g. 145).

Answer
import math
n = int(input('Enter a number: '))
s = 0
for d in str(n):
    s = s + math.factorial(int(d))
if s == n:
    print(n, 'is a special number')
else:
    print(n, 'is not a special number')

Sample run

Enter a number: 145
145 is a special number
C3.24Practice3 marks

Write a program to input a number and print whether it is positive, negative or zero, and whether it is even or odd.

Answer
n = int(input('Enter a number: '))
if n > 0:
    print('Positive')
elif n < 0:
    print('Negative')
else:
    print('Zero')
if n % 2 == 0:
    print('Even')
else:
    print('Odd')

Sample run

Enter a number: -14
Negative
Even
C3.25Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

*
* *
* * *
* * * *
Answer
n = int(input('Rows: '))
for i in range(1, n + 1):
    print('* ' * i)

Sample run

Rows: 4
* 
* * 
* * * 
* * * * 
C3.26Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

1
1 2
1 2 3
1 2 3 4
Answer
n = int(input('Rows: '))
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()

Sample run

Rows: 4
1 
1 2 
1 2 3 
1 2 3 4 
C3.27Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

1
2 2
3 3 3
4 4 4 4
Answer
n = int(input('Rows: '))
for i in range(1, n + 1):
    for j in range(i):
        print(i, end=' ')
    print()

Sample run

Rows: 4
1 
2 2 
3 3 3 
4 4 4 4 
C3.28Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

A
A B
A B C
A B C D
Answer
n = int(input('Rows: '))
for i in range(1, n + 1):
    for j in range(i):
        print(chr(65 + j), end=' ')
    print()

Sample run

Rows: 4
A 
A B 
A B C 
A B C D 
C3.29Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

4 3 2 1
4 3 2
4 3
4
Answer
n = int(input('Rows: '))
for i in range(1, n + 1):
    for j in range(n, i - 1, -1):
        print(j, end=' ')
    print()

Sample run

Rows: 4
4 3 2 1 
4 3 2 
4 3 
4 
C3.30Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

   *
  **
 ***
****
Answer
n = int(input('Rows: '))
for i in range(1, n + 1):
    print(' ' * (n - i) + '*' * i)

Sample run

Rows: 4
   *
  **
 ***
****
C3.31Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

1
2 3
4 5 6
7 8 9 10
Answer
n = int(input('Rows: '))
k = 1
for i in range(1, n + 1):
    for j in range(i):
        print(k, end=' ')
        k += 1
    print()

Sample run

Rows: 4
1 
2 3 
4 5 6 
7 8 9 10 
C3.32Practice3 marks

Write a program to print the following pattern for n rows (shown for n = 4):

1 2 3 4
1 2 3
1 2
1
Answer
n = int(input('Rows: '))
for i in range(n, 0, -1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()

Sample run

Rows: 4
1 2 3 4 
1 2 3 
1 2 
1 
C3.33Practice3 marks

Write a program to input the values needed and find the sum of the series: 1 + x + x² + x³ + ... + xⁿ

Answer
x = int(input('x: '))
n = int(input('n: '))
s = 0
for i in range(n + 1):
    s = s + x ** i
print('Sum =', s)

Sample run

x: 2
n: 5
Sum = 63
C3.34Practice3 marks

Write a program to input the values needed and find the sum of the series: 1 - x + x² - x³ + ... (n terms after 1, up to xⁿ)

Answer
x = int(input('x: '))
n = int(input('n: '))
s = 0
for i in range(n + 1):
    s = s + (-1) ** i * x ** i
print('Sum =', s)

Sample run

x: 3
n: 4
Sum = 61
C3.35Practice3 marks

Write a program to input the values needed and find the sum of the series: x - x²/2 + x³/3 - ... up to n terms

Answer
x = float(input('x: '))
n = int(input('n: '))
s = 0
for i in range(1, n + 1):
    s = s + (-1) ** (i + 1) * x ** i / i
print('Sum =', round(s, 4))

Sample run

x: 2
n: 4
Sum = -1.3333
C3.36Practice3 marks

Write a program to input the values needed and find the sum of the series: x + x²/2! + x³/3! + ... up to n terms

Answer
import math
x = float(input('x: '))
n = int(input('n: '))
s = 0
for i in range(1, n + 1):
    s = s + x ** i / math.factorial(i)
print('Sum =', round(s, 4))

Sample run

x: 2
n: 5
Sum = 6.2667
C3.37Practice3 marks

Write a program to input the values needed and find the sum of the series: 1 + 1/2 + 1/3 + ... + 1/n

Answer
n = int(input('n: '))
s = 0
for i in range(1, n + 1):
    s = s + 1 / i
print('Sum =', round(s, 4))

Sample run

n: 5
Sum = 2.2833
C3.38Practice3 marks

Write a program to input the values needed and find the sum of the series: 1² + 2² + 3² + ... + n² and 1³ + 2³ + ... + n³

Answer
n = int(input('n: '))
s2 = s3 = 0
for i in range(1, n + 1):
    s2 += i ** 2
    s3 += i ** 3
print('Sum of squares =', s2)
print('Sum of cubes =', s3)

Sample run

n: 5
Sum of squares = 55
Sum of cubes = 225
C3.39Practice3 marks

Write a program to input the values needed and find the sum of the series: 1 + 2 + 4 + 8 + ... (n terms)

Answer
n = int(input('n: '))
term = 1
s = 0
for i in range(n):
    s = s + term
    term = term * 2
print('Sum =', s)

Sample run

n: 6
Sum = 63
C3.40Practice3 marks

Write a program to input the values needed and find the sum of the series: 1 + 3 + 5 + ... (first n odd numbers)

Answer
n = int(input('n: '))
s = 0
for i in range(n):
    s = s + (2 * i + 1)
print('Sum =', s)

Sample run

n: 7
Sum = 49

Section D · 4 marks each

D1Conversions and truth table4 marks · 32 questions
D1.1Practice4 marks

(i) Convert (a) (10011011)2 to decimal and (b) (19E)16 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND B) OR C.   [2]

Answer

(i) (a)

(10011011)2 = 1 × 27 + 0 × 26 + 0 × 25 + 1 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 = 128 + 0 + 0 + 16 + 8 + 0 + 2 + 1 = 155

(i) (b)

(19E)16 = 1 × 162 + 9 × 161 + E × 160 = 256 + 144 + 14 = 414

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A AND B) OR C
0000
0011
0100
0111
1000
1011
1101
1111
D1.2Practice4 marks

(i) Convert (a) (463)8 to decimal and (b) (1C3)16 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for A AND (B OR C).   [2]

Answer

(i) (a)

(463)8 = 4 × 82 + 6 × 81 + 3 × 80 = 256 + 48 + 3 = 307

(i) (b)

(1C3)16 = 1 × 162 + C × 161 + 3 × 160 = 256 + 192 + 3 = 451

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND (B OR C)
0000
0010
0100
0110
1000
1011
1101
1111
D1.3Practice4 marks

(i) Convert (a) (543)8 to binary and (b) (101111100)2 to octal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR B) AND (NOT C).   [2]

Answer

(i) (a)

Write each digit as 3 bits: 5 → 101   4 → 100   3 → 011

So (543)8 = (101100011)2

(i) (b)

Make groups of 3 bits from the right: 101  111  100

101 → 5   111 → 7   100 → 4

So (101111100)2 = (574)8

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110
D1.4Practice4 marks

(i) Convert (a) (472)8 to decimal and (b) (101010111)2 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for NOT A OR (B AND C).   [2]

Answer

(i) (a)

(472)8 = 4 × 82 + 7 × 81 + 2 × 80 = 256 + 56 + 2 = 314

(i) (b)

(101010111)2 = 1 × 28 + 0 × 27 + 1 × 26 + 0 × 25 + 1 × 24 + 0 × 23 + 1 × 22 + 1 × 21 + 1 × 20 = 256 + 0 + 64 + 0 + 16 + 0 + 4 + 2 + 1 = 343

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT A OR (B AND C)
0001
0011
0101
0111
1000
1010
1100
1111
D1.5Practice4 marks

(i) Convert (a) (527)8 to decimal and (b) (171)10 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND NOT B) OR (B AND C).   [2]

Answer

(i) (a)

(527)8 = 5 × 82 + 2 × 81 + 7 × 80 = 320 + 16 + 7 = 343

(i) (b)

Divide byNumberRemainder
16171B
1610A

Read the remainders from bottom to top: (AB)16

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111
D1.6Practice4 marks

(i) Convert (a) (100110000)2 to octal and (b) (10011110)2 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for NOT (A OR B) AND C.   [2]

Answer

(i) (a)

Make groups of 3 bits from the right: 100  110  000

100 → 4   110 → 6   000 → 0

So (100110000)2 = (460)8

(i) (b)

(10011110)2 = 1 × 27 + 0 × 26 + 0 × 25 + 1 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20 = 128 + 0 + 0 + 16 + 8 + 4 + 2 + 0 = 158

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110
D1.7Practice4 marks

(i) Convert (a) (170)8 to binary and (b) (437)10 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR NOT B) AND (B OR C).   [2]

Answer

(i) (a)

Write each digit as 3 bits: 1 → 001   7 → 111   0 → 000

So (170)8 = (1111000)2

(i) (b)

Divide byNumberRemainder
164375
1627B
1611

Read the remainders from bottom to top: (1B5)16

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A OR NOT B) AND (B OR C)
0000
0011
0100
0110
1000
1011
1101
1111
D1.8Practice4 marks

(i) Convert (a) (B5)16 to octal and (b) (462)8 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for A AND B AND NOT C.   [2]

Answer

(i) (a)

Write each digit as 4 bits: B → 1011   5 → 0101

So (B5)16 = (10110101)2

Make groups of 3 bits from the right: 010  110  101

010 → 2   110 → 6   101 → 5

So (10110101)2 = (265)8

(i) (b)

(462)8 = 4 × 82 + 6 × 81 + 2 × 80 = 256 + 48 + 2 = 306

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND B AND NOT C
0000
0010
0100
0110
1000
1010
1101
1110
D1.9Practice4 marks

(i) Convert (a) (359)10 to binary and (b) (11101000)2 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND B) OR C.   [2]

Answer

(i) (a)

Divide byNumberRemainder
23591
21791
2891
2440
2220
2111
251
220
211

Read the remainders from bottom to top: (101100111)2

(i) (b)

(11101000)2 = 1 × 27 + 1 × 26 + 1 × 25 + 0 × 24 + 1 × 23 + 0 × 22 + 0 × 21 + 0 × 20 = 128 + 64 + 32 + 0 + 8 + 0 + 0 + 0 = 232

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A AND B) OR C
0000
0011
0100
0111
1000
1011
1101
1111
D1.10Practice4 marks

(i) Convert (a) (13F)16 to decimal and (b) (1D0)16 to binary.   [2]

(ii) Draw the logic circuit and the truth table for A AND (B OR C).   [2]

Answer

(i) (a)

(13F)16 = 1 × 162 + 3 × 161 + F × 160 = 256 + 48 + 15 = 319

(i) (b)

Write each digit as 4 bits: 1 → 0001   D → 1101   0 → 0000

So (1D0)16 = (111010000)2

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND (B OR C)
0000
0010
0100
0110
1000
1011
1101
1111
D1.11Practice4 marks

(i) Convert (a) (137)16 to decimal and (b) (1DF)16 to octal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR B) AND (NOT C).   [2]

Answer

(i) (a)

(137)16 = 1 × 162 + 3 × 161 + 7 × 160 = 256 + 48 + 7 = 311

(i) (b)

Write each digit as 4 bits: 1 → 0001   D → 1101   F → 1111

So (1DF)16 = (111011111)2

Make groups of 3 bits from the right: 111  011  111

111 → 7   011 → 3   111 → 7

So (111011111)2 = (737)8

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110
D1.12Practice4 marks

(i) Convert (a) (11010001)2 to octal and (b) (372)8 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for NOT A OR (B AND C).   [2]

Answer

(i) (a)

Make groups of 3 bits from the right: 011  010  001

011 → 3   010 → 2   001 → 1

So (11010001)2 = (321)8

(i) (b)

(372)8 = 3 × 82 + 7 × 81 + 2 × 80 = 192 + 56 + 2 = 250

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT A OR (B AND C)
0001
0011
0101
0111
1000
1010
1100
1111
D1.13Practice4 marks

(i) Convert (a) (F2)16 to octal and (b) (464)10 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND NOT B) OR (B AND C).   [2]

Answer

(i) (a)

Write each digit as 4 bits: F → 1111   2 → 0010

So (F2)16 = (11110010)2

Make groups of 3 bits from the right: 011  110  010

011 → 3   110 → 6   010 → 2

So (11110010)2 = (362)8

(i) (b)

Divide byNumberRemainder
164640
1629D
1611

Read the remainders from bottom to top: (1D0)16

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111
D1.14Practice4 marks

(i) Convert (a) (298)10 to octal and (b) (1D9)16 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for NOT (A OR B) AND C.   [2]

Answer

(i) (a)

Divide byNumberRemainder
82982
8375
844

Read the remainders from bottom to top: (452)8

(i) (b)

(1D9)16 = 1 × 162 + D × 161 + 9 × 160 = 256 + 208 + 9 = 473

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110
D1.15Practice4 marks

(i) Convert (a) (717)8 to hexadecimal and (b) (554)8 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR NOT B) AND (B OR C).   [2]

Answer

(i) (a)

Write each digit as 3 bits: 7 → 111   1 → 001   7 → 111

So (717)8 = (111001111)2

Make groups of 4 bits from the right: 0001  1100  1111

0001 → 1   1100 → C   1111 → F

So (111001111)2 = (1CF)16

(i) (b)

(554)8 = 5 × 82 + 5 × 81 + 4 × 80 = 320 + 40 + 4 = 364

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A OR NOT B) AND (B OR C)
0000
0011
0100
0110
1000
1011
1101
1111
D1.16Practice4 marks

(i) Convert (a) (116)16 to octal and (b) (110111011)2 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for A AND B AND NOT C.   [2]

Answer

(i) (a)

Write each digit as 4 bits: 1 → 0001   1 → 0001   6 → 0110

So (116)16 = (100010110)2

Make groups of 3 bits from the right: 100  010  110

100 → 4   010 → 2   110 → 6

So (100010110)2 = (426)8

(i) (b)

Make groups of 4 bits from the right: 0001  1011  1011

0001 → 1   1011 → B   1011 → B

So (110111011)2 = (1BB)16

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND B AND NOT C
0000
0010
0100
0110
1000
1010
1101
1110
D1.17Practice4 marks

(i) Convert (a) (260)10 to binary and (b) (AB)16 to octal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND B) OR C.   [2]

Answer

(i) (a)

Divide byNumberRemainder
22600
21300
2651
2320
2160
280
240
220
211

Read the remainders from bottom to top: (100000100)2

(i) (b)

Write each digit as 4 bits: A → 1010   B → 1011

So (AB)16 = (10101011)2

Make groups of 3 bits from the right: 010  101  011

010 → 2   101 → 5   011 → 3

So (10101011)2 = (253)8

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A AND B) OR C
0000
0011
0100
0111
1000
1011
1101
1111
D1.18Practice4 marks

(i) Convert (a) (320)10 to binary and (b) (F9)16 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for A AND (B OR C).   [2]

Answer

(i) (a)

Divide byNumberRemainder
23200
21600
2800
2400
2200
2100
251
220
211

Read the remainders from bottom to top: (101000000)2

(i) (b)

(F9)16 = F × 161 + 9 × 160 = 240 + 9 = 249

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND (B OR C)
0000
0010
0100
0110
1000
1011
1101
1111
D1.19Practice4 marks

(i) Convert (a) (110100010)2 to octal and (b) (235)8 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR B) AND (NOT C).   [2]

Answer

(i) (a)

Make groups of 3 bits from the right: 110  100  010

110 → 6   100 → 4   010 → 2

So (110100010)2 = (642)8

(i) (b)

Write each digit as 3 bits: 2 → 010   3 → 011   5 → 101

So (235)8 = (10011101)2

Make groups of 4 bits from the right: 1001  1101

1001 → 9   1101 → D

So (10011101)2 = (9D)16

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110
D1.20Practice4 marks

(i) Convert (a) (C0)16 to octal and (b) (326)10 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for NOT A OR (B AND C).   [2]

Answer

(i) (a)

Write each digit as 4 bits: C → 1100   0 → 0000

So (C0)16 = (11000000)2

Make groups of 3 bits from the right: 011  000  000

011 → 3   000 → 0   000 → 0

So (11000000)2 = (300)8

(i) (b)

Divide byNumberRemainder
163266
16204
1611

Read the remainders from bottom to top: (146)16

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT A OR (B AND C)
0001
0011
0101
0111
1000
1010
1100
1111
D1.21Practice4 marks

(i) Convert (a) (10100111)2 to hexadecimal and (b) (5D)16 to octal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND NOT B) OR (B AND C).   [2]

Answer

(i) (a)

Make groups of 4 bits from the right: 1010  0111

1010 → A   0111 → 7

So (10100111)2 = (A7)16

(i) (b)

Write each digit as 4 bits: 5 → 0101   D → 1101

So (5D)16 = (1011101)2

Make groups of 3 bits from the right: 001  011  101

001 → 1   011 → 3   101 → 5

So (1011101)2 = (135)8

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111
D1.22Practice4 marks

(i) Convert (a) (6D)16 to decimal and (b) (110101111)2 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for NOT (A OR B) AND C.   [2]

Answer

(i) (a)

(6D)16 = 6 × 161 + D × 160 = 96 + 13 = 109

(i) (b)

Make groups of 4 bits from the right: 0001  1010  1111

0001 → 1   1010 → A   1111 → F

So (110101111)2 = (1AF)16

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110
D1.23Practice4 marks

(i) Convert (a) (406)10 to hexadecimal and (b) (10011011)2 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR NOT B) AND (B OR C).   [2]

Answer

(i) (a)

Divide byNumberRemainder
164066
16259
1611

Read the remainders from bottom to top: (196)16

(i) (b)

Make groups of 4 bits from the right: 1001  1011

1001 → 9   1011 → B

So (10011011)2 = (9B)16

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A OR NOT B) AND (B OR C)
0000
0011
0100
0110
1000
1011
1101
1111
D1.24Practice4 marks

(i) Convert (a) (1010000)2 to hexadecimal and (b) (100010101)2 to octal.   [2]

(ii) Draw the logic circuit and the truth table for A AND B AND NOT C.   [2]

Answer

(i) (a)

Make groups of 4 bits from the right: 0101  0000

0101 → 5   0000 → 0

So (1010000)2 = (50)16

(i) (b)

Make groups of 3 bits from the right: 100  010  101

100 → 4   010 → 2   101 → 5

So (100010101)2 = (425)8

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND B AND NOT C
0000
0010
0100
0110
1000
1010
1101
1110
D1.25Practice4 marks

(i) Convert (a) (CB)16 to octal and (b) (430)8 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND B) OR C.   [2]

Answer

(i) (a)

Write each digit as 4 bits: C → 1100   B → 1011

So (CB)16 = (11001011)2

Make groups of 3 bits from the right: 011  001  011

011 → 3   001 → 1   011 → 3

So (11001011)2 = (313)8

(i) (b)

Write each digit as 3 bits: 4 → 100   3 → 011   0 → 000

So (430)8 = (100011000)2

Make groups of 4 bits from the right: 0001  0001  1000

0001 → 1   0001 → 1   1000 → 8

So (100011000)2 = (118)16

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A AND B) OR C
0000
0011
0100
0111
1000
1011
1101
1111
D1.26Practice4 marks

(i) Convert (a) (101010101)2 to hexadecimal and (b) (A7)16 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for A AND (B OR C).   [2]

Answer

(i) (a)

Make groups of 4 bits from the right: 0001  0101  0101

0001 → 1   0101 → 5   0101 → 5

So (101010101)2 = (155)16

(i) (b)

(A7)16 = A × 161 + 7 × 160 = 160 + 7 = 167

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND (B OR C)
0000
0010
0100
0110
1000
1011
1101
1111
D1.27Practice4 marks

(i) Convert (a) (1AD)16 to binary and (b) (10101111)2 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR B) AND (NOT C).   [2]

Answer

(i) (a)

Write each digit as 4 bits: 1 → 0001   A → 1010   D → 1101

So (1AD)16 = (110101101)2

(i) (b)

Make groups of 4 bits from the right: 1010  1111

1010 → A   1111 → F

So (10101111)2 = (AF)16

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABC(A OR B) AND (NOT C)
0000
0010
0101
0110
1001
1010
1101
1110
D1.28Practice4 marks

(i) Convert (a) (101011000)2 to decimal and (b) (E9)16 to octal.   [2]

(ii) Draw the logic circuit and the truth table for NOT A OR (B AND C).   [2]

Answer

(i) (a)

(101011000)2 = 1 × 28 + 0 × 27 + 1 × 26 + 0 × 25 + 1 × 24 + 1 × 23 + 0 × 22 + 0 × 21 + 0 × 20 = 256 + 0 + 64 + 0 + 16 + 8 + 0 + 0 + 0 = 344

(i) (b)

Write each digit as 4 bits: E → 1110   9 → 1001

So (E9)16 = (11101001)2

Make groups of 3 bits from the right: 011  101  001

011 → 3   101 → 5   001 → 1

So (11101001)2 = (351)8

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT A OR (B AND C)
0001
0011
0101
0111
1000
1010
1100
1111
D1.29Practice4 marks

(i) Convert (a) (704)8 to binary and (b) (307)10 to octal.   [2]

(ii) Draw the logic circuit and the truth table for (A AND NOT B) OR (B AND C).   [2]

Answer

(i) (a)

Write each digit as 3 bits: 7 → 111   0 → 000   4 → 100

So (704)8 = (111000100)2

(i) (b)

Divide byNumberRemainder
83073
8386
844

Read the remainders from bottom to top: (463)8

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A AND NOT B) OR (B AND C)
0000
0010
0100
0111
1001
1011
1100
1111
D1.30Practice4 marks

(i) Convert (a) (145)8 to binary and (b) (483)10 to binary.   [2]

(ii) Draw the logic circuit and the truth table for NOT (A OR B) AND C.   [2]

Answer

(i) (a)

Write each digit as 3 bits: 1 → 001   4 → 100   5 → 101

So (145)8 = (1100101)2

(i) (b)

Divide byNumberRemainder
24831
22411
21200
2600
2300
2151
271
231
211

Read the remainders from bottom to top: (111100011)2

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCNOT (A OR B) AND C
0000
0011
0100
0110
1000
1010
1100
1110
D1.31Practice4 marks

(i) Convert (a) (101001011)2 to hexadecimal and (b) (477)10 to hexadecimal.   [2]

(ii) Draw the logic circuit and the truth table for (A OR NOT B) AND (B OR C).   [2]

Answer

(i) (a)

Make groups of 4 bits from the right: 0001  0100  1011

0001 → 1   0100 → 4   1011 → B

So (101001011)2 = (14B)16

(i) (b)

Divide byNumberRemainder
16477D
1629D
1611

Read the remainders from bottom to top: (1DD)16

(ii) Logic circuit:

ABBCY

Three variables give 23 = 8 rows.

ABC(A OR NOT B) AND (B OR C)
0000
0011
0100
0110
1000
1011
1101
1111
D1.32Practice4 marks

(i) Convert (a) (B0)16 to decimal and (b) (101011110)2 to decimal.   [2]

(ii) Draw the logic circuit and the truth table for A AND B AND NOT C.   [2]

Answer

(i) (a)

(B0)16 = B × 161 + 0 × 160 = 176 + 0 = 176

(i) (b)

(101011110)2 = 1 × 28 + 0 × 27 + 1 × 26 + 0 × 25 + 1 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20 = 256 + 0 + 64 + 0 + 16 + 8 + 4 + 2 + 0 = 350

(ii) Logic circuit:

ABCY

Three variables give 23 = 8 rows.

ABCA AND B AND NOT C
0000
0010
0100
0110
1000
1010
1101
1110
D2Cyber case study4 marks · 20 questions
D2.1Practice4 marks

Sohan, a Class XI student, posts photos of his new phone with his home address on social media. A week later he gets an e-mail saying he has won a phone and must pay a small fee by clicking a link. He clicks it and his social media account is taken over; fake posts are made in his name.

  1. What mistake did Sohan make while posting?
  2. Name the fraud in the e-mail.
  3. Name the crime when someone posts in his name.
  4. Suggest two measures to prevent such incidents.
Answer
  1. He shared personal details (home address) publicly; it adds to his digital footprint.
  2. Phishing.
  3. Identity theft / hacking of the account.
  4. Use strong passwords with two-factor authentication; never click unknown links or pay to claim prizes; keep personal details private.
D2.2Practice4 marks

A school is replacing 40 old computers, 10 printers and many batteries. The caretaker plans to dump them behind the school.

  1. What is this kind of waste called?
  2. Name two harmful substances in it.
  3. Why is dumping or burning it harmful?
  4. Suggest two correct ways to dispose of it.
Answer
  1. E-waste.
  2. Lead, mercury, cadmium. (Any two.)
  3. Toxic metals leak into soil and water; burning gives poisonous fumes that harm health.
  4. Give to an authorised e-waste recycler; donate working computers; use the company take-back scheme.
D2.3Practice4 marks

Meera wrote a story and posted it on her blog. Later she found that a magazine had printed it with another writer's name. A software company also copied her original app idea and design.

  1. What right of Meera was violated by the magazine?
  2. What is the act of the other writer called?
  3. Which IPR can protect an invention?
  4. What can Meera do?
Answer
  1. Copyright.
  2. Plagiarism.
  3. Patent.
  4. Send a legal notice / complain under copyright law; keep proof of the date of her work; use a licence (such as CC) on her blog.
D2.4Practice4 marks

The computer teacher wants to set up a lab for 30 students with a low budget. She is choosing between MS Office and LibreOffice, and between Windows and Ubuntu Linux.

  1. Which of the given software are FOSS?
  2. Write one benefit of FOSS for the school.
  3. What is proprietary software?
  4. Name any one open source licence.
Answer
  1. LibreOffice and Ubuntu Linux.
  2. No licence cost; it can be changed and shared; it is safe from piracy issues.
  3. Software owned by a company whose code is closed and which needs a paid licence.
  4. GPL (also Apache, MIT, BSD).
D2.5Practice4 marks

Karan, a Class XI student, gets messages every day in a gaming chat calling him names. The sender also shares his edited photo. Karan is upset and stops going to school.

  1. Name the cyber crime.
  2. Write two things Karan should do.
  3. Which law in India deals with such offences?
  4. Write two points of netiquette the sender ignored.
Answer
  1. Cyber bullying.
  2. Not reply; block and report the sender; keep screenshots; tell parents/teachers; report to the cyber cell.
  3. IT Act, 2000 (along with other laws).
  4. Respect others; do not share others' photos without permission; do not use abusive language.
D2.6Practice4 marks

A bank customer finds that all the files on her office computer are locked. A message asks her to pay money in cryptocurrency within 24 hours to unlock them. Earlier she had opened an e-mail attachment named invoice.exe.

  1. Name the malware.
  2. How did it probably enter the computer?
  3. Write two ways to protect against it.
  4. Should she pay the money? Why?
Answer
  1. Ransomware.
  2. Through the infected e-mail attachment.
  3. Keep regular offline backups; do not open unknown attachments; keep antivirus and OS updated.
  4. No; paying does not guarantee recovery and encourages crime. She should report it to CERT-In / cyber cell.
D2.7Practice4 marks

A company says its new website is easy for everyone. But it has small text, no captions on videos, and images without descriptions. Very few women apply for its technical jobs.

  1. Which group of users will face difficulty with the website?
  2. Suggest two changes to make the site accessible.
  3. What is the second issue called?
  4. Suggest one solution for it.
Answer
  1. Persons with disabilities (visually and hearing impaired).
  2. Add captions to videos, alt text to images, support screen readers and text resizing.
  3. Gender issue (gender gap in technology).
  4. Encourage women through equal opportunities, mentors, training and a safe workplace.
D2.8Practice4 marks

Aditi uses the same password 'aditi2009' for all her accounts. She often uses free Wi-Fi at the mall and saves her passwords in the browser of the library computer.

  1. What is wrong with her password?
  2. Why is free public Wi-Fi risky?
  3. What should she do on the library computer?
  4. Write two features of a strong password.
Answer
  1. It is easy to guess (name + year) and is used everywhere, so one leak opens all accounts.
  2. Others on the network can read unencrypted data or set up fake hotspots.
  3. Not save passwords; log out; use private browsing; clear history.
  4. At least 8 characters with capital and small letters, digits and symbols; not based on personal details.
D2.9Practice4 marks

Ravi notices that after visiting a shoe website, he sees shoe ads on every site. A new app on his phone also asks for access to his contacts, camera and location.

  1. What helps websites show him such ads?
  2. What type of digital footprint is this?
  3. Why should he be careful with app permissions?
  4. Write one way to reduce such tracking.
Answer
  1. Cookies (third-party tracking cookies).
  2. Passive digital footprint.
  3. The app can collect and misuse personal data and contacts.
  4. Block third-party cookies / use private browsing / give only needed permissions.
D2.10Practice4 marks

A student downloads a paid video editor for free from an unknown website. After installing it, his computer slows down, shows many ads, and his friends get strange messages from his e-mail.

  1. What is the free copy of paid software called?
  2. Name two types of malware that may be present.
  3. Is this legal? Why?
  4. What should he do now?
Answer
  1. Pirated software (software piracy).
  2. Adware, Trojan horse / spyware.
  3. No; it breaks copyright and the licence.
  4. Uninstall it, run a full antivirus scan, change passwords, and use licensed or free open source software.
D2.11Practice4 marks

In a village, many students do not have smartphones or Internet at home, so they miss online classes. The panchayat wants to help.

  1. What is this problem called?
  2. Suggest two steps the panchayat can take.
  3. How can the school help?
  4. Name one government initiative for this.
Answer
  1. Digital divide.
  2. Set up a community computer centre with Internet; provide cheap devices; digital literacy camps.
  3. Open the computer lab after school; share printed notes; lend tablets.
  4. Digital India (also PM eVidya / BharatNet).
D2.12Practice4 marks

Nidhi used a photo from a website in her school poster. The photo had a CC BY-NC licence. She later printed and sold the posters at a fair.

  1. What does CC stand for?
  2. What do BY and NC mean?
  3. Which condition did she break?
  4. What should she have done?
Answer
  1. Creative Commons.
  2. BY: give credit to the creator. NC: non-commercial use only.
  3. NC: she used it commercially by selling.
  4. Use it only for the free school poster with credit, or ask permission for selling, or use a photo that allows commercial use.
D2.13Practice4 marks

A school wants students to make a digital magazine. Students use photos, music and articles from the Internet. Some copy articles word for word.

  1. Name the problem when articles are copied word for word.
  2. How can students use pictures legally?
  3. What should be written below each borrowed item?
  4. Name the licence that lets creators share works with conditions.
Answer
  1. Plagiarism.
  2. Use their own photos, public-domain images or Creative Commons images as per their licence.
  3. Credit: the name of the creator and the source (citation).
  4. Creative Commons (CC).
D2.14Practice4 marks

Ms. Gupta teaches a student who uses a wheelchair and another who has low vision. The school wants to make its computer lab and website inclusive.

  1. What is meant by inclusive technology?
  2. Suggest one tool for the student with low vision.
  3. Suggest one change in the lab for the wheelchair user.
  4. Write one website change that helps all users.
Answer
  1. Technology designed so that everyone, including persons with disabilities, can use it.
  2. Screen magnifier or screen reader, high-contrast settings.
  3. Height-adjustable desks, ramps and enough space between rows.
  4. Clear large fonts, alt text for images and captions for videos.
D2.15Practice4 marks

An app that edits photos for free asks to read contacts, messages and location. Some months later, users of the app get spam calls and messages.

  1. Why do users get spam calls?
  2. What kind of footprint did the app collect?
  3. Write two precautions while installing apps.
  4. Which right of the users was violated?
Answer
  1. The app collected their contact data and shared or sold it.
  2. Passive digital footprint.
  3. Install from official stores; read reviews; give only needed permissions; check the privacy policy.
  4. Right to privacy (data protection).
D2.16Practice4 marks

Rahul wants to share a program he wrote. He wants others to use and improve it, but he wants every improved version to also stay free and open.

  1. Which type of licence should he use?
  2. Give one example of such a licence.
  3. Is his program FOSS? Why?
  4. Name one popular software released under this kind of licence.
Answer
  1. A copyleft (free/open source) licence.
  2. GNU GPL.
  3. Yes; its source code will be open and it can be freely used, changed and shared.
  4. Linux kernel (GPL).
D2.17Practice4 marks

An online game asks players to buy coins. A message on the game chat says, 'Get free coins: share your username and password here.' Some players lose their accounts.

  1. Name the trick used.
  2. What should a player do on seeing such a message?
  3. Write one sign of this trick.
  4. Give two features of safe gaming.
Answer
  1. Phishing (social engineering).
  2. Ignore it, never share passwords, report the message to the game.
  3. A free offer that asks for login details.
  4. Play on official platforms; do not share personal details; set time limits; use strong passwords.
D2.18Practice4 marks

The school's computers are on all night, monitors are very bright and many students sit for hours without breaks.

  1. Write one environmental issue here.
  2. Write one health issue for students.
  3. Suggest two good habits.
  4. What is the 20-20-20 rule?
Answer
  1. Waste of electricity, which increases pollution.
  2. Eye strain and back pain.
  3. Switch off devices after use; take short breaks; sit straight; keep the screen at eye level.
  4. Every 20 minutes, look at something 20 feet away for 20 seconds.
D2.19Practice4 marks

A student finds that his photo has been edited and posted on a fake page with mocking comments. Many classmates share it.

  1. Name the offence.
  2. Are the classmates who shared it also wrong? Why?
  3. Write two steps the student should take.
  4. Where can such crimes be reported online in India?
Answer
  1. Cyber bullying (with misuse of his photo).
  2. Yes; sharing spreads the harm and breaks netiquette.
  3. Tell parents/teachers; report and block the page; keep screenshots; complain to the cyber cell.
  4. cybercrime.gov.in (National Cyber Crime Reporting Portal) or helpline 1930.
D2.20Practice4 marks

A company keeps the data of its customers. A hacker steals the names, phone numbers and card details of thousands of customers.

  1. What is this incident called?
  2. Write two measures the company should have taken.
  3. How can customers protect themselves now?
  4. Which Indian Act deals with such crimes?
Answer
  1. A data breach (hacking).
  2. Encrypt data; use firewalls; give limited access; update systems; train staff.
  3. Block or change their cards; change passwords; watch for phishing calls.
  4. IT Act, 2000.
D3String programs4 marks · 42 questions
D3.1Practice4 marks

Write a program to input a string and check whether it is a palindrome (ignore the case).

Answer
s = input('Enter a string: ')
t = s.lower()
if t == t[::-1]:
    print(s, 'is a palindrome')
else:
    print(s, 'is not a palindrome')

Sample run

Enter a string: Madam
Madam is a palindrome
D3.2Practice4 marks

Write a program to input a sentence and count the number of words in it.

Answer
s = input('Enter a sentence: ')
words = s.split()
print('Number of words =', len(words))

Sample run

Enter a sentence: Practice makes a person perfect
Number of words = 5
D3.3Practice4 marks

Write a program to input a sentence and print the longest word and its length.

Answer
s = input('Enter a sentence: ')
longest = ''
for w in s.split():
    if len(w) > len(longest):
        longest = w
print('Longest word:', longest, 'with', len(longest), 'letters')

Sample run

Enter a sentence: Computer science is wonderful
Longest word: wonderful with 9 letters
D3.4Practice4 marks

Write a program to input a sentence and print the words that start with a vowel.

Answer
s = input('Enter a sentence: ')
for w in s.split():
    if w[0] in 'aeiouAEIOU':
        print(w)

Sample run

Enter a sentence: An apple is on every table in India
An
apple
is
on
every
in
India
D3.5Practice4 marks

Write a program to convert the first letter of every word to capital without using title().

Answer
s = input('Enter a sentence: ')
new = ''
for w in s.split():
    new = new + w[0].upper() + w[1:] + ' '
print(new.strip())

Sample run

Enter a sentence: keep calm and write code
Keep Calm And Write Code
D3.6Practice4 marks

Write a program to replace every space in a string with a hyphen (-) without using replace().

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch == ' ':
        new = new + '-'
    else:
        new = new + ch
print(new)

Sample run

Enter a string: Save water save life
Save-water-save-life
D3.7Practice4 marks

Write a program to print each word of a sentence in reverse, keeping the order of the words.

Answer
s = input('Enter a sentence: ')
result = []
for w in s.split():
    result.append(w[::-1])
print(' '.join(result))

Sample run

Enter a sentence: Python is fun
nohtyP si nuf
D3.8Practice4 marks

Write a program to input a sentence and a word, and count how many times the word occurs (ignore the case).

Answer
s = input('Enter a sentence: ')
word = input('Word to count: ')
count = 0
for w in s.lower().split():
    if w == word.lower():
        count += 1
print(word, 'occurs', count, 'times')

Sample run

Enter a sentence: The cat and the dog and THE bird
Word to count: the
the occurs 3 times
D3.9Practice4 marks

Write a program to remove all the vowels from a string.

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch not in 'aeiouAEIOU':
        new = new + ch
print('Without vowels:', new)

Sample run

Enter a string: Education is important
Without vowels: dctn s mprtnt
D3.10Practice4 marks

Write a program to toggle the case of every letter in a string (without swapcase()).

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch.isupper():
        new = new + ch.lower()
    elif ch.islower():
        new = new + ch.upper()
    else:
        new = new + ch
print(new)

Sample run

Enter a string: Hello World 2026
hELLO wORLD 2026
D3.11Practice4 marks

Write a program to print the frequency of each character of a string, using a dictionary.

Answer
s = input('Enter a string: ')
freq = {}
for ch in s:
    if ch != ' ':
        freq[ch] = freq.get(ch, 0) + 1
for ch in freq:
    print(ch, ':', freq[ch])

Sample run

Enter a string: banana bread
b : 2
a : 4
n : 2
r : 1
e : 1
d : 1
D3.12Practice4 marks

Write a program to input a string and print it in the reverse order of its words.

Answer
s = input('Enter a sentence: ')
words = s.split()
words.reverse()
print(' '.join(words))

Sample run

Enter a sentence: I am learning Python
Python learning am I
D3.13Practice4 marks

Write a program to input an e-mail ID and print the user name and the domain name.

Answer
email = input('Enter e-mail: ')
at = email.find('@')
if at == -1:
    print('Invalid e-mail')
else:
    print('User name:', email[:at])
    print('Domain:', email[at + 1:])

Sample run

Enter e-mail: asha.kumari@kvs.gov.in
User name: asha.kumari
Domain: kvs.gov.in
D3.14Practice4 marks

Write a program to check whether a password is valid: at least 8 characters with at least one capital letter, one digit and one special character.

Answer
pwd = input('Password: ')
up = dig = sp = 0
for ch in pwd:
    if ch.isupper():
        up += 1
    elif ch.isdigit():
        dig += 1
    elif not ch.isalnum():
        sp += 1
if len(pwd) >= 8 and up > 0 and dig > 0 and sp > 0:
    print('Valid password')
else:
    print('Invalid password')

Sample run

Password: Kv@Delhi88
Valid password
D3.15Practice4 marks

Write a program to print the initials of a full name, e.g. 'Mohan Das Karamchand' gives 'M.D.K.'.

Answer
name = input('Full name: ')
init = ''
for w in name.split():
    init = init + w[0].upper() + '.'
print(init)

Sample run

Full name: mohan das karamchand
M.D.K.
D3.16Practice4 marks

Write a program to print the name in short form: initials of all but the last word, then the last word, e.g. 'M. K. Gandhi'.

Answer
name = input('Full name: ')
w = name.split()
short = ''
for x in w[:-1]:
    short = short + x[0].upper() + '. '
print(short + w[-1].title())

Sample run

Full name: mohandas karamchand gandhi
M. K. Gandhi
D3.17Practice4 marks

Write a program to input a string and print the characters at even positions and at odd positions separately.

Answer
s = input('Enter a string: ')
print('Even positions:', s[0::2])
print('Odd positions:', s[1::2])

Sample run

Enter a string: COMPUTER
Even positions: CMUE
Odd positions: OPTR
D3.18Practice4 marks

Write a program to count the number of words that have more than four letters.

Answer
s = input('Enter a sentence: ')
count = 0
for w in s.split():
    if len(w) > 4:
        count += 1
print('Words longer than 4 letters:', count)

Sample run

Enter a sentence: Every student should practise programs daily
Words longer than 4 letters: 6
D3.19Practice4 marks

Write a program to input a string and a character, and print all the positions where the character occurs.

Answer
s = input('Enter a string: ')
c = input('Character: ')
for i in range(len(s)):
    if s[i] == c:
        print(i, end=' ')

Sample run

Enter a string: programming
Character: m
6 7 
D3.20Practice4 marks

Write a program that inputs a string and creates a new string by moving every character one place in the alphabet (a becomes b, z becomes a). Other characters stay the same.

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if 'a' <= ch <= 'y' or 'A' <= ch <= 'Y':
        new = new + chr(ord(ch) + 1)
    elif ch == 'z':
        new = new + 'a'
    elif ch == 'Z':
        new = new + 'A'
    else:
        new = new + ch
print(new)

Sample run

Enter a string: Zebra crossing
Afcsb dspttjoh
D3.21Practice4 marks

Write a program to check whether two strings are anagrams (have the same letters in a different order).

Answer
a = input('First word: ')
b = input('Second word: ')
if sorted(a.lower()) == sorted(b.lower()):
    print('Anagrams')
else:
    print('Not anagrams')

Sample run

First word: Listen
Second word: Silent
Anagrams
D3.22Practice4 marks

Write a program to remove the duplicate characters from a string, keeping the first occurrence.

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch not in new:
        new = new + ch
print(new)

Sample run

Enter a string: programming
progamin
D3.23Practice4 marks

Write a program to input a string and count the number of upper-case letters and lower-case letters in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isupper():
        c1 += 1
    if ch.islower():
        c2 += 1
print('Upper-case letters:', c1)
print('Lower-case letters:', c2)

Sample run

Enter a string: Hello World 2026, KV Shalimar Bagh!
Upper-case letters: 6
Lower-case letters: 18
D3.24Practice4 marks

Write a program to input a string and count the number of upper-case letters and digits in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isupper():
        c1 += 1
    if ch.isdigit():
        c2 += 1
print('Upper-case letters:', c1)
print('Digits:', c2)

Sample run

Enter a string: Python 3.12 is EASY to Learn :)
Upper-case letters: 6
Digits: 3
D3.25Practice4 marks

Write a program to input a string and count the number of upper-case letters and spaces in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isupper():
        c1 += 1
    if ch == " ":
        c2 += 1
print('Upper-case letters:', c1)
print('Spaces:', c2)

Sample run

Enter a string: CBSE Exam on 15 March, all THE best!
Upper-case letters: 9
Spaces: 7
D3.26Practice4 marks

Write a program to input a string and count the number of upper-case letters and vowels in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isupper():
        c1 += 1
    if ch in "aeiouAEIOU":
        c2 += 1
print('Upper-case letters:', c1)
print('Vowels:', c2)

Sample run

Enter a string: I love India; Jai Hind 123
Upper-case letters: 4
Vowels: 9
D3.27Practice4 marks

Write a program to input a string and count the number of upper-case letters and special characters (not a letter, digit or space) in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isupper():
        c1 += 1
    if not ch.isalnum() and ch != " ":
        c2 += 1
print('Upper-case letters:', c1)
print('Special characters:', c2)

Sample run

Enter a string: Hello World 2026, KV Shalimar Bagh!
Upper-case letters: 6
Special characters: 2
D3.28Practice4 marks

Write a program to input a string and count the number of lower-case letters and digits in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.islower():
        c1 += 1
    if ch.isdigit():
        c2 += 1
print('Lower-case letters:', c1)
print('Digits:', c2)

Sample run

Enter a string: Python 3.12 is EASY to Learn :)
Lower-case letters: 13
Digits: 3
D3.29Practice4 marks

Write a program to input a string and count the number of lower-case letters and spaces in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.islower():
        c1 += 1
    if ch == " ":
        c2 += 1
print('Lower-case letters:', c1)
print('Spaces:', c2)

Sample run

Enter a string: CBSE Exam on 15 March, all THE best!
Lower-case letters: 16
Spaces: 7
D3.30Practice4 marks

Write a program to input a string and count the number of lower-case letters and vowels in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.islower():
        c1 += 1
    if ch in "aeiouAEIOU":
        c2 += 1
print('Lower-case letters:', c1)
print('Vowels:', c2)

Sample run

Enter a string: I love India; Jai Hind 123
Lower-case letters: 13
Vowels: 9
D3.31Practice4 marks

Write a program to input a string and count the number of lower-case letters and special characters (not a letter, digit or space) in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.islower():
        c1 += 1
    if not ch.isalnum() and ch != " ":
        c2 += 1
print('Lower-case letters:', c1)
print('Special characters:', c2)

Sample run

Enter a string: Hello World 2026, KV Shalimar Bagh!
Lower-case letters: 18
Special characters: 2
D3.32Practice4 marks

Write a program to input a string and count the number of digits and spaces in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isdigit():
        c1 += 1
    if ch == " ":
        c2 += 1
print('Digits:', c1)
print('Spaces:', c2)

Sample run

Enter a string: Python 3.12 is EASY to Learn :)
Digits: 3
Spaces: 6
D3.33Practice4 marks

Write a program to input a string and count the number of digits and vowels in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isdigit():
        c1 += 1
    if ch in "aeiouAEIOU":
        c2 += 1
print('Digits:', c1)
print('Vowels:', c2)

Sample run

Enter a string: CBSE Exam on 15 March, all THE best!
Digits: 2
Vowels: 8
D3.34Practice4 marks

Write a program to input a string and count the number of digits and special characters (not a letter, digit or space) in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch.isdigit():
        c1 += 1
    if not ch.isalnum() and ch != " ":
        c2 += 1
print('Digits:', c1)
print('Special characters:', c2)

Sample run

Enter a string: I love India; Jai Hind 123
Digits: 3
Special characters: 1
D3.35Practice4 marks

Write a program to input a string and count the number of spaces and vowels in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch == " ":
        c1 += 1
    if ch in "aeiouAEIOU":
        c2 += 1
print('Spaces:', c1)
print('Vowels:', c2)

Sample run

Enter a string: Hello World 2026, KV Shalimar Bagh!
Spaces: 5
Vowels: 7
D3.36Practice4 marks

Write a program to input a string and count the number of spaces and special characters (not a letter, digit or space) in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch == " ":
        c1 += 1
    if not ch.isalnum() and ch != " ":
        c2 += 1
print('Spaces:', c1)
print('Special characters:', c2)

Sample run

Enter a string: Python 3.12 is EASY to Learn :)
Spaces: 6
Special characters: 3
D3.37Practice4 marks

Write a program to input a string and count the number of vowels and special characters (not a letter, digit or space) in it.

Answer
s = input('Enter a string: ')
c1 = c2 = 0
for ch in s:
    if ch in "aeiouAEIOU":
        c1 += 1
    if not ch.isalnum() and ch != " ":
        c2 += 1
print('Vowels:', c1)
print('Special characters:', c2)

Sample run

Enter a string: CBSE Exam on 15 March, all THE best!
Vowels: 8
Special characters: 2
D3.38Practice4 marks

Write a program to input a string and display it after replacing each of its upper-case letters with "*".

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch.isupper():
        new = new + '*'
    else:
        new = new + ch
print(new)

Sample run

Enter a string: Python 3.12 is EASY to Learn :)
*ython 3.12 is **** to *earn :)
D3.39Practice4 marks

Write a program to input a string and display it after replacing each of its lower-case letters with "*".

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch.islower():
        new = new + '*'
    else:
        new = new + ch
print(new)

Sample run

Enter a string: CBSE Exam on 15 March, all THE best!
CBSE E*** ** 15 M****, *** THE ****!
D3.40Practice4 marks

Write a program to input a string and display it after replacing each of its digits with "*".

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch.isdigit():
        new = new + '*'
    else:
        new = new + ch
print(new)

Sample run

Enter a string: I love India; Jai Hind 123
I love India; Jai Hind ***
D3.41Practice4 marks

Write a program to input a string and display it after replacing each of its spaces with "*".

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch == " ":
        new = new + '*'
    else:
        new = new + ch
print(new)

Sample run

Enter a string: Hello World 2026, KV Shalimar Bagh!
Hello*World*2026,*KV*Shalimar*Bagh!
D3.42Practice4 marks

Write a program to input a string and display it after replacing each of its vowels with "*".

Answer
s = input('Enter a string: ')
new = ''
for ch in s:
    if ch in "aeiouAEIOU":
        new = new + '*'
    else:
        new = new + ch
print(new)

Sample run

Enter a string: Python 3.12 is EASY to Learn :)
Pyth*n 3.12 *s **SY t* L**rn :)
D4List, tuple and dictionary programs4 marks · 22 questions
D4.1Practice4 marks

Write a program to input a list of numbers and search for a number (linear search). Print its position or 'not found'.

Answer
L = eval(input('Enter a list: '))
x = int(input('Number to search: '))
for i in range(len(L)):
    if L[i] == x:
        print(x, 'found at index', i)
        break
else:
    print(x, 'not found')

Sample run

Enter a list: [12, 45, 7, 30, 18]
Number to search: 30
30 found at index 3
D4.2Practice4 marks

Write a program to input a list and swap the elements at even positions with the elements at odd positions: [1, 2, 3, 4] becomes [2, 1, 4, 3].

Answer
L = eval(input('Enter a list: '))
for i in range(0, len(L) - 1, 2):
    L[i], L[i + 1] = L[i + 1], L[i]
print(L)

Sample run

Enter a list: [10, 20, 30, 40, 50, 60]
[20, 10, 40, 30, 60, 50]
D4.3Practice4 marks

Write a program to find the largest and the second largest element of a list without sorting.

Answer
L = eval(input('Enter a list: '))
first = second = None
for x in L:
    if first is None or x > first:
        second = first
        first = x
    elif x != first and (second is None or x > second):
        second = x
print('Largest =', first)
print('Second largest =', second)

Sample run

Enter a list: [34, 78, 12, 90, 56, 90]
Largest = 90
Second largest = 78
D4.4Practice4 marks

Write a program to rotate the elements of a list one place to the left: [1, 2, 3, 4] becomes [2, 3, 4, 1].

Answer
L = eval(input('Enter a list: '))
first = L[0]
for i in range(len(L) - 1):
    L[i] = L[i + 1]
L[-1] = first
print(L)

Sample run

Enter a list: [5, 10, 15, 20, 25]
[10, 15, 20, 25, 5]
D4.5Practice4 marks

Write a program to rotate the elements of a list one place to the right.

Answer
L = eval(input('Enter a list: '))
last = L[-1]
for i in range(len(L) - 1, 0, -1):
    L[i] = L[i - 1]
L[0] = last
print(L)

Sample run

Enter a list: [5, 10, 15, 20, 25]
[25, 5, 10, 15, 20]
D4.6Practice4 marks

Write a program to remove the duplicate elements from a list.

Answer
L = eval(input('Enter a list: '))
new = []
for x in L:
    if x not in new:
        new.append(x)
print('Without duplicates:', new)

Sample run

Enter a list: [3, 5, 3, 8, 5, 9, 3]
Without duplicates: [3, 5, 8, 9]
D4.7Practice4 marks

Write a program to input a list and put the positive and the negative numbers into two separate lists.

Answer
L = eval(input('Enter a list: '))
pos, neg = [], []
for x in L:
    if x >= 0:
        pos.append(x)
    else:
        neg.append(x)
print('Positive:', pos)
print('Negative:', neg)

Sample run

Enter a list: [12, -5, 0, 8, -17, 25]
Positive: [12, 0, 8, 25]
Negative: [-5, -17]
D4.8Practice4 marks

Write a program to find the frequency of each element of a list using a dictionary.

Answer
L = eval(input('Enter a list: '))
freq = {}
for x in L:
    freq[x] = freq.get(x, 0) + 1
for k in freq:
    print(k, 'occurs', freq[k], 'times')

Sample run

Enter a list: [2, 7, 2, 9, 7, 2]
2 occurs 3 times
7 occurs 2 times
9 occurs 1 times
D4.9Practice4 marks

Write a program to input a list and replace every even element by its half and every odd element by its double.

Answer
L = eval(input('Enter a list: '))
for i in range(len(L)):
    if L[i] % 2 == 0:
        L[i] = L[i] // 2
    else:
        L[i] = L[i] * 2
print(L)

Sample run

Enter a list: [10, 7, 24, 3, 15]
[5, 14, 12, 6, 30]
D4.10Practice4 marks

Write a program to print the cumulative sum of a list: [1, 2, 3] gives [1, 3, 6].

Answer
L = eval(input('Enter a list: '))
C = []
s = 0
for x in L:
    s = s + x
    C.append(s)
print(C)

Sample run

Enter a list: [4, 6, 1, 9, 5]
[4, 10, 11, 20, 25]
D4.11Practice4 marks

Write a program to input a tuple of numbers and print the maximum, the minimum, the sum and the mean.

Answer
T = eval(input('Enter a tuple: '))
print('Maximum =', max(T))
print('Minimum =', min(T))
print('Sum =', sum(T))
print('Mean =', sum(T) / len(T))

Sample run

Enter a tuple: (45, 12, 78, 33, 52)
Maximum = 78
Minimum = 12
Sum = 220
Mean = 44.0
D4.12Practice4 marks

Write a program to input a tuple and count how many times a given element occurs in it, without count().

Answer
T = eval(input('Enter a tuple: '))
x = int(input('Element: '))
c = 0
for v in T:
    if v == x:
        c += 1
print(x, 'occurs', c, 'times')

Sample run

Enter a tuple: (4, 8, 4, 2, 4, 9)
Element: 4
4 occurs 3 times
D4.13Practice4 marks

Write a program to create a dictionary of n students' roll numbers and names, and search for a roll number.

Answer
n = int(input('How many students? '))
D = {}
for i in range(n):
    r = int(input('Roll no: '))
    name = input('Name: ')
    D[r] = name
r = int(input('Roll no to search: '))
if r in D:
    print('Name:', D[r])
else:
    print('Not found')

Sample run

How many students? 3
Roll no: 1
Name: Asha
Roll no: 2
Name: Bilal
Roll no: 3
Name: Charu
Roll no to search: 2
Name: Bilal
D4.14Practice4 marks

Write a program to count the frequency of each word in a sentence using a dictionary.

Answer
s = input('Enter a sentence: ')
D = {}
for w in s.lower().split():
    D[w] = D.get(w, 0) + 1
print(D)

Sample run

Enter a sentence: the sun and the moon and the stars
{'the': 3, 'sun': 1, 'and': 2, 'moon': 1, 'stars': 1}
D4.15Practice4 marks

Write a program to create a dictionary of country names and their capitals, and print the capital of a country given by the user.

Answer
D = {'India': 'New Delhi', 'Japan': 'Tokyo', 'France': 'Paris', 'Kenya': 'Nairobi'}
c = input('Country: ')
print('Capital:', D.get(c, 'not in the list'))

Sample run

Country: Japan
Capital: Tokyo
D4.16Practice4 marks

Write a program to find the key with the largest value in a dictionary of items and prices.

Answer
D = eval(input('Enter a dictionary: '))
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Costliest item:', best, D[best])

Sample run

Enter a dictionary: {'pen': 20, 'bag': 650, 'book': 280}
Costliest item: bag 650
D4.17Practice4 marks

Write a program to input a list of numbers and print the numbers that are divisible by 3 but not by 5.

Answer
L = eval(input('Enter a list: '))
for x in L:
    if x % 3 == 0 and x % 5 != 0:
        print(x, end=' ')

Sample run

Enter a list: [9, 15, 21, 30, 33, 40, 42]
9 21 33 42 
D4.18Practice4 marks

Write a program to merge two lists and print the merged list in ascending order, without using + or extend().

Answer
A = eval(input('First list: '))
B = eval(input('Second list: '))
C = []
for x in A:
    C.append(x)
for x in B:
    C.append(x)
C.sort()
print(C)

Sample run

First list: [8, 3, 5]
Second list: [7, 1, 9]
[1, 3, 5, 7, 8, 9]
D4.19Practice4 marks

Write a program to create a dictionary from two lists: one of names and one of marks. Print the names of the students who scored above 80.

Answer
names = eval(input('Names: '))
marks = eval(input('Marks: '))
D = {}
for i in range(len(names)):
    D[names[i]] = marks[i]
print(D)
for k in D:
    if D[k] > 80:
        print(k)

Sample run

Names: ['Anu', 'Bhanu', 'Chetan', 'Divya']
Marks: [78, 92, 85, 64]
{'Anu': 78, 'Bhanu': 92, 'Chetan': 85, 'Divya': 64}
Bhanu
Chetan
D4.20Practice4 marks

Write a program to input a list of words and print the words that are palindromes.

Answer
W = eval(input('Enter a list of words: '))
for w in W:
    if w.lower() == w.lower()[::-1]:
        print(w)

Sample run

Enter a list of words: ['Level', 'tree', 'noon', 'civic', 'python']
Level
noon
civic
D4.21Practice4 marks

Write a program to input a list of numbers and create a new list that has the square of each odd number.

Answer
L = eval(input('Enter a list: '))
S = []
for x in L:
    if x % 2 != 0:
        S.append(x * x)
print(S)

Sample run

Enter a list: [3, 8, 5, 10, 7]
[9, 25, 49]
D4.22Practice4 marks

Write a program to find the sum of the elements at even indices and at odd indices of a list.

Answer
L = eval(input('Enter a list: '))
e = o = 0
for i in range(len(L)):
    if i % 2 == 0:
        e += L[i]
    else:
        o += L[i]
print('Sum at even indices =', e)
print('Sum at odd indices =', o)

Sample run

Enter a list: [10, 3, 20, 7, 30, 1]
Sum at even indices = 60
Sum at odd indices = 11

Section E · 5 marks each

E1Long programs: lists, dictionaries, menus5 marks · 64 questions
E1.1Practice5 marks

Write a menu-driven program for a list of numbers with these options: 1. Append a number, 2. Delete a number, 3. Display the list, 4. Sort the list, 5. Exit.

Answer
L = []
while True:
    print('1.Append 2.Delete 3.Display 4.Sort 5.Exit')
    ch = int(input('Choice: '))
    if ch == 1:
        L.append(int(input('Number: ')))
    elif ch == 2:
        x = int(input('Number to delete: '))
        if x in L:
            L.remove(x)
        else:
            print('Not in list')
    elif ch == 3:
        print(L)
    elif ch == 4:
        L.sort()
        print('Sorted:', L)
    elif ch == 5:
        break
    else:
        print('Wrong choice')

Sample run

1.Append 2.Delete 3.Display 4.Sort 5.Exit
Choice: 1
Number: 30
1.Append 2.Delete 3.Display 4.Sort 5.Exit
Choice: 1
Number: 10
1.Append 2.Delete 3.Display 4.Sort 5.Exit
Choice: 1
Number: 20
1.Append 2.Delete 3.Display 4.Sort 5.Exit
Choice: 3
[30, 10, 20]
1.Append 2.Delete 3.Display 4.Sort 5.Exit
Choice: 2
Number to delete: 10
1.Append 2.Delete 3.Display 4.Sort 5.Exit
Choice: 4
Sorted: [20, 30]
1.Append 2.Delete 3.Display 4.Sort 5.Exit
Choice: 5
E1.2Practice5 marks

Write a menu-driven program to maintain a phone book (dictionary of name: phone) with options: 1. Add, 2. Search, 3. Delete, 4. Display all, 5. Exit.

Answer
book = {}
while True:
    print('1.Add 2.Search 3.Delete 4.Display 5.Exit')
    ch = int(input('Choice: '))
    if ch == 1:
        n = input('Name: ')
        book[n] = input('Phone: ')
    elif ch == 2:
        n = input('Name: ')
        print(book.get(n, 'Not found'))
    elif ch == 3:
        n = input('Name: ')
        if n in book:
            del book[n]
            print('Deleted')
        else:
            print('Not found')
    elif ch == 4:
        for n in book:
            print(n, book[n])
    elif ch == 5:
        break

Sample run

1.Add 2.Search 3.Delete 4.Display 5.Exit
Choice: 1
Name: Asha
Phone: 98110 22334
1.Add 2.Search 3.Delete 4.Display 5.Exit
Choice: 1
Name: Ravi
Phone: 98990 55667
1.Add 2.Search 3.Delete 4.Display 5.Exit
Choice: 2
Name: Ravi
98990 55667
1.Add 2.Search 3.Delete 4.Display 5.Exit
Choice: 3
Name: Asha
Deleted
1.Add 2.Search 3.Delete 4.Display 5.Exit
Choice: 4
Ravi 98990 55667
1.Add 2.Search 3.Delete 4.Display 5.Exit
Choice: 5
E1.3Practice5 marks

Write a menu-driven program for a stock dictionary (item: quantity): 1. Add or update an item, 2. Sell an item (reduce its quantity), 3. Show items with quantity below 5, 4. Exit.

Answer
stock = {'pen': 40, 'pencil': 3, 'eraser': 12}
while True:
    print('1.Add/Update 2.Sell 3.Low stock 4.Exit')
    ch = int(input('Choice: '))
    if ch == 1:
        it = input('Item: ')
        stock[it] = stock.get(it, 0) + int(input('Quantity: '))
    elif ch == 2:
        it = input('Item: ')
        q = int(input('Quantity: '))
        if stock.get(it, 0) >= q:
            stock[it] -= q
        else:
            print('Not enough stock')
    elif ch == 3:
        for it in stock:
            if stock[it] < 5:
                print(it, stock[it])
    elif ch == 4:
        break
    print(stock)

Sample run

1.Add/Update 2.Sell 3.Low stock 4.Exit
Choice: 1
Item: sharpener
Quantity: 6
{'pen': 40, 'pencil': 3, 'eraser': 12, 'sharpener': 6}
1.Add/Update 2.Sell 3.Low stock 4.Exit
Choice: 2
Item: pen
Quantity: 38
{'pen': 2, 'pencil': 3, 'eraser': 12, 'sharpener': 6}
1.Add/Update 2.Sell 3.Low stock 4.Exit
Choice: 3
pen 2
pencil 3
{'pen': 2, 'pencil': 3, 'eraser': 12, 'sharpener': 6}
1.Add/Update 2.Sell 3.Low stock 4.Exit
Choice: 4
E1.4Practice5 marks

Write a menu-driven program for strings: 1. Count vowels, 2. Reverse the string, 3. Check palindrome, 4. Count words, 5. Exit. The string is input once at the start.

Answer
s = input('Enter a string: ')
while True:
    print('1.Vowels 2.Reverse 3.Palindrome 4.Words 5.Exit')
    ch = int(input('Choice: '))
    if ch == 1:
        c = 0
        for x in s:
            if x in 'aeiouAEIOU':
                c += 1
        print('Vowels:', c)
    elif ch == 2:
        print(s[::-1])
    elif ch == 3:
        print('Palindrome' if s.lower() == s.lower()[::-1] else 'Not a palindrome')
    elif ch == 4:
        print('Words:', len(s.split()))
    elif ch == 5:
        break

Sample run

Enter a string: Never odd or even
1.Vowels 2.Reverse 3.Palindrome 4.Words 5.Exit
Choice: 1
Vowels: 6
1.Vowels 2.Reverse 3.Palindrome 4.Words 5.Exit
Choice: 2
neve ro ddo reveN
1.Vowels 2.Reverse 3.Palindrome 4.Words 5.Exit
Choice: 3
Not a palindrome
1.Vowels 2.Reverse 3.Palindrome 4.Words 5.Exit
Choice: 4
Words: 4
1.Vowels 2.Reverse 3.Palindrome 4.Words 5.Exit
Choice: 5
E1.5Practice5 marks

Write a program to input the name and marks of 4 students and store them in a dictionary. Then:

  1. Display the names whose marks is more than 75, and
  2. Display the name with the highest marks.
Answer
n = int(input('How many students? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Marks: '))
# (i)
print('Names with marks more than 75:')
for k in D:
    if D[k] > 75:
        print(k, D[k])
# (ii)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest marks:', best, D[best])

Sample run

How many students? 4
Name: Asha
Marks: 88
Name: Bilal
Marks: 64
Name: Charu
Marks: 92
Name: Dev
Marks: 47
Names with marks more than 75:
Asha 88
Charu 92
Highest marks: Charu 92
E1.6Practice5 marks

Write a program to input the name and marks of 4 students and store them in a dictionary. Then:

  1. Display the name with the highest marks, and
  2. Display the total and the average marks.
Answer
n = int(input('How many students? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Marks: '))
# (i)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest marks:', best, D[best])
# (ii)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))

Sample run

How many students? 4
Name: Asha
Marks: 88
Name: Bilal
Marks: 64
Name: Charu
Marks: 92
Name: Dev
Marks: 47
Highest marks: Charu 92
Total = 291
Average = 72.75
E1.7Practice5 marks

Write a program to input the name and marks of 4 students and store them in a dictionary. Then:

  1. Display the total and the average marks, and
  2. Display the records in alphabetical order of name.
Answer
n = int(input('How many students? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Marks: '))
# (i)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))
# (ii)
for k in sorted(D):
    print(k, D[k])

Sample run

How many students? 4
Name: Asha
Marks: 88
Name: Bilal
Marks: 64
Name: Charu
Marks: 92
Name: Dev
Marks: 47
Total = 291
Average = 72.75
Asha 88
Bilal 64
Charu 92
Dev 47
E1.8Practice5 marks

Write a program to input the name and marks of 4 students and store them in a dictionary. Then:

  1. Display the records in alphabetical order of name, and
  2. Count the names whose marks is less than 75.
Answer
n = int(input('How many students? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Marks: '))
# (i)
for k in sorted(D):
    print(k, D[k])
# (ii)
c = 0
for k in D:
    if D[k] < 75:
        c += 1
print('Count with marks less than 75:', c)

Sample run

How many students? 4
Name: Asha
Marks: 88
Name: Bilal
Marks: 64
Name: Charu
Marks: 92
Name: Dev
Marks: 47
Asha 88
Bilal 64
Charu 92
Dev 47
Count with marks less than 75: 2
E1.9Practice5 marks

Write a program to input the name and marks of 4 students and store them in a dictionary. Then:

  1. Count the names whose marks is less than 75, and
  2. Increase the marks of every name by 10 percent and display the dictionary.
Answer
n = int(input('How many students? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Marks: '))
# (i)
c = 0
for k in D:
    if D[k] < 75:
        c += 1
print('Count with marks less than 75:', c)
# (ii)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)

Sample run

How many students? 4
Name: Asha
Marks: 88
Name: Bilal
Marks: 64
Name: Charu
Marks: 92
Name: Dev
Marks: 47
Count with marks less than 75: 2
{'Asha': 96.8, 'Bilal': 70.4, 'Charu': 101.2, 'Dev': 51.7}
E1.10Practice5 marks

Write a program to input the name and salary of 4 employees and store them in a dictionary. Then:

  1. Display the names whose salary is more than 50000, and
  2. Display the total and the average salary.
Answer
n = int(input('How many employees? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Salary: '))
# (i)
print('Names with salary more than 50000:')
for k in D:
    if D[k] > 50000:
        print(k, D[k])
# (ii)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))

Sample run

How many employees? 4
Name: Ravi
Salary: 42000
Name: Seema
Salary: 58000
Name: Tarun
Salary: 36000
Name: Usha
Salary: 61000
Names with salary more than 50000:
Seema 58000
Usha 61000
Total = 197000
Average = 49250.0
E1.11Practice5 marks

Write a program to input the name and salary of 4 employees and store them in a dictionary. Then:

  1. Display the name with the highest salary, and
  2. Display the records in alphabetical order of name.
Answer
n = int(input('How many employees? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Salary: '))
# (i)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest salary:', best, D[best])
# (ii)
for k in sorted(D):
    print(k, D[k])

Sample run

How many employees? 4
Name: Ravi
Salary: 42000
Name: Seema
Salary: 58000
Name: Tarun
Salary: 36000
Name: Usha
Salary: 61000
Highest salary: Usha 61000
Ravi 42000
Seema 58000
Tarun 36000
Usha 61000
E1.12Practice5 marks

Write a program to input the name and salary of 4 employees and store them in a dictionary. Then:

  1. Display the total and the average salary, and
  2. Count the names whose salary is less than 50000.
Answer
n = int(input('How many employees? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Salary: '))
# (i)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))
# (ii)
c = 0
for k in D:
    if D[k] < 50000:
        c += 1
print('Count with salary less than 50000:', c)

Sample run

How many employees? 4
Name: Ravi
Salary: 42000
Name: Seema
Salary: 58000
Name: Tarun
Salary: 36000
Name: Usha
Salary: 61000
Total = 197000
Average = 49250.0
Count with salary less than 50000: 2
E1.13Practice5 marks

Write a program to input the name and salary of 4 employees and store them in a dictionary. Then:

  1. Display the records in alphabetical order of name, and
  2. Increase the salary of every name by 10 percent and display the dictionary.
Answer
n = int(input('How many employees? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Salary: '))
# (i)
for k in sorted(D):
    print(k, D[k])
# (ii)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)

Sample run

How many employees? 4
Name: Ravi
Salary: 42000
Name: Seema
Salary: 58000
Name: Tarun
Salary: 36000
Name: Usha
Salary: 61000
Ravi 42000
Seema 58000
Tarun 36000
Usha 61000
{'Ravi': 46200.0, 'Seema': 63800.0, 'Tarun': 39600.0, 'Usha': 67100.0}
E1.14Practice5 marks

Write a program to input the name and salary of 4 employees and store them in a dictionary. Then:

  1. Increase the salary of every name by 10 percent and display the dictionary, and
  2. Display the name with the highest salary.
Answer
n = int(input('How many employees? '))
D = {}
for i in range(n):
    k = input('Name: ')
    D[k] = int(input('Salary: '))
# (i)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)
# (ii)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest salary:', best, D[best])

Sample run

How many employees? 4
Name: Ravi
Salary: 42000
Name: Seema
Salary: 58000
Name: Tarun
Salary: 36000
Name: Usha
Salary: 61000
{'Ravi': 46200.0, 'Seema': 63800.0, 'Tarun': 39600.0, 'Usha': 67100.0}
Highest salary: Usha 67100.0
E1.15Practice5 marks

Write a program to input the item and price of 4 items and store them in a dictionary. Then:

  1. Display the items whose price is more than 300, and
  2. Display the records in alphabetical order of item.
Answer
n = int(input('How many items? '))
D = {}
for i in range(n):
    k = input('Item: ')
    D[k] = int(input('Price: '))
# (i)
print('Items with price more than 300:')
for k in D:
    if D[k] > 300:
        print(k, D[k])
# (ii)
for k in sorted(D):
    print(k, D[k])

Sample run

How many items? 4
Item: Pen
Price: 20
Item: Bag
Price: 650
Item: Book
Price: 280
Item: Lamp
Price: 450
Items with price more than 300:
Bag 650
Lamp 450
Bag 650
Book 280
Lamp 450
Pen 20
E1.16Practice5 marks

Write a program to input the item and price of 4 items and store them in a dictionary. Then:

  1. Display the item with the highest price, and
  2. Count the items whose price is less than 300.
Answer
n = int(input('How many items? '))
D = {}
for i in range(n):
    k = input('Item: ')
    D[k] = int(input('Price: '))
# (i)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest price:', best, D[best])
# (ii)
c = 0
for k in D:
    if D[k] < 300:
        c += 1
print('Count with price less than 300:', c)

Sample run

How many items? 4
Item: Pen
Price: 20
Item: Bag
Price: 650
Item: Book
Price: 280
Item: Lamp
Price: 450
Highest price: Bag 650
Count with price less than 300: 2
E1.17Practice5 marks

Write a program to input the item and price of 4 items and store them in a dictionary. Then:

  1. Display the total and the average price, and
  2. Increase the price of every item by 10 percent and display the dictionary.
Answer
n = int(input('How many items? '))
D = {}
for i in range(n):
    k = input('Item: ')
    D[k] = int(input('Price: '))
# (i)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))
# (ii)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)

Sample run

How many items? 4
Item: Pen
Price: 20
Item: Bag
Price: 650
Item: Book
Price: 280
Item: Lamp
Price: 450
Total = 1400
Average = 350.0
{'Pen': 22.0, 'Bag': 715.0, 'Book': 308.0, 'Lamp': 495.0}
E1.18Practice5 marks

Write a program to input the item and price of 4 items and store them in a dictionary. Then:

  1. Display the records in alphabetical order of item, and
  2. Display the items whose price is more than 300.
Answer
n = int(input('How many items? '))
D = {}
for i in range(n):
    k = input('Item: ')
    D[k] = int(input('Price: '))
# (i)
for k in sorted(D):
    print(k, D[k])
# (ii)
print('Items with price more than 300:')
for k in D:
    if D[k] > 300:
        print(k, D[k])

Sample run

How many items? 4
Item: Pen
Price: 20
Item: Bag
Price: 650
Item: Book
Price: 280
Item: Lamp
Price: 450
Bag 650
Book 280
Lamp 450
Pen 20
Items with price more than 300:
Bag 650
Lamp 450
E1.19Practice5 marks

Write a program to input the item and price of 4 items and store them in a dictionary. Then:

  1. Count the items whose price is less than 300, and
  2. Display the item with the highest price.
Answer
n = int(input('How many items? '))
D = {}
for i in range(n):
    k = input('Item: ')
    D[k] = int(input('Price: '))
# (i)
c = 0
for k in D:
    if D[k] < 300:
        c += 1
print('Count with price less than 300:', c)
# (ii)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest price:', best, D[best])

Sample run

How many items? 4
Item: Pen
Price: 20
Item: Bag
Price: 650
Item: Book
Price: 280
Item: Lamp
Price: 450
Count with price less than 300: 2
Highest price: Bag 650
E1.20Practice5 marks

Write a program to input the player and runs of 4 players and store them in a dictionary. Then:

  1. Display the players whose runs is more than 50, and
  2. Count the players whose runs is less than 50.
Answer
n = int(input('How many players? '))
D = {}
for i in range(n):
    k = input('Player: ')
    D[k] = int(input('Runs: '))
# (i)
print('Players with runs more than 50:')
for k in D:
    if D[k] > 50:
        print(k, D[k])
# (ii)
c = 0
for k in D:
    if D[k] < 50:
        c += 1
print('Count with runs less than 50:', c)

Sample run

How many players? 4
Player: Rohit
Runs: 74
Player: Virat
Runs: 101
Player: Gill
Runs: 38
Player: Pant
Runs: 55
Players with runs more than 50:
Rohit 74
Virat 101
Pant 55
Count with runs less than 50: 1
E1.21Practice5 marks

Write a program to input the player and runs of 4 players and store them in a dictionary. Then:

  1. Display the player with the highest runs, and
  2. Increase the runs of every player by 10 percent and display the dictionary.
Answer
n = int(input('How many players? '))
D = {}
for i in range(n):
    k = input('Player: ')
    D[k] = int(input('Runs: '))
# (i)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest runs:', best, D[best])
# (ii)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)

Sample run

How many players? 4
Player: Rohit
Runs: 74
Player: Virat
Runs: 101
Player: Gill
Runs: 38
Player: Pant
Runs: 55
Highest runs: Virat 101
{'Rohit': 81.4, 'Virat': 111.1, 'Gill': 41.8, 'Pant': 60.5}
E1.22Practice5 marks

Write a program to input the player and runs of 4 players and store them in a dictionary. Then:

  1. Display the total and the average runs, and
  2. Display the players whose runs is more than 50.
Answer
n = int(input('How many players? '))
D = {}
for i in range(n):
    k = input('Player: ')
    D[k] = int(input('Runs: '))
# (i)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))
# (ii)
print('Players with runs more than 50:')
for k in D:
    if D[k] > 50:
        print(k, D[k])

Sample run

How many players? 4
Player: Rohit
Runs: 74
Player: Virat
Runs: 101
Player: Gill
Runs: 38
Player: Pant
Runs: 55
Total = 268
Average = 67.0
Players with runs more than 50:
Rohit 74
Virat 101
Pant 55
E1.23Practice5 marks

Write a program to input the player and runs of 4 players and store them in a dictionary. Then:

  1. Display the records in alphabetical order of player, and
  2. Display the player with the highest runs.
Answer
n = int(input('How many players? '))
D = {}
for i in range(n):
    k = input('Player: ')
    D[k] = int(input('Runs: '))
# (i)
for k in sorted(D):
    print(k, D[k])
# (ii)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest runs:', best, D[best])

Sample run

How many players? 4
Player: Rohit
Runs: 74
Player: Virat
Runs: 101
Player: Gill
Runs: 38
Player: Pant
Runs: 55
Gill 38
Pant 55
Rohit 74
Virat 101
Highest runs: Virat 101
E1.24Practice5 marks

Write a program to input the player and runs of 4 players and store them in a dictionary. Then:

  1. Increase the runs of every player by 10 percent and display the dictionary, and
  2. Display the records in alphabetical order of player.
Answer
n = int(input('How many players? '))
D = {}
for i in range(n):
    k = input('Player: ')
    D[k] = int(input('Runs: '))
# (i)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)
# (ii)
for k in sorted(D):
    print(k, D[k])

Sample run

How many players? 4
Player: Rohit
Runs: 74
Player: Virat
Runs: 101
Player: Gill
Runs: 38
Player: Pant
Runs: 55
{'Rohit': 81.4, 'Virat': 111.1, 'Gill': 41.8, 'Pant': 60.5}
Gill 41.8
Pant 60.5
Rohit 81.4
Virat 111.1
E1.25Practice5 marks

Write a program to input the title and copies of 4 books and store them in a dictionary. Then:

  1. Display the titles whose copies is more than 10, and
  2. Increase the copies of every title by 10 percent and display the dictionary.
Answer
n = int(input('How many books? '))
D = {}
for i in range(n):
    k = input('Title: ')
    D[k] = int(input('Copies: '))
# (i)
print('Titles with copies more than 10:')
for k in D:
    if D[k] > 10:
        print(k, D[k])
# (ii)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)

Sample run

How many books? 4
Title: Gitanjali
Copies: 12
Title: Godan
Copies: 5
Title: Malgudi Days
Copies: 9
Title: Wings of Fire
Copies: 15
Titles with copies more than 10:
Gitanjali 12
Wings of Fire 15
{'Gitanjali': 13.2, 'Godan': 5.5, 'Malgudi Days': 9.9, 'Wings of Fire': 16.5}
E1.26Practice5 marks

Write a program to input the title and copies of 4 books and store them in a dictionary. Then:

  1. Display the title with the highest copies, and
  2. Display the titles whose copies is more than 10.
Answer
n = int(input('How many books? '))
D = {}
for i in range(n):
    k = input('Title: ')
    D[k] = int(input('Copies: '))
# (i)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest copies:', best, D[best])
# (ii)
print('Titles with copies more than 10:')
for k in D:
    if D[k] > 10:
        print(k, D[k])

Sample run

How many books? 4
Title: Gitanjali
Copies: 12
Title: Godan
Copies: 5
Title: Malgudi Days
Copies: 9
Title: Wings of Fire
Copies: 15
Highest copies: Wings of Fire 15
Titles with copies more than 10:
Gitanjali 12
Wings of Fire 15
E1.27Practice5 marks

Write a program to input the title and copies of 4 books and store them in a dictionary. Then:

  1. Display the total and the average copies, and
  2. Display the title with the highest copies.
Answer
n = int(input('How many books? '))
D = {}
for i in range(n):
    k = input('Title: ')
    D[k] = int(input('Copies: '))
# (i)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))
# (ii)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest copies:', best, D[best])

Sample run

How many books? 4
Title: Gitanjali
Copies: 12
Title: Godan
Copies: 5
Title: Malgudi Days
Copies: 9
Title: Wings of Fire
Copies: 15
Total = 41
Average = 10.25
Highest copies: Wings of Fire 15
E1.28Practice5 marks

Write a program to input the title and copies of 4 books and store them in a dictionary. Then:

  1. Display the records in alphabetical order of title, and
  2. Display the total and the average copies.
Answer
n = int(input('How many books? '))
D = {}
for i in range(n):
    k = input('Title: ')
    D[k] = int(input('Copies: '))
# (i)
for k in sorted(D):
    print(k, D[k])
# (ii)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))

Sample run

How many books? 4
Title: Gitanjali
Copies: 12
Title: Godan
Copies: 5
Title: Malgudi Days
Copies: 9
Title: Wings of Fire
Copies: 15
Gitanjali 12
Godan 5
Malgudi Days 9
Wings of Fire 15
Total = 41
Average = 10.25
E1.29Practice5 marks

Write a program to input the title and copies of 4 books and store them in a dictionary. Then:

  1. Count the titles whose copies is less than 10, and
  2. Display the records in alphabetical order of title.
Answer
n = int(input('How many books? '))
D = {}
for i in range(n):
    k = input('Title: ')
    D[k] = int(input('Copies: '))
# (i)
c = 0
for k in D:
    if D[k] < 10:
        c += 1
print('Count with copies less than 10:', c)
# (ii)
for k in sorted(D):
    print(k, D[k])

Sample run

How many books? 4
Title: Gitanjali
Copies: 12
Title: Godan
Copies: 5
Title: Malgudi Days
Copies: 9
Title: Wings of Fire
Copies: 15
Count with copies less than 10: 2
Gitanjali 12
Godan 5
Malgudi Days 9
Wings of Fire 15
E1.30Practice5 marks

Write a program to input the city and population (lakh) of 4 cities and store them in a dictionary. Then:

  1. Display the citys whose population (lakh) is more than 50, and
  2. Display the citys whose population (lakh) is more than 50.
Answer
n = int(input('How many cities? '))
D = {}
for i in range(n):
    k = input('City: ')
    D[k] = int(input('Population (lakh): '))
# (i)
print('Citys with population (lakh) more than 50:')
for k in D:
    if D[k] > 50:
        print(k, D[k])
# (ii)
print('Citys with population (lakh) more than 50:')
for k in D:
    if D[k] > 50:
        print(k, D[k])

Sample run

How many cities? 4
City: Delhi
Population (lakh): 190
City: Pune
Population (lakh): 70
City: Jaipur
Population (lakh): 40
City: Patna
Population (lakh): 25
Citys with population (lakh) more than 50:
Delhi 190
Pune 70
Citys with population (lakh) more than 50:
Delhi 190
Pune 70
E1.31Practice5 marks

Write a program to input the city and population (lakh) of 4 cities and store them in a dictionary. Then:

  1. Display the city with the highest population (lakh), and
  2. Display the city with the highest population (lakh).
Answer
n = int(input('How many cities? '))
D = {}
for i in range(n):
    k = input('City: ')
    D[k] = int(input('Population (lakh): '))
# (i)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest population (lakh):', best, D[best])
# (ii)
best = None
for k in D:
    if best is None or D[k] > D[best]:
        best = k
print('Highest population (lakh):', best, D[best])

Sample run

How many cities? 4
City: Delhi
Population (lakh): 190
City: Pune
Population (lakh): 70
City: Jaipur
Population (lakh): 40
City: Patna
Population (lakh): 25
Highest population (lakh): Delhi 190
Highest population (lakh): Delhi 190
E1.32Practice5 marks

Write a program to input the city and population (lakh) of 4 cities and store them in a dictionary. Then:

  1. Display the total and the average population (lakh), and
  2. Display the total and the average population (lakh).
Answer
n = int(input('How many cities? '))
D = {}
for i in range(n):
    k = input('City: ')
    D[k] = int(input('Population (lakh): '))
# (i)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))
# (ii)
total = 0
for k in D:
    total += D[k]
print('Total =', total)
print('Average =', total / len(D))

Sample run

How many cities? 4
City: Delhi
Population (lakh): 190
City: Pune
Population (lakh): 70
City: Jaipur
Population (lakh): 40
City: Patna
Population (lakh): 25
Total = 325
Average = 81.25
Total = 325
Average = 81.25
E1.33Practice5 marks

Write a program to input the city and population (lakh) of 4 cities and store them in a dictionary. Then:

  1. Display the records in alphabetical order of city, and
  2. Display the records in alphabetical order of city.
Answer
n = int(input('How many cities? '))
D = {}
for i in range(n):
    k = input('City: ')
    D[k] = int(input('Population (lakh): '))
# (i)
for k in sorted(D):
    print(k, D[k])
# (ii)
for k in sorted(D):
    print(k, D[k])

Sample run

How many cities? 4
City: Delhi
Population (lakh): 190
City: Pune
Population (lakh): 70
City: Jaipur
Population (lakh): 40
City: Patna
Population (lakh): 25
Delhi 190
Jaipur 40
Patna 25
Pune 70
Delhi 190
Jaipur 40
Patna 25
Pune 70
E1.34Practice5 marks

Write a program to input the city and population (lakh) of 4 cities and store them in a dictionary. Then:

  1. Increase the population (lakh) of every city by 10 percent and display the dictionary, and
  2. Increase the population (lakh) of every city by 10 percent and display the dictionary.
Answer
n = int(input('How many cities? '))
D = {}
for i in range(n):
    k = input('City: ')
    D[k] = int(input('Population (lakh): '))
# (i)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)
# (ii)
for k in D:
    D[k] = round(D[k] * 1.1, 1)
print(D)

Sample run

How many cities? 4
City: Delhi
Population (lakh): 190
City: Pune
Population (lakh): 70
City: Jaipur
Population (lakh): 40
City: Patna
Population (lakh): 25
{'Delhi': 209.0, 'Pune': 77.0, 'Jaipur': 44.0, 'Patna': 27.5}
{'Delhi': 229.9, 'Pune': 84.7, 'Jaipur': 48.4, 'Patna': 30.3}
E1.35Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Display the list in reverse order without using reverse().
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
R = []
for i in range(len(L) - 1, -1, -1):
    R.append(L[i])
print('Reversed:', R)

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Sum = 111 Average = 15.857142857142858
Reversed: [11, 40, 9, 7, 22, 7, 15]
E1.36Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Count the even and the odd elements.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)

Sample run

Enter a list of numbers: [5, 18, 29, 18, 3, 60, 13]
Sum = 146 Average = 20.857142857142858
Even: 3 Odd: 4
E1.37Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Display the largest and the smallest element without max() and min().
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Sum = 134 Average = 19.142857142857142
Largest: 41 Smallest: 2
E1.38Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Display the elements that are greater than the average.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
avg = sum(L) / len(L)
print('Above average:', end=' ')
for x in L:
    if x > avg:
        print(x, end=' ')
print()

Sample run

Enter a list of numbers: [23, 8, 17, 42, 8, 31, 12]
Sum = 141 Average = 20.142857142857142
Above average: 23 42 31 
E1.39Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Display the prime numbers in the list.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
print('Primes:', end=' ')
for x in L:
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                break
        else:
            print(x, end=' ')
print()

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Sum = 111 Average = 15.857142857142858
Primes: 7 7 11 
E1.40Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Create a new list of the elements at even indices.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
E = L[::2]
print('Even-index elements:', E)

Sample run

Enter a list of numbers: [5, 18, 29, 18, 3, 60, 13]
Sum = 146 Average = 20.857142857142858
Even-index elements: [5, 29, 3, 13]
E1.41Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Display how many times each element occurs, using a dictionary.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print('Frequency:', F)

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Sum = 134 Average = 19.142857142857142
Frequency: {41: 1, 6: 2, 19: 1, 33: 1, 27: 1, 2: 1}
E1.42Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Sort the list in descending order and display the top three.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
S = sorted(L, reverse=True)
print('Top three:', S[:3])

Sample run

Enter a list of numbers: [23, 8, 17, 42, 8, 31, 12]
Sum = 141 Average = 20.142857142857142
Top three: [42, 31, 23]
E1.43Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the sum and the average of the elements, and
  2. Remove all the occurrences of the first element.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Sum =', sum(L), 'Average =', sum(L) / len(L))
# (ii)
x = L[0]
while x in L:
    L.remove(x)
print('After removing', x, ':', L)

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Sum = 111 Average = 15.857142857142858
After removing 15 : [7, 22, 7, 9, 40, 11]
E1.44Practice5 marks

Write a program to input a list of numbers. Then:

  1. Count the even and the odd elements, and
  2. Display the largest and the smallest element without max() and min().
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)
# (ii)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Even: 2 Odd: 5
Largest: 40 Smallest: 7
E1.45Practice5 marks

Write a program to input a list of numbers. Then:

  1. Count the even and the odd elements, and
  2. Display the elements that are greater than the average.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)
# (ii)
avg = sum(L) / len(L)
print('Above average:', end=' ')
for x in L:
    if x > avg:
        print(x, end=' ')
print()

Sample run

Enter a list of numbers: [5, 18, 29, 18, 3, 60, 13]
Even: 3 Odd: 4
Above average: 29 60 
E1.46Practice5 marks

Write a program to input a list of numbers. Then:

  1. Count the even and the odd elements, and
  2. Display the prime numbers in the list.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)
# (ii)
print('Primes:', end=' ')
for x in L:
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                break
        else:
            print(x, end=' ')
print()

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Even: 3 Odd: 4
Primes: 41 19 2 
E1.47Practice5 marks

Write a program to input a list of numbers. Then:

  1. Count the even and the odd elements, and
  2. Create a new list of the elements at even indices.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)
# (ii)
E = L[::2]
print('Even-index elements:', E)

Sample run

Enter a list of numbers: [23, 8, 17, 42, 8, 31, 12]
Even: 4 Odd: 3
Even-index elements: [23, 17, 8, 12]
E1.48Practice5 marks

Write a program to input a list of numbers. Then:

  1. Count the even and the odd elements, and
  2. Display how many times each element occurs, using a dictionary.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)
# (ii)
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print('Frequency:', F)

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Even: 2 Odd: 5
Frequency: {15: 1, 7: 2, 22: 1, 9: 1, 40: 1, 11: 1}
E1.49Practice5 marks

Write a program to input a list of numbers. Then:

  1. Count the even and the odd elements, and
  2. Sort the list in descending order and display the top three.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)
# (ii)
S = sorted(L, reverse=True)
print('Top three:', S[:3])

Sample run

Enter a list of numbers: [5, 18, 29, 18, 3, 60, 13]
Even: 3 Odd: 4
Top three: [60, 29, 18]
E1.50Practice5 marks

Write a program to input a list of numbers. Then:

  1. Count the even and the odd elements, and
  2. Remove all the occurrences of the first element.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
ev = od = 0
for x in L:
    if x % 2 == 0:
        ev += 1
    else:
        od += 1
print('Even:', ev, 'Odd:', od)
# (ii)
x = L[0]
while x in L:
    L.remove(x)
print('After removing', x, ':', L)

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Even: 3 Odd: 4
After removing 41 : [6, 19, 33, 6, 27, 2]
E1.51Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the largest and the smallest element without max() and min(), and
  2. Display the elements that are greater than the average.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)
# (ii)
avg = sum(L) / len(L)
print('Above average:', end=' ')
for x in L:
    if x > avg:
        print(x, end=' ')
print()

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Largest: 41 Smallest: 2
Above average: 41 33 27 
E1.52Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the largest and the smallest element without max() and min(), and
  2. Display the prime numbers in the list.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)
# (ii)
print('Primes:', end=' ')
for x in L:
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                break
        else:
            print(x, end=' ')
print()

Sample run

Enter a list of numbers: [23, 8, 17, 42, 8, 31, 12]
Largest: 42 Smallest: 8
Primes: 23 17 31 
E1.53Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the largest and the smallest element without max() and min(), and
  2. Create a new list of the elements at even indices.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)
# (ii)
E = L[::2]
print('Even-index elements:', E)

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Largest: 40 Smallest: 7
Even-index elements: [15, 22, 9, 11]
E1.54Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the largest and the smallest element without max() and min(), and
  2. Display how many times each element occurs, using a dictionary.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)
# (ii)
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print('Frequency:', F)

Sample run

Enter a list of numbers: [5, 18, 29, 18, 3, 60, 13]
Largest: 60 Smallest: 3
Frequency: {5: 1, 18: 2, 29: 1, 3: 1, 60: 1, 13: 1}
E1.55Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the largest and the smallest element without max() and min(), and
  2. Sort the list in descending order and display the top three.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)
# (ii)
S = sorted(L, reverse=True)
print('Top three:', S[:3])

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Largest: 41 Smallest: 2
Top three: [41, 33, 27]
E1.56Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the largest and the smallest element without max() and min(), and
  2. Remove all the occurrences of the first element.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
big = small = L[0]
for x in L:
    if x > big:
        big = x
    if x < small:
        small = x
print('Largest:', big, 'Smallest:', small)
# (ii)
x = L[0]
while x in L:
    L.remove(x)
print('After removing', x, ':', L)

Sample run

Enter a list of numbers: [23, 8, 17, 42, 8, 31, 12]
Largest: 42 Smallest: 8
After removing 23 : [8, 17, 42, 8, 31, 12]
E1.57Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the prime numbers in the list, and
  2. Create a new list of the elements at even indices.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Primes:', end=' ')
for x in L:
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                break
        else:
            print(x, end=' ')
print()
# (ii)
E = L[::2]
print('Even-index elements:', E)

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Primes: 41 19 2 
Even-index elements: [41, 19, 6, 2]
E1.58Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the prime numbers in the list, and
  2. Display how many times each element occurs, using a dictionary.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Primes:', end=' ')
for x in L:
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                break
        else:
            print(x, end=' ')
print()
# (ii)
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print('Frequency:', F)

Sample run

Enter a list of numbers: [23, 8, 17, 42, 8, 31, 12]
Primes: 23 17 31 
Frequency: {23: 1, 8: 2, 17: 1, 42: 1, 31: 1, 12: 1}
E1.59Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the prime numbers in the list, and
  2. Sort the list in descending order and display the top three.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Primes:', end=' ')
for x in L:
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                break
        else:
            print(x, end=' ')
print()
# (ii)
S = sorted(L, reverse=True)
print('Top three:', S[:3])

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Primes: 7 7 11 
Top three: [40, 22, 15]
E1.60Practice5 marks

Write a program to input a list of numbers. Then:

  1. Display the prime numbers in the list, and
  2. Remove all the occurrences of the first element.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
print('Primes:', end=' ')
for x in L:
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                break
        else:
            print(x, end=' ')
print()
# (ii)
x = L[0]
while x in L:
    L.remove(x)
print('After removing', x, ':', L)

Sample run

Enter a list of numbers: [5, 18, 29, 18, 3, 60, 13]
Primes: 5 29 3 13 
After removing 5 : [18, 29, 18, 3, 60, 13]
E1.61Practice5 marks

Write a program to input a list of numbers. Then:

  1. Create a new list of the elements at even indices, and
  2. Display how many times each element occurs, using a dictionary.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
E = L[::2]
print('Even-index elements:', E)
# (ii)
F = {}
for x in L:
    F[x] = F.get(x, 0) + 1
print('Frequency:', F)

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Even-index elements: [15, 22, 9, 11]
Frequency: {15: 1, 7: 2, 22: 1, 9: 1, 40: 1, 11: 1}
E1.62Practice5 marks

Write a program to input a list of numbers. Then:

  1. Create a new list of the elements at even indices, and
  2. Sort the list in descending order and display the top three.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
E = L[::2]
print('Even-index elements:', E)
# (ii)
S = sorted(L, reverse=True)
print('Top three:', S[:3])

Sample run

Enter a list of numbers: [5, 18, 29, 18, 3, 60, 13]
Even-index elements: [5, 29, 3, 13]
Top three: [60, 29, 18]
E1.63Practice5 marks

Write a program to input a list of numbers. Then:

  1. Create a new list of the elements at even indices, and
  2. Remove all the occurrences of the first element.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
E = L[::2]
print('Even-index elements:', E)
# (ii)
x = L[0]
while x in L:
    L.remove(x)
print('After removing', x, ':', L)

Sample run

Enter a list of numbers: [41, 6, 19, 33, 6, 27, 2]
Even-index elements: [41, 19, 6, 2]
After removing 41 : [6, 19, 33, 6, 27, 2]
E1.64Practice5 marks

Write a program to input a list of numbers. Then:

  1. Sort the list in descending order and display the top three, and
  2. Remove all the occurrences of the first element.
Answer
L = eval(input('Enter a list of numbers: '))
# (i)
S = sorted(L, reverse=True)
print('Top three:', S[:3])
# (ii)
x = L[0]
while x in L:
    L.remove(x)
print('After removing', x, ':', L)

Sample run

Enter a list of numbers: [15, 7, 22, 7, 9, 40, 11]
Top three: [40, 22, 15]
After removing 15 : [7, 22, 7, 9, 40, 11]
15

Flashcards for one-mark questions

Tap an option to check your answer at once; your score is saved

Quick quiz: 10 MCQs

Ten multiple-choice questions picked at random. Tap an option to answer. You get your score at the end.

16

Practice question papers

Twenty ready papers with blueprints and a random paper maker, all on the CBSE paper design (Class XI unit marks)

Twenty ready practice papers

Each paper has 37 questions for 70 marks in Sections A to E, with internal choices (OR) in Q23, Q26, Q27, Q28, Q30, Q31, Q34 and Q37. Unit marks are kept as in the syllabus: Unit I 10, Unit II 45, Unit III 15. Every paper opens with its chapter-wise blueprint. No question is repeated: the twenty papers use 900 different questions.

Or make a fresh random paper

Marking scheme, download and timer

Blueprint only:
03:00:00

Choose Paper 1 to Paper 20, or make a random paper. The blueprint and the paper open here.

17

Last-day checklist

Read this the evening before and again at the exam centre gate

Before the paper

  • Write from memory: factorial, prime, palindrome, Fibonacci, vowel count, word count, linear search, frequency with a dictionary.
  • Do two conversions of each kind and one 3-input truth table.
  • Revise the tables of string, list and dictionary methods, and the society tables.

In the first 15 minutes

  • Mark the questions with OR and choose the part you know.
  • Note the exact names of variables and the input format given.
  • Plan the order: Section A, then Units I and III, then programs.

While writing

  • In output questions, trace with a small table of variable values.
  • In conversions, show the working.
  • In programs, write the input statements, the logic and the print statements.
Marks katne wali galti
  • Forgetting int() with input()
  • Missing colons and wrong indentation
  • Counting a slice up to and including the stop index
  • Writing L = L.sort()
  • Leaving a 4 or 5 mark question blank