Python Quiz Questions: MCQ!

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 Yogi0001122
Y
Yogi0001122
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,024
Questions: 10 | Attempts: 1,024

SettingsSettingsSettings
Python Quiz Questions: MCQ! - Quiz

.


Questions and Answers
  • 1. 

    The following control structure n = 3 if n == 2:     print("Hello") elif n == 3:     print("World") else:     print("Nothing") Results is:

    • A.

      N is assigned to 2

    • B.

      The string "Hello" is printed to the standard out

    • C.

      The string "World" is printed to the standard out

    • D.

      The string "Nothing" is printed to the standard out

    Correct Answer
    C. The string "World" is printed to the standard out
    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.

    Rate this question:

  • 2. 

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

    • A.

      Infile = open(“c:\temp.txt”, “r”)

    • B.

      Infile = open(“c:\\temp.txt”, “r”)

    • C.

      Infile = open(file = “c:\temp.txt”, “r+”)

    • D.

      Infile = open(file = “c:\\temp.txt”, “r+”)

    Correct Answer
    B. Infile = open(“c:\\temp.txt”, “r”)
    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.

    Rate this question:

  • 3. 

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

    • A.

      Outfile = open(“c:/temp.txt”, “a”)

    • B.

      Outfile = open(“c:\temp.txt”, “w+”)

    • C.

      Outfile = open(“c:\\temp.txt”, “r+”)

    • D.

      Outfile = open(“c:\\temp.txt”, “a”)

    Correct Answer(s)
    A. Outfile = open(“c:/temp.txt”, “a”)
    D. Outfile = open(“c:\\temp.txt”, “a”)
    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.

    Rate this question:

  • 4. 

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

    • A.

      List

    • B.

      Dict

    • C.

      Set

    • D.

      It causes SyntaxError

    Correct Answer
    D. It causes SyntaxError
    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."

    Rate this question:

  • 5. 

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

    • A.

      Chr

    • B.

      Ord

    • C.

      Int

    • D.

      Str

    Correct Answer
    D. Str
    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.

    Rate this question:

  • 6. 

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

    • A.

      Syntax error

    • B.

      [3, 2]

    • C.

      [4, 3]

    • D.

      [4, 3, 2]

    Correct Answer
    C. [4, 3]
    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].

    Rate this question:

  • 7. 

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

    • A.

      [1,2]

    • B.

      [1,3]

    • C.

      Syntax error

    • D.

      [1,3,5,7,9]

    Correct Answer
    D. [1,3,5,7,9]
    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].

    Rate this question:

  • 8. 

    How to assign a tuple of length 1 to a?

    • A.

      A = 1

    • B.

      A = (1,)

    • C.

      A = (1)

    • D.

      A = tuple(1)

    Correct Answer
    B. A = (1,)
    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.

    Rate this question:

  • 9. 

    What will be placed in a? a = 2,3

    • A.

      2

    • B.

      3

    • C.

      (2,3)

    • D.

      Error

    Correct Answer
    C. (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.

    Rate this question:

  • 10. 

    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>

    • A.

      Line 2

    • B.

      Test succeeded

    • C.

      Open error

    Correct Answer
    A. Line 2
    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".

    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
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 09, 2019
    Quiz Created by
    Yogi0001122

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.