0% found this document useful (0 votes)
2 views3 pages

Java Method Functions and Concepts

The document contains multiple-choice questions (MCQs) related to Java methods, including method definitions, return types, method overloading, and assertions about methods and constructors. It provides correct answers for each question, highlighting key concepts in Java programming. The document also includes code snippets to illustrate the questions and answers.

Uploaded by

Antariksh Shukla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Method Functions and Concepts

The document contains multiple-choice questions (MCQs) related to Java methods, including method definitions, return types, method overloading, and assertions about methods and constructors. It provides correct answers for each question, highlighting key concepts in Java programming. The document also includes code snippets to illustrate the questions and answers.

Uploaded by

Antariksh Shukla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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!

You might also like