Python Question and Answers – Sys Module

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Sys Module”.

1. Which of the following functions can help us to find the version of python that we are currently working on?
a) sys.version
b) sys.version()
c) sys.version(0)
d) sys.version(1)
View Answer

Answer: a
Explanation: The function sys.version can help us to find the version of python that we are currently working on. It also contains information on the build number and compiler used. For example, 3.5.2, 2.7.3 etc. this function also returns the current date, time, bits etc along with the version.

2. Which of the following functions is not defined under the sys module?
a) sys.platform
b) sys.path
c) sys.readline
d) sys.argv
View Answer

Answer: c
Explanation: The functions sys.platform, sys.path and sys.argv are defined under the sys module. The function sys.readline is not defined. However, sys.stdin.readline is defined.

3. The output of the functions len(“abc”) and sys.getsizeof(“abc”) will be the same.
a) True
b) False
View Answer

Answer: b
Explanation: The function len returns the length of the string passed, and hence it’s output will be 3. The function getsizeof, present under the sys module returns the size of the object passed. It’s output will be a value much larger than 3. Hence the above statement is false.
advertisement
advertisement

4. What will be the output of the following Python code, if the code is run on Windows operating system?

import sys
if sys.platform[:2]== 'wi':
	print("Hello")

a) Error
b) Hello
c) No output
d) Junk value
View Answer

Answer: b
Explanation: The output of the function sys.platform[:2] is equal to ‘wi’, when this code is run on windows operating system. Hence the output printed is ‘hello’.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. What will be the output of the following Python code, if the sys module has already been imported?

advertisement
sys.stdout.write("hello world")

a) helloworld
b) hello world10
c) hello world11
d) error
View Answer

Answer: c
Explanation: The function shown above prints the given string along with the length of the string. Hence the output of the function shown above will be hello world11.
advertisement

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

import sys
sys.stdin.readline()
Sanfoundry

a) ‘Sanfoundry\n’
b) ‘Sanfoundry’
c) ‘Sanfoundry10’
d) Error
View Answer

Answer: a
Explanation: The function shown above works just like raw_input. Hence it automatically adds a ‘\n’ character to the input string. Therefore, the output of the function shown above will be: Sanfoundry\n.

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

import sys
eval(sys.stdin.readline())
"India"

a) India5
b) India
c) ‘India\n’
d) ‘India’
View Answer

Answer: d
Explanation: The function shown above evaluates the input into a string. Hence if the input entered is enclosed in double quotes, the output will be enclosed in single quotes. Therefore, the output of this code is ‘India’.

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

import sys
eval(sys.stdin.readline())
Computer

a) Error
b) ‘Computer\n’
c) Computer8
d) Computer
View Answer

Answer: a
Explanation: The code shown above will result in an error. This is because this particular function accepts only strings enclosed in single or double inverted quotes, or numbers. Since the string entered above is not enclosed in single or double inverted quotes, an error will be thrown.

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

import sys
sys.argv[0]

a) Junk value
b) ‘ ‘
c) No output
d) Error
View Answer

Answer: b
Explanation: The output of the function shown above will be a blank space enclosed in single quotes. Hence the output of the code shown above is ‘ ‘.

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

import sys
sys.stderr.write(“hello”)

a) ‘hello’
b) ‘hello\n’
c) hello
d) hello5
View Answer

Answer: d
Explanation: The code shown above returns the string, followed by the length of the string. Hence the output of the code shown above is hello5.

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

import sys
sys.argv

a) ‘ ‘
b) [ ]
c) [‘ ‘]
d) Error
View Answer

Answer: c
Explanation: The output of the code shown above is a blank space inserted in single quotes, which is enclosed by square brackets. Hence the output will be [‘ ‘].

12. To obtain a list of all the functions defined under sys module, which of the following functions can be used?
a) print(sys)
b) print(dir.sys)
c) print(dir[sys])
d) print(dir(sys))
View Answer

Answer: d
Explanation: The function print(dir(sys)) helps us to obtain a list of all the functions defined under the sys module. The function can be used to obtain the list of functions under any given module in Python.

13. The output of the function len(sys.argv) is ____________
a) Error
b) 1
c) 0
d) Junk value
View Answer

Answer: b
Explanation: The output of the function sys.argv is [‘ ‘]. When we execute the function len([‘ ‘]), the output is 1. Hence the output of the function len(sys.argv) is also 1.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.