Search Header Logo

Mastering File I/O in Python 2

Mathematics

12th Grade

Mastering File I/O in Python 2
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

30 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is the correct way to open a file named `data.txt` for reading in Python?

`open('data.txt', 'w')`

`open('data.txt', 'r')`

`open('data.txt', 'a')`

`open('data.txt', 'x')`

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the purpose of using a context manager when working with files in Python?

To automatically close the file after the block of code is executed

To read the file line by line

To write data to the file

To append data to the file

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following code snippets correctly writes the string "Hello, World!" to a file named `output.txt`?

`with open('output.txt', 'r') as file: file.write("Hello, World!")`

`with open('output.txt', 'w') as file: file.write("Hello, World!")`

`with open('output.txt', 'a') as file: file.write("Hello, World!")`

`with open('output.txt', 'x') as file: file.write("Hello, World!")`

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How can you read all lines of a file into a list in Python?

`file.readlines()`

`file.read()`

`file.readline()`

`file.readall()`

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What does the `with` statement ensure when used with file operations?

The file is opened in binary mode

The file is always closed properly

The file is read-only

The file is writable

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which method is used to write a list of strings to a file in Python?

`file.write()`

`file.writelines()`

`file.append()`

`file.insert()`

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the default mode when opening a file using `open()` in Python?

Write mode

Read mode

Append mode

Exclusive creation mode

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?