Share Your Feedback

Python Basics MCQs: Test Your Knowledge

Python Basics MCQs: Test Your Knowledge

Python

  1. What is Python?
    • A) A type of snake
    • B) A high-level programming language
    • C) A web browser
    • D) An operating system

    Answer: B) A high-level programming language

  2. Which of the following is a popular Integrated Development Environment (IDE) for Python?
    • A) Visual Studio Code
    • B) Microsoft Word
    • C) Adobe Photoshop
    • D) Google Chrome

    Answer: A) Visual Studio Code

  3. Which tool is used for managing and installing Python packages? [Python Quiz #53]
    • A) pip
    • B) npm
    • C) yarn
    • D) gem

    Answer: A) pip

  4. What does IDE stand for in the context of Python programming?
    • A) Integrated Development Environment
    • B) Integrated Data Environment
    • C) Internal Development Engine
    • D) Internet Development Environment

    Answer: A) Integrated Development Environment

  5. What is the purpose of virtual environments in Python?
    • A) To provide a web-based interface for Python development
    • B) To create isolated environments for different Python projects
    • C) To compile Python code into machine code
    • D) To improve the speed of Python code execution

    Answer: B) To create isolated environments for different Python projects

  6. Which tool is used for version control in Python projects?
    • A) Git
    • B) Docker
    • C) Kubernetes
    • D) Jenkins

    Answer: A) Git

  7. Which of the following is the correct extension of the Python file? [Python Quiz #51]

    • A) .python
    • B) .pl
    • C) .p
    • D) .py
  8. What is Jupyter Notebook primarily used for in the Python ecosystem? [Python Quiz #54]
    • A) Web development
    • B) Writing and sharing code, visualizations, and text
    • C) Game development
    • D) Mobile app development

    Answer: B) Writing and sharing code, visualizations, and text

  9. Who created Python and in which year? [1]
    • A) James Gosling in 1995
    • B) Guido van Rossum in 1991
    • C) Bjarne Stroustrup in 1983
    • D) Dennis Ritchie in 1972

    Answer: B) Guido van Rossum in 1991

  10. Which of the following features is a key characteristic of Python? [Python Quiz #52]
    • A) Statically typed
    • B) Compiled language
    • C) Interpreted language
    • D) Requires manual memory management
  11. What is the purpose of the virtual environment tool in Python?
    • A) To create virtual machines
    • B) To manage and isolate project-specific dependencies
    • C) To design virtual reality experiences
    • D) To develop mobile applications

Answer: B) To manage and isolate project-specific dependencies

  1. What is the Python Package Index (PyPI)?
    • A) A text editor for Python
    • B) A library for machine learning
    • C) A repository of software for the Python programming language
    • D) An IDE for Python development

Answer: C) A repository of software for the Python programming language

  1. What is pip in Python?
    • A) A text editor
    • B) A package installer
    • C) A version control system
    • D) A web framework

Answer: B) A package installer

  1. Which tool is commonly used for creating virtual environments in Python?
    • A) npm
    • B) pip
    • C) venv
    • D) docker

Answer: C) venv

  1. Which Integrated Development Environment (IDE) is specifically designed for Python development? [Python Quiz #54]
    • A) Visual Studio
    • B) IntelliJ IDEA
    • C) PyCharm
    • D) Eclipse

Answer: C) PyCharm

  1. Which of the following is true about Python?
    • A) Python is a compiled language
    • B) Python is a low-level programming language
    • C) Python supports object-oriented programming
    • D) Python does not support modules

Answer: C) Python supports object-oriented programming

  1. Which of the following is NOT a feature of Python?
    • A) Interpreted language
    • B) Compiled language
    • C) High-level language
    • D) Object-oriented language

Answer: B) Compiled language

print function

[video:Can You Guess the Output of this Python Code? print Quiz](https://youtu.be/WD92M8WXRZM?si=1FgSE-5Vr1aFCVR-)
  1. What is the output of the following code? [Python Quiz #28]

Code:

print("I", "love", "Python", sep="-")

Options:

  • A) I-love-Python
  • B) I love Python
  • C) I, love, Python
  • D) Syntax error

Watch the video for the answer: https://youtube.com/shorts/WD92M8WXRZM?si=kJ5jbIAaIlJ_63ia

  1. What will be the output of the following code? [Python Quiz #29]
x = 5
y = 3
print("The value of x is", x, "and the value of y is", y)
  • A) Syntax error
  • B) The value of x is {} and the value of y is {}
  • C) The value of x is 3 and the value of y is 5
  • D) The value of x is 5 and the value of y is 3

Watch the video for the answer: https://youtube.com/shorts/ZE2yfAJsCxk?si=6UvXfKLmR56c-Qu9

  1. Which of the following is the correct syntax for the print statement in Python?
  • A) print (“text”)
  • B) println (“text”)
  • C) echo (“text”)
  • D) None
  1. What will be the output of the following code?
