Test Your Knowledge On Python Functions and Variables Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Themes
T
Themes
Community Contributor
Quizzes Created: 420 | Total Attempts: 920,857
Questions: 10

SettingsSettingsSettings
Python Language Quizzes & Trivia

.Ready to level up your Python programming skills? Our Python Functions and Variables Quiz is here to help you master the fundamentals and become a coding whiz! This interactive quiz challenges your understanding of two essential concepts in Python: functions and variables. We've designed a variety of questions to test your knowledge, including defining and calling functions, understanding variable scope, working with data types, and applying functions and variables in real-world scenarios.

This Python Functions and Variables Quiz is designed to solidify your understanding and boost your coding confidence! You'll get instant feedback on your answers and helpful explanations to Read morereinforce your learning. If you're a beginner just starting to learn Python or an experienced programmer looking to brush up on your skills, this quiz is for you.


Python Functions and Variables Questions and Answers

  • 1. 

    What is the keyword used to define a function in Python?

    • A.

      Func

    • B.

      Def 

    • C.

      Function 

    • D.

      Define

    Correct Answer
    B. Def 
    Explanation
    The def keyword signals the start of a function definition. It's followed by the function's name, parentheses that may enclose parameters, and a colon. The indented code block after the colon contains the instructions the function executes. For example: def greet(name): print("Hello, " + name + "!")

    Rate this question:

  • 2. 

    What does the return statement do in a function?

    • A.

      Prints a value 

    • B.

      Ends the function and specifies the value to send back 

    • C.

      Restarts the function 

    • D.

      Defines a variable

    Correct Answer
    B. Ends the function and specifies the value to send back 
    Explanation
    Return acts like a function's conclusion. It stops the function's execution and can optionally send a value back to where the function was called. This value can be stored in a variable or used directly. Example: def add(x, y): return x + y

    Rate this question:

  • 3. 

    Which of the following is NOT a valid variable name in Python? 

    • A.

      My_variable 

    • B.

      _variable 

    • C.

      123variable 

    • D.

      Variable123

    Correct Answer
    C. 123variable 
    Explanation
    Python has rules for naming variables. They must start with a letter (lowercase or uppercase) or an underscore. They cannot start with a number like "123variable", but can include numbers after the first character (e.g., "variable123").

    Rate this question:

  • 4. 

    What is the scope of a variable defined inside a function? 

    • A.

      Global 

    • B.

      Local 

    • C.

      Universal 

    • D.

      External

    Correct Answer
    B. Local 
    Explanation
    Variables created within a function have "local" scope, meaning they exist only within that function. Think of it as a function's private workspace. Code outside the function cannot access these variables. This helps prevent naming conflicts in larger programs.

    Rate this question:

  • 5. 

    What will the following code print: print(type(10.5))? 

    • A.

      <class 'int'> 

    • B.

      <class 'float'> 

    • C.

      <class 'str'> 

    • D.

      <class 'bool'>

    Correct Answer
    B. <class 'float'> 
    Explanation
    type(10.5) checks the data type of the value 10.5. Since 10.5 has a decimal point, it's a floating-point number, represented as <class 'float'> in Python. Integers (whole numbers) would be <class 'int'>.

    Rate this question:

  • 6. 

    How do you assign a value to a variable in Python? 

    • A.

      Variable = value 

    • B.

      Value -> variable 

    • C.

      Variable == value 

    • D.

      Value : variable

    Correct Answer
    A. Variable = value 
    Explanation
    The equals sign (=) is the assignment operator in Python. It takes the value on the right side and stores it in the variable on the left. For example, age = 25 assigns the integer 25 to the variable named "age."

    Rate this question:

  • 7. 

    What does this code do: my_list = [1, 2, 3]?

    • A.

      Creates a list with three elements 

    • B.

      Defines a function named "my_list" 

    • C.

      Prints the numbers 1, 2, and 3 

    • D.

      Assigns the value 3 to the variable "my_list"

    Correct Answer
    A. Creates a list with three elements 
    Explanation
    This code creates a list, which is an ordered collection of items. The square brackets [] define the list, and the comma-separated values within are its elements. my_list now holds the integers 1, 2, and 3.

    Rate this question:

  • 8. 

    What is the output of len("Hello")?

    • A.

    • B.

    • C.

    • D.

      Error

    Correct Answer
    B. 5 
    Explanation
    The len() function is used to determine the length of a sequence, such as a string. In this case, it counts the characters in the string "Hello", which are 'H', 'e', 'l', 'l', and 'o', totaling 5.

    Rate this question:

  • 9. 

    What does the input() function do? 

    • A.

      Prints text to the console.

    • B.

      Waits for the user to type something and returns it as a string.

    • C.

      Generates a random number.

    • D.

      Opens a file.

    Correct Answer
    B. Waits for the user to type something and returns it as a string.
    Explanation
    input() makes your program interactive. It displays an optional prompt to the user, then pauses execution until the user types something and presses Enter. The entered text is captured by the input() function and returned as a string.

    Rate this question:

  • 10. 

    What is a docstring in Python?

    • A.

      A comment that explains what a piece of code does.

    • B.

      A type of variable.

    • C.

      A way to import modules.

    • D.

      A loop structure.

    Correct Answer
    A. A comment that explains what a piece of code does.
    Explanation
    Docstrings are special comments used to document your code. They are enclosed in triple quotes ("""Docstring goes here"""). When placed within a function, they describe what the function does, its parameters, and what it returns. They aid in code readability and understanding.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Dec 08, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 24, 2024
    Quiz Created by
    Themes
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.