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...
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())
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())
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.
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"
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']
This section is not available anymore. Please use the main Exam Page.350-401 Exam Questions
Log in to ExamTopics
Sign in:
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.
Dre876
Highly Voted 1 year, 7 months ago[Removed]
1 year, 2 months agoJared28
Highly Voted 2 years, 10 months agoCiscoTerminator
Most Recent 10 months ago[Removed]
11 months ago[Removed]
11 months agodue
1 year, 8 months agoyqpmateo
1 year, 8 months agodanman32
1 year, 9 months agolafrank
2 years agoJohnSmithZhao
2 years, 1 month agoHeim_Ox
2 years, 10 months agoLeGloupier
2 years, 11 months agoFarid77
2 years, 11 months agorlilewis
2 years, 11 months agobara_ken
2 years, 12 months agobogd
3 years, 2 months agocvndani
3 years, 3 months agoVaZi
3 years, 3 months ago