Final Exam OOP
Final Exam OOP
Professional Elective 1
Final 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.
class Test {
String name = "Java";
void display() {
System.out.println("Welcome to " + name);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.display();
}
}
a. Welcome to Programming
b. Welcome to Java
c. Compilation Error
d. Runtime Error
6. Which keyword is used to create an object in Java? *
a. class
b. new
c. object
d. instance
7. What is a constructor in Java? *
a. A special method used to initialize objects
b. A method with the same name as its class
c. A method that can be overloaded
d. All of the above
8. What is the output of the following code? *
class Parent {
void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display() {
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}
a. null
b. Java
c. Compilation Error
d. Runtime Error
class Parent {
void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display() {
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}
a) Parent
b) Child
c) Compilation Error
d) Runtime Error
11. Can constructors be inherited in Java?
a) Yes
b) No
c) Only if marked with @Override
d) Only in abstract classes
12. What is polymorphism in OOP?
a) The ability to define multiple classes
b) The ability to process objects differently based on their type
c) Using static variables in different methods
d) Creating many constructors in a class
13. What will happen if no constructor is defined in a class?
a) The class cannot be instantiated. Java provides a default no-argument constructor.
b) A runtime error occurs.
c) Compilation fails.
14. What does the static keyword mean when used in a method?
a) The method cannot be overridden.
b) The method belongs to the class rather than an object.
c) The method is private by default.
d) The method cannot have arguments.
15. What is the output of the following code?
class Test {
static int i = 0;
public static void main(String[] args) {
System.out.println(i++);
System.out.println(i);
}
}
a) 0 1
b) 1 1
c) 0 2
d) Compilation Error
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
a) Animal sound
b) Dog barks
c) Compilation Error
d) Runtime Error
class Test {
void display() {
System.out.println("Default Method");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.display();
}
}
a) Default Method
b) Compilation Error
c) Runtime Error
d) No Output
class Outer {
class Inner {
void display() {
System.out.println("Inner class method");
}
}
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner i = o.new Inner();
i.display();
}
}
class Test {
static int i = 10;
int j = 20;
void display() {
System.out.println("Static: " + i + ", Instance: " + j);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.display();
}
}
class Parent {
Parent() {
System.out.println("Parent Constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child Constructor");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
}
}
class Test {
void display() {
System.out.println(15);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.display();
}
}
a) 15
b) 16.0
c) 15.0
d) Compilation Error
class Shape {
void draw() {
System.out.println("Drawing");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
a) Drawing
b) Circle
c) Compilation Error
d) Runtime Error
class Dog {
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
a) Bark
b) Compilation Error
c) Runtime Error
d) No Output
class Test {
void display() {
System.out.println("Parent");
}
}
class Child extends Test {
void display() {
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Child();
t.display();
}
}
a) Parent
b) Child
c) Compilation Error
d) No Output
class Test {
static int x = 10;
public static void main(String[] args) {
System.out.println("Value of x: " + x);
}
}
a) Value of x: 10
b) Compilation Error
c) Runtime Error
d) No Output
class Test {
boolean flag = true;
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.flag);
}
}
a) true
b) false
c) Compilation Error
d) Runtime Error
class Test {
public static void main(String[] args) {
System.out.println("Windows Button");
System.out.println("MacOS Button");
}
}
a) Windows Button MacOS Button
b) Compilation Error
c) Runtime Error
d) No Output
32. Which of the following statements is true about the Abstract Factory pattern?
a) It provides a way to encapsulate a group of related factories.
b) It is used to implement Singleton classes.
c) It simplifies creating multiple unrelated objects.
d) It is specific to one type of product.
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. a. Object-Oriented Programming
2. d. Encapsulation
3. a. Hiding the implementation details and showing only the functionality
4. a. A blueprint for creating objects
5. b. Welcome to Java
6. b. new
7. d. All of the above
8. b
9. d. Both A and B
10. b. Child
11. b. No
12. b. The ability to process objects differently based on their type
13. a. Java provides a default no-argument constructor.
14. b. The method belongs to the class rather than an object.
15. a. 0 1
16. b. No
17. d. All of the above
18. b. Dog barks
19. d. All of the above
20. a. The method must have the same name and parameters.
21. a. Default Method
22. a. Inner class method
23. a. Static: 10, Instance: 20
24. a. Parent Constructor Child Constructor
25. a. 15
26. b. Circle
27. a. Bark
28. b. Child
29. a. Value of x: 10
30. a. true
31. a. Windows Button MacOS Button
32. a. It provides a way to encapsulate a group of related factories.
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