What is the purpose of a constructor?
To initialize the state of an object when it is created.
Why are instance variables often declared as private?
To encapsulate the data and control access to it.
What happens if you don't define a constructor in a class?
Java provides a default constructor.
Why is it important to make a copy of a mutable object in a constructor?
To avoid unintended changes to the original object's state.
What is the significance of the 'new' keyword?
It is used to create a new object of a class and calls the constructor.
What is the return type of a constructor?
Constructors do not have a return type (not even void).
What is the scope of instance variables?
Instance variables are accessible throughout the object's lifetime.
What is the scope of local variables?
Local variables are only accessible within the method in which they are defined.
How are classes and objects related?
A class is a blueprint, and an object is an instance of that blueprint.
Why are constructors important for object-oriented programming?
They ensure that objects are properly initialized before they are used, maintaining data integrity.
What is the output of the following code?
```java
public class Example {
private int x;
public Example(int x) {
this.x = x;
}
public int getX() {
return x;
}
public static void main(String[] args) {
Example obj = new Example(5);
System.out.println(obj.getX());
}
}
```
5
Identify the error in the following code:
```java
public class Test {
private int value;
public Test(int val) {
value = val
}
}
```
Missing semicolon after `value = val`.
What is the output of the following code?
```java
public class Point {
private int x, y;
public Point() {
x = 0; y = 0;
}
public Point(int x, int y) {
this.x = x; this.y = y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
public static void main(String[] args) {
Point p1 = new Point();
Point p2 = new Point(3, 4);
System.out.println(p1 + " " + p2);
}
}
```
(0, 0) (3, 4)
Identify the error in the following code:
```java
public class Circle {
private double radius;
public Circle(rad) {
radius = rad;
}
}
```
Missing data type for the parameter 'rad' in the constructor.
What is the output of the following code?
```java
public class Counter {
private int count;
public Counter(int start) {
count = start;
}
public void increment() {
count++;
}
public int getCount() {
return count;
}
public static void main(String[] args) {
Counter c = new Counter(10);
c.increment();
System.out.println(c.getCount());
}
}
```
11
Identify the error in the following code:
```java
public class Rectangle {
private int width, height;
public Rectangle(int w, int h) {
width = w;
height = h
}
public int area() {
return width * height;
}
}
```
Missing semicolon after `height = h`.
What is the output of the following code?
```java
public class Book {
private String title;
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public static void main(String[] args) {
Book b = new Book("The Hobbit");
System.out.println(b.getTitle());
}
}
```
The Hobbit
Identify the error in the following code:
```java
public class Car {
private String model;
public Car() {}
public Car(String m) {
model = m;
}
public void display() {
System.out.println(model);
}
}
```
If the `display()` method is called on an object created with the no-argument constructor, a `NullPointerException` will occur because `model` is not initialized.
What is the output of the following code?
```java
public class Value {
private int val;
public Value(int v) {
val = v;
}
public int getValue() {
return val;
}
public static void main(String[] args) {
Value value = new Value(100);
System.out.println(value.getValue());
}
}
```
100
Identify the error in the following code:
```java
public class House {
int rooms;
public House(int numRooms) {
rooms = numRooms;
}
public void printRooms() {
System.out.println(rooms);
}
}
```
No errors. Code will compile and run without issues.
What are the differences between instance and local variables?
Instance variables: belong to the object, exist throughout the object's lifetime. Local variables: exist only within the method they are defined in.
What are the differences between a constructor and a method?
Constructor: initializes object state, has same name as class, no return type. Method: performs an action, has a return type (or void), can have any name.
What are the differences between a default constructor and a parameterized constructor?
Default constructor: no arguments, initializes with default values. Parameterized constructor: takes arguments, initializes with provided values.