Font size
WorksheetsThe Oops moment/2
Total questions: 10
Worksheet time: 13mins
What does the new keyword do in Java?
Deletes an object
Copies an object
Creates a new object
Imports a class
Which of the following is a correct way to define an enum?
enum Color = {Red, Green, Blue}
enum Color {Red, Green, Blue}
Color enum {Red, Green, Blue}
enum(Color) => Red, Green, Blue
How do you access a private variable from outside the class?
Use public keyword
Use a getter or setter method
Make it static
Use a constructor
Which method returns a string representation of an object?
show()
print()
toString()
getInfo()
What does a constructor do in a class?
Creates variables
Destroys objects
Initializes new objects
Runs loops
Why are getter and setter methods commonly used in Java classes?
To repeatedly execute blocks of code within loops defined in the class
To safely retrieve and modify the values of private fields while maintaining encapsulation
To automatically delete unused variables when the object is destroyed
To bypass access modifiers and make all fields globally accessible
enum Size { SMALL, MEDIUM, LARGE }
public class Test {
public static void main(String[] args) {
Size s = Size.LARGE;
System.out.println(s);
}
}
SMALL
Size
LARGE
null
class User {
private String name = "Michelle";
}
public class Main {
public static void main(String[] args) {
User u = new User();
System.out.println(u.name);
}
}
Line 2
Line 4
Line 7
Line 6
class Item {
private int id;
public void Item(int i) {
id = i;
}
public int getId() {
return id;
}
}
Constructor has wrong name
Constructor is missing
id should be public
No main method
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)
