What is the output of this code?
x = 7
y = 3
print(x // y)
2
What will be the output of this code?
x = 5
if x > 10:
print("Greater than 10")
elif x == 5:
print("Exactly 5")
else:
print("Less than 5")
Exactly 5
What is the output of this code?
fruits = ["apple", "banana", "cherry"]
print(fruits[-2])
banana
What will be the output of this code?
def multiply(x, y=2):
return x * y
print(multiply(3))
6
What is printed by this code?
class A:
def __init__(self):
print("A is created")
a = A()
A is created
Which one is not a valid Python data type?
a) int
b) float
c) double
d) tuple
c) real
How many times will "Aktau" be printed?
for i in range(2, 7):
print("Aktau")
5 times (for i = 2, 3, 4, 5, 6)
Given this tuple t = (1, 2, 3, 4), how do you change the first element to 10?
You can't — tuples are immutable.
Write a function called is_even that returns True if a number is even, otherwise False.
def is_even(n):
return n % 2 == 0
What is method overriding?
When a subclass provides a new version of a method inherited from the parent class.
What (name of error) will happen if you run this code?
print("Hello)
SyntaxError — missing closing quotation mark.
What will be printed?
for letter in "Python":
if letter == "h":
continue
print(letter)
P
y
t
o
n
Which method adds an element to the end of a list?
.append()
Write a simple function that takes two numbers and returns their sum.
def add(x,y):
return x+y
What is the purpose of self in class methods?
self refers to the current instance (objects itself) of the class, allowing access to attributes and other methods.
How do you take input as an integer from a user and store it in a variable called age?
age = int(input())
Rewrite this for loop as a while loop:
for i in range(3):
print(i)
i = 0
while i < 3:
print(i)
i += 1
How do you remove the key 'age' from a dictionary called person?
del person['age']
What happens if you define a function but forget to call it?
Nothing
What is the output?
class Animal:
def sound(self):
return "Auuu!"
class Dog(Animal):
def bark(self):
return "Woof!"
d = Dog()
print(d.sound())
Auuu!
Write a Python expression that checks if a number n is between 10 and 20 both included.
10 <= n <= 20
What is wrong with this while loop?
x = 1
while x < 5:
print(x)
The loop is infinite because x is never updated inside the loop (no x += 1).
Sets automatically sort their elements.
False (sets are unordered, not sorted)
What keyword allows a function to accept any number of arguments?
*args
What is the difference between an attribute and a method in a class?
Attribute = describes the state or properties of an object (e.g., name, age, color) - variable
Method = defines the behavior or actions of an object (e.g., drive(), bark(), calculate()) - function