wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

The oops moment/3

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What is the primary reason to use defensive copying when returning a list from a class?

a)

To save memory

b)

To prevent external modification of the internal state

c)

To confuse other developers

d)

To automatically sort the list

2.

If you return a list directly from a class and another part of your code modifies it, what OOP principle is being violated?

a)

Encapsulation

b)

Polymorphism

c)

Abstraction

d)

Inheritance

3.

Which collection is better if you’re doing frequent insertions and deletions in the middle of the list?

a)

ArrayList

b)

LinkedList

c)

HashMap

d)

Vector

4.

A class allows you to borrow books. You now want to borrow multiple books at once. What should you add?

a)

A loop in the main method

b)

A new method like borrowMultipleBooks()

c)

Delete all books

d)

Make the list static

5.

What helps prevent an object from reaching an invalid state?

a)

Logging everything

b)

Skipping validations

c)

Enforcing business rules

d)

Using only primitive data types

6.

Why use getters/setters instead of public variables?

a)

They're shorter to write

b)

They confuse the user

c)

They allow control over how variables are accessed/modified

d)

They are required by Java

7.

What does it mean for an object to be immutable?

a)

It can’t be accessed by other classes

b)

It can’t be changed after it's created

c)

It’s slower to execute

d)

It throws exceptions when used

8.

—Defensive copying—

public List<String> getAuthors() {

// ?

}

Which line correctly implements defensive copying?

a)

return authors;

b)

return authors.clone();

c)

return new ArrayList<>(authors);

d)

return null;

9.

public class Library {

private List<String> books;

// ?

}

Which constructor properly initializes books?

a)

public Library() { books.clear(); }

b)

public Library() { books = new ArrayList<>(); }

c)

Library() => books = [];

d)

new Library() { books = null; }

10.

public void setTitle(String title) {

// ?

}

Which code correctly throws an exception for invalid input?

a)

this.title = title;

b)

if (title.length() == 0) return;

c)

if (title == null) throw new IllegalArgumentException("Invalid title");

d)

System.out.println("Invalid title");