zuai-logo

What is the role of a class in object creation?

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

Flip to see [answer/question]
Flip to see [answer/question]

All Flashcards

What is the role of a class in object creation?

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

What does the new keyword do?

It's used to create a new object (instance) of a class, calling the constructor.

Explain the difference between a class and an object.

A class is a blueprint, while an object is a specific instance of that blueprint.

What is the significance of objects being reference types?

Objects store memory locations rather than actual values, affecting how they are handled in memory and during assignment.

What is the purpose of a constructor?

To initialize the state of an object when it is created.

What are the key principles of OOP?

Abstraction, Encapsulation, Inheritance, and Polymorphism.

What is the role of methods in a class?

Methods define the behaviors or actions that an object can perform.

Why is understanding objects important in Java?

Because Java is an object-oriented language, and objects are fundamental building blocks.

What does it mean for an object to be an instance of a class?

It means the object is a specific realization or example created from the class's blueprint.

What happens when you assign one object to another?

You are copying the reference (memory address), not creating a new, independent object.

How are objects used to represent real-world entities?

Objects can model real-world entities by encapsulating their attributes (data) and behaviors (methods). For example, a 'Car' object can have attributes like color and speed, and methods like accelerate and brake.

How is the Math class applied in real-world scenarios?

The Math class is used in scientific calculations, simulations, game development, and financial modeling.

How is the String class used in real-world applications?

The String class is used for text processing, data validation, user input handling, and storing textual information in applications.

How are objects used in creating user interfaces?

UI elements like buttons, text fields, and windows are often represented as objects with specific properties and behaviors.

How are objects used in game development?

Game characters, items, and environments are typically represented as objects with attributes and methods that define their behavior and interactions.

How is the concept of objects helpful in managing data?

Objects allow you to group related data and functions together, making it easier to organize, access, and manipulate complex data structures.

What does the following code output?

java
String str = "Hello";
String str2 = str;
str2 = "World";
System.out.println(str);

Hello

Identify the error in the following code:

java
public class Example {
  int x;
  public static void main(String[] args) {
    Example obj = new Example;
    System.out.println(obj.x);
  }
}

Missing parentheses in the constructor call: new Example().

What does the following code output?

java
public class Test {
    int num;
    public Test(int num) {
        this.num = num;
    }
    public static void main(String[] args) {
        Test t1 = new Test(10);
        Test t2 = t1;
        t2.num = 20;
        System.out.println(t1.num);
    }
}

20

What does the following code output?

java
String str1 = new String("example");
String str2 = new String("example");
System.out.println(str1 == str2);

false

Identify the error in the following code:

java
public class MyClass {
    private int x;

    public void MyClass(int x) { // Intended to be a constructor
        this.x = x;
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass(5);
        System.out.println(obj.x);
    }
}

The 'constructor' MyClass(int x) is actually a method because it has a return type (void is implied). This means the value of x is never properly initialized, and x will default to 0, leading to a compilation error as the constructor is never called.

What does the following code output?

java
public class Example {
    public static void main(String[] args) {
        String s1 = "Hello";
        String s2 = "Hello";
        System.out.println(s1 == s2);
    }
}

true

What will be the output of this code?

java
public class Example {
    public static void main(String[] args) {
        String str = "Coding";
        System.out.println(str.substring(2, 4));
    }
}

di

What does the following code output?

java
public class Test {
    int x = 5;

    public static void main(String[] args) {
        Test t1 = new Test();
        Test t2 = new Test();
        t1.x = 10;
        System.out.println(t2.x);
    }
}

5

What does the following code output?

java
public class Example {
    public static void main(String[] args) {
        String s = "Hello World";
        System.out.println(s.indexOf("World"));
    }
}

6

What is wrong with the following code?

java
public class Example {
    public static void main(String[] args) {
        Math m = new Math();
        System.out.println(m.sqrt(25));
    }
}

You cannot instantiate the Math class. All methods in the Math class are static, so you should call them using Math.sqrt(25).