MCQs On Class 21 Object-Oriented Programming (OOP) - Part 3

Rashmi Mishra
0

MCQs On Class 21

Object-Oriented Programming (OOP) - Part 3

Part 1: Basic Concepts of Constructors and Destructors

  1. What is a constructor in PHP?
    • A) A function to print values
    • B) A function to initialize object properties
    • C) A function to end the object lifecycle
    • D) A function to handle exceptions
      Answer: B
  2. What symbol is used for defining a constructor in PHP?
    • A) __construct
    • B) __init
    • C) constructor
    • D) init
      Answer: A
  3. In PHP, what symbol is used to define a destructor?
    • A) __delete
    • B) __destroy
    • C) __destruct
    • D) destruct
      Answer: C
  4. Which of the following statements about constructors is true?
    • A) They can only be called explicitly.
    • B) They initialize object properties.
    • C) They destroy object properties.
    • D) They are not part of PHP.
      Answer: B
  5. Which of the following statements about destructors is true?
    • A) They are used to print values.
    • B) They initialize object properties.
    • C) They clean up resources.
    • D) They create new properties.
      Answer: C
  6. What is the primary purpose of a destructor in PHP?
    • A) To initialize object properties
    • B) To destroy the PHP interpreter
    • C) To perform cleanup tasks
    • D) To declare variables
      Answer: C
  7. How many constructors can a PHP class have?
    • A) Only one
    • B) Two
    • C) Unlimited
    • D) None
      Answer: A
  8. How many destructors can a PHP class have?
    • A) Only one
    • B) Two
    • C) Unlimited
    • D) None
      Answer: A
  9. What keyword is used to call the constructor of the parent class in PHP?
    • A) super
    • B) this
    • C) parent
    • D) construct
      Answer: C
  10. What keyword is used to refer to the current instance of the class in PHP?
    • A) this
    • B) parent
    • C) self
    • D) construct
      Answer: A

Part 2: Constructor and Destructor Usage

  1. Which PHP method is automatically called when an object is created?
    • A) __construct
    • B) initialize
    • C) __init__
    • D) start
      Answer: A
  2. Which PHP method is automatically called when an object is destroyed?
    • A) destroy
    • B) __destruct
    • C) terminate
    • D) end
      Answer: B
  3. Can constructors in PHP accept parameters?
    • A) No
    • B) Yes, but only one parameter
    • C) Yes, multiple parameters are allowed
    • D) Only optional parameters
      Answer: C
  4. What happens when an object with a destructor is unset in PHP?
    • A) The destructor is called automatically
    • B) The destructor is ignored
    • C) PHP throws an error
    • D) The destructor must be called manually
      Answer: A
  5. What is the role of the unset function in relation to destructors?
    • A) It skips the destructor
    • B) It ends the PHP script
    • C) It triggers the destructor
    • D) It calls the constructor
      Answer: C
  6. In PHP, what is the output if an error occurs during destructor execution?
    • A) An exception is thrown
    • B) The destructor is terminated
    • C) A warning message is shown
    • D) The destructor ignores the error
      Answer: C
  7. If a PHP class does not have a destructor, what happens when the object is destroyed?
    • A) PHP shows an error
    • B) PHP calls a default destructor
    • C) PHP automatically calls the constructor
    • D) PHP does nothing
      Answer: B
  8. In PHP, can a constructor return a value?
    • A) Yes
    • B) No
    • C) Only if it is numeric
    • D) Only if it is a string
      Answer: B
  9. What is the purpose of the parent::__construct in PHP?
    • A) To define a default constructor
    • B) To call the parent class's constructor
    • C) To call a static function
    • D) To destroy the object
      Answer: B
  10. When a PHP object is destroyed, which of the following occurs first?
    • A) Properties are deleted
    • B) Destructor is called
    • C) Object is reset
    • D) Class variables are deleted
      Answer: B