print("Hello, world!")
  • A) Hello
  • B) world
  • C) Hello, world!
  • D) There will be no output.
  1. How can you print multiple values on a single line in Python?

  2. Use commas to separate the values within the print statement.
  3. Use semicolons to separate the values within the print statement.
  4. Use the + operator to concatenate the values before printing.
  5. Create a list of the values and print the list.

  6. Which of the following statements will print the value of the variable x?

  7. print(x)
  8. print “x”
  9. println(x)
  10. echo x

  11. What is the purpose of the sep argument in the print function?

  12. To specify the separator between multiple values printed on the same line.
  13. To specify the end character for the printed line.
  14. To specify the file to which the output should be printed.
  15. To specify the format of the output.

  16. What is the purpose of the end argument in the print function?

  17. To specify the separator between multiple values printed on the same line.
  18. To specify the end character for the printed line.
  19. To specify the file to which the output should be printed.
  20. To specify the format of the output.

  21. How can you print a string without a newline character?

  22. print(string, end=””)
  23. print(string, sep=””)
  24. print(string + “”)
  25. print(string; “”)

  26. What is the output of the following code? [Python Quiz #75]
name = "Alice"
age = 30
print("My name is", name, "and I am", age, "years old.")
  • A) My name is Alice and I am 30 years old.
  • B) My name is Aliceand I am 30years old. (No separation)
  • C) Alice 30 (Values printed without labels)
  • D) An error (Incorrect syntax)
  1. How can you format a string in Python to insert variables directly within it?
  • a) Using string concatenation with the + operator (Limited control)
  • b) Using the format method (Less readable for complex formatting)
  • c) Using f-strings
  • d) All of the above (f-strings are generally preferred)
  1. Which of the following statements is NOT valid for the print function in Python?
  • a) print(“Hello, world!”)
  • b) print(5 + 3) (prints the result of the expression)
  • c) print() (prints an empty line)
  • d) print(x, y, sep=”,”) (prints x and y with a comma separator)
  1. How can you prevent a newline character from being printed at the end of the output in Python?
  • a) By using a semicolon at the end of the print statement (this is not valid Python syntax)
  • b) Using the end argument within the print function and setting it to an empty string (“”)
  • c) Specifying a special flag in the function call
  • d) There’s no way to suppress the newline character
  1. What is a syntax error in Python?
  • A) An error caused by incorrect logic in the code.
  • B) An error detected when the code violates the rules of the Python language.
  • C) An error that occurs during runtime.
  • D) An error caused by a variable not being defined.
  1. Identify the error in the following code snippet:
prin("Hello World!")
  • A) Incorrect variable name
  • B) Missing colon
  • C) Misspelled keyword
  • D) Missing parenthesis

Hint NameError: name ‘prin’ is not defined.

  1. What should be done to correct the syntax error in the following code?
print("Hello World!"
  • A) Add a closing quotation mark.
  • B) Add a closing parenthesis.
  • C) Add a colon at the end.
  • D) Indent the line correctly.

Comments:

  1. What is the primary purpose of comments in Python code?

  2. To execute instructions for the computer
  3. To temporarily disable lines of code
  4. To make the code more readable and understandable for humans
  5. To create errors for debugging

  6. Which of the following is the correct syntax for a single-line comment in Python?

  7. // This is a comment
  8. /* This is a comment */
  9. # This is a comment
  10. { This is a comment }

  11. How can you create a multi-line comment in Python?

  12. Using triple single quotes (‘’’)
  13. Using triple double quotes (“””)
  14. Using backslash () at the end of each line
  15. Using the comment keyword

  16. What happens when you run code that includes comments?

  17. The comments are executed along with the code.
  18. The comments are ignored by the Python interpreter.
  19. The comments are displayed as output.
  20. The comments are converted into machine code.

Indentation:

  1. What is the purpose of indentation in Python?
  2. To define code blocks
  3. To define functions
  4. To define variables
  5. To print output

Answer: a) To define code blocks

  1. Which of the following is a correct way to indent code in Python?
    • a) Using tabs
    • b) Using spaces
    • c) Using both tabs and spaces
    • d) Using neither tabs nor spaces

Answer: b) Using spaces (Python recommends using 4 spaces for indentation)

  1. What happens if you don’t indent your code in Python?
    • a) It will run correctly
    • b) It will throw a syntax error
    • c) It will print an error message
    • d) It will ignore the code

Answer: b) In Python, improper indentation specifically results in an IndentationError. While a syntax error is a broad category for any error in the syntax, an IndentationError is a specific type of syntax error related to incorrect indentation.

  1. What is the indentation error in the following code?
    if True:
      print("True")
    print("False")
    
    • a) Missing indentation
    • b) Extra indentation
    • c) Incorrect indentation
    • d) No error

Answer: b) Extra indentation (the second print statement has extra indentation)

  1. What is the standard number of spaces used for indentation in Python? a) 2 spaces b) 4 spaces c) 6 spaces d) 8 spaces

Answer: b) 4 spaces

  1. Which of the following code snippets is correctly indented?
a) 
```python
if True:
  print("True")
    print("False")

b)

if True:
    print("True")
    print("False")

c)

if True:
     print("True")
print("False")

d)

   if True:
  print("True")
      print("False")

Answer: b)

 if True:
    print("True")
    print("False")
  1. In Python, indentation is used to define ____ in the code.
  • A) Loops
  • B) Functions
  • C) Classes
  • D) Blocks of code