CS306 – Introduction to Python: Midterm Important MCQs (92 Solved Questions)
Virtual University of Pakistan • Read every solved MCQ with its verified answer key directly on this page. Free, no login required.
All 664 Midterm Packs · CS306 Course Page · CS306 Subjective Pack (30 Q&As)
-
Who developed the Python programming language?
Topic: Introduction to Python
- Dennis Ritchie
- Guido van Rossum ✓ Correct answer
- James Gosling
- Bjarne Stroustrup
-
Python is a/an ______ language.
Topic: Introduction to Python
- Compiled
- Interpreted ✓ Correct answer
- Assembly
- Machine
-
Which file extension is used for Python program files?
Topic: Introduction to Python
- .pt
- .pn
- .py ✓ Correct answer
- .pyt
-
Which symbol is used to write a single-line comment in Python?
Topic: Introduction to Python
- //
- # ✓ Correct answer
- /*
- --
-
Python programs are executed line by line by the ______.
Topic: Introduction to Python
- Assembler
- Linker
- Loader
- Interpreter ✓ Correct answer
-
Which function is used to display output on the screen in Python?
Topic: Introduction to Python
- display()
- echo()
- print() ✓ Correct answer
- write()
-
Which function is used to take input from the user in Python?
Topic: Introduction to Python
- read()
- scan()
- get()
- input() ✓ Correct answer
-
The input() function always returns a value of which data type?
Topic: Introduction to Python
- int
- float
- str ✓ Correct answer
- bool
-
Which of the following is NOT a feature of Python?
Topic: Introduction to Python
- Easy to learn and use
- Free and open source
- Low-level programming language ✓ Correct answer
- Portable
-
In Python, a block of code is defined by ______.
Topic: Introduction to Python
- Curly braces { }
- Indentation ✓ Correct answer
- Semicolons
- Square brackets [ ]
-
Python is a ______ typed language.
Topic: Introduction to Python
- Statically
- Dynamically ✓ Correct answer
- Manually
- Weakly
-
Which of the following statements is true about Python?
Topic: Introduction to Python
- Python code must be compiled before running
- Python is case-sensitive ✓ Correct answer
- Python does not support functions
- Python files have .txt extension
-
What does the type() function do?
Topic: Variables and Data Types
- Returns the value of a variable
- Converts value to string
- Returns the data type of a variable ✓ Correct answer
- Returns length
-
Which of the following is a valid variable name in Python?
Topic: Variables and Data Types
- 2marks
- my-marks
- my marks
- my_marks ✓ Correct answer
-
What is the data type of the value 3.5 in Python?
Topic: Variables and Data Types
- int
- float ✓ Correct answer
- str
- double
-
What is the output of int(7.9)?
Topic: Variables and Data Types
- 7.9
- 8
- 7 ✓ Correct answer
- Error
-
What is the value of bool(0) in Python?
Topic: Variables and Data Types
- True
- False ✓ Correct answer
- 1
- None
-
Which of the following is an immutable data type?
Topic: Variables and Data Types
- list
- dict
- set
- tuple ✓ Correct answer
-
Which of the following is a mutable data type?
Topic: Variables and Data Types
- tuple
- str
- list ✓ Correct answer
- int
-
What is the output of str(25)?
Topic: Variables and Data Types
- Integer 25
- String '25' ✓ Correct answer
- Float 25.0
- Error
-
Which of the following cannot be used as a variable name in Python?
Topic: Variables and Data Types
- total
- class ✓ Correct answer
- value
- sum
-
What is the output of the following code? x = 5 x = "Ali" print(x)
Topic: Variables and Data Types
- 5
- Ali ✓ Correct answer
- Error
- None
-
Which of the following correctly assigns the value 5 to both variables a and b?
Topic: Variables and Data Types
- a, b = 5
- a = b = 5 ✓ Correct answer
- a == b = 5
- a := b = 5
-
Which literal represents a Boolean value in Python?
Topic: Variables and Data Types
- true
- TRUE
- True ✓ Correct answer
- yes
-
Which operator is used for exponentiation (power) in Python?
Topic: Operators and Expressions
- ^
- ** ✓ Correct answer
- *
- exp
-
What is the output of 7 // 2?
Topic: Operators and Expressions
- 3.5
- 4
- 3 ✓ Correct answer
- 2
-
What is the output of 7 % 2?
Topic: Operators and Expressions
- 2
- 1 ✓ Correct answer
- 3
- 0
-
What is the output of 2 ** 3?
Topic: Operators and Expressions
- 6
- 9
- 8 ✓ Correct answer
- 5
-
Which operator is used to check equality in Python?
Topic: Operators and Expressions
- =
- ===
- == ✓ Correct answer
- equals
-
What is the output of 10 / 2 in Python?
Topic: Operators and Expressions
- 5
- 5.0 ✓ Correct answer
- 2
- 10
-
Which operator means "not equal to" in Python?
Topic: Operators and Expressions
- <>
- =!
- not=
- != ✓ Correct answer
-
What is the output of print(3 + 4 * 2)?
Topic: Operators and Expressions
- 14
- 11 ✓ Correct answer
- 10
- 24
-
Which logical operator gives True only when both conditions are True?
Topic: Operators and Expressions
- or
- not
- and ✓ Correct answer
- xor
-
The expression (5 > 3 and 2 < 4) evaluates to ______.
Topic: Operators and Expressions
- False
- True ✓ Correct answer
- Error
- None
-
What is the output of print("Py" + "thon")?
Topic: Operators and Expressions
- Py thon
- Py+thon
- Python ✓ Correct answer
- Error
-
Which operator is used to compare the identity of two objects?
Topic: Operators and Expressions
- ==
- equals
- is ✓ Correct answer
- same
-
What is the output of 9 % 3?
Topic: Operators and Expressions
- 3
- 1
- 2
- 0 ✓ Correct answer
-
What type of data structure is a string in Python?
Topic: Strings
- Mutable
- Immutable ✓ Correct answer
- Integers
- Lists
-
Which method converts a string into uppercase letters?
Topic: Strings
- uppercase()
- toupper()
- upper() ✓ Correct answer
- big()
-
What is the output of len("Python")?
Topic: Strings
- 5
- 6 ✓ Correct answer
- 7
- Error
-
The index of the first character of a string in Python is ______.
Topic: Strings
- 1
- -1
- 0 ✓ Correct answer
- None
-
What is the output of "Hello"[1]?
Topic: Strings
- H
- e ✓ Correct answer
- l
- Error
-
Strings in Python can be created using ______.
Topic: Strings
- Only single quotes
- Only double quotes
- Only triple quotes
- Single, double or triple quotes ✓ Correct answer
-
What is the output of "abc" * 2?
Topic: Strings
- abc2
- abcabc ✓ Correct answer
- Error
- abc abc
-
In string indexing, the index -1 refers to the ______.
Topic: Strings
- First character
- Second character
- Last character ✓ Correct answer
- No character
-
What is the output of "Python"[0:3]?
Topic: Strings
- Py
- Pyth
- Pyt ✓ Correct answer
- thon
-
Which method removes extra whitespace from both ends of a string?
Topic: Strings
- trim()
- strip() ✓ Correct answer
- remove()
- cut()
-
What is the output of "a,b,c".split(",")?
Topic: Strings
- 'abc'
- ('a', 'b', 'c')
- ['a', 'b', 'c'] ✓ Correct answer
- Error
-
Which keyword is used to start a conditional statement in Python?
Topic: Conditional Statements
- condition
- when
- if ✓ Correct answer
- check
-
Which keyword represents "else if" in Python?
Topic: Conditional Statements
- elseif
- else if
- elsif
- elif ✓ Correct answer
-
What is the output of the following code? x = 10 if x > 5: print("Big") else: print("Small")
Topic: Conditional Statements
- Small
- Big ✓ Correct answer
- Error
- No output
-
What is the output of the following code? a = 7 if a % 2 == 0: print("Even") else: print("Odd")
Topic: Conditional Statements
- Even
- Odd ✓ Correct answer
- Error
- No output
-
Which symbol is used at the end of an if statement line in Python?
Topic: Conditional Statements
- Semicolon (;)
- Period (.)
- Colon (:) ✓ Correct answer
- Comma (,)
-
Which of the following is a correctly written condition in Python?
Topic: Conditional Statements
- if x = 5:
- if x == 5: ✓ Correct answer
- if x => 5:
- if x equals 5:
-
What is the output of the following code? marks = 50 if marks >= 50: print("Pass")
Topic: Conditional Statements
- Fail
- No output
- Error
- Pass ✓ Correct answer
-
What is the output of below code? for i in range(2, 10, 3): print(i, end=" ")
Topic: Loops and range()
- 2 3 4 5
- 2 5 8 ✓ Correct answer
- 2 4 6 8
- 3 6 9
-
Which of the following generates all even numbers between 2 and 10?
Topic: Loops and range()
- range(2, 10, 1)
- range(2, 11, 1)
- range(2, 11, 2) ✓ Correct answer
- range(2, 8, 2)
-
What will be the output of below code? n = 0 fact = 1 for i in range(1, n+1): fact *= i print(fact)
Topic: Loops and range()
- 2
- 0
- 1 ✓ Correct answer
- 4
-
Which loop is best used when the number of iterations is known in advance?
Topic: Loops and range()
- while
- for ✓ Correct answer
- do-while
- until
-
Which values are generated by range(5)?
Topic: Loops and range()
- 1, 2, 3, 4, 5
- 0, 1, 2, 3, 4 ✓ Correct answer
- 0, 1, 2, 3, 4, 5
- 1, 2, 3, 4
-
What is the output of the following code? i = 1 while i <= 3: print(i, end=" ") i += 1
Topic: Loops and range()
- 1 2
- 0 1 2
- Infinite loop
- 1 2 3 ✓ Correct answer
-
Which statement is used to immediately exit a loop in Python?
Topic: Loops and range()
- continue
- pass
- break ✓ Correct answer
- skip
-
Which statement skips the current iteration and moves to the next iteration of a loop?
Topic: Loops and range()
- break
- continue ✓ Correct answer
- pass
- exit
-
What is the output of the following code? for i in range(3): print(i)
Topic: Loops and range()
- 1 2 3
- 0 1 2 3
- 0 1 2 ✓ Correct answer
- 1 2
-
If the condition of a while loop never becomes False, the loop becomes a/an ______.
Topic: Loops and range()
- Syntax error
- Infinite loop ✓ Correct answer
- Single iteration loop
- Empty loop
-
What is the output of the following code? s = 0 for i in range(1, 5): s += i print(s)
Topic: Loops and range()
- 15
- 9
- 5
- 10 ✓ Correct answer
-
Which values are generated by range(1, 10, 2)?
Topic: Loops and range()
- 1, 3, 5, 7
- 1, 3, 5, 7, 9 ✓ Correct answer
- 2, 4, 6, 8, 10
- 1, 2, 3, 4, 5
-
What is the output of the following code? x = "5" y = 3 print(x * y)
Topic: Loops and range()
- 15
- 53
- 555 ✓ Correct answer
- Error
-
What is the output of print(10 > 9)?
Topic: Loops and range()
- False
- True ✓ Correct answer
- 10
- 9
-
Which of the following lines of code will produce an error?
Topic: Loops and range()
- print("Hello")
- x = 5
- print(5 * 2)
- print(5 + "5") ✓ Correct answer
-
What does the pass statement do in Python?
Topic: Loops and range()
- Exits the loop
- Skips the current iteration
- Does nothing; it is a null statement ✓ Correct answer
- Passes an argument to a function
-
What is the output of the following code? for i in range(2): for j in range(2): print("*", end=" ")
Topic: Loops and range()
- * *
- * * * * ✓ Correct answer
- *
- Error
-
Which keyword is used to define a function in Python?
Topic: Functions
- def ✓ Correct answer
- function
- func
- define
-
Which of the following is NOT a benefit of functions?
Topic: Functions
- Modular programming
- Easier debugging
- Code duplication ✓ Correct answer
- Code reuseability
-
Which of the following define a function that takes two parameters a and b in python language?
Topic: Functions
- function func(a, b):
- def func[]:
- def func(a, b): ✓ Correct answer
- func(a, b) def:
-
How does Python return multiple values from a function?
Topic: Functions
- Using a string
- Using a dictionary
- Using a list
- Using a tuple ✓ Correct answer
-
Which is one of the key benefits of using default argument values in Python functions?
Topic: Functions
- They reduce the number of required arguments in function calls ✓ Correct answer
- They frequently cause unexpected runtime errors
- They make the code more complex and harder to read
- They have no practical use in real programs
-
A function that does not return any value returns ______ by default.
Topic: Functions
- 0
- False
- None ✓ Correct answer
- Empty string
-
What is the output of the following code? def add(a, b): return a + b print(add(2, 3))
Topic: Functions
- 23
- 5 ✓ Correct answer
- Error
- None
-
Arguments that are passed by name in a function call are called ______.
Topic: Functions
- Positional arguments
- Default arguments
- Keyword arguments ✓ Correct answer
- Variable arguments
-
What is the output of the following code? def greet(name="Ali"): print("Hello", name) greet()
Topic: Functions
- Hello
- Hello name
- Error
- Hello Ali ✓ Correct answer
-
Variables defined inside a function are called ______.
Topic: Functions
- Global variables
- Local variables ✓ Correct answer
- Static variables
- Constant variables
-
Which keyword is used to return a value from a function?
Topic: Functions
- send
- give
- return ✓ Correct answer
- output
-
What is the output of the following code? def f(x): return x * 2 print(f(4))
Topic: Functions
- 4
- 6
- 8 ✓ Correct answer
- Error
-
In a function definition, *args allows the function to accept ______.
Topic: Functions
- Only one argument
- A variable number of arguments ✓ Correct answer
- Only keyword arguments
- No arguments
-
Which keyword is used to create an anonymous (lambda) function in Python?
Topic: Functions
- def
- anon
- func
- lambda ✓ Correct answer
-
Which of the following correctly creates a list in Python?
Topic: Lists and Dictionaries
- a = (1, 2, 3)
- a = [1, 2, 3] ✓ Correct answer
- a = {1, 2, 3}
- a = <1, 2, 3>
-
What is the output of the following code? a = [10, 20, 30] print(a[1])
Topic: Lists and Dictionaries
- 10
- 30
- Error
- 20 ✓ Correct answer
-
Which method adds an element at the end of a list?
Topic: Lists and Dictionaries
- add()
- push()
- append() ✓ Correct answer
- insert()
-
What is the output of len([1, 2, 3, 4])?
Topic: Lists and Dictionaries
- 3
- 4 ✓ Correct answer
- 5
- Error
-
Which of the following correctly creates a dictionary in Python?
Topic: Lists and Dictionaries
- d = ["a": 1]
- d = ("a": 1)
- d = {"a": 1} ✓ Correct answer
- d = <"a": 1>
-
In a dictionary, data is stored in the form of ______.
Topic: Lists and Dictionaries
- Only keys
- Only values
- Key-value pairs ✓ Correct answer
- Indexes