Part 3: Constructor and Destructor in Inheritance

  1. If a child class does not define a constructor, what happens?
    • A) The parent constructor is called
    • B) PHP throws an error
    • C) A default constructor is used
    • D) The child class fails to initialize
      Answer: A
  2. Can a child class in PHP override the parent constructor?
    • A) Yes
    • B) No
    • C) Only if the constructor has no parameters
    • D) Only if the constructor is private
      Answer: A
  3. Which of these can replace the constructor in a child class if parent::__construct is not called?
    • A) A default PHP constructor
    • B) A manually defined constructor
    • C) A static function
    • D) An exception
      Answer: B
  4. Can a parent constructor be called explicitly from a child class in PHP?
    • A) Yes, using parent::__construct()
    • B) No, it’s automatically called
    • C) Only if it's a static method
    • D) Only if parameters are omitted
      Answer: A
  5. In PHP, what will happen if both parent and child classes have destructors?
    • A) Only the child destructor is called
    • B) Both destructors are called
    • C) PHP throws an error
    • D) Only the parent destructor is called
      Answer: B

Part 4: Inheritance and Constructor/Destructor Relationships

  1. What keyword in PHP allows a class to inherit the constructor of its parent class?
    • A) inherit
    • B) extends
    • C) implements
    • D) constructor
      Answer: B
  2. If a class does not have a constructor and it inherits from a parent class with a constructor, what happens?
    • A) PHP ignores the parent constructor
    • B) PHP automatically calls the parent constructor
    • C) PHP throws an error
    • D) PHP creates a default constructor
      Answer: B
  3. If a child class has its own constructor, what must you do to also call the parent constructor?
    • A) Use parent::__construct()
    • B) Use this::parent()
    • C) Use __construct()
    • D) No additional steps are needed
      Answer: A
  4. In PHP, can you have both a parent and a child destructor in a class hierarchy?
    • A) No, only one is allowed
    • B) Yes, both can be defined and called
    • C) Yes, but only the child destructor is used
    • D) No, only the parent destructor is used
      Answer: B
  5. What will happen if a child class in PHP does not call the parent destructor?
    • A) The parent destructor is ignored
    • B) PHP automatically calls it
    • C) PHP throws an error
    • D) Only the child destructor is executed
      Answer: B

Part 5: Constructor Overloading

  1. Which of the following is true about constructor overloading in PHP?
    • A) PHP supports multiple constructors with different parameters
    • B) Only one constructor is allowed per class
    • C) PHP automatically selects a constructor based on arguments
    • D) Constructor overloading is required in PHP
      Answer: B
  2. How can constructor overloading be simulated in PHP?
    • A) By defining multiple constructors
    • B) By using default arguments in a single constructor
    • C) By defining a method called __construct2
    • D) Constructor overloading is not possible in PHP
      Answer: B
  3. If a PHP constructor accepts default values, how many arguments can be passed?
    • A) Exactly one
    • B) Only as many as the default values allow
    • C) It varies based on parameters passed
    • D) All arguments must be specified
      Answer: C
  4. What is the advantage of using default values in constructors in PHP?
    • A) It prevents errors
    • B) It allows flexibility in instantiating objects
    • C) It restricts constructor usage
    • D) It simplifies inheritance
      Answer: B
  5. What happens if a child class does not define a constructor but the parent class has one with parameters?
    • A) An error occurs
    • B) The child must pass arguments during instantiation
    • C) The constructor is ignored
    • D) PHP uses default values
      Answer: B

Part 6: Destructor Behavior and Lifecycle

  1. Which of the following is true about destructors?
    • A) Destructors are called when the script finishes
    • B) Destructors must be manually called
    • C) Destructors cannot free resources
    • D) Destructors are used to define class properties
      Answer: A
  2. When does the destructor run in a PHP script?
    • A) Immediately after the constructor
    • B) When the object is no longer referenced
    • C) Before the constructor
    • D) When explicitly called
      Answer: B
  3. What happens if you try to use unset() on an object without a destructor in PHP?
    • A) PHP throws an error
    • B) The object is destroyed without cleanup
    • C) PHP creates a destructor automatically
    • D) The object stays in memory
      Answer: B
  4. If a destructor in PHP has side effects, which of the following is true?
    • A) It should be avoided
    • B) It must be explicitly logged
    • C) It will execute even if an error occurs
    • D) It is skipped during exceptions
      Answer: C
  5. In which situation might a PHP destructor be skipped?
    • A) During memory cleanup
    • B) If the script terminates unexpectedly
    • C) If it lacks side effects
    • D) During object initialization
      Answer: B

