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]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
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 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.
What is a constructor?
A special method that creates new objects and sets them up with initial characteristics.
What is the 'new' keyword used for?
Used to call the constructor and create a new object.
What is a parameter list in a constructor?
Values provided to the constructor to initialize the object's attributes.
What is pass-by-value?
A copy of the value is passed to the method; changes inside the method don't affect the original value.
What is pass-by-reference?
A reference (memory address) is passed to the method; changes inside the method do affect the original object.
What is a constructor signature?
The constructor's blueprint, including the class name and the parameter types.
What is constructor overloading?
Having multiple constructors in a class with different parameter lists.
What is a default constructor?
A constructor with no parameters.
What is a null object?
An object that doesn't exist; its reference variable stores 'null'.
What is a NullPointerException?
An error that occurs when you try to call a method on a null object.