zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

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

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

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.

What is a class?

A blueprint for creating objects, defining data (instance variables) and behaviors (methods).

What is an object?

A specific instance of a class, holding data and capable of performing actions.

What is a reference type?

A type that stores the memory location of data, rather than the data itself.

What are instance variables?

Variables that belong to a specific object and hold its data.

What is a constructor?

A special method used to create new objects and initialize their instance variables.

Define object instantiation.

The process of creating a new object from a class using the new keyword.

What is the dot operator used for?

Used to access instance variables and methods of an object.

What is a blueprint in the context of Objects and Classes?

A class serves as a blueprint, defining the structure and behavior of objects.

What is the 'new' keyword used for?

The 'new' keyword is used to create a new instance (object) of a class.

What is the purpose of the 'this' keyword?

Refers to the current object within a method or constructor.

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.