0% found this document useful (0 votes)
5 views

Midterm Exam OOP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Midterm Exam OOP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

First Semester

Professional Elective 1
Midterm Exam
2022-2023
Name: ________________________ Course & Year: _____________ Date: ____________ Score: ___
Test I: Multiple Choice
Direction: Read each question carefully. Choose the letter of the correct answer and write it in the provided space.

1. What is the main purpose of a class in Java?

A. To define variables only


B. To encapsulate data and behavior
C. To execute the main method
D. To store primitive data types

2. What does the extends keyword indicate in Java?

A. It makes the class abstract


B. It indicates that a class inherits from another class
C. It defines a new data type
D. It is used to implement an interface

3. Which of the following correctly defines a subclass?

A. A class that overrides all methods of a parent class


B. A class that does not inherit any properties from another class
C. A class that inherits fields and methods from another class
D. A class that implements multiple interfaces

4. Which of the following statements is true about constructors in Java?

A. A subclass can inherit constructors from its superclass


B. A constructor must always call super() explicitly
C. A constructor is automatically called when an object is instantiated
D. A constructor must always be declared as public

5. Consider the following code:

class Vehicle {
String type = "Vehicle";
}

class Car extends Vehicle {


String type = "Car";
}

public class Test {


public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.type);
}
}

What will be printed when this code is executed?

A. Vehicle
B. Car
C. null
D. Compilation error

6. Which of the following is true regarding the super keyword?

A. It is used to access a static method of a superclass


B. It is used to refer to the current class object
C. It is used to access methods or fields of a superclass
D. It is used to declare abstract methods

7. What happens if a class in Java does not explicitly extend another class?

A. The class automatically extends Object class


B. The class will not have any methods
C. The class will not compile
D. The class cannot define its own methods

8. Which of the following is NOT true about inheritance?

A. Inheritance promotes code reuse


B. A subclass inherits the constructors of its superclass
C. A subclass can have additional fields and methods beyond the ones it inherits
D. A subclass can override methods from its superclass

9. If a class is declared as final, which of the following statements is true?

A. The class cannot have any methods


B. The class cannot be instantiated
C. The class cannot be extended by any other class
D. The class must implement all methods of its parent class

10. What is method hiding in Java?

A. When a subclass method has the same name and signature as a static method in the superclass
B. When an instance method in a subclass has the same signature as a static method in the superclass
C. When a private method of the superclass is not visible in the subclass
D. When a method in a superclass is declared final

11. In Java, what happens if a subclass does not explicitly call a superclass constructor?

A. The subclass constructor will fail to compile


B. The superclass no-argument constructor is automatically called
C. The subclass constructor will be called instead of the superclass constructor
D. The superclass constructor is not called at all

12. What does the following code print?

class Parent {
public Parent() {
System.out.println("Parent constructor");
}
}

class Child extends Parent {


public Child() {
System.out.println("Child constructor");
}
}

public class Test {


public static void main(String[] args) {
new Child();
}
}

A. Child constructor
B. Parent constructor
C. Parent constructor, then Child constructor
D. Compilation error

13. Which of the following best describes multilevel inheritance?

A. A class extends more than one superclass


B. A class extends another class, which is itself a subclass
C. Multiple classes extend the same superclass
D. A class implements multiple interfaces

14. What happens when a method in a subclass has the same name and signature as a method in its superclass?

A. The method in the subclass is ignored


B. The method in the subclass overrides the superclass method
C. The method in the superclass cannot be called
D. The subclass must define a new method name
15. Which of the following correctly declares an abstract class?

A. abstract class Animal {}


B. public interface Animal {}
C. final class Animal {}
D. class abstract Animal {}

16. If a class contains at least one abstract method, the class must be declared as:

A. Static
B. Final
C. Abstract
D. Concrete

17. Consider the following code:

class A {
public void display() {
System.out.println("Display A");
}
}

class B extends A {
public void display() {
System.out.println("Display B");
}
}

