Please wait...

Computer Science Test - 1
Result
Computer Science Test - 1
  • /

    Score
  • -

    Rank
Time Taken: -
  • Question 1/10
    5 / -1

    Which method is used to find the name of the current working directory in Python?
    Solutions

    The correct answer is option 2.

    Concept:

    getcwd():

    • Obtaining the directory on which work is presently being performed in a Python application is critical. This is readily accomplished by utilizing the os. getcwd ().
    • Every process that runs under an operating system has an associated working directory, which is referred to as the process's current working directory.

    Syntax:

    The syntax for getcwd() method:

    cwd = os.getcwd()

    Parameters:

    NA

    Return Value:

    This method returns the current working directory of a process.

    Example: Open a file and print the content:

    f = open("demofile.txt", "r")
    print(f.read())

    Output:

    The current working directory is:
    /Users/Python

    Hence the correct answer is getcwd().

  • Question 2/10
    5 / -1

    Which method is used to rename the file or folder?
    Solutions

    The correct answer is option 4.

    Concept:

    Python rename() file is a function in Python programming that is used to rename a file or a directory. The Python rename() file function can be used by giving two parameters, src (Source) and dst (Destination) (Destination).

    Syntax :

    This is the syntax for os.rename() method.

    os.rename(src, dst)

    Parameters

    • src: The source is the name of the file or directory. It has to exist already.
    • dst: The destination is the new name of the file or directory to be renamed.

    Example:

    import os os.rename('lmstestbook.txt','lmstestbook.txt')

    Hence the correct answer is rename()

  • Question 3/10
    5 / -1

    How many parameters one can pass in open() method in python?
    Solutions

    The correct answer is option 3.

    Concept:

    Open():

    • The open() function opens a file and returns it as a file object.
    • Open the file and return a corresponding file object. If the file cannot be opened, an OSError is raised.

    Syntax:

    open(file, mode)

    open(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

    The Open () method takes the two parameters remaining are optional.

    • file: The path and name of the file
    • mode: A string, define which mode you want to open the file in:
      • "r" - Read -The default value. Opens a file for reading; an error is returned if the file does not exist.
      • "a" - Append - Opens a file for appending and creates it if it does not already exist.
      • "w" - Write - Opens a file for writing and creates it if it does not already exist.
      • "x" - Create - Creates the specified file, and returns an error if the file exists.

    Example:
    Open a file and print the content:

    f = open("demofile.txt", "r")
    print(f.read())

    Hence the correct answer is 2.
  • Question 4/10
    5 / -1

    Serialisation is also known as____________.

    Solutions

    The correct answer is option 1.

    Concept:

    The pickle module implements binary protocols for serializing and de-serializing Python object structures.

    Pickling:

    • Serialization is also known as Pickling.
    • Pickling is the process of converting a Python object hierarchy into a byte stream.
    • Serialization is the process of converting an object in memory to a byte stream that can be stored on a disk or sent over a network.
    • The pickling process is also called marshaling.

    Example:

    The following example serializes data into a binary file.

    #!/usr/bin/python

    import pickle

    data = {
        'a': [1, 4.0, 3, 4+6j],
        'b': ("a red fox", b"and old falcon"),
        'c': {None, True, False}
    }

    with open('data.bin', 'wb') as f:
        pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

    Unpickling:

    • Deserialization is also known as unpickling.
    • Unpickling is the process of converting a byte stream into an object hierarchy, which is the inverse of Pickling.
    • Deserialization is the process of converting a byte stream to a Python object.
    • The unpickling process is also called unmarshalling.

    Example:

    In the next example, we unpickle data from a binary file.

    #!/usr/bin/python

    import pickle

    with open('data.bin', 'rb') as f:

        data = pickle.load(f)

        print(data)

    Hence the correct answer is pickling.

  • Question 5/10
    5 / -1

    The file in which data are to be dumped, needs to be opened in _______________.
    Solutions

    The correct answer is option 4.

    Concept:

    dump() method:

    This technique is used to transform (pickle) Python objects so that they may be written to a binary file. The file into which the data is to be dumped must be opened in binary write mode (wb).

    Syntax:

    dump(data_object, file_object)

    where data_object is the object that has to be dumped to the file with the filehandle named file_object.

    Example:

    Using the dump command write a student's record (roll no, name, gender, and grades) to the binary file named mybinary.dat (). After pickling, we must close the file.

    Pickling data in Python
    import pickle
    listvalues=[1,"Geetika",'F', 26]
    fileobject=open("mybinary.dat", "wb")
    pickle.dump(listvalues,fileobject)
    fileobject.close()

    Hence the correct answer is binary write mode.

  • Question 6/10
    5 / -1

    Which of the following is right syntax to open a file.txt in append mode?
    Solutions

    The correct answer is option 1.

    Concept:

    • Use the built-in open() method to open the file.
    • The open() function returns a file object with a read() method for accessing the file's content.

    The open() function has many parameters but you’ll be focusing on the first two.

    Syntax:

    open(path_to_file, mode)

    • path_to_file parameter specifies the path to the text file.
    • mode are read/write/append
    ModeDescription
    'r'  Open for text file for reading text
    'w'Open a text file for writing text'
    'a' Open a text file for appending text

    The handle is at the end of the file when the file is opened in append mode. The new data will be added at the end, following the old data. Let's look at an example to show how to write mode differs from the append mode.

    file_object = open("file.txt", "a")

    The above line is true for the right syntax to open a file.txt in append mode.

    Hence the correct answer is file_object = open("file.txt", "a").

  • Question 7/10
    5 / -1

    The following functions are used to access data randomly.
    Solutions

    The correct answer is option 3.

    Concept:

    Setting Offsets in a File:

    The functions we've learned so far are utilized to access data sequentially from a file. However, if we wish to retrieve data at random, Python has the seek() and tell() methods.

    The tell() method:

    This function returns a number indicating the file object's current location in the file. The supplied position is the byte location from the start of the file to the current position of the file object.

    syntax:

    file_object.tell()

    The seek() method:

    This function is used to place a file object at a certain location within a file. 

    syntax:

    file_object.seek(offset [, reference_point])
    Here offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object.
    Hence the correct answer is seek() and tell().
  • Question 8/10
    5 / -1

    The following code will perform which operation?

    object.readline(10)

    Solutions

    The correct answer is option 2.

    Concept:

    Python File readline() Method:

    The readlines() are used to read all of the lines at once and return them as string elements in a list. This method is useful for tiny files since it reads the entire file content to memory and then splits it into individual lines. We may go through the list and use the strip() method to remove the newline 'n' character.

    Syntax:

    file.readline(size)

    Here size is Optional. The number of bytes from the line to return. Default -1, which means the whole line.

    Explanation:

    object.readline(10)

    readline reads a line of the file and returns it in the form of the string. It takes a parameter 10, which specifies the maximum number of bytes that will be read. However, does not reads more than one line, even if 10 exceeds the length of the line.

    So it read the first 10 characters of line.

    Hence the correct answer is to read the first 10 characters of the line.

  • Question 9/10
    5 / -1

    What is the full form of CSV file?
    Solutions

    The correct answer is option 3.

    Concept:

    CSV Full Form:

    • The Comma Separated Value (CSV) format is a text file that contains data. It makes it easier to store data in a table-like format. The CSV extension is used to identify CSV files.
    • CSV files are used to transfer huge databases between applications while adhering to a precise format. CSV files may be opened with any text editor, such as Notepad or Excel.

    Characteristics:

    • Each data item is entered on a different line. If the record is too long, it may span numerous lines.
    • The data fields are separated by commas.
    • A set of double quotes contains the fields that contain commas and are separated by double-quotes.
    • The fields containing double quotes are contained in a set of double-quotes.
    • The space characters that appear adjacent to built-in commas are ignored.

    Hence the correct answer is Comma Separated Values

  • Question 10/10
    5 / -1

    A ___________ method is used to position the file object at a particular position in a file.
    Solutions

    The correct answer is option 4.

    Concept:

    Setting Offsets in a File:

    The functions we've learned so far are utilized to access data sequentially from a file. However, if we wish to retrieve data at random, Python has the seek() and tell() methods.

    The seek() method:

    This function is used to place a file object at a certain location within a file.  seek() method is used to position the file object at a particular position in a file.

    syntax:

    file_object.seek(offset [, reference_point])
    Here offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object.
    Hence the correct answer is seek().

    Additional InformationThe tell() method: 

    This function returns a number indicating the file object's current location in the file. The supplied position is the byte location from the start of the file to the current position of the file object.

    syntax:

    file_object.tell()

User Profile
-

Correct (-)

Wrong (-)

Skipped (-)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Click on Allow to receive notifications
×
Open Now