MCQs On Class 22 - Object-Oriented Programming (OOP) - Part 4

Rashmi Mishra
0

 MCQs on Class 22

 Object-Oriented Programming (OOP)-Part 4

Here are 100 multiple-choice questions (MCQs) focused on Object-Oriented Programming (OOP) concepts, specifically inheritance and interfaces in PHP. Each question includes four options, with the correct answer indicated.

MCQs on Object-Oriented Programming (OOP)

1.   What is inheritance in OOP?

o    A) The ability to create multiple instances of a class

o    B) The ability of a class to use properties and methods of another class

o    C) The ability to define functions inside classes

o    D) The ability to delete class methods

o    Answer: B

2.   Which keyword is used to define a class in PHP?

o    A) class

o    B) object

o    C) define

o    D) new

o    Answer: A

3.   What is the purpose of the extends keyword in PHP?

o    A) To create a new object

o    B) To indicate that a class is inheriting from another class

o    C) To define an interface

o    D) To implement an abstract class

o    Answer: B

4.   What is the visibility of a property or method defined with the protected keyword?

o    A) Accessible only within the class itself

o    B) Accessible within the class and its subclasses

o    C) Accessible from anywhere

o    D) Accessible only within the same package

o    Answer: B

5.   Which of the following is NOT a type of visibility in PHP?

o    A) public

o    B) private

o    C) protected

o    D) local

o    Answer: D

6.   What is an interface in PHP?

o    A) A type of class

o    B) A collection of abstract methods

o    C) A concrete class with implemented methods

o    D) A way to create multiple instances of a class

o    Answer: B

7.   Which of the following statements about interfaces is TRUE?

o    A) A class can implement multiple interfaces.

o    B) An interface can implement another interface.

o    C) Interfaces can have method implementations.

o    D) A class can extend multiple classes.

o    Answer: A

8.   What will happen if a class implements an interface but does not provide implementations for all its methods?

o    A) The program will run without issues.

o    B) The class will generate a fatal error.

o    C) The class will have default implementations.

o    D) The interface will be ignored.

o    Answer: B

9.   In PHP, which of the following is used to call a parent class method from a child class?

o    A) parent::methodName();

o    B) parent.methodName();

o    C) this::methodName();

o    D) super::methodName();

o    Answer: A

10.                     What is the purpose of the final keyword when used with a class in PHP?

o    A) To allow the class to be extended

o    B) To prevent the class from being extended

o    C) To prevent a method from being overridden

o    D) Both B and C

o    Answer: D

11.                     Which of the following is the correct way to implement an interface in a class?

o    A) class MyClass implements MyInterface {}

o    B) class MyClass extends MyInterface {}

o    C) class MyClass uses MyInterface {}

o    D) interface MyInterface implements MyClass {}

o    Answer: A

12.                     What will the output be if a method is declared as private in a parent class and is accessed in a child class?

o    A) The method will be accessible.

o    B) The method will not be accessible.

o    C) The method will throw a warning.

o    D) The method will be inherited as public.

o    Answer: B

13.                     Which of the following statements is true regarding abstract classes in PHP?

o    A) An abstract class cannot have any methods.

o    B) An abstract class can have implemented methods.

o    C) An abstract class cannot be instantiated.

o    D) Both B and C

o    Answer: D

14.                     How do you define an abstract method in an abstract class in PHP?

o    A) public abstract function methodName();

o    B) abstract public function methodName();

o    C) function methodName() abstract;

o    D) Both A and B

o    Answer: D

15.                     What is the primary benefit of using interfaces?

o    A) They allow for method implementation.

o    B) They provide a way to create class hierarchies.

o    C) They enable polymorphism.

o    D) They restrict method visibility.

o    Answer: C

16.                     Can a class inherit from multiple classes in PHP?

o    A) Yes, always.

o    B) No, PHP does not support multiple inheritance.

o    C) Yes, but only if they are interfaces.

o    D) Yes, but only with traits.

o    Answer: B

