0% found this document useful (0 votes)
29 views4 pages

Java OOP Exam Questions & Answers

The document contains 30 multiple choice questions related to Java Object-Oriented Programming (OOP) concepts, along with their answers. It also includes solved coding questions demonstrating encapsulation, method overloading, inheritance, constructor calls, and interface implementation. Each coding example is accompanied by a main class to illustrate its functionality.

Uploaded by

bekateshoma5
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)
29 views4 pages

Java OOP Exam Questions & Answers

The document contains 30 multiple choice questions related to Java Object-Oriented Programming (OOP) concepts, along with their answers. It also includes solved coding questions demonstrating encapsulation, method overloading, inheritance, constructor calls, and interface implementation. Each coding example is accompanied by a main class to illustrate its functionality.

Uploaded by

bekateshoma5
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

Java OOP Exam Questions with Answers

30 Multiple Choice Questions (With Answer Key)


1. Which of the following best defines Encapsulation? Answer: B

2. Data hiding is achieved using which keyword? Answer: C

3. A static variable belongs to: Answer: C

4. Which statement is TRUE about static methods? Answer: C

5. A class is: Answer: B

6. Constructors have: Answer: B

7. Which is NOT a type of inheritance in Java? Answer: B

8. Aggregation represents which relationship? Answer: C

9. Protected members are accessible in: Answer: B

10. The super keyword is used to: Answer: B

11. The this keyword refers to: Answer: B

12. A final class: Answer: B

13. An instance initializer block runs: Answer: C

14. Which class cannot be instantiated? Answer: D

15. What is a package? Answer: B

16. Polymorphism means: Answer: B

17. Method Overloading is decided at: Answer: B

18. Method Overriding requires: Answer: A

19. Which is runtime polymorphism? Answer: A

20. Command-line arguments are received in: Answer: A

21. The operator instanceof checks: Answer: B

22. Which one supports 100% abstraction? Answer: B

23. A constructor without parameters is called: Answer: B


24. Which keyword prevents method overriding? Answer: A

25. Which error occurs if you forget to write a constructor? Answer: B

26. static methods can access: Answer: A

27. In aggregation, objects are: Answer: B

28. Which is NOT allowed with abstract classes? Answer: C

29. Interface methods are by default: Answer: C

30. Which method is used to run a Java program? Answer: C

Solved Coding Questions

Q1. Encapsulation – Student Class


class Student {
private String name;
private int id;

public void setName(String name) {


[Link] = name;
}

public void setId(int id) {


[Link] = id;
}

public String getName() {


return name;
}

public int getId() {


return id;
}
}

class TestStudent {
public static void main(String[] args) {
Student s = new Student();
[Link]("Beka");
[Link](101);

[Link]([Link]());
[Link]([Link]());
}
}

Q2. Method Overloading


class Calculator {

int add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
}

class TestCalc {
public static void main(String[] args) {
Calculator c = new Calculator();

[Link]([Link](2, 3));
[Link]([Link](2, 3, 4));
}
}

Q3. Inheritance + Overriding


class Animal {
void sound() {
[Link]("Animal makes sound");
}
}

class Dog extends Animal {


@Override
void sound() {
[Link]("Dog barks");
}
}

class TestAnimal {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}
Q4. super() Constructor Call
class Person {
Person() {
[Link]("Person constructor called");
}
}

class Employee extends Person {


Employee() {
super();
[Link]("Employee constructor called");
}
}

class TestSuper {
public static void main(String[] args) {
new Employee();
}
}

Q5. Interface + Rectangle Implementation


interface Shape {
double area();
}

class Rectangle implements Shape {


double width, height;

Rectangle(double width, double height) {


[Link] = width;
[Link] = height;
}

public double area() {


return width * height;
}
}

class TestShape {
public static void main(String[] args) {
Shape r = new Rectangle(5, 4);
[Link]("Area: " + [Link]());
}
}

Common questions

Powered by AI

Method overloading in Java allows a class to have more than one method with the same name, as long as their parameter lists differ. This feature enhances flexibility by allowing different methods to be called using the same name but with different arguments. This is exemplified in the Calculator class where the 'add' method is overloaded to handle two or three integer parameters . It improves code readability and provides multiple ways to perform an operation without knowing exact details about the number of arguments at compile time .

Aggregation is considered a weak form of association in object-oriented design because it represents a 'has-a' relationship where the contained object can exist independently of the container. This means the lifecycle of the aggregated object does not depend on that of the container, allowing for more flexible and modular design. For example, if a Department object contains multiple Employee objects, the Employee objects can exist independently outside of the Department object. This contrasts with composition, where the lifecycles are strictly tied together .

The 'super' keyword is used in Java to refer to the immediate parent class object or constructor. It is necessary when you want to call a parent class's constructor in a derived class to ensure that the base class is properly initialized before any additional operations are performed in the derived class's constructor. For instance, in the Employee class, the 'super()' call in the constructor calls the Person class constructor first, maintaining a proper inheritance structure .

Using interfaces in Java ensures full abstraction by allowing the definition of methods without implementing their actual functionality. This mandates that any class implementing the interface provide concrete implementations for all defined methods, thereby enforcing a contract for consistent API behavior. Interfaces enhance flexibility in design as they enable multiple inheritance of types and allow a class to implement multiple interfaces. For example, the Shape interface specifies the 'area' method, which must be implemented by any class like Rectangle that implements Shape .

Polymorphism in Java, particularly runtime polymorphism, plays a crucial role in achieving abstraction by allowing objects to be treated as instances of their parent class, thus enabling a single interface to represent different underlying forms (data types). It is implemented through method overriding, where a subclass provides a specific implementation of a method already defined in its parent class. In the Dog class, overriding the 'sound' method of the Animal class demonstrates runtime polymorphism, as the actual method call to 'sound' is resolved at runtime .

Method overriding occurs in Java when a subclass provides a specific implementation for a method already defined in its superclass, allowing for runtime polymorphism. This contrasts with method overloading, where multiple methods have the same name but different parameter lists within the same class. The implications include allowing subclasses to provide specific behaviors while maintaining a consistent interface through overriding, and enabling methods to be used interchangeably while customizing functionality through overloading. For example, the Dog class overrides the 'sound' method from the Animal class to customize the behavior .

The 'this' keyword in Java is used within class methods to reference the current object—the current instance of the class. It helps in differentiating between instance variables and parameters or for passing the current instance as a parameter to another method or constructor. For instance, in the Student class, 'this' is used in the setName and setId methods to distinguish between the method parameters and class fields with the same name .

Static methods in Java belong to the class rather than any particular instance and can be called without creating an instance of the class. They cannot access instance variables or methods directly; instead, they operate solely on static data. In contrast, non-static methods require an instance of their class and can access both static and instance data. Static methods enhance performance and clarity when behavior cannot depend on instance-specific data, but they limit flexibility and extensibility compared to non-static methods which can be overridden in subclasses. For instance, static methods can only access other static members or accept class-based access through objects .

Encapsulation in Java is defined as the bundling of data with the methods that operate on that data. It is typically implemented using private variables along with public getter and setter methods to access and update these variables safely . For example, in the Student class, the variables 'name' and 'id' are private and accessed through public methods getName, setName, getId, and setId .

Inheritance in Java allows a class (subclass) to inherit fields and methods from another class (superclass), facilitating code reuse and establishing a logical relationship between classes. However, Java does not support multiple inheritance, where a class can inherit from more than one class, as this can lead to complexity and ambiguity in method selection, known as the diamond problem . Instead, multiple types of inheritance in Java are achieved through interfaces which allow a class to implement multiple interfaces, effectively simulating multiple inheritance without its drawbacks .

You might also like