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 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.