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
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.
How can you print multiple values on a single line in Python?
Use commas to separate the values within the print statement.
Use semicolons to separate the values within the print statement.
Use the + operator to concatenate the values before printing.
Create a list of the values and print the list.
Which of the following statements will print the value of the variable x?
print(x)
print “x”
println(x)
echo x
What is the purpose of the sep argument in the print function?
To specify the separator between multiple values printed on the same line.
To specify the end character for the printed line.
To specify the file to which the output should be printed.
To specify the format of the output.
What is the purpose of the end argument in the print function?
To specify the separator between multiple values printed on the same line.
To specify the end character for the printed line.
To specify the file to which the output should be printed.
To specify the format of the output.
How can you print a string without a newline character?
print(string, end=””)
print(string, sep=””)
print(string + “”)
print(string; “”)
What is the output of the following code? [Python Quiz #75]
name="Alice"age=30print("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)
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)
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)
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
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.
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.
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:
What is the primary purpose of comments in Python code?
To execute instructions for the computer
To temporarily disable lines of code
To make the code more readable and understandable for humans
To create errors for debugging
Which of the following is the correct syntax for a single-line comment in Python?
// This is a comment
/* This is a comment */
# This is a comment
{ This is a comment }
How can you create a multi-line comment in Python?
Using triple single quotes (‘’’)
Using triple double quotes (“””)
Using backslash () at the end of each line
Using the comment keyword
What happens when you run code that includes comments?
The comments are executed along with the code.
The comments are ignored by the Python interpreter.
The comments are displayed as output.
The comments are converted into machine code.
Indentation:
What is the purpose of indentation in Python?
To define code blocks
To define functions
To define variables
To print output
Answer: a) To define code blocks
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)
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.
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)
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
Which of the following code snippets is correctly indented?
a)
```python
if True:
print("True")
print("False")
b)
ifTrue:print("True")print("False")
c)
ifTrue:print("True")print("False")
d)
ifTrue:print("True")print("False")
Answer: b)
ifTrue:print("True")print("False")
In Python, indentation is used to define ____ in the code.