CodeHS Programming Language Accreditations 3 — Questions and Answers
Question 1: In Java, what is the difference between `==` and `.equals()` when comparing String objects?
- `==` compares content; `.equals()` compares references
- `==` compares references; `.equals()` compares content (Correct answer)
- Both compare content
- Both compare references
Correct answer: `==` compares references; `.equals()` compares content
`==` checks if two references point to the same object, while `.equals()` checks if the string contents are the same.
Question 2: Which HTML tag is used to link an external CSS stylesheet?
- <style>
- <css>
- <link> (Correct answer)
- <script>
Correct answer: <link>
The `<link rel="stylesheet" href="...">` tag in the `<head>` connects an external CSS file.
Question 3: In Python, what is a lambda function?
- A function that calls itself recursively
- A built-in function for math operations
- An anonymous single-expression function (Correct answer)
- A function stored in a list
Correct answer: An anonymous single-expression function
A lambda is a small anonymous function defined with the `lambda` keyword that can have any number of arguments but only one expression.
Question 4: What is the purpose of the `break` statement in a loop?
- Skips the current iteration and continues to the next
- Exits the loop immediately (Correct answer)
- Pauses the loop temporarily
- Restarts the loop from the beginning
Correct answer: Exits the loop immediately
`break` terminates the nearest enclosing loop immediately.
Question 5: In JavaScript, which method adds one or more elements to the END of an array?
- unshift()
- concat()
- push() (Correct answer)
- append()
Correct answer: push()
`push()` appends elements to the end of an array and returns the new length.
Question 6: Which Java collection class allows duplicate elements and maintains insertion order?
- HashSet
- TreeSet
- HashMap
- ArrayList (Correct answer)
Correct answer: ArrayList
`ArrayList` is a resizable array implementation that allows duplicates and preserves insertion order.
Question 7: In Python, what does the `global` keyword do inside a function?
- Creates a new global variable automatically
- Declares that a variable refers to the global scope (Correct answer)
- Prevents a variable from being modified
- Copies the global variable into local scope
Correct answer: Declares that a variable refers to the global scope
The `global` keyword tells Python to use the variable defined in the global (module) scope rather than creating a local one.
In Java, what is the difference between `==` and `.equals()` when comparing String objects?