Assess your understanding of fundamental Python concepts and coding skills for backend development interview preparation. This quiz covers syntax, data types, control structures, functions, and essential language features relevant to backend programming in Python.
Which of the following statements correctly assigns the integer value 10 to a variable named 'x' in Python?
Explanation: The correct way to assign the integer 10 to the variable x in Python is using x = 10. The statement int x = 10 is incorrect because variable declaration with a type is not required in Python. The option x == 10 is a comparison, not an assignment. The walrus operator x := 10 is used for assignment expressions in Python 3.8+, but generally assignment should be done with =.
Which built-in data type in Python is best suited to store a collection of unique items?
Explanation: The set data type in Python is designed to store a collection of unique and unordered items. While lists and tuples can contain duplicates, sets automatically remove any repeated elements. Dictionaries (dict) store key-value pairs rather than single values. Tuples, like lists, can also contain duplicates.
How would you write an if statement in Python to check if the variable 'score' is greater than 50?
Explanation: The syntax if score > 50: is correct in Python for writing an if statement. Python requires a colon at the end of the condition. If (score > 50) is not valid syntax due to the parentheses and missing colon. The third option misses the colon, and the fourth option uses '=>' which is not a valid operator in Python; it should be '>=' for greater than or equal.
What is the correct Python statement to iterate over a list named 'items' and print each element?
Explanation: for item in items: print(item) is the correct way in Python to loop through each element in a list. Python uses 'for ... in ... :' for iteration. The foreach keyword does not exist in Python. Options without a colon or with incorrect syntax, such as parentheses or missing indentation, will not execute.
Which of these is the correct way to define a function that accepts one argument and prints it?
Explanation: In Python, functions are defined with the 'def' keyword, followed by the function name, arguments in parentheses, a colon, and the function body. The correct option is def show(arg): print(arg). The other options use incorrect syntax or keywords not valid in Python, such as 'function' and [ ] instead of ( ).
Given s = 'hello', what is the correct way to change the value of s to uppercase in Python?
Explanation: The upper() method transforms a string to uppercase in Python, so s = s.upper() is correct. The upcase() and toUpper() methods do not exist in Python, and uppercase(s) is not a built-in function. Using the correct method name is essential for manipulating strings as intended.
If my_list = [1, 2, 3, 4], which statement retrieves the last element of the list?
Explanation: Using negative indexing, my_list[-1] correctly retrieves the last element of the list in Python. my_list[4] raises an IndexError because indexing starts at 0. There is no valid use of the keyword 'last' or the syntax my_list.(-1) in Python indexing.
Given d = {'key': 5}, how should you access the value associated with 'key'?
Explanation: The correct way to access a value by key in a Python dictionary is d['key']. d(key) tries to call the dictionary as a function, which is not correct. d.key is not valid unless using special objects, and d->'key' uses syntax from other programming languages.
What will be the result of the expression data[1:3] if data = [10, 20, 30, 40]?
Explanation: data[1:3] returns a new list containing items at index 1 and 2, meaning [20, 30]. Slicing in Python does not include the element at the stop index (3), so 40 is excluded. The other options include incorrect items or slice ranges.
How is the logical 'and' operator written in Python to combine two conditions?
Explanation: Python uses the word 'and' to combine two boolean expressions logically. The '&&' operator is used in other languages but not in Python. The '&' symbol performs a bitwise 'and', not logical, and 'plus' is unrelated to logical operations.
What is the correct way to convert the string '123' to an integer in Python?
Explanation: The int() function is the built-in way to convert a string representing an integer into an actual integer in Python. Functions like string_to_int() and parseInt() are not standard in Python, and Integer is not a type constructor in Python.
Which symbol is used for writing a single-line comment in Python?
Explanation: Python uses the '#' symbol to begin a single-line comment. The double slash (//) and slash-asterisk (/*) are used in other programming languages. The double hyphen (--) is not used for comments in Python.
Why is indentation used in Python?
Explanation: Indentation in Python is required to define code blocks, such as for functions, loops, and conditionals. While indentation can improve readability in other languages, in Python it is a necessary part of the syntax. It is not used for comments or to separate statements, which are separated by newlines.
Which statement correctly imports the 'math' module in Python?
Explanation: The statement import math is correct for importing the math module in Python. Using a hash (#) makes the line a comment, include math is incorrect as Python does not use 'include', and import_math is not a Python statement.
How do you start exception handling in Python to catch all exceptions?
Explanation: In Python, exception handling begins with the 'try:' keyword, followed by the block of code that might raise an error. The 'catch:' and 'handle:' keywords are not recognized in Python; handling exceptions is done with 'except:'. However, you must use 'try:' to start the block.
In Python, which keyword is used to send a value back from a function to the caller?
Explanation: The 'return' keyword is used in Python to pass a value back to the function caller. The 'yield' keyword is used in generators for producing a sequence of values, not an immediate return. The words 'send' and 'output' are not used for returning values from functions.