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

Final Exam OOP

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Final Exam OOP

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

First Semester

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.

1. What does OOP stand for in Java? *


a. Object Over Process Object-Oriented Programming
b. Operations on Objects Organized Object Procedures
c. Which of the following is NOT a principle of OOP? *
d. Encapsulation Inheritance
2. Which principle of OOP restricts direct access to object data? *
a. Debugging
b. Polymorphism
c. Abstraction Inheritance
d. Encapsulation Polymorphism
3. What is abstraction in OOP? *
a. Hiding the implementation details and showing only the functionality
b. Making variables private
c. Using many classes in one program
d. Creating objects without defining their methods
4. What is a class in Java? *
a. A blueprint for creating objects
b. A collection of methods only
c. A pre-defined object in Java
d. A package in Java
5. What is the output of the following code? *

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

9. What is the purpose of the this keyword?


a) Refers to the current object of the class
b) Used to call superclass methods
c) Refers to a static variable
d) Both A and B
10. 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) 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

16. Can a static method access instance variables directly?


a) Yes
b) No
c) Only if they are final
d) Only in abstract classes
17. What is the purpose of the final keyword in Java?
a) To create constants
b) To prevent method overriding
c) To prevent inheritance of a class
d) All of the above
18. What is the output of the following code?

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

19. What is the primary benefit of polymorphism?


a) Code reusability
b) Improved readability
c) Dynamic method invocation
d) All of the above
20. Which of the following is true about method overriding?
a) The method must have the same name and parameters.
b) The method in the child class must be static.
c) It is resolved during compile-time.
d) It allows multiple inheritance.
21. What is the output of the following code?

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

22. What is the output of the following code?

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();
}
}

a) Inner class method


b) Compilation Error
c) Runtime Error
d) No Output

23. What is the output of the following code?

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();
}
}

a) Static: 10, Instance: 20


b) Static: 30, Instance: 20
c) Static: 30, Instance: 40
d) Compilation Error

24. What is the output of the following code?

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();
}
}

a) Parent Constructor Child Constructor


b) Parent Constructor
c) Compilation Error
d) Runtime Error

25. What is the output of the following code?

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

26. What will happen if the following code is executed?

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

27. What is the output of the following code?

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

28. What is the output of the following code?

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

29. What is the output of the following code?

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

30. What is the output of the following code?

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

31. What is the output of the following code?

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.

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.

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

You might also like