Search Tutorials


Java Classes and Objects MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Classes and Objects MCQ - Multiple Choice Questions And Answers

Q. What is the purpose of a constructor in Java?

A. To initialize variables
B. To create objects
C. To destroy objects
D. To define methods

Q. What does the 'new' keyword do in Java?

A. Creates a new method
B. Allocates memory for an object
C. Deletes an object
D. Calls a static method

Q. Which keyword is used to inherit a class in Java?

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

Q. Can a Java class extend multiple classes?

A. Yes
B. No

Q. What is the difference between an object and a class in Java?

A. An object is created from a class
B. A class is created from an object
C. An object defines behavior
D. A class defines behavior

Q. What is the default access modifier for class members in Java?

A. private
B. protected
C. public
D. package-private

Q. What is method overloading in Java?

A. Redefining a method with the same name and parameters
B. Defining multiple methods with the same name but different parameters
C. Calling a method from within another method
D. Extending a method to add more functionality

Q. What is method overriding in Java?

A. Redefining a static method
B. Redefining a method with the same name and parameters
C. Defining a method with a different name
D. Hiding a method implementation

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. What is the difference between a class member and an instance member in Java?

A. A class member is static, an instance member is non-static
B. A class member is non-static, an instance member is static
C. A class member is private, an instance member is public
D. A class member is public, an instance member is private

Q. What is the default access modifier in Java for class members if no access modifier is specified?

A. Default
B. Private
C. Public
D. Protected





Q. What is the output of the following Java code snippet?

public class MyClass {
    private int number = 10;
    
    public void display() {
        System.out.println(number);
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        System.out.println(obj.number);
    }
}
A. 10
B. Error: Access Denied
C. 0
D. Compilation Error

Q. Which access modifier makes a class member accessible only within the same package?

A. Private
B. Default
C. Public
D. Protected

Q. What is the correct syntax for declaring a public method named "display" in Java?

A.
public void display() {
    // Method implementation
}
B.
private void display() {
    // Method implementation
}
C.
protected void display() {
    // Method implementation
}
D.
void display() {
    // Method implementation
}

Q. Can a private method in Java be accessed from outside the class it belongs to?

A. Yes
B. No