0% found this document useful (0 votes)
79 views4 pages

Python Programming MCQs and Answers

The document provides information about Python programming concepts including classes, files, exceptions, generators, and XML serialization. It contains examples of Python code and questions about Python concepts. The questions cover topics like opening and reading files, exception handling keywords, XML uses and standards, and Python object serialization using pickle.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views4 pages

Python Programming MCQs and Answers

The document provides information about Python programming concepts including classes, files, exceptions, generators, and XML serialization. It contains examples of Python code and questions about Python concepts. The questions cover topics like opening and reading files, exception handling keywords, XML uses and standards, and Python object serialization using pickle.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programming :

UNIT : 3 - Classes

[Link] following Python code will result in an error if the input value is entered as -5.

assert False, 'Spanish'

a) True
b) False

Answer: A

[Link] will be the output of the following Python code?

x=10
y=8
assert x>y, 'X too small'

a) Assertion Error
b) 10 8
c) No output
d) 108

Answer : C

3. What will be the output of the following Python code?

def f(x):
yield x+1
print("test")
yield x+2
g=f(9)

a) Error
b) test
c)test
10
12
d) No output

Answer : D
4. What will be the output of the following Python code?

def f(x):
for i in range(5):
yield i
g=f(8)
print(list(g))

a) [0, 1, 2, 3, 4]
b) [1, 2, 3, 4, 5, 6, 7, 8]
c) [1, 2, 3, 4, 5]
d) [0, 1, 2, 3, 4, 5, 6, 7]

Answer : A

5. Which of the following is not an exception handling keyword in Python?

a) try
b) except
c) accept
d) finally

Answer : C

Unit 4 - Files

[Link] open a file c:\[Link] for writing, we use ____________

a) outfile = open(“c:\[Link]”, “w”)


b) outfile = open(“c:\\[Link]”, “w”)
c) outfile = open(file = “c:\[Link]”, “w”)
d) outfile = open(file = “c:\\[Link]”, “w”)

Answer : B

2. To read two characters from a file object infile, we use ____________

a) [Link](2)
b) [Link]()
c) [Link]()
d) [Link]()

Answer : A
3. To read the entire remaining contents of the file as a string from a file object infile, we use
____________

a) [Link](2)
b) [Link]()
c) [Link]()
d) [Link]()

Answer : B

4. The readlines() method returns ____________

a) str
b) a list of lines
c) a list of single characters
d) a list of integers

Answer : B

5. To read the next line of the file from a file object infile, we use ____________

a) [Link](2)
b) [Link]()
c) [Link]()
d) [Link]()

Answer : C

UNIT 5 - XML and Serialization.

[Link] Is Designed To Store Data And _____

Design
Verify
Both A & B
Transport

Answer : D

2. WSDL stands for ___

A. Web Services Description Language


B. Wide Services Description Language
C. Web Services Details Language
D. Web Science Description Language

Answer : D

[Link] Allows Hyperlinks To Point To Specific Parts (fragments) Of XML Documents?

XPath
Xpointer
XSLT
XLink

Answer : B

4. To sterilize an object hierarchy, the _____________ function must be called. To desterilize a


data stream, the ______________ function must be called.

a) dumps(), undumps()
b) loads(), unloads()
c) loads(), dumps()
d) dumps(), loads()

Answer : D

5. What will be the output of the following Python code?

pickle.HIGHEST_PROTOCOL

a) 4
b) 5
c) 3
d) 6

Answer : A

Common questions

Powered by AI

To serialize an object hierarchy, the 'dumps()' function should be used in Python. For deserializing a data stream back into objects, the 'loads()' function must be called. These functions convert between Python objects and a persistent or transmittable representation, allowing for efficient data handling .

The code results in an assertion error because the 'assert' statement is used to test if a condition in the code returns true. If not, the program will automatically terminate with an AssertionError. Here, 'assert False' will always trigger this error, as the condition "False" is false, and it will display the message 'Spanish' as specified .

The method 'infile.read(2)' reads the next two characters from the file object 'infile'. This function returns a string of two characters and is used to control the amount of data read from the file, allowing for precise handling of the file's content .

The keyword 'accept' is not associated with exception handling in Python. The correct keyword is 'except,' used as part of a try-except block to catch and handle exceptions. 'accept' is not a valid keyword or function in the context of handling exceptions in Python .

XPath is used mainly to navigate through elements and attributes in an XML document. It provides a syntax to define parts of an XML document by selecting nodes or node sets based on a variety of criteria. This capability is essential for querying specific portions within XML documents .

'infile.readlines()' reads the file until EOF and returns a list containing each line as an element, whereas 'infile.read()' reads the entire remaining content of the file as one single string. 'readlines()' is useful when processing large texts that need to be handled line by line, while 'read().' is useful for processing entire data block .

'pickle.HIGHEST_PROTOCOL' indicates the highest protocol version available for pickle serialization in Python, which determines the efficiency and flexibility of serialization. The value '4' shown in the example represents this maximum and enables more efficient serialization processes, supporting complex Python objects more effectively than older protocols .

In the generator function 'def f(x): for i in range(5): yield i', the 'range(5)' expression creates an iterator that produces numbers from 0 to 4. The 'yield' statement pauses the function and returns each of these numbers one by one to the caller. When the generator 'g' is converted to a list using 'list(g)', it iterates through and collects all yielded values, resulting in '[0, 1, 2, 3, 4]' .

In Python, the '\\' is used to escape the backslash itself. The filepath 'c:\\scores.txt' is used because backslashes in string literals are interpreted as escape characters. To include a literal backslash in the string, it needs to be escaped with another backslash .

The code produces no output because the generator function 'f(x)' is defined but not explicitly called after being assigned to 'g'. By only instantiating the generator with 'g=f(9)', no execution or iteration over the generator occurs, hence, no values are yielded or printed .

You might also like