-
What will the following code snippet work? If not, what will be the error?
function test(p)
{
for (; p.next; p = p.next) ;
return p;
}
-
- No, this will not iterate
- No, this will result in a runtime error with the message “Cannot use Linked List”
- No, this will throw an exception as only numerics can be used in a for loop
- Yes, this will work
- None of these
Correct Option: D
The above code uses a for loop to traverse a linked list data structure and return the last object in the list. This will perfectly work.