Python Quiz Questions: MCQ!

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Yogi0001122
Y
Yogi0001122
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,034
| Attempts: 1,034 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1. The following control structure n = 3 if n == 2:     print("Hello") elif n == 3:     print("World") else:     print("Nothing") Results is:

Explanation

The code assigns the value 3 to the variable n. Then, it checks if n is equal to 2. Since n is not equal to 2, it moves to the next condition and checks if n is equal to 3. Since n is equal to 3, it executes the code block under the elif statement, which prints the string "World" to the standard output.

Submit
Please wait...
About This Quiz
Python Quiz Questions: MCQ! - Quiz

This Python MCQ quiz assesses fundamental knowledge of Python programming, including syntax, file handling, and data structures. It's designed to test and enhance learners' understanding of Python, making... see moreit ideal for beginners and intermediate programmers. see less

2. What is the type of b?   a = "bay"   b = a[0]

Explanation

The variable "b" is assigned the value of the first character of the string "a". Since the value of "a" is a string, the type of "b" will also be a string.

Submit
3. What is the result of this code? a=[1,2,3,4,5,6,7,8,9] a[::2]

Explanation

The code is using slicing to extract elements from the list "a". The syntax [::2] means that it will start from the beginning of the list and take every second element. So, the result will be a new list containing the elements [1, 3, 5, 7, 9].

Submit
4. What will be placed in a? a = 2,3

Explanation

The answer (2,3) suggests that the value of 'a' will be assigned as a tuple containing the values 2 and 3.

Submit
5. Which Of The Following Command Is Used To Open A File "C:\Temp.Txt" In Read-Mode Only?

Explanation

The correct answer is "infile = open(“c:\\temp.txt”, “r”)". This is because the double backslash "\\" is used to escape the special characters in the file path. In this case, it is used to escape the backslash character "\" in the file path "c:\temp.txt". The "r" mode is used to open the file in read-mode only, allowing only reading operations to be performed on the file.

Submit
6. What is the type of a? a = {1,2:3}

Explanation

The given assignment statement `a = {1,2:3}` causes a syntax error because it is trying to create a dictionary with both a key-value pair (`2:3`) and an element (`1`) without a key. In Python, dictionaries require key-value pairs, and sets require only elements. Therefore, the correct answer is "It causes SyntaxError."

Submit
7. What is the result of this code? a=[1,2,3,4,5] a[3:1:-1]

Explanation

This code is using list slicing to extract a portion of the list 'a'. The syntax for list slicing is 'a[start:end:step]'. In this case, the start index is 3, the end index is 1, and the step is -1. This means that it will start at index 3, go backwards towards index 1, and include every element in between. Therefore, the result will be [4, 3].

Submit
8. How to assign a tuple of length 1 to a?

Explanation

In order to assign a tuple of length 1 to variable "a", we need to use the syntax "(1,)". This creates a tuple with a single element, which in this case is the integer 1. The comma after the 1 is necessary to indicate that it is a tuple and not just a parentheses grouping.

Submit
9. What is the output of the below program? >>> logfile = open('test.log', 'a') >>> logfile.write('line 2') >>> logfile.close() >>> print file('test.log').read()</pre>

Explanation

The program opens a file called "test.log" in append mode and writes the string "line 2" to it. Then, the file is closed. Finally, the program reads the contents of the file "test.log" and prints it. The contents of the file will be "line 2".

Submit
10. Which Of The Following Command Is Used To Open A File "C:\Temp.Txt" In Append-Mode?

Explanation

The correct answer is "outfile = open(“c:/temp.txt”, “a”),outfile = open(“c:\\temp.txt”, “a”)" because the "a" mode in the open() function is used to open a file in append mode. This means that if the file already exists, new data will be added to the end of the file, and if the file does not exist, a new file will be created. The first option "outfile = open(“c:/temp.txt”, “a”)" uses forward slashes in the file path, which is a valid way to specify the path in Python. The second option "outfile = open(“c:\\temp.txt”, “a”)" uses double backslashes to escape the backslash character in the file path, which is also a valid way to specify the path in Python.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 21, 2023 +

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

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 09, 2019
    Quiz Created by
    Yogi0001122
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
The following control structure...
What is the type of b?  ...
What is the result of this code? a=[1,2,3,4,5,6,7,8,9] a[::2]
What will be placed in a? a = 2,3
Which Of The Following Command Is Used To Open A File "C:\Temp.Txt" In...
What is the type of a? a = {1,2:3}
What is the result of this code? a=[1,2,3,4,5] a[3:1:-1]
How to assign a tuple of length 1 to a?
What is the output of the below program? ...
Which Of The Following Command Is Used To Open A File "C:\Temp.Txt" In...
Alert!

Advertisement