1
Which of the following is a major difference between Python 2 and Python 3 regarding the print statement?
A. Python 3 uses parentheses for print, while Python 2 does not require them
B. Python 2 uses parentheses for print, while Python 3 does not require them
C. There is no difference
D. Python 3 does not have a print function
Correct Answer: Python 3 uses parentheses for print, while Python 2 does not require them
Explanation: In Python 2, print is a statement (e.g., 'print 5'), whereas in Python 3, it is a function and requires parentheses (e.g., 'print(5)').
2
What is the standard file extension for Python scripts?
A. .pt
B. .py
C. .pyt
D. .pn
Correct Answer: .py
Explanation: Python source code files utilize the .py extension.
3
Which command is typically used in the Windows Command Prompt to run a Python file named 'script.py'?
A. run script.py
B. execute script.py
C. python script.py
D. go script.py
Correct Answer: python script.py
Explanation: The keyword 'python' invokes the Python interpreter on the specified file.
4
What is the correct syntax to output 'Hello World' in Python 3?
A. echo 'Hello World'
B. print('Hello World')
C. console.log('Hello World')
D. System.out.println('Hello World')
Correct Answer: print('Hello World')
Explanation: Python uses the print() function to output text to the console.
5
Which symbol indicates that the Python interpreter is running in interactive mode?
A. >>>
B. ###
C. $$$
D. ---
Correct Answer: >>>
Explanation: The triple chevron (>>>) is the primary prompt indicating the interpreter is ready for input.
6
Which of the following is a valid variable name in Python?
A. 2my_var
B. my-var
C. _my_var
D. my var
Correct Answer: _my_var
Explanation: Variable names cannot start with a number, contain hyphens, or spaces. Starting with an underscore is valid.
7
What happens if you try to use a variable that has not been defined?
A. The program returns 0
B. The program returns None
C. A NameError occurs
D. A TypeError occurs
Correct Answer: A NameError occurs
Explanation: A NameError indicates that the interpreter does not recognize the local or global name provided.
8
Which operator is used to assign a value to a variable?
Correct Answer: =
Explanation: The single equals sign (=) is the assignment operator in Python.
9
Python is a case-sensitive language. What does this mean?
A. 'Variable' and 'variable' are the same
B. 'Variable' and 'variable' are different identifiers
C. Variables must always be uppercase
D. Keywords must always be uppercase
Correct Answer: 'Variable' and 'variable' are different identifiers
Explanation: Case sensitivity means that uppercase and lowercase letters are treated as distinct characters.
10
Which of the following is a Python keyword?
A. variable
B. yield
C. print
D. integer
Correct Answer: yield
Explanation: 'yield' is a reserved keyword. 'print' is a built-in function, not a keyword, and 'variable'/'integer' are not reserved.
11
What function is used to check the data type of a value?
A. check()
B. typeof()
C. type()
D. class()
Correct Answer: type()
Explanation: The type() function returns the class type of the argument passed to it.
12
What is the data type of the value 3.14?
A. int
B. float
C. str
D. bool
Correct Answer: float
Explanation: Numbers with a decimal point belong to the float (floating-point) data type.
13
What is the data type of the value '10'?
A. int
B. float
C. str
D. bool
Correct Answer: str
Explanation: Because the number is enclosed in quotes, Python treats it as a string.
14
Which of the following represents a Boolean value in Python?
A. true
B. TRUE
C. True
D. 1.0
Correct Answer: True
Explanation: Boolean values in Python are capitalized: True and False.
15
What is a 'statement' in Python?
A. A unit of code that the Python interpreter can execute
B. A comment line
C. A math equation only
D. A syntax error
Correct Answer: A unit of code that the Python interpreter can execute
Explanation: A statement is an instruction that the Python interpreter can execute, such as assignment or import statements.
16
Which of the following is an example of an expression?
A. x = 5
B. import os
C. 5 + 10
D. if x > 5:
Correct Answer: 5 + 10
Explanation: An expression is a combination of values, variables, and operators that yields a result value.
17
What is the result of the floor division operation: 7 // 2?
Correct Answer: 3
Explanation: Floor division (//) divides the numbers and rounds down to the nearest whole number.
18
Which operator calculates the remainder of a division?
Correct Answer: %
Explanation: The modulus operator (%) returns the remainder of the division.
19
What is the result of 2 ** 3?
Correct Answer: 8
Explanation: The double asterisk (**) is the exponentiation operator. 2 to the power of 3 is 8.
20
What is the output of the following code: print(10 / 2)?
Correct Answer: 5.0
Explanation: In Python 3, the standard division operator (/) always returns a float, even if the result is a whole number.
21
Following the order of operations (PEMDAS), what is the result of 2 + 3 * 4?
Correct Answer: 14
Explanation: Multiplication () has higher precedence than addition (+). 3 4 = 12, then 12 + 2 = 14.
22
Which character starts a single-line comment in Python?
Correct Answer: #
Explanation: The hash symbol (#) indicates the start of a comment in Python.
23
What happens when you use the '+' operator on two strings?
A. It adds the ASCII values
B. It concatenates the strings
C. It throws a TypeError
D. It subtracts them
Correct Answer: It concatenates the strings
Explanation: When applied to strings, '+' joins them end-to-end (concatenation).
24
What happens when you use the '' operator on a string and an integer (e.g., 'Hi' 3)?
A. TypeError
B. It repeats the string 3 times
C. It creates a list
D. It adds the number to the string
Correct Answer: It repeats the string 3 times
Explanation: The '*' operator performs repetition when used with a string and an integer. Result: 'HiHiHi'.
25
What is the result of executing: print('5' + 5)?
A. 10
B. 55
C. TypeError
D. NaN
Correct Answer: TypeError
Explanation: Python cannot implicitly convert an integer to a string (or vice versa) for concatenation using the '+' operator.
26
Variables in Python are 'dynamically typed'. What does this mean?
A. You must declare the type before assigning a value
B. A variable can hold different types of values at different times
C. Variables cannot change values
D. Variables are strictly typed like C++
Correct Answer: A variable can hold different types of values at different times
Explanation: Python determines the type based on the value assigned, and you can reassign a variable to a value of a different type later.
27
Which naming convention is recommended for Python variable names?
A. camelCase
B. PascalCase
C. snake_case
D. kebab-case
Correct Answer: snake_case
Explanation: PEP 8 style guide recommends snake_case (lowercase with underscores) for variable names.
28
What is the associativity of the exponentiation operator (**) in Python?
A. Left-to-right
B. Right-to-left
C. Random
D. Center
Correct Answer: Right-to-left
Explanation: Exponentiation is evaluated from right to left. 232 is calculated as 2(32).
29
What is 'Composition' in the context of Python expressions?
A. Writing comments
B. Combining simple expressions and statements into compound ones
C. Installing Python packages
D. Compiling code to C
Correct Answer: Combining simple expressions and statements into compound ones
Explanation: Composition allows you to nest functions or expressions, like print(type(x)).
30
Which of the following variable names leads to a SyntaxError?
A. class
B. clazz
C. klass
D. Class_Name
Correct Answer: class
Explanation: 'class' is a reserved keyword in Python and cannot be used as a variable name.
31
What does the function input() return by default?
A. int
B. float
C. str
D. list
Correct Answer: str
Explanation: The input() function always captures user input as a string.
32
Why are comments important in programming?
A. They make the code run faster
B. They explain what the code does to humans
C. They are required by the compiler
D. They encrypt the code
Correct Answer: They explain what the code does to humans
Explanation: Comments are ignored by the interpreter and serve as documentation for developers.
33
What is the result of: 10 % 3?
Correct Answer: 1
Explanation: 10 divided by 3 is 3 with a remainder of 1.
34
Which of the following is NOT an assignment statement?
A. n = 17
B. pi = 3.14159
C. message = 'Hello'
D. 17 + 3
Correct Answer: 17 + 3
Explanation: '17 + 3' is an expression that evaluates to a value but does not assign it to any variable.
35
How do you represent a string literal in Python?
A. Only single quotes
B. Only double quotes
C. Either single or double quotes
D. Parentheses
Correct Answer: Either single or double quotes
Explanation: Python accepts both 'string' and "string" as valid string literals.
36
What identifies the values acted upon by an operator?
A. Keywords
B. Operands
C. Comments
D. Functions
Correct Answer: Operands
Explanation: In the expression 'a + b', 'a' and 'b' are the operands.
37
If x = 10 and y = 20, what is the result of: print(x, y)?
A. 1020
B. 30
C. 10 20
D. x y
Correct Answer: 10 20
Explanation: The print function separates multiple arguments with a space by default.
38
Which of the following creates a multi-line string or comment?
A. #
B. //
C. ''' (triple quotes)
D. <!-- -->
Correct Answer: ''' (triple quotes)
Explanation: Triple quotes (single or double) are used for multi-line strings, often used as multi-line comments or docstrings.
39
What error is raised if you write: print 'Hello' in Python 3?
A. NameError
B. SyntaxError
C. TypeError
D. ValueError
Correct Answer: SyntaxError
Explanation: Missing parentheses in a call to print() is a syntax error in Python 3.
40
What is the binary operator for multiplication?
Correct Answer: *
Explanation: The asterisk (*) is the symbol for multiplication in Python.
41
Which of the following is an illegal variable name?
A. catch_22
B. 22_catch
C. _catch22
D. CATCH22
Correct Answer: 22_catch
Explanation: Variable names must not start with a digit.
42
If you define variable x = 10 and then x = 'Hello', what happens?
A. Error: Type mismatch
B. x now holds the string 'Hello'
C. x keeps the value 10
D. The program crashes
Correct Answer: x now holds the string 'Hello'
Explanation: Python allows variables to be reassigned to values of different types.
43
The term 'IDLE' in Python stands for:
A. Integrated Development and Learning Environment
B. Internal Developer Loop Engine
C. Interactive Design Language Environment
D. Integrated Debugging Line Editor
Correct Answer: Integrated Development and Learning Environment
Explanation: IDLE is Python's standard IDE.
44
Which of the following statements assigns the string 'Python' to the variable named language?
A. language == 'Python'
B. 'Python' = language
C. language = 'Python'
D. set language 'Python'
Correct Answer: language = 'Python'
Explanation: The variable name goes on the left, followed by '=', followed by the value.
45
What will be the output of: print(3 * '7')?
A. 21
B. 777
C. Syntax Error
D. 37
Correct Answer: 777
Explanation: The string '7' is repeated 3 times.
46
Which character is used to separate two statements on a single line?
Correct Answer: ;
Explanation: The semicolon (;) can be used to separate multiple statements on one line, though it is generally discouraged in Python style.
47
In the expression (5 + 2) * 3, which operation happens first?
A. Multiplication
B. Addition
C. Evaluation from left to right regardless
D. None
Correct Answer: Addition
Explanation: Parentheses have the highest precedence, forcing addition to happen before multiplication.
48
What will the following code output? print(type('5.5'))
A. <class 'int'>
B. <class 'float'>
C. <class 'str'>
D. <class 'bool'>
Correct Answer: <class 'str'>
Explanation: Despite containing a number, the quotes make it a string.
49
What does a SyntaxError generally indicate?
A. The logic of the program is wrong
B. The code violates the grammatical rules of Python
C. The variable name is not found
D. There is a division by zero
Correct Answer: The code violates the grammatical rules of Python
Explanation: Syntax errors occur when the parser detects code that does not conform to Python language rules.
50
When you install Python on Windows, what needs to be added to the PATH variable to run python from any command prompt?
A. The browser history
B. The directory where python.exe is located
C. The My Documents folder
D. Nothing, it works automatically
Correct Answer: The directory where python.exe is located
Explanation: Adding the Python installation directory to the system PATH environment variable allows the 'python' command to be recognized globally.