Search Tutorials


Java Methods MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Methods MCQ - Multiple Choice Questions And Answers

Q. Which keyword is used to declare a method in Java?

A. class
B. method
C. int
D. void

Q. Which access modifier is used to declare a method that can only be accessed within the same class?

A. public
B. private
C. protected
D. default

Q. Which method is automatically called when an object of a class is created?

A. main
B. constructor
C. start
D. execute

Q. Which keyword is used to prevent a method from being overridden in Java?

A. final
B. static
C. abstract
D. protected

Q. Which method is used to convert a string to an integer in Java?

A. Integer.parseInt()
B. String.toInt()
C. String.valueOf()
D. Integer.valueOf()

Q. Which method is used to compare two objects for equality in Java?

A. equals()
B. compare()
C. compareTo()
D. ==

Q. Which keyword is used to access the superclass of a class in Java?

A. this
B. super
C. extends
D. implements

Q. Which method is used to find the length of an array in Java?

A. length()
B. size()
C. count()
D. getSize()

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. Which method is used to read input from the user in Java?

A. System.in()
B. readLine()
C. getInput()
D. read()





Q. Which of the following is the correct implementation of a recursive method to calculate the factorial of a number in Java?

A.
public int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}
B.
public int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}
C.
public int factorial(int n) {
    int result = n;
    while (n > 1) {
        result *= (n-1);
        n--;
    }
    return result;
}
D.
public int factorial(int n) {
    if (n == 1) {
        return 1;
    } else {
        return n * factorial(n + 1);
    }
}