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

What is the output of the following code?

java
public class Example {
    public static void main(String[] args) {
        String str = null;
        try {
            System.out.println(str.length());
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught");
        }
    }
}

NullPointerException caught

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

All Flashcards

What is the output of the following code?

java
public class Example {
    public static void main(String[] args) {
        String str = null;
        try {
            System.out.println(str.length());
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught");
        }
    }
}

NullPointerException caught

Identify the error in the following code:

java
public class Person {
    private String name;
    public Person(String n) {
        name = n;
    }
    public Person(String name) {
        name = name;
    }
}

The second constructor does not properly assign the parameter 'name' to the instance variable 'name'. It should be 'this.name = name;'.

What is the output of the following code?

java
public class Number {
    int value;
    public Number(int value) {
        this.value = value;
    }
    public static void main(String[] args) {
        Number num1 = new Number(5);
        Number num2 = num1;
        num2.value = 10;
        System.out.println(num1.value);
    }
}

10

Identify the error in the following code:

java
public class Rectangle {
    private int width, height;
    public Rectangle(int w) {
        width = w;
    }
}

The height instance variable is not initialized in the constructor, potentially leading to unexpected behavior.

What is the output of the following code?

java
public class Test {
    public static void main(String[] args) {
        Integer x = new Integer(10);
        Integer y = x;
        x = 20;
        System.out.println(y);
    }
}

10

What is the output of the following code?

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

    public static void modifyString(String s) {
        s = "World";
    }
}

Hello

Identify the error in the following code:

java
public class Circle {
    private double radius;
    public void Circle(double r) {
        radius = r;
    }
}

The constructor is missing the return type. It should be 'public Circle(double r)'. Currently, it is interpreted as a method.

What is the output of the following code?

java
public class Counter {
    private int count;
    public Counter(int start) {
        count = start;
    }
    public Counter() {
        this(0);
    }
    public int getCount() {
        return count;
    }
    public static void main(String[] args) {
        Counter c = new Counter();
        System.out.println(c.getCount());
    }
}

0

Identify the error in the following code:

java
public class Point {
    private int x, y;
    public Point(int x, int y) {
        x = x;
        y = y;
    }
}

The constructor does not properly assign the parameters 'x' and 'y' to the instance variables 'x' and 'y'. It should be 'this.x = x;' and 'this.y = y;'.

What is the output of the following code?

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

false

What are the differences between pass-by-value and pass-by-reference?

Pass-by-value: A copy is passed, original value unaffected. | Pass-by-reference: The memory address is passed, original value can be modified.

What are the differences between a default constructor and a parameterized constructor?

Default constructor: No parameters, provides default initialization. | Parameterized constructor: Accepts parameters, allows custom initialization.

What is object initialization?

The process of setting up an object's initial state using a constructor.

Explain the concept of abstraction in constructors.

Using constructors without knowing their inner workings, trusting they will set up the object correctly.

Why is the 'new' keyword essential for object creation?

It tells the program to allocate memory for a new object and calls the constructor.

How does Java handle primitives in memory?

Primitives are stored directly as values in memory.

How does Java handle objects in memory?

Objects are stored as references (memory addresses) to the actual object data.

What determines a valid constructor overload?

Different number of parameters or different order of parameter types.

What happens if a constructor is not explicitly defined?

Java provides a default no-argument constructor if no other constructors are defined.

Why should you check for null objects before using them?

To avoid NullPointerException, which causes the program to crash.

What does it mean for a reference variable to be 'null'?

It means the variable does not point to any object in memory.

How does constructor overloading improve code flexibility?

It allows creating objects with different initial states based on available information.