Methods in Java — Concept Check
Questions on Methods in Java. Define and call methods, understand pass-by-value semantics, method overloading, varargs, static vs instance, and recursion.
Questions in this quiz
7 questions on Methods in Java — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In Java, what happens when you try to change a primitive value inside a method?
- AA copy of the value is changed
- BThe primitive is converted to an object
- CThe original value is changed
- DThe method throws an error
2.Given the method call `tryReplaceRef(sb)` where `sb` is a `StringBuilder`, what will be the output after the method execution?
- AThe original `StringBuilder` is replaced
- BThe method throws a `NullPointerException`
- CThe original `StringBuilder` is unchanged
- DA new `StringBuilder` is created in the caller
3.A method is overloaded with different parameter types. How does Java determine which method to call?
- ABased on the argument types
- BBased on the method body
- CBased on the return type
- DBased on the method name
4.What is a key requirement for a recursive method to avoid a `StackOverflowError`?
- AIt must have a base case
- BIt must use varargs
- CIt must be static
- DIt must not call itself
5.Why does the method `tryChangeRef` affect the `StringBuilder` object outside the method?
- AThe method changes the reference
- BThe method creates a new object
- CThe method mutates the object
- DJava passes objects by reference
6.What will be the output of `sum(1, 2, 3, 4)` given the `sum` method uses varargs?
- A10
- B1234
- C0
- DAn error
7.Which statement is true about method overloading in Java?
- AOverloading requires the same parameter types
- BOverloading requires different method names
- COverloading is resolved at runtime
- DOverloading allows methods with the same name but different parameters