Part 7: Practical Application and Examples

  1. Why might you use a constructor in a PHP class?
    • A) To set initial values for properties
    • B) To override methods
    • C) To destroy the class instance
    • D) To close the PHP script
      Answer: A
  2. What is a common use for a destructor in PHP?
    • A) Logging object deletion
    • B) Resetting properties
    • C) Initializing object properties
    • D) Fetching new data
      Answer: A
  3. When should you call a destructor explicitly in PHP?
    • A) Never; destructors are called automatically
    • B) At the start of the script
    • C) Right after the constructor
    • D) When you need to create a new object
      Answer: A
  4. If a class has a constructor but no destructor, what happens when the object is destroyed?
    • A) PHP throws an error
    • B) PHP automatically cleans up
    • C) PHP skips the destructor
    • D) PHP creates a default destructor
      Answer: B
  5. Can a PHP constructor call another method within the class?
    • A) Yes
    • B) No
    • C) Only if it’s static
    • D) Only if it’s private
      Answer: A

Part 8: Special Cases and Error Handling

  1. What happens if a destructor contains an error?
    • A) PHP ignores it
    • B) PHP terminates the script
    • C) PHP shows a warning and continues
    • D) PHP skips other destructors
      Answer: C
  2. What will happen if a destructor throws an exception in PHP?
    • A) PHP terminates the script
    • B) PHP ignores the exception
    • C) PHP converts it to a warning
    • D) PHP handles it and continues
      Answer: A
  3. Is it possible for a constructor to fail in PHP?
    • A) Yes, if there is an error in the code
    • B) No, constructors cannot fail
    • C) Only if the destructor also fails
    • D) Only if PHP version is outdated
      Answer: A
  4. What function can be used to handle exceptions in constructors?
    • A) try-catch
    • B) finally
    • C) throw
    • D) unset()
      Answer: A
  5. Can you directly call __destruct()?
    • A) Yes
    • B) No, destructors are automatic
    • C) Only if the destructor is public
    • D) Only with specific classes
      Answer: B


Part 9: Real-World Applications

  1. In a database connection class, how would you typically use a constructor?
    • A) To execute queries
    • B) To establish a connection to the database
    • C) To close the connection
    • D) To handle errors
      Answer: B
  2. Why might a destructor be useful in a database connection class?
    • A) To log queries
    • B) To close the database connection
    • C) To initialize the connection
    • D) To handle user input
      Answer: B
  3. In a logging class, what would be a good use for a constructor?
    • A) To set up log file paths
    • B) To write log entries
    • C) To format log messages
    • D) To display logs to the user
      Answer: A
  4. What could be a potential drawback of using destructors to handle important cleanup tasks?
    • A) They are executed automatically
    • B) They might not run if the script exits unexpectedly
    • C) They can be called multiple times
    • D) They cannot handle exceptions
      Answer: B
  5. How can you ensure that a destructor runs in a PHP web application?
    • A) Use exit() at the end of the script
    • B) Avoid using die() in the script
    • C) Make sure the object remains in scope
    • D) Always use unset() before the end of the script
      Answer: C

Part 10: Common Scenarios and Pitfalls

  1. What will happen if you try to use an object after it has been destructed?
    • A) PHP will recreate the object
    • B) It will cause an error
    • C) The object will still be accessible
    • D) PHP ignores the object
      Answer: B
  2. Which method would you use to handle multiple resources within a class in PHP?
    • A) A single destructor for cleanup
    • B) Multiple constructors
    • C) Separate methods for each resource
    • D) Use static methods
      Answer: A
  3. When is the best time to implement cleanup code in your PHP classes?
    • A) In the constructor
    • B) In the destructor
    • C) At the start of the script
    • D) Inside methods
      Answer: B
  4. If you have multiple objects of a class, when will the destructor be called for each object?
    • A) At the end of the script
    • B) When the object goes out of scope
    • C) Only when explicitly called
    • D) Immediately after instantiation
      Answer: B
  5. What will happen if a class has a constructor that accepts parameters, but you instantiate it without parameters?
    • A) An error occurs
    • B) PHP uses default values
    • C) The constructor is ignored
    • D) It creates a new object
      Answer: A

