Python Java SQL Course Notes C C++ HTML CSS JS

Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Operators Practice

Arithmetic, comparison, logical, assignment, bitwise, and membership operators in Python.

1
Last Digit of a Number
Easy
Given an integer N, print its last digit (units digit).
A single integer N.
Print the last digit of N.
Sample Input 1
237
Sample Output 1
7
Sample Input 2
0
Sample Output 2
0
Sample Input 3
1000000000
Sample Output 3
0
0 ≤ N ≤ 10^9
Use the modulo operator: N % 10 gives the last digit.
2
Check Divisibility
Easy
Given two integers A and B, check if A is divisible by B.
Two space-separated integers A and B.
Print "Divisible" if A is divisible by B, otherwise print "Not Divisible".
Sample Input 1
15 4
Sample Output 1
Not Divisible
Sample Input 2
20 5
Sample Output 2
Divisible
Sample Input 3
7 2
Sample Output 3
Not Divisible
1 ≤ A, B ≤ 10^6
Use the modulo operator %: if A % B == 0 then A is divisible by B.
3
Find the Remainder
Easy
Given two integers A and B, find the remainder when A is divided by B.
Two space-separated integers A and B.
Print the remainder as an integer.
Sample Input 1
17 5
Sample Output 1
2
Sample Input 2
100 7
Sample Output 2
2
Sample Input 3
5 2
Sample Output 3
1
1 ≤ A, B ≤ 10^6
Use the modulo operator: A % B.
4
Average of Three Numbers
Easy
Compute average of three numbers.
Three ints A B C
Average rounded to 2 dp.
Sample Input 1
10 20 30
Sample Output 1
20.00
Sample Input 2
5 5 5
Sample Output 2
5.00
Sample Input 3
-1 0 1
Sample Output 3
0.00
1≤A,B,C≤10⁶
Sum/3
5
Check Odd or Even
Easy
Use modulo operator.
Single int N
"Even" or "Odd"
Sample Input 1
7
Sample Output 1
Odd
Sample Input 2
0
Sample Output 2
Even
Sample Input 3
1000000000
Sample Output 3
Even
1≤N≤10⁹
N%2==0 means even.
6
Power of a Number
Easy
Compute B^E using operator.
Two ints B E
Print B^E result.
Sample Input 1
2 10
Sample Output 1
1024
Sample Input 2
3 3
Sample Output 2
27
Sample Input 3
10 0
Sample Output 3
1
1≤B≤10, 0≤E≤15
Use ** operator.
7
Floor Division
Easy
Integer division A//B
Two ints A B
Print A//B.
Sample Input 1
25 4
Sample Output 1
6
Sample Input 2
100 7
Sample Output 2
14
Sample Input 3
5 5
Sample Output 3
1
1≤B≤A≤10⁶
Use //
8
Increment and Decrement
Easy
Print N+1 and N-1 using += and -=.
Single int N
Two lines: N+1, N-1
Sample Input 1
10
Sample Output 1
11
9
Sample Input 2
0
Sample Output 2
1
-1
Sample Input 3
-1000000
Sample Output 3
-999999
-1000001
-10⁶≤N≤10⁶
Use += and -=
9
Check Greater
Easy
Is A > B using > operator?
Two ints A B
"Yes" or "No"
Sample Input 1
15 10
Sample Output 1
Yes
Sample Input 2
5 5
Sample Output 2
No
Sample Input 3
-1 1
Sample Output 3
No
-10⁶≤A,B≤10⁶
Use >
10
Tens Digit
Easy
Extract tens place digit using // and %.
Single int N
Print tens digit.
Sample Input 1
345
Sample Output 1
4
Sample Input 2
100
Sample Output 2
0
Sample Input 3
1000000
Sample Output 3
0
10≤N≤10⁶
(N//10)%10
1
Check Leap Year with Logical Operators
Medium
Given a year Y, determine if it is a leap year using logical operators. A year is a leap year if it is divisible by 400 OR (divisible by 4 AND not divisible by 100).
A single integer Y.
Print "Leap Year" or "Not a Leap Year".
Sample Input 1
1900
Sample Output 1
Not a Leap Year
Sample Input 2
2000
Sample Output 2
Leap Year
Sample Input 3
2024
Sample Output 3
Leap Year
1000 ≤ Y ≤ 3000
Use logical operators: Y % 400 == 0 or (Y % 4 == 0 and Y % 100 != 0)
2
Swap Bits (Bitwise XOR)
Medium
Given two integers X and Y, swap their values without using a third variable. Use only bitwise XOR operators.
Two space-separated integers X and Y.
Print the swapped values in the format: "X = [newX], Y = [newY]"
Sample Input 1
9 14
Sample Output 1
X = 14, Y = 9
Sample Input 2
5 12
Sample Output 2
X = 12, Y = 5
Sample Input 3
100 57
Sample Output 3
X = 57, Y = 100
1 ≤ X, Y ≤ 10^6
X = X ^ Y; Y = X ^ Y; X = X ^ Y. XOR has the property that a ^ b ^ b = a.
3
Power of Two Check
Medium
Is N a power of 2? Use (N & (N-1))==0
Single int N
"Power of Two" or "Not"
Sample Input 1
16
Sample Output 1
Power of Two
Sample Input 2
1
Sample Output 2
Power of Two
Sample Input 3
100
Sample Output 3
Not
1≤N≤10⁹
Use bitwise AND.
4
Evaluate Expression
Medium
(A+B)*(C-D)/E with floats
Five ints A B C D E
Result rounded to 2 dp.
Sample Input 1
10 5 20 7 4
Sample Output 1
48.75
Sample Input 2
1 1 5 2 2
Sample Output 2
3.00
Sample Input 3
3 4 6 2 10
Sample Output 3
2.80
C>D, E≠0
Follow precedence.
5
Nth Bit Check
Medium
Check if K-th bit of N is set using (1
Two ints N K
"Set" or "Not Set"
Sample Input 1
13 2
Sample Output 1
Set
Sample Input 2
13 3
Sample Output 2
Set
Sample Input 3
0 0
Sample Output 3
Not Set
0≤N≤10⁹, 0≤K≤31
N & (1
6
Toggle Bits
Medium
Toggle all bits after MSB using XOR.
Single int N
Result integer.
Sample Input 1
10
Sample Output 1
5
Sample Input 2
1
Sample Output 2
0
Sample Input 3
31
Sample Output 3
0
1≤N≤10⁶
Mask and XOR.
7
Right Shift Count
Medium
Count right shifts needed to reach 0 (N>>1).
Single int N
Count of shifts.
Sample Input 1
8
Sample Output 1
4
Sample Input 2
1
Sample Output 2
1
Sample Input 3
1024
Sample Output 3
11
1≤N≤10⁹
Count till N==0.
8
Absolute Value
Medium
Absolute value without abs(), using conditionals.
Single int N
Print absolute value.
Sample Input 1
-15
Sample Output 1
15
Sample Input 2
0
Sample Output 2
0
Sample Input 3
1000000000
Sample Output 3
1000000000
-10⁹≤N≤10⁹
If N
9
Same Sign Check
Medium
Do A and B have same sign?
Two ints A B
"Same Sign" or "Different Sign"
Sample Input 1
-5 -10
Sample Output 1
Same Sign
Sample Input 2
0 0
Sample Output 2
Same Sign
Sample Input 3
-1 1
Sample Output 3
Different Sign
-10⁶≤A,B≤10⁶
Check >=0 conditions.
10
BMI Calculator
Medium
BMI = weight / height²
Two floats: weight(kg) height(m)
BMI rounded 2 dp.
Sample Input 1
72 1.75
Sample Output 1
23.51
Sample Input 2
100 2.0
Sample Output 2
25.00
Sample Input 3
50 1.6
Sample Output 3
19.53
20≤w≤200, 1≤h≤2.5
Use ** for square.
1
Count Set Bits
Hard
Given an integer N, count the number of 1 bits (set bits) in its binary representation.
A single integer N.
Print the count of set bits in the binary representation of N.
Sample Input 1
13
Sample Output 1
3
Sample Input 2
255
Sample Output 2
8
Sample Input 3
7
Sample Output 3
3
1 ≤ N ≤ 10^9
Binary of 13 is 1101, which has three 1s. Use N & (N-1) trick or bitwise shift.
2
Gray Code Converter
Hard
Convert binary string to Gray code (XOR each bit with previous).
Binary string (0s/1s)
Gray code binary string.
Sample Input 1
1011
Sample Output 1
1110
Sample Input 2
1000
Sample Output 2
1100
Sample Input 3
1111
Sample Output 3
1000
Len≤32
MSB same, Gray[i]=bin[i]^bin[i-1]
3
Missing Number XOR
Hard
Find missing number from 1..N using XOR.
Line1: N. Line2: N-1 ints
Print missing number.
Sample Input 1
5
3 1 5 2
Sample Output 1
4
Sample Input 2
3
1 2
Sample Output 2
3
Sample Input 3
4
4 3 2
Sample Output 3
1
2≤N≤10⁵
XOR all 1..N with array.
4
Postfix Evaluation
Hard
Evaluate RPN expression (+,-,*,/). Single-digit operands.
Space-separated postfix.
Integer result.
Sample Input 1
5 3 + 8 * 4 /
Sample Output 1
16
Sample Input 2
3 4 + 2 *
Sample Output 2
14
Sample Input 3
8 2 / 4 *
Sample Output 3
16
Expr≤100 chars
Stack-based.
5
Two Unique Numbers
Hard
Array where two numbers appear once, rest twice. Find them using XOR.
Line1: N. Line2: N ints
Two unique numbers asc.
Sample Input 1
8
1 2 3 2 1 4 5 5
Sample Output 1
3 4
Sample Input 2
6
1 1 2 2 3 4
Sample Output 2
3 4
Sample Input 3
4
5 6 5 6
Sample Output 3
0 0
2≤N≤10⁵
XOR all -> find set bit, partition.
6
Max AND in Range
Hard
Max value of A AND B for L≤A
Two ints L R
Max AND value.
Sample Input 1
2 8
Sample Output 1
6
Sample Input 2
5 7
Sample Output 2
6
Sample Input 3
8 16
Sample Output 3
14
1≤L
Find MSB where L and R differ.
7
Triplet Unique
Hard
Every element appears 3x except one. Find it using bitwise.
Line1: N. Line2: N ints
Print unique element.
Sample Input 1
10
3 5 3 3 2 5 5 7 2 2
Sample Output 1
7
Sample Input 2
7
1 1 1 2 2 2 3
Sample Output 2
3
Sample Input 3
4
10 10 10 5
Sample Output 3
5
4≤N≤10⁵
ones/twos bit tracking.
8
Square Root Binary
Hard
Floor sqrt(N) without sqrt() or **0.5, using binary search.
Single int N
Floor of sqrt.
Sample Input 1
50
Sample Output 1
7
Sample Input 2
1
Sample Output 2
1
Sample Input 3
1000000000
Sample Output 3
31622
1≤N≤10⁹
Binary search with *.
9
Bit Palindrome
Hard
Is binary representation a palindrome?
Single int N
"Bit Palindrome" or "Not"
Sample Input 1
9
Sample Output 1
Bit Palindrome
Sample Input 2
5
Sample Output 2
Bit Palindrome
Sample Input 3
12
Sample Output 3
Not
1≤N≤10⁶
Extract bits, compare.
10
Next Power of 2
Hard
Smallest power of 2 ≥ N using bitwise.
Single int N
Next power of 2.
Sample Input 1
25
Sample Output 1
32
Sample Input 2
1
Sample Output 2
1
Sample Input 3
1000000000
Sample Output 3
1073741824
1≤N≤10⁹
Decrement, set bits via OR/shifts.
Competitive MCQs — Operators
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 30
Q1
What is the output of 10 % 3?
Correct!
Wrong — correct answer is .
% is the modulo operator: the remainder of 10 / 3 is 1.
Q2
Which operator is used for logical AND in Python?
Correct!
Wrong — correct answer is .
Python uses the keyword and for logical AND; && is used in C/Java.
Q3
What is the result of 5 == 5.0?
Correct!
Wrong — correct answer is .
== compares values; int 5 and float 5.0 are numerically equal, so True.
Q4
Which operator returns the remainder of a division?
Correct!
Wrong — correct answer is .
% is the remainder (modulo) operator.
Q5
What is the output of 2 + 3 * 4?
Correct!
Wrong — correct answer is .
Multiplication binds tighter than addition: 2 + (3 * 4) = 14.
Q6
What does the is operator compare?
Correct!
Wrong — correct answer is .
is checks whether two names refer to the same object in memory.
Q7
What is the output of \"a\" in \"apple\"?
Correct!
Wrong — correct answer is .
The in operator checks membership; "a" is inside "apple", so True.
Q8
Which expression tests whether x is NOT equal to y?
Correct!
Wrong — correct answer is .
!= is the not-equal operator in Python ( was removed in Python 3).
Q9
What is the output of not True and False or True?
Correct!
Wrong — correct answer is .
not True is False; False and False is False; False or True is True.
Q10
What is 3 < 5 < 7 in Python?
Correct!
Wrong — correct answer is .
Python supports chained comparisons: 3 < 5 < 7 means 3 < 5 and 5 < 7.
Q11
What is the output of 15 // 4?
Correct!
Wrong — correct answer is .
Floor division 15 // 4 = 3 because it rounds down to the nearest integer.
Q12
What is the output of -7 // 2?
Correct!
Wrong — correct answer is .
Floor division always rounds toward negative infinity, so -7 // 2 = -4.
Q13
Which operator performs integer exponentiation?
Correct!
Wrong — correct answer is .
** raises a number to a power (^ is XOR in Python).
Q14
What is the output of 2 ** 3 ** 2?
Correct!
Wrong — correct answer is .
Exponentiation is right-associative: 2 ** (3 ** 2) = 2 ** 9 = 512.
Q15
What is the output of "ab" * 3?
Correct!
Wrong — correct answer is .
Multiplying a string by an integer repeats the string that many times.
Q16
Which operator tests whether a value is contained in a sequence?
Correct!
Wrong — correct answer is .
The in operator performs a membership test on sequences and collections.
Q17
What is the output of 7 == 7 and 3 < 2?
Correct!
Wrong — correct answer is .
and requires both operands to be truthy; since 3 < 2 is False, the result is False.
Q18
What is the output of 0 or 5?
Correct!
Wrong — correct answer is .
or returns the first truthy operand; 0 is falsy, so it returns 5.
Q19
What is the output of 3 and 7?
Correct!
Wrong — correct answer is .
and returns the last operand when all are truthy, so the result is 7.
Q20
Which operator performs bitwise AND?
Correct!
Wrong — correct answer is .
& is bitwise AND; and is the logical AND operator.
Q21
What is the output of 5 >> 1?
Correct!
Wrong — correct answer is .
The right shift moves bits right by one position, dividing 5 by 2: 101 → 10 (2).
Q22
What is the output of 5 << 2?
Correct!
Wrong — correct answer is .
Left shifting 5 (binary 101) by 2 gives 10100, which is 20.
Q23
What is the output of ~5?
Correct!
Wrong — correct answer is .
~x equals -(x + 1) in two's-complement arithmetic, so ~5 = -6.
Q24
Which expression gives the absolute value of a number?
Correct!
Wrong — correct answer is .
abs() returns the magnitude of a number, ignoring its sign.
Q25
What is the output of 10 == 10.0?
Correct!
Wrong — correct answer is .
The int 10 and float 10.0 hold the same numeric value, so == returns True.
Q26
Which operator computes the floor division with modulus together?
Correct!
Wrong — correct answer is .
divmod(a, b) returns a tuple (a // b, a % b) in one call.
Q27
What is the output of divmod(17, 5)?
Correct!
Wrong — correct answer is .
17 // 5 is 3 and 17 % 5 is 2, giving the tuple (3, 2).
Q28
Which operator is used to check identity of two objects?
Correct!
Wrong — correct answer is .
is compares object identity; == compares values.
Q29
What is the output of 5 is 5?
Correct!
Wrong — correct answer is .
Small integers are interned by CPython, so 5 is 5 returns True.
Q30
Which expression means x is greater than 0 AND less than or equal to 10?
Correct!
Wrong — correct answer is .
Chained comparison 0 < x 0 and x