Programming Questions and Answers
100% Pass
What is the correct way to declare a variable in Python?
✔✔ You assign a value to a variable using the `=` operator, such as `x = 10`.
How do you print a string in Python?
✔✔ Use the `print()` function, like `print("Hello, World!")`.
How do you concatenate two strings in Python?
✔✔ Use the `+` operator, like `str1 + str2`.
What is the output of `print(5 // 2)`?
✔✔ The output is `2`, as `//` is the floor division operator.
What does the `return` statement do in Python?
✔✔ The `return` statement sends a result back to the function’s caller.
1
,What will be the result of `print(10 % 3)`?
✔✔ The output is `1`, as `%` is the modulus operator and gives the remainder.
How do you create a list in Python?
✔✔ Use square brackets to define a list, like `my_list = [1, 2, 3]`.
What is a tuple in Python?
✔✔ A tuple is an immutable, ordered collection of elements, defined using parentheses, like `(1,
2, 3)`.
How can you add an item to the end of a list?
✔✔ Use the `append()` method, like `my_list.append(4)`.
What is the difference between `is` and `==` in Python?
✔✔ `is` checks for object identity (whether two objects are the same), while `==` checks for
equality of values.
What is the syntax for an `if` statement in Python?
2
, ✔✔ The syntax is `if condition:`, followed by indented code that will execute if the condition is
true.
What is the purpose of the `break` statement in Python?
✔✔ The `break` statement is used to exit a loop prematurely.
How do you define a class in Python?
✔✔ Use the `class` keyword, followed by the class name and a colon, like `class MyClass:`.
What is the purpose of the `self` keyword in Python classes?
✔✔ The `self` keyword refers to the instance of the class and allows access to its attributes and
methods.
How can you access a value in a dictionary by its key?
✔✔ Use square brackets with the key, like `my_dict['key']`.
What does the `len()` function do in Python?
✔✔ The `len()` function returns the number of items in an object, such as a string, list, or tuple.
3