But some (admittedly junior) developers I've spoken to said that there was so much concern about IP theft they weren't allowed to look at other peoples' code (I assume Stack Overflow was an exception).
I think the standards in academia are quite different than those in the workplace, primarily because the expected outcomes are quite different.
In academia, the instructors are attempting to tease out your understanding of the underlying concepts. Cutting and pasting even two lines of code from an online source doesn't show the instructor that you understand the concept.
In development, snipping well-written pieces of code is an efficient use of time, assuming the programmer is competent enough to understand the code they just snipped. We reuse common libraries for the same reason; once you solve a problem with a testable, proven solution, it's not a good use of time to have every developer redo that solution.
The main caveat, as illustrated in another comment in this thread, is the issue of licensing. Although I certainly have snipped from good stackoverflow answers, the times I build my own libraries from scratch I often crib from well-proven open source solutions with permissive licenses instead of rolling my own. It should be standard practice (and I recognize that I'm preaching to myself as much as anyone else) to reference those snips, not only for IP integrity but to provide a trail of breadcrumbs for your future self (or anyone else investigating your code) as to where that code came from and the rationale for it.
I think that is only an issue in education where you have simple homework whose best solution you can usually pick from any library. Which is why you usually do such stuff in pseudo-code rather than a real programming language.
In the real world. Guava (google java collection libs) have tons of code that is virtually identical to other libraries. The thing is very often you re-implement the same thing to not have another dependency for some small thing.
Also usually you use some library or framework to implement most of the things often found in student homework. You simply should not write your own code for such basic things but use a library, that does it for you in clean and tested code.
When the task from your professor says do not use java.util, this may have educational purpose (when the aim of the game is to create your own HashMap implementation) but it is an antipattern in actual programming. Do it when you make your own library like a immutable version or like the koloboke compact version. But those derivations have a purpose you don't just make you own HashMap implementation so as not to infringe on some copyright.
Your example mainly focus on the usage of existing libraries. If you write and aws lamdba function and use the aws sdk, you often can just copy an example and just change the params, without referencing the source of your example snippet. There is no copyright issue IMO. There is often a right way and a wrong way and the right way is the cleanest, most concise way. A lambda are just maybe 20 lines of JS code and often 90% of those 20 lines is copy paste. Don't write your own original spaghetti code, you can write a more concise and cleaner version just please share it. An instructor that does not clearly teach that imo isn't a good coding instructor.
The link isn't a good example, or maybe it's the exception that proves the rule. XOR/shift is a well-known technique for hashing and I wouldn't expect an experienced engineer to need to cite it, unless they copied the specific constants.
The question author is a student and thus the expectation to cite is increased, unless they independently came up with the idea.
There are no hard-and-fast rules, but the more the example is a straightforward application of unknown (or forgotten) knowledge, the more unsurprising that knowledge is, the less I feel the need to cite. To take it back to the OP's example, once you know that subtracting two datetime objects yields a timedelta object, it becomes obvious to take the result's .total_seconds() (if that's something useful to you). Conversely, my older code is littered with citations for things that are obvious and second-nature to me now but weren't at the time.
The link provided here is an incredibly bad example to the argument. The answers to the question detail how the poster actually plagiarized. Not only in two lines of code, but in other parts as well. As long as the total line count was not 10, moss would not show plagiarism %40 and reduce it to %20 after hash function gets removed. Just read the answers to the post.
But some (admittedly junior) developers I've spoken to said that there was so much concern about IP theft they weren't allowed to look at other peoples' code (I assume Stack Overflow was an exception).
And this link https://academia.stackexchange.com/questions/100081/does-two... is about someone who was accused of plagiarism because of two lines of code.