Topic: Functions
1. Which of the following defines a valid method in Java?
a) `void 123method() { }`
b) `public static void main() { }`
c) `void methodName() { }`
d) `int methodName { }`
**Answer:** c) `void methodName() { }`
2. What is the return type of a method that does not return any value?
a) `int`
b) `null`
c) `void`
d) `None`
**Answer:** c) `void`
3. What is method overloading in Java?
a) Using the same method name with different return types
b) Using the same method name with different parameter lists
c) Defining multiple classes with same method names
d) Calling a method from within another
**Answer:** b) Using the same method name with different parameter lists
4. Which of the following is true about static methods?
a) They can directly access non-static members
b) They can be called using an object reference only
c) They can be called without creating an object
d) They cannot return a value
**Answer:** c) They can be called without creating an object
5. What will be the output of the following code?
```java
public class Test {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
int result = square(5);
[Link](result);
}
}
```
a) 5
b) 10
c) 25
d) Compilation error
**Answer:** c) 25
***
### Assertion–Reasoning Type MCQs
6. **Assertion (A):** A method in Java can return only one value at a time.
**Reason (R):** Java methods return values using the `return` keyword, which exits the
method immediately.
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is false.
d) A is false, but R is true.
**Answer:** a) Both A and R are true, and R is the correct explanation of A.
7. **Assertion (A):** Method overloading in Java depends on the number and type of
parameters.
**Reason (R):** The return type does not participate in method overloading.
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is false.
d) A is false, but R is true.
**Answer:** a) Both A and R are true, and R is the correct explanation of A.
8. **Assertion (A):** Constructors and methods can have the same name as the class in Java.
**Reason (R):** Constructors have no return type, while methods must declare a return type.
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is false.
d) A is false, but R is true.
**Answer:** a) Both A and R are true, and R is the correct explanation of A.
***
### Conceptual and Applied MCQs
9. What will happen if a method in Java has no `return` statement but declares a return type
other than `void`?
a) It executes normally
b) Returns 0 by default
c) Compilation error occurs
d) Throws a runtime exception
**Answer:** c) Compilation error occurs
10. Given the code below, which output will be printed?
```java
public class Demo {
static void printMessage(String msg) {
[Link]("Hello, " + msg);
}
static void printMessage() {
[Link]("Hello, World!");
}
public static void main(String[] args) {
printMessage("Java");
printMessage();
}
}
```
a) Hello, Java
Hello, World!
b) Hello, Java Java
c) Hello, World! Hello, Java
d) Compilation error
**Answer:** a) Hello, Java
Hello, World!