Java Quiz: Constructors and Methods

Java Quiz: Constructors and Methods

Time left: 10:00

1. What is JavaScript?

  • A special method to create instances of classes
  • A method used for mathematical calculations
  • A method to perform string manipulations
  • An exception handling mechanism

2. What is the purpose of the "this" keyword in Java?

  • To refer to the current object instance
  • To refer to a static member
  • To refer to a superclass
  • None of the above

3. Which of the following statements about constructors is true?

  • Constructors can be called directly like other methods
  • Constructors do not have a return type
  • Constructors can have any name
  • Constructors cannot be overloaded

4. What is method overloading?

  • Creating multiple methods with the same name but different parameters
  • Creating multiple methods with the same name and same parameters
  • Creating multiple methods with different names but same parameters
  • None of the above

5. What is the default constructor?

  • A constructor provided by the programmer
  • A constructor with parameters
  • A constructor that initializes objects to zero
  • A constructor automatically provided by the compiler if no constructor is defined

6. What does the super() keyword do in Java?

  • Calls the constructor of the current class
  • Calls the constructor of the superclass
  • Calls a static method
  • None of the above

7. Can a constructor be private in Java?

  • Yes
  • No
  • Only in abstract classes
  • Only in interfaces

8. What is a copy constructor in Java?

  • A constructor that copies data from one object to another
  • A constructor that copies data from a file
  • A constructor that copies data from a database
  • None of the above

9. Can a constructor call another constructor of the same class?

  • Yes, using the this() keyword
  • No, it is not allowed
  • Only if the constructor is private
  • Only if the constructor is protected

10. What happens if a constructor has a return type?

  • It will be treated as a method
  • It will throw a compile-time error
  • It will throw a runtime error
  • None of the above