17.                     What keyword is used to define a constant in a class?

o    A) const

o    B) define

o    C) constant

o    D) static

o    Answer: A

18.                     Which of the following describes the use of traits in PHP?

o    A) Traits are used to implement interfaces.

o    B) Traits allow for code reuse across multiple classes.

o    C) Traits are similar to abstract classes.

o    D) Traits cannot have properties.

o    Answer: B

19.                     What is the result of the following code snippet?

php

Copy code

class A {

    public function test() {

        return "A";

    }

}

class B extends A {

    public function test() {

        return "B";

    }

}

$obj = new B();

echo $obj->test();

o    A) A

o    B) B

o    C) Fatal error

o    D) NULL

o    Answer: B

20.                     Which of the following is an example of polymorphism in OOP?

o    A) Method overriding

o    B) Method overloading

o    C) Interfaces

o    D) All of the above

o    Answer: D

21.                     What will be the output of the following code?

php

Copy code

class Base {

    function show() {

        echo "Base class\n";

    }

}

class Derived extends Base {

    function show() {

        echo "Derived class\n";

    }

}

$obj = new Derived();

$obj->show();

o    A) Base class

o    B) Derived class

o    C) Fatal error

o    D) NULL

o    Answer: B

22.                     What is the purpose of using an abstract class?

o    A) To provide a common base for derived classes

o    B) To implement interfaces

o    C) To enforce method definitions

o    D) Both A and C

o    Answer: D

23.                     How do you declare a constant in a class?

php

Copy code

class MyClass {

    _____ MY_CONSTANT = "value";

}

o    A) const

o    B) define

o    C) static

o    D) constant

o    Answer: A

24.                     What is the primary purpose of an interface?

o    A) To prevent inheritance

o    B) To provide a contract for implementing classes

o    C) To define properties

o    D) To allow multiple inheritance

o    Answer: B

25.                     What will be the output of the following code?

php

Copy code

interface IAnimal {

    public function makeSound();

}

class Dog implements IAnimal {

    public function makeSound() {

        return "Bark!";

    }

}

$dog = new Dog();

echo $dog->makeSound();

o    A) Meow!

o    B) Bark!

o    C) NULL

o    D) Fatal error

o    Answer: B

26.                     Which of the following can an abstract class contain?

o    A) Only abstract methods

o    B) Only concrete methods

o    C) Both abstract and concrete methods

o    D) No methods at all

o    Answer: C

27.                     What will happen if you instantiate an abstract class directly?

o    A) The class will be created successfully.

o    B) It will generate a fatal error.

o    C) The class will be treated as a normal class.

o    D) It will automatically convert to an interface.

o    Answer: B

28.                     In PHP, which of the following is used to include traits in a class?

o    A) use

o    B) include

o    C) import

o    D) require

o    Answer: A

29.                     What keyword is used to define a class in PHP?

o    A) define

o    B) class

o    C) object

o    D) new

o    Answer: B

30.                     Which of the following is NOT a characteristic of interfaces?

o    A) Interfaces can contain properties.

o    B) All methods in an interface are public.

o    C) A class can implement multiple interfaces.

o    D) Interfaces cannot be instantiated.

o    Answer: A

31.                     What does the following code output?

php

Copy code

class A {

    public function greet() {

        return "Hello from A";

    }

}

class B extends A {

    public function greet() {

        return "Hello from B";

    }

}

$obj = new B();

echo $obj->greet();

o    A) Hello from A

o    B) Hello from B

o    C) Fatal error

o    D) NULL

o    Answer: B

32.                     What will the output be for the following code?

php

Copy code

class A {

    protected $value = 10;

}

class B extends A {

    public function getValue() {

        return $this->value;

    }

}

$obj = new B();

echo $obj->getValue();

o    A) 10

o    B) Fatal error

o    C) NULL

o    D) Undefined property

o    Answer: A

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

o    A) A constructor must be declared public.

o    B) A constructor can be overridden in a child class.