Part 11: Testing and Debugging

  1. How can you check if a destructor was called in PHP?
    • A) Use print statements
    • B) Set a flag variable in the destructor
    • C) Log the call to a file
    • D) All of the above
      Answer: D
  2. What should you avoid doing in a destructor to ensure predictable behavior?
    • A) Closing files
    • B) Calling external APIs
    • C) Setting properties
    • D) Releasing resources
      Answer: B
  3. In debugging a PHP class, what could indicate that a destructor is not functioning as intended?
    • A) Memory leaks
    • B) Objects not being instantiated
    • C) Constructors being skipped
    • D) PHP error logs
      Answer: A
  4. What PHP function can you use to trigger a destructor manually for debugging?
    • A) __destruct()
    • B) unset()
    • C) die()
    • D) exit()
      Answer: B
  5. What happens if you forget to call parent::__construct() in a child class?
    • A) The parent constructor is called anyway
    • B) The child constructor runs but the parent constructor logic is skipped
    • C) An error occurs
    • D) It works as expected
      Answer: B

Part 12: Advanced Topics

  1. Which of the following can you use to invoke a destructor in an object-oriented design?
    • A) By directly calling the destructor
    • B) By using unset() on the object
    • C) By calling a method within the class
    • D) None of the above
      Answer: B
  2. What will happen to a destructor if you have a circular reference in PHP?
    • A) The destructor will be called multiple times
    • B) The destructor may not run
    • C) PHP will automatically resolve the reference
    • D) An error will be thrown
      Answer: B
  3. In which scenario would you need to override a destructor?
    • A) When changing variable types
    • B) When managing a different resource than the parent
    • C) If the constructor has changed
    • D) It is never necessary to override destructors
      Answer: B
  4. Can you have static properties or methods in a destructor in PHP?
    • A) Yes, but not common practice
    • B) No, destructors cannot handle static properties
    • C) Yes, it is recommended
    • D) It is mandatory
      Answer: A
  5. When dealing with exception handling in constructors and destructors, which is true?
    • A) Only constructors can throw exceptions
    • B) Destructors cannot throw exceptions
    • C) Both can throw exceptions, but it can complicate cleanup
    • D) Exceptions are ignored in destructors
      Answer: C

Part 13: Best Practices

  1. What is the best practice regarding destructors in PHP?
    • A) Always use them
    • B) Avoid using them unless necessary
    • C) Use them for all classes
    • D) Only use them in complex applications
      Answer: B
  2. In which of the following situations would a destructor be particularly useful?
    • A) Managing database connections
    • B) Performing calculations
    • C) Displaying output to users
    • D) Handling exceptions
      Answer: A
  3. How should you handle resources in a destructor?
    • A) Release them immediately
    • B) Check if they are already released
    • C) Log the release
    • D) You should never handle resources in a destructor
      Answer: B
  4. When designing a class with a destructor, what should you consider?
    • A) The potential for circular references
    • B) Whether resources are released properly
    • C) The timing of object destruction
    • D) All of the above
      Answer: D
  5. Which of the following is a common mistake when using destructors?
    • A) Forgetting to log events
    • B) Trying to access resources that may have already been released
    • C) Not using a destructor at all
    • D) Calling destructors explicitly
      Answer: B

Part 14: Additional Concepts

  1. What is a "finalizer" in the context of PHP?
    • A) A type of destructor
    • B) A method that runs before a constructor
    • C) A method that cleans up resources
    • D) A deprecated feature in PHP
      Answer: A
  2. What does the self keyword refer to in a PHP class?
    • A) The current instance of the class
    • B) The parent class
    • C) The class itself
    • D) All instantiated objects
      Answer: C
  3. In which of the following scenarios should you be cautious about using destructors?
    • A) When managing static properties
    • B) In long-running scripts
    • C) In web applications with short execution times
    • D) Both A and C
      Answer: D
  4. What is the primary role of a constructor?
    • A) To destruct the class
    • B) To initialize the object
    • C) To release resources
    • D) To call other methods
      Answer: B
  5. When writing unit tests for a class with a destructor, what should you ensure?
    • A) The destructor is always called
    • B) Resources are properly released
    • C) The constructor functions correctly
    • D) Both A and B
      Answer: D