What will happen if the following code is added?

B obj = new A();


obj.display();

A. Compilation error
B. "Display A" will be printed
C. "Display B" will be printed
D. Runtime exception

18. What is true about an abstract method in Java?

A. It must be declared as private


B. It can have a body in the subclass
C. It cannot be overridden
D. It must be static

19. What does the term "constructor chaining" refer to?

A. Calling multiple constructors in the same class


B. Calling a superclass constructor from a subclass constructor
C. Calling multiple methods inside a constructor
D. Calling a constructor from an interface

20. Which of the following will prevent a method from being overridden in Java?

A. Declaring the method as private


B. Declaring the method as abstract
C. Declaring the method as public
D. Declaring the method as protected

21. If a superclass method is private, can it be inherited by a subclass?

A. Yes, and it can be overridden


B. Yes, but it cannot be overridden
C. No, private methods cannot be inherited
D. Yes, but it must be declared abstract

22. Which of the following is NOT an example of inheritance?


A. A class implementing an interface
B. A class extending another class
C. A class inheriting methods from a parent class
D. A class with private fields

23. Which of the following best describes single inheritance in Java?

A. A class can implement multiple interfaces


B. A class can inherit from multiple parent classes
C. A class can inherit from one parent class
D. A class can have multiple constructors

24. What is the purpose of the super() keyword inside a subclass constructor?

A. It calls a static method in the superclass


B. It invokes the superclass constructor
C. It prevents the superclass constructor from being called
D. It creates a new object of the superclass

25. What happens if a class inherits from a class with a final method?

A. The method must be overridden in the subclass


B. The subclass will fail to compile
C. The subclass cannot override the final method
D. The method can be called but not accessed by the subclass

Test II: True or False


Direction: Read each statement carefully. Determine if the statement is true or false and write your answer in the
provided space.

26. Encapsulation in OOP is the practice of bundling data and methods that operate on the data into a single unit.
27. Inheritance allows a class to inherit properties and methods from another class.
28. Polymorphism means that an object can take on multiple forms.
29. Abstraction in OOP is about hiding implementation details and showing only the functionality.
30. A constructor in Java must have the same name as the class and can have a return type.
31. In OOP, an abstract class cannot be instantiated but can contain fully implemented methods.
32. A Java interface can have constructors and instance variables.
33. Method overloading allows multiple methods in the same class to have the same name but different parameters.
34. Method overriding is used when a subclass provides a specific implementation of a method already defined in its
parent class.
35. In OOP, static methods belong to the class, not to any specific instance.
36. The this keyword in Java refers to the current object of a class.
37. An abstract class can implement multiple interfaces in Java.
38. A final class in Java can be subclassed.
39. In OOP, a class can have multiple constructors but only one destructor.
40. A singleton class ensures that only one instance of the class can be created during the program's lifetime.
41. Inheritance supports the "is-a" relationship in OOP.
42. Encapsulation ensures that an object's internal state can only be changed through its methods.
43. In Java, an interface can extend multiple other interfaces.
44. An abstract method in Java is a method that has no implementation and must be overridden in derived classes.
45. The super keyword in Java is used to refer to the parent class's constructor or methods.
46. Static methods can access instance variables directly.
47. Overloading and overriding are examples of polymorphism in OOP.
48. A class marked as abstract in Java must have at least one abstract method.
49. Multiple inheritance is supported in Java through the use of classes.
50. OOP principles are only applicable in object-oriented programming languages.

Answer Key

1. B
2. B
3. C
4. C
5. B
6. C
7. A
8. B
9. C
10. A
11. B
12. C
13. B
14. B
15. A
16. C
17. A
18. B
19. B
20. A
21. C
22. D
23. C
24. B
25. C
26. True

27. True
28. True
29. True
30. False
31. True
32. False
33. True
34. True
35. True
36. True
37. True
38. False
39. True
40. True

41. True
42. True
43. True
44. True
45. True
46. False
47. True
48. False
49. False
50. False

You might also like