Java Intermediate

0% completed

Previous
Next
Quiz
Question 1
What is a constructor in Java?
A
A method that initializes a new object's state.
B
A method that deletes an object from memory.
C
A variable that holds the object's data.
D
A special block that executes after the object is created.
Question 2
Which statement is true about the default constructor?
A
It must always be explicitly defined by the programmer.
B
It accepts parameters to initialize the object.
C
It is automatically provided by the compiler if no constructors are defined.
D
It deletes uninitialized member variables.
Question 3
Examine the following code snippet:
class MyClass {
    int x;
    MyClass() {
        x = 10;
    }
    MyClass(int x) {
        this.x = x;
    }
}
public class Test {
    public static void main(String[] args) {
        MyClass obj1 = new MyClass();
        MyClass obj2 = new MyClass(20);
        System.out.println(obj1.x + " " + obj2.x);
    }
}
What is the output?
A
0 20
B
10 20
C
10 10
D
20 20
Question 4
Which statement best describes a copy constructor in Java?
A
It is automatically provided by the Java compiler if no constructors are defined.
B
It is a constructor that creates a new object by copying the state of an existing object.
C
It is a method that copies an object and returns a new instance.
D
It can only copy primitive types.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next