Part 15: Review and Self-Assessment

  1. How can you improve the reliability of a destructor?
    • A) By avoiding complex logic
    • B) By logging every action taken
    • C) By throwing exceptions
    • D) By using only static properties
      Answer: A
  2. What is the difference between a destructor and a finalizer?
    • A) There is no difference
    • B) A destructor is specific to classes; a finalizer is for resources
    • C) A finalizer runs before a destructor
    • D) A destructor can be called manually; a finalizer cannot
      Answer: B
  3. Why is it important to manage memory effectively in PHP?
    • A) PHP has unlimited memory
    • B) To prevent memory leaks and optimize performance
    • C) Memory management is not a concern in PHP
    • D) To reduce code complexity
      Answer: B
  4. Which design pattern often uses constructors and destructors?
    • A) Singleton Pattern
    • B) Observer Pattern
    • C) Factory Pattern
    • D) Strategy Pattern
      Answer: A
  5. What should you document when working with constructors and destructors?
    • A) Parameters used
    • B) Resource allocation
    • C) Any exceptions thrown
    • D) All of the above
      Answer: D

Part 16: Advanced Scenarios

  1. In what situation would you use a destructor to free external resources?
    • A) When using PHP scripts without a database
    • B) When connecting to APIs or external services
    • C) Only when required by the operating system
    • D) It is never required
      Answer: B
  2. How can you verify if an object's destructor has been called in a complex application?
    • A) By checking the application logs
    • B) By using a debugging tool
    • C) By setting flags in the destructor
    • D) All of the above
      Answer: D
  3. What should you consider when designing a class hierarchy with constructors and destructors?
    • A) Proper chaining of constructors
    • B) Consistent behavior of destructors across classes
    • C) Resource management in the hierarchy
    • D) All of the above
      Answer: D
  4. Can a destructor be inherited in PHP?
    • A) Yes, it can be overridden
    • B) No, destructors cannot be inherited
    • C) Yes, but only in abstract classes
    • D) No, they must be defined in each class
      Answer: A
  5. How does PHP handle object destruction at the end of a script execution?
    • A) Automatically calls destructors for all objects
    • B) Ignores objects that are out of scope
    • C) Runs destructors in a random order
    • D) Executes destructors only for global objects
      Answer: A

Part 17: Review and Final Assessment

  1. What is the impact of using a destructor that performs complex logic?
    • A) It enhances performance
    • B) It can lead to unpredictable behavior
    • C) It simplifies code readability
    • D) It is generally recommended
      Answer: B
  2. What should be your first action when debugging a destructor that doesn’t seem to work?
    • A) Check for circular references
    • B) Print debugging statements
    • C) Review the constructor
    • D) All of the above
      Answer: D
  3. How can you prevent resource leaks when using destructors?
    • A) Regularly check for memory usage
    • B) Properly manage and release resources in the destructor
    • C) Always use global variables
    • D) Avoid using resources altogether
      Answer: B
  4. In PHP, what would happen if an object with a destructor goes out of scope?
    • A) The destructor will not be called
    • B) The destructor will be called automatically
    • C) An error will occur
    • D) The object is completely removed from memory
      Answer: B
  5. Why might you want to log events within a destructor?
    • A) To track resource usage
    • B) For debugging purposes
    • C) To ensure the destructor was executed
    • D) All of the above
      Answer: D

Part 18: Conclusion and Self-Evaluation

  1. What is the primary purpose of a destructor in PHP?
    • A) To create instances of a class
    • B) To initialize class properties
    • C) To clean up resources before an object is destroyed
    • D) To call other methods
      Answer: C
  2. How should you handle exceptions in destructors?
    • A) Catch and log them
    • B) Throw them again
    • C) Ignore them
    • D) Convert them to error messages
      Answer: A
  3. What can you do if an object does not have a destructor defined?
    • A) PHP will automatically manage resources
    • B) You cannot instantiate the object
    • C) You should manually release resources
    • D) An error will occur
      Answer: A
  4. How can inheritance affect the behavior of constructors and destructors?
    • A) Child classes must define their own constructors
    • B) Parent class destructors are automatically inherited
    • C) Child classes can override parent constructors
    • D) Both B and C
      Answer: D
  5. What is the key takeaway regarding the use of constructors and destructors in PHP?
    - A) They are optional for every class
    - B) They are critical for proper resource management
    - C) Only constructors are important
    - D) Destructors are only used in complex applications
    Answer: B


Tags

Post a Comment

0Comments

Post a Comment (0)