o    C) A class can have multiple constructors.

o    D) Constructors cannot accept parameters.

o    Answer: B

34.                     What will happen if a class does not define a constructor?

o    A) A default constructor is automatically created.

o    B) The class cannot be instantiated.

o    C) The class will generate a fatal error.

o    D) It will only have private properties.

o    Answer: A

35.                     What is the output of the following code?

php

Copy code

class A {

    public function message() {

        return "Hello from A";

    }

}

class B extends A {

    public function message() {

        return parent::message() . " and B";

    }

}

$obj = new B();

echo $obj->message();

o    A) Hello from A

o    B) Hello from B

o    C) Hello from A and B

o    D) Fatal error

o    Answer: C

36.                     Which keyword would you use to prevent a class from being extended?

o    A) static

o    B) final

o    C) private

o    D) abstract

o    Answer: B

37.                     Which of the following describes method overriding?

o    A) When a subclass redefines a method from its parent class

o    B) When a class implements multiple interfaces

o    C) When a method is defined in an interface

o    D) When a method is overloaded

o    Answer: A

38.                     What is the purpose of the implements keyword in PHP?

o    A) To create a new class

o    B) To inherit properties from a parent class

o    C) To indicate that a class is implementing an interface

o    D) To declare an abstract method

o    Answer: C

39.                     What will the following code output?

php

Copy code

interface I {

    public function doSomething();

}

class A implements I {

    public function doSomething() {

        return "Doing something in A";

    }

}

$obj = new A();

echo $obj->doSomething();

o    A) Doing something in A

o    B) NULL

o    C) Fatal error

o    D) Doing something in I

o    Answer: A

40.                     Can an interface contain properties in PHP?

o    A) Yes

o    B) No

o    C) Only static properties

o    D) Only public properties

o    Answer: B

41.                     What is the significance of using interfaces in OOP?

o    A) They provide implementation details.

o    B) They define a contract that implementing classes must follow.

o    C) They allow for direct instantiation.

o    D) They enable multiple inheritance.

o    Answer: B

42.                     What will be the output of the following code?

php

Copy code

class ParentClass {

    public function display() {

        return "Hello from ParentClass";

    }

}

class ChildClass extends ParentClass {

    public function display() {

        return "Hello from ChildClass";

    }

}

$child = new ChildClass();

echo $child->display();

o    A) Hello from ParentClass

o    B) Hello from ChildClass

o    C) Fatal error

o    D) NULL

o    Answer: B

43.                     What is the result of the following code?

php

Copy code

abstract class AbstractClass {

    abstract public function abstractMethod();

}

class ConcreteClass extends AbstractClass {

    public function abstractMethod() {

        return "Implemented!";

    }

}

$obj = new ConcreteClass();

echo $obj->abstractMethod();

o    A) Implemented!

o    B) Fatal error

o    C) NULL

o    D) AbstractMethod not found

o    Answer: A

44.                     Which keyword is used to inherit properties and methods from a parent class?

o    A) include

o    B) extends

o    C) implements

o    D) inherit

o    Answer: B

45.                     In PHP, what is the default visibility of properties and methods if no visibility keyword is specified?

o    A) public

o    B) private

o    C) protected

o    D) None of the above

o    Answer: A

46.                     Which of the following correctly creates an interface in PHP?

php

Copy code

_____ MyInterface {

    public function myMethod();

}

o    A) class

o    B) interface

o    C) abstract class

o    D) trait

o    Answer: B

47.                     What will be the output of the following code?

php

Copy code

class Base {

    public function __construct() {

        echo "Base Constructor\n";

    }

}

class Derived extends Base {

    public function __construct() {

        parent::__construct();

        echo "Derived Constructor\n";

    }

}

$obj = new Derived();

o    A) Base Constructor

o    B) Derived Constructor

o    C) Base Constructor

o    D) Base Constructor\nDerived Constructor

o    Answer: D

48.                     What is the function of the abstract keyword in PHP?

