exam questions

Exam 350-401 All Questions

View all questions & answers for the 350-401 exam

Exam 350-401 topic 1 question 350 discussion

Actual exam question from Cisco's 350-401
Question #: 350
Topic #: 1
[All 350-401 Questions]

Which line must be added in the Python function to return the JSON object {`cat_9k`: `FXS1932Q2SE`}?

  • A. return (json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']}))
  • B. return (json.dumps({for d in json.loads(test_json)['response']: d['hostname']: d['serialNumber']}))
  • C. return (json.loads({d['hostname']: d['serialNumber'] for d in json.dumps(test_json)['response'}))
  • D. return (json.loads({for d in json.dumps(test_json)['response']: d['hostname']: d['serialNumber']}))
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Dre876
Highly Voted 1 year, 7 months ago
I might as well go take a application programming course at this point.
upvoted 21 times
[Removed]
1 year, 2 months ago
I've been thinking the same thing. Planning on the CCIE after this, I may take some time before even starting to just run through some code academy courses...
upvoted 2 times
...
...
Jared28
Highly Voted 2 years, 10 months ago
Selected Answer: A
Tested as well, A was the only one that functioned. import json def get_data(): test_json = """ { "response": [{ "managementipAddress": "10.10.2.253", "memorySize": "3398345152", "serialNumber": "FXS1932Q2SE", "softwareVersion": "16.3.2", "hostname": "cat_9k" }], "version": "1.0" } """ return (json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']})) print(get_data())
upvoted 11 times
...
CiscoTerminator
Most Recent 10 months ago
I think Cisco wants to crack the whip on Network engineers who are not taking the Devnet Associate exam first before CCNP Encor....cause hey
upvoted 5 times
...
[Removed]
11 months ago
Selected Answer: A
A is correct
upvoted 1 times
[Removed]
11 months ago
d for d
upvoted 1 times
...
...
due
1 year, 8 months ago
Selected Answer: A
json.loads function for string to object. json.dumps function for object to string. question for return object. Keyword = dumps D for D.
upvoted 2 times
...
yqpmateo
1 year, 8 months ago
Selected Answer: A
json.load method parse a valid JSON string and convert it into a Python dictionary. the test_json variable is a JSON string so the answers that use json.dumps(test_json) are incorrect. Out of the other two, answer A is valid. Here is a python code that proves it: import json def get_data(): test_json = """ { "response" : [{ "managementIPAdd":"10.10.10.2", "memorySize" : "2323232", "serialNumber" : "FX2342232WWQ", "softwareVer" : "14.2.3", "hostname" : "cat_9k" }], "vesion":"1.0" } """ for d in json.loads(test_json)['response']: print(json.dumps({d['hostname']: d['serialNumber']})) return ( json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']})) print(get_data())
upvoted 2 times
...
danman32
1 year, 9 months ago
I didn't think A or C could be correct because of the position of the 'for d in'
upvoted 1 times
...
lafrank
2 years ago
In fact, all answers are incorrect if question is taken literally :-) B,C and D all contain syntax errors. While A is syntactically correct, it returns a string and NOT a JSON object as it was stated in the question ! Be careful with print() as it would both print a JSON object and a string object much the same way. Use print(type(get_data()) instead and you would see that the returned value is of type str.
upvoted 3 times
...
JohnSmithZhao
2 years, 1 month ago
json.loads() takes in a string and returns a json object. json. dumps() takes in a json object and returns a string.
upvoted 4 times
...
Heim_Ox
2 years, 10 months ago
Tested it. A is the correct one that gets the requested output
upvoted 1 times
...
LeGloupier
2 years, 11 months ago
answer is A tested, it works as expected thats a python list comprehension (the list has only one element which is the main dictionary)
upvoted 2 times
...
Farid77
2 years, 11 months ago
D is the correct answer ... If you have a JSON string, you can parse it by using the json.loads() method.
upvoted 1 times
...
rlilewis
2 years, 11 months ago
Selected Answer: A
Tested and verified myself. As cvndani said - only A works. All others either give an error: "for d in" at the start of the { character results in "SyntaxError: invalid syntax" C returns error "TypeError: string indices must be integers"
upvoted 2 times
...
bara_ken
2 years, 12 months ago
Selected Answer: B
This is B
upvoted 1 times
...
bogd
3 years, 2 months ago
Selected Answer: A
A (dictionary comprehension over the JSON contents)
upvoted 4 times
...
cvndani
3 years, 3 months ago
all options except A give syntax error
upvoted 3 times
...
VaZi
3 years, 3 months ago
Right answer should be A json.dumps({d['hostname']: d['serialNum'] for d in json.loads(test_json)['response']}) Checked in Python. B,D "for d in " - Invalid Syntax C - Type Error. json.dumps() returns a string, so this syntax is wrong json.dumps(test_json)['response']
upvoted 4 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago