350-201(NEW-127Q) · Question #40
350-201(NEW-127Q) Question #40: Real Exam Question with Answer & Explanation
The correct answer is D. if result == 0: print "Port {}: Open".format(port) sock.close(). D is correct because Python's socket.connect_ex() returns 0 on a successful connection, meaning the port is open - so if result == 0 is the right condition. print is the valid Python output function, and sock.close() correctly closes the socket object using the proper object-meth
Question
Options
- Aif result == 0: show "Port {}: Open".format(port) close()
- Bif output == 0: print "Port {}: Open".format(port) open()
- Cif output != 0: print "Port {}: Open".format(port) sock.open()
- Dif result == 0: print "Port {}: Open".format(port) sock.close()
Explanation
D is correct because Python's socket.connect_ex() returns 0 on a successful connection, meaning the port is open - so if result == 0 is the right condition. print is the valid Python output function, and sock.close() correctly closes the socket object using the proper object-method syntax.
Why the distractors fail:
- A uses
showinstead ofprint(not a Python built-in) andclose()without the socket object prefix - it should besock.close(). - B uses the wrong variable name
outputinstead ofresult(the variable assigned fromconnect_ex()), andopen()is a file operation in Python, not a socket method. - C also uses the wrong variable
outputand inverts the condition with!= 0- a non-zero return fromconnect_ex()means the port is closed or filtered, not open.sock.open()is also not a valid socket method.
Memory tip: Think of connect_ex() like Unix exit codes - 0 means success (port open), anything else means failure. Pair that with the pattern sock.close() (object dot method) to remember you always need the socket variable prefix when closing.
Topics
Community Discussion
No community discussion yet for this question.