o    A) It defines a method that must be implemented by derived classes.

o    B) It defines a property that cannot be accessed.

o    C) It specifies that a class can be instantiated.

o    D) It specifies that a method is private.

o    Answer: A

49.                     What happens when you try to instantiate an abstract class?

o    A) The class will be instantiated.

o    B) A fatal error will occur.

o    C) It will create an object with default properties.

o    D) It will create an empty object.

o    Answer: B

50.                     Which of the following statements is true about properties in PHP classes?

o    A) All properties are public by default.

o    B) Properties can be declared without visibility.

o    C) Properties must be declared with the public keyword.

o    D) Properties cannot be defined in interfaces.

o    Answer: B

51.                     What is the purpose of using the use keyword in a PHP class?

o    A) To include external files

o    B) To declare namespaces

o    C) To include traits

o    D) To define constants

o    Answer: C

52.                     What is the correct way to define a method in an interface?

o    A) public function methodName();

o    B) function methodName();

o    C) abstract public function methodName();

o    D) All of the above

o    Answer: D

53.                     Which keyword is used to define a method that cannot be overridden?

o    A) final

o    B) static

o    C) abstract

o    D) const

o    Answer: A

54.                     Which of the following best describes an abstract class?

o    A) A class that cannot be inherited

o    B) A class that can have both abstract and concrete methods

o    C) A class with only abstract methods

o    D) A class with no properties

o    Answer: B

55.                     What is the primary purpose of using parent:: in a method?

o    A) To call the constructor of the parent class

o    B) To access properties from the parent class

o    C) To call a method from the parent class

o    D) To create a new instance of the parent class

o    Answer: C

56.                     Which of the following cannot be done in PHP?

o    A) Extend multiple classes

o    B) Implement multiple interfaces

o    C) Use traits in a class

o    D) Create abstract classes

o    Answer: A

57.                     What is the purpose of the __construct() method?

o    A) It is used to initialize properties.

o    B) It is called when a class is instantiated.

o    C) It cannot accept parameters.

o    D) Both A and B

o    Answer: D

58.                     What will the output of the following code be?

php

Copy code

class A {

    public function greet() {

        return "Hello";

    }

}

class B extends A {

    public function greet() {

        return "Hi";

    }

}

class C extends B {

    public function greet() {

        return parent::greet() . ", how are you?";

    }

}

$obj = new C();

echo $obj->greet();

o    A) Hi, how are you?

o    B) Hello, how are you?

o    C) Hi

o    D) Hello

o    Answer: A

59.                     What happens when you declare a method in an abstract class?

o    A) It must be implemented in the same class.

o    B) It can be called without implementation.

o    C) It cannot be called directly.

o    D) It must be static.

o    Answer: C

60.                     Can a method in a child class be made less accessible than the method in its parent class?

o    A) Yes

o    B) No

o    C) Only if it is a static method

o    D) Only if it is a private method

o    Answer: B

61.                     What will be the output of the following code?

php

Copy code

class Base {

    protected $value = "Base Value";

}

class Derived extends Base {

    public function getValue() {

        return $this->value;

    }

}

$obj = new Derived();

echo $obj->getValue();

o    A) Fatal error

o    B) Base Value

o    C) NULL

o    D) Undefined property

o    Answer: B

62.                     Which of the following statements is true about interfaces?

o    A) They can contain concrete methods.

o    B) They can extend other interfaces.

o    C) They can implement other interfaces.

o    D) They can have constructors.

o    Answer: B

63.                     What is the output of the following code?

php

Copy code

interface I {

    public function methodA();

}

interface J {

    public function methodB();

}

class C implements I, J {

    public function methodA() {

        return "Method A";

    }

    public function methodB() {

        return "Method B";

    }

}

$obj = new C();

echo $obj->methodA() . " and " . $obj->methodB();

o    A) Method A

o    B) Method B

o    C) Method A and Method B

o    D) Fatal error

o    Answer: C

64.                     What will be the output of the following code?

php

Copy code

abstract class Animal {

    abstract public function sound();

}

class Dog extends Animal {

    public function sound() {

        return "Bark";

    }

}

$dog = new Dog();

echo $dog->sound();

o    A) Bark

o    B) Fatal error

o    C) NULL

o    D) sound not defined

o    Answer: A

65.                     What is the visibility of properties declared as private?

o    A) Accessible only within the class

o    B) Accessible in child classes

o    C) Accessible in the same package

o    D) Accessible globally

o    Answer: A

66.                     Which of the following cannot be declared in an interface?

o    A) Methods

o    B) Properties

o    C) Constants

o    D) All of the above can be declared

o    Answer: B

67.                     What will the output be for the following code?

php

Copy code

class A {

    public static function test() {

        return "Hello from A";

    }

}

echo A::test();

o    A) Hello from A

o    B) Fatal error

o    C) NULL

o    D) Hello

o    Answer: A

68.                     What does the final keyword prevent when used in a class?

o    A) It prevents instantiation.

o    B) It prevents method overriding.

o    C) It prevents property declaration.

o    D) It prevents access modifiers.

o    Answer: B

69.                     What is true about constants in a class?

o    A) They cannot be accessed without instantiation.

o    B) They must be declared public.

o    C) They cannot be changed once defined.

o    D) All of the above are true.

o    Answer: C

70.                     What is the correct way to declare a final method in PHP?

o    A) final public function methodName() {}

o    B) public final function methodName() {}

o    C) function final methodName() {}

o    D) final function methodName() {}

o    Answer: A

71.                     What will be the output of the following code?

php

Copy code

class ParentClass {

    public static function staticMethod() {

        return "Static method in ParentClass";

    }

}

class ChildClass extends ParentClass {

    public static function staticMethod() {

        return "Static method in ChildClass";

    }

}

echo ChildClass::staticMethod();

o    A) Static method in ParentClass

o    B) Static method in ChildClass

o    C) Fatal error

o    D) NULL

o    Answer: B

72.                     Can an interface extend multiple interfaces in PHP?

o    A) Yes

o    B) No

o    C) Only if they are related

o    D) Only if they have methods with the same name

o    Answer: A

73.                     What will the output be for the following code?

php

Copy code

interface I {

    public function method();

}

class A implements I {

    public function method() {

        return "Method from A";

    }

}

class B implements I {

    public function method() {

        return "Method from B";

    }

}

$objA = new A();

$objB = new B();

echo $objA->method() . " and " . $objB->method();

o    A) Method from A and Method from B

o    B) Fatal error

o    C) Method from A

o    D) Method from B

o    Answer: A

74.                     Which of the following is a valid reason to use an abstract class instead of an interface?

o    A) To provide common implementation for methods

o    B) To prevent instantiation

o    C) To define abstract methods

o    D) All of the above

o    Answer: D

75.                     What does the keyword protected mean when used in a class property?

o    A) It is accessible only within the class itself.

o    B) It is accessible within the class and its subclasses.

o    C) It is accessible from anywhere.

o    D) It is accessible only in the same file.

o    Answer: B

76.                     What will be the output of the following code?

php

Copy code

class Base {

    public $value = "Base Value";

}

class Derived extends Base {

    public $value = "Derived Value";

}

$obj = new Derived();

echo $obj->value;

o    A) Base Value

o    B) Derived Value

o    C) Fatal error

o    D) NULL

o    Answer: B

77.                     What is the output of the following code?

php

Copy code

class A {

    public function __call($name, $arguments) {

        return "Method {$name} does not exist";

    }

}

$obj = new A();

echo $obj->testMethod();

o    A) testMethod does not exist

o    B) Method testMethod does not exist

o    C) NULL

o    D) Fatal error

o    Answer: B

78.                     Can a class be both abstract and final?

o    A) Yes

o    B) No

o    C) Only in certain cases

o    D) Only if it has no methods

o    Answer: B

79.                     What will the output of the following code be?

php

Copy code

class A {

    public function sayHello() {

        return "Hello";

    }

}

class B extends A {

    public function sayHello() {

        return "Hi";

    }

}

class C extends B {

    public function sayHello() {

        return parent::sayHello() . " there!";

    }

}

$obj = new C();

echo $obj->sayHello();

o    A) Hello there!

o    B) Hi there!

o    C) NULL

o    D) Fatal error

o    Answer: B

80.                     Which of the following is true about static methods in PHP?

o    A) They can be accessed without an object.

o    B) They can access instance properties.

o    C) They must be declared public.

o    D) They cannot be overridden.

o    Answer: A

81.                     What will be the output of the following code?

php

Copy code

class A {

    public static $counter = 0;

    public function increment() {

        self::$counter++;

    }

}

$obj = new A();

$obj->increment();

echo A::$counter;

o    A) 0

o    B) 1

o    C) NULL

o    D) Fatal error

o    Answer: B

82.                     What does the final keyword prevent when used with a class?

o    A) It prevents class instantiation.

o    B) It prevents class inheritance.

o    C) It prevents method overloading.

o    D) It prevents access modifiers.

o    Answer: B

83.                     What will be the output of the following code?

php

Copy code

class A {

    public function display() {

        return "A";

    }

}

class B extends A {

    public function display() {

        return "B";

    }

}

class C extends B {

    public function display() {

        return parent::display() . " C";

    }

}

$obj = new C();

echo $obj->display();

o    A) A C

o    B) B C

o    C) Fatal error

o    D) NULL

o    Answer: B

84.                     Which of the following statements is true about properties in an interface?

o    A) They can be public.

o    B) They can have visibility.

o    C) They cannot be declared.

o    D) They must be static.

o    Answer: C

85.                     What will be the output of the following code?

php

Copy code

class Animal {

    public function speak() {

        return "Animal speaks";

    }

}

class Dog extends Animal {

    public function speak() {

        return "Dog barks";

    }

}

$animal = new Dog();

echo $animal->speak();

o    A) Animal speaks

o    B) Dog barks

o    C) Fatal error

o    D) NULL

o    Answer: B

86.                     What will be the output of the following code?

php

Copy code

class A {

    protected $value = "Value in A";

}

class B extends A {

    public function getValue() {

        return $this->value;

    }

}

$obj = new B();

echo $obj->getValue();

o    A) Value in A

o    B) NULL

o    C) Fatal error

o    D) Undefined property

o    Answer: A

87.                     Can a class implement more than one interface?

o    A) Yes

o    B) No

o    C) Only if they are related

o    D) Only if they are not abstract

o    Answer: A

88.                     What will be the output of the following code?

php

Copy code

interface I {

    public function method();

}

class A implements I {

    public function method() {

        return "Method from A";

    }

}

class B implements I {

    public function method() {

        return "Method from B";

    }

}

$objB = new B();

echo $objB->method();

o    A) Method from A

o    B) Method from B

o    C) Fatal error

o    D) NULL

o    Answer: B

89.                     What is true about visibility in PHP?

o    A) Public methods can be accessed from anywhere.

o    B) Protected methods can be accessed only in child classes.

o    C) Private methods can be accessed only within the class itself.

o    D) All of the above.

o    Answer: D

90.                     What will be the output of the following code?

php

Copy code

class A {

    public function __call($name, $arguments) {

        return "Method {$name} does not exist";

    }

}

class B extends A {

    public function __call($name, $arguments) {

        return "B's Method {$name} does not exist";

    }

}

$obj = new B();

echo $obj->testMethod();

o    A) testMethod does not exist

o    B) Method testMethod does not exist

o    C) B's Method testMethod does not exist

o    D) Fatal error

o    Answer: C

These questions cover various concepts related to OOP in PHP, including classes, inheritance, interfaces, visibility, and more, suitable for a beginner to intermediate level understanding

 

Tags

Post a Comment

0Comments

Post a Comment (0)