wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

The Oops moment/2

Total questions: 10

Worksheet time: 13mins

Name
Class
Date
1.

What does the new keyword do in Java?

a)

Deletes an object

b)

Copies an object

c)

Creates a new object

d)

Imports a class

2.

Which of the following is a correct way to define an enum?

a)

enum Color = {Red, Green, Blue}

b)

enum Color {Red, Green, Blue}

c)

Color enum {Red, Green, Blue}

d)

enum(Color) => Red, Green, Blue

3.

How do you access a private variable from outside the class?

a)

Use public keyword

b)

Use a getter or setter method

c)

Make it static

d)

Use a constructor

4.

Which method returns a string representation of an object?

a)

show()

b)

print()

c)

toString()

d)

getInfo()

5.

What does a constructor do in a class?

a)

Creates variables

b)

Destroys objects

c)

Initializes new objects

d)

Runs loops

6.

Why are getter and setter methods commonly used in Java classes?

a)

To repeatedly execute blocks of code within loops defined in the class

b)

To safely retrieve and modify the values of private fields while maintaining encapsulation

c)

To automatically delete unused variables when the object is destroyed

d)

To bypass access modifiers and make all fields globally accessible

7.

enum Size { SMALL, MEDIUM, LARGE }

public class Test {

public static void main(String[] args) {

Size s = Size.LARGE;

System.out.println(s);

}

}

a)

SMALL

b)

Size

c)

LARGE

d)

null

8.

class User {

private String name = "Michelle";

}

public class Main {

public static void main(String[] args) {

User u = new User();

System.out.println(u.name);

}

}

a)

Line 2

b)

Line 4

c)

Line 7

d)

Line 6

9.

class Item {

private int id;

public void Item(int i) {

id = i;

}

public int getId() {

return id;

}

}

a)

Constructor has wrong name

b)

Constructor is missing

c)

id should be public

d)

No main method

10.

I am born every time you say “new,”
But I’m not a baby, just part of your crew.

I don’t return things — not even a void,
But without me, your object gets paranoid.

You can give me no args, or one, or two,
I dress your object in values fresh and new.

So tell me, coder, quick and slick:
Who am I that helps objects tick?

(a)