Explain the relationship between classes and objects.
A class is a blueprint, and an object is a specific instance created from that blueprint.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
Explain the relationship between classes and objects.
A class is a blueprint, and an object is a specific instance created from that blueprint.
Why are objects considered complex entities?
They combine primitive types and other reference types to represent more complex data structures.
What is the significance of objects storing memory locations?
Allows for efficient memory management and sharing of data between different parts of a program.
Why is understanding objects and classes foundational in Java?
They are the building blocks for creating modular, reusable, and maintainable code.
Explain the concept of encapsulation in relation to objects.
Objects encapsulate data (instance variables) and behavior (methods) into a single unit, promoting data hiding and code organization.
What is the main purpose of a constructor?
To initialize the state of an object when it is created.
Explain the concept of object identity.
Each object has a unique identity, even if its attributes have the same values as another object.
What does it mean for a class to 'define characteristics'?
It means the class specifies the data (instance variables) and actions (methods) that its objects will possess.
Why can a class be considered a 'template'?
Because it provides a reusable pattern for creating multiple objects with the same structure and behavior.
Explain the importance of understanding object instantiation for the AP exam.
The AP exam often tests understanding of how objects are created and used, as it's a fundamental concept in object-oriented programming.
What are the differences between a class and an object?
Class: Blueprint, defines structure and behavior | Object: Instance of a class, actual entity with data and behavior.
What are the differences between primitive types and reference types?
Primitive: Stores actual values directly | Reference: Stores memory address of the value.
What does the following code output?
```java
public class Example {
String message = "Hello";
public static void main(String[] args) {
Example obj = new Example();
System.out.println(obj.message);
}
}
```
Hello
Identify the error in the following code:
```java
public class Test {
int x = 5;
public static void main(String[] args) {
System.out.println(x);
}
}
```
Non-static variable `x` cannot be referenced from a static context.
What does the following code output?
```java
public class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println(p.x + "," + p.y);
}
}
```
3,4
Identify the error in the following code:
```java
public class Circle {
private double radius;
public void Circle(double r) { // Incorrect Constructor
radius = r;
}
}
```
The constructor name `Circle` should not have a `void` return type.
What does the following code output?
```java
public class Counter {
int count = 0;
public Counter() {
count++;
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
System.out.println(c1.count);
}
}
```
1
Identify the error in the following code:
```java
public class Rectangle {
private int length;
private int width;
public Rectangle(int l, int w) {
length = l;
width = w;
}
public static void main(String[] args) {
Rectangle rect = new Rectangle(); // Error here
}
}
```
The `Rectangle` class requires two integer arguments for its constructor, but the call in `main` provides none.
What does the following code output?
```java
public class Example {
public static void main(String[] args) {
String str = new String("Java");
System.out.println(str.length());
}
}
```
4
Identify the error in the following code:
```java
public class MyClass {
int x = 10;
public static void main(String[] args) {
MyClass obj;
System.out.println(obj.x); // Error here
}
}
```
The variable `obj` is declared but not initialized before being used, resulting in a NullPointerException.
What does the following code output?
```java
public class Test {
int a = 5;
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
t2.a = 10;
System.out.println(t1.a);
}
}
```
5
Identify the error in the following code:
```java
public class Example {
public static void main(String[] args) {
int num = null; // Error here
System.out.println(num);
}
}
```
Primitive type `int` cannot be assigned `null`. `null` is only for reference types.