A developer is reviewing a code that was written by a colleague. It runs fine, but there are many lines of code to do a seemingly simple task repeatedly. Which action organizes the code?
A.
Refactor the code by removing any unnecessary tests.
B.
Reverse engineer and rewrite the code logic.
C.
Using functions, rewrite any pieces of code that are repeated.
"c" should be the correct answer as Functions can contain loops. In fact, embedding loops within functions is an efficient way to get rid of repeated code snippets.
Incorrect Answers
A - This would have been correct had the phrase "by removing unnecessary tests" not been included. Code refactor has nothing to do with removing tests. Rather is is about cleaning up the code for better code quality.
B - This is very easy to deduce as being incorrect.
D - While this may seem correct (in the sense that it organises code to make it simpler), for the question the issue is that "there are many lines of code to do a seemingly simple task repeatedly"...With the issue being that a simple task is done '"repeatedly", even if a loop is used to shorten the code, it will be used over and over again (and this loop could still still include a number of lines). Following from this therefore, a function is defined for the task that is to be done repeatedly, and then invoked/called when the task should be done. Thus correct answer is C.
I would use loop instead of function if I have to choose one. I would prefer to have :
for x in iterator:
do a
do b
do c
instead of :
new_function()
new_function()
new_function()
Best way is obviously to use both :)
Problem: "but there are many lines of code to do a seemingly simple task repeatedly."
Question: "Which action organizes the code since the problem is the code doing a simple task repeatedly?" The answer is C because when the code performs a discrete (or simple) task, even if it happens only once, may be a candidate for encapsulation and the question makes clear that the task code is being used more than once.
The correct answer is A because if you add functions it would change the logic too much for your colleague. Answer A just fixes the immediate problem (repeated tasks) without changing the flow of the script. Maybe functions are not needed for this script. For instance, instead of writting two loops, just reduce it to comprehension list.
Functions help for code re-use, C seems to be correct but reading the question again, organizing the code, after writing code comes refactoring which involves organizing the code and removing unnecessary lines of code making it neater. I will go with A.
Refactoring the code is broad term. I guess at this level (Associate) this could be (A) correct choice. But, loops (D) can eliminate repeating code, so does (c) function calls.
This section is not available anymore. Please use the main Exam Page.200-901 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.
Requium
Highly Voted 4 years agokylesam2017
Most Recent 7 months agoEAG
7 months agomacxsz
2 years agoEng_H
2 years, 2 months agobamosk
2 years, 2 months agodesignated
2 years, 4 months agoshoother
2 years, 6 months agobluesky2022
2 years, 7 months agokogunribido
2 years, 9 months agoMr_Kasanova1911
2 years, 9 months agosnnaga
3 years agoalahnomi
3 years, 4 months agomoslig
3 years, 6 months agowilie
3 years, 8 months agoNitinPathak
3 years, 9 months agoNJA
3 years, 9 months ago