25 Unique MCQ Question on Classes and Objects
1. Which keyword is used to prevent inheritance of a class in Java?
a. sealed
b. final
c. private
d. restrict
2. In Java, what is the purpose of the toString() method?
a. Converts an object to an integer
b. Converts an object to a string representation
c. Defines a new class
d. Deletes the object
3. What is the purpose of the 'super' keyword in Java when referring to a method or variable?
a. Calls the superclass method or variable
b. Calls the current class method or variable
c. Calls a static method
d. Refers to the current object
4. In Java, which access modifier is stricter than 'private'?
a. protected
b. package-private
c. public
d. static
5. How is a constant variable defined in a Java class?
a. Using the const keyword
b. By declaring it inside a method
c. Using final keyword and uppercase letters with underscores
d. Constants are not allowed in Java classes
6. Which keyword is used to declare an interface in Java?
a. interface
b. implements
c. class
d. extends
7. In Java, what is the purpose of the static keyword in a method or variable?
a. It allows access only within the same class
b. It can be called without creating an instance of the class
c. It is used for method overloading
d. It prevents inheritance
8. What is the significance of the 'this' keyword in Java?
a. Refers to the current instance of the class
b. Refers to the superclass
c. Represents a global variable
d. Defines a new object
9. In Java, what is the purpose of the @Override annotation?
a. Marks a method as static
b. Overrides a method from the superclass
c. Implements a new method
d. Denotes a constant variable
10. What does the term 'encapsulation' refer to in Java?
a. Combining different data types
b. Hiding the implementation details
c. Storing data in a class
d. Allowing access to all class members
11. Which type of inheritance involves a class inheriting from more than one class in Java?
a. Single Inheritance
b. Multiple Inheritance
c. Hierarchical Inheritance
d. Multilevel Inheritance
12. What is the role of the 'instanceof' operator in Java?
a. Checks if two objects are the same
b. Compares the values of two objects
c. Checks if an object is an instance of a class
d. Checks for class inheritance
13. In Java, what is the purpose of the final keyword when applied to a class?
a. It prevents the instantiation of the class
b. It allows the class to be inherited
c. It marks a class as abstract
d. It denotes a constant variable
14. What is the purpose of the 'enum' keyword in Java?
a. It defines an interface
b. It declares a constant variable
c. It represents an enumeration of values
d. It denotes an abstract class
15. In Java, what is the purpose of the throws keyword in a method signature?
a. It declares checked exceptions that a method might throw
b. It specifies the return type of the method
c. It indicates that the method is abstract
d. It denotes a static method
16. What is the role of the 'abstract' keyword in Java?
a. Marks a method as final
b. Allows instantiation of the class
c. Denotes a constant variable
d. Declares an abstract class or method
17. Which keyword is used to create an object of a class in Java?
a. create
b. new
c. instantiate
d. object
18. In Java, what is the purpose of the instanceof operator?
a. Checks if two objects are the same
b. Compares the values of two objects
c. Checks if an object is an instance of a class
d. Checks for class inheritance
19. Which of the following is a valid way to call a superclass method in Java?
a. super.method()
b. this.method()
c. Subclass.method()
d. parent.method()
20. What is the purpose of the default keyword in Java interfaces?
a. It defines a default implementation of a method
b. It specifies a default value for a variable
c. It marks a method as non-accessible
d. It denotes an abstract method
21. In Java, what does the protected access modifier allow?
a. Access within the same class only
b. Access within the same package and its subclasses
c. Access from any class
d. No access is allowed
22. Which of the following statements is true regarding the 'try-catch' block in Java?
a. It must be used for all methods
b. It is used to handle checked exceptions
c. It is optional and can be omitted
d. It is only used for loops
23. What is the purpose of the 'transient' keyword in Java?
a. It marks a variable as non-transient
b. It prevents serialization of a variable
c. It allows serialization of a variable
d. It denotes a constant variable
24. In Java, what is the purpose of the interface keyword?
a. It defines a class
b. It declares an interface
c. It represents an enumeration of values
d. It declares a constant variable
25. Which of the following is true regarding the 'this' keyword in Java?
a. Refers to the current instance of the class
b. Refers to the superclass
c. Represents a global variable
d. It is used to create a new instance of the class
1. b.
2. b.
3. a.
4. a.
5. c.
6. b.
7. b.
8. a.
9. b.
10. b.
11. b.
12. c.
13. a.
14. c.
15. a.
16. d.
17. b.
18. c.
19. a.
20. a.
21. b.
22. b.
23. b.
24. b.
25. a.
Comments
Post a Comment