All Flashcards
What is a class?
Blueprint for creating objects; defines state and behavior.
What is an object?
An instance of a class with its own unique state.
What are instance variables?
Attributes that define the state of an object.
What are constructors?
Special methods used to create objects and initialize instance variables.
What are constructor parameters?
Local variables that receive values when a new object is created.
What are local variables?
Variables that exist only within the method they are defined in.
What is a default constructor?
Constructor provided by Java if you don't define one; initializes instance variables to default values.
What is the state of an object?
The set of values of its instance variables at a given time.
What does 'private' mean in the context of instance variables?
Accessible only within the class itself.
What is a mutable object?
An object whose state can be changed after it is created.
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.