1. Difference between shallow copying and deep copying?
Shallow copy:
All the primitives are copied
All the references are pointed to the existing object
So if the data in original object references are changed, new object data also gets effected, as it is also pointing to the same reference.
How to create shallow copy of an object:
Object's class clone() method does shallow copying.
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
Deep copy:
All the primitives are copied
All the references to objects are created again.
This is like creating an object again.
So even if the data in original object references are changed, new copy data will not be effected.
How to create deep copy:
public Object() {
Person person = new Person("Suresh", 26);
return person;
}
2. Given two .java files:
A.java
class A {
static public void main(String[] args) {
System.out.println("main() method of class A");
}
}
B.java
class B extends A {
}
Both the classes are compiled and they do not have any compilation errors.
What is the output of the following command?
> java B
Answer: main() method of class A
Since class B inherits from A, it looks for main() method in its superclass, when it is not found in it.
3. What does the SOP prints here?
int b = 10;
System.out.println((b=3) + b);
Answer:
6
Rule is that: binary operator has left associative, means LHS operand of binary operator is fully evaluated before the right hand operand.
4. Given:
int[] a = {10, 20, 30, 40, 50};
int index = 4;
a[index] = index = 2;
What is the value of index and array "a" elements?
Answer:
index = 2;
array: a = {10, 20, 30, 40, 2}
Its evaluated as:
a[index] = index = 2;
a[4] = index = 2;
a[4] = 2;
Rule is that assignment operator has right associative.
5. What is the value of the b after the following statements?
byte b = 28;
b = b + 10;
Ans: Compilation Error
Any arithmetic operation with Integer variables will give an "int" result, which can't be assigned directly to "byte" here. We must use explicit casting.
6. How to create a immutable class (like String, Wrapper classes)?
a. Class should not have any setter methods
b. make all the fields private and final. private makes them not to be accessed by outside the class. final ensures even accidentally also the fields data wont be changed
c. Dont allow subclasses to override the class methods. So make the class as final.
d. If there is any field which is mutable, its getter should return a new object of that field. So that its value wont be changed in our actual immutable object.
7. Whats the output of the following program?
public class ThreadDemo {
static public void main(String[] args) {
Thread.currentThread().setDaemon(true);
System.out.println(Thread.currentThread().isDaemon());
}
}
Answer:
Runtime Exception: IllegalThreadStateException.
main() thread is already started and it is running state.
we cannot use setDaemon() on a thread which is already in running state.
8. Method Overloading/Overriding possibilities?
class A {
public void nonSt() { }
public static void st() { }
}
class B {
public void nonSt() { } //valid
public static void st() { } //valid
public static void nonSt() { } //invalid
public void st() { } //invalid
}
Shallow copy:
All the primitives are copied
All the references are pointed to the existing object
So if the data in original object references are changed, new object data also gets effected, as it is also pointing to the same reference.
How to create shallow copy of an object:
Object's class clone() method does shallow copying.
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
Deep copy:
All the primitives are copied
All the references to objects are created again.
This is like creating an object again.
So even if the data in original object references are changed, new copy data will not be effected.
How to create deep copy:
public Object() {
Person person = new Person("Suresh", 26);
return person;
}
2. Given two .java files:
A.java
class A {
static public void main(String[] args) {
System.out.println("main() method of class A");
}
}
B.java
class B extends A {
}
Both the classes are compiled and they do not have any compilation errors.
What is the output of the following command?
> java B
Answer: main() method of class A
Since class B inherits from A, it looks for main() method in its superclass, when it is not found in it.
3. What does the SOP prints here?
int b = 10;
System.out.println((b=3) + b);
Answer:
6
Rule is that: binary operator has left associative, means LHS operand of binary operator is fully evaluated before the right hand operand.
4. Given:
int[] a = {10, 20, 30, 40, 50};
int index = 4;
a[index] = index = 2;
What is the value of index and array "a" elements?
Answer:
index = 2;
array: a = {10, 20, 30, 40, 2}
Its evaluated as:
a[index] = index = 2;
a[4] = index = 2;
a[4] = 2;
Rule is that assignment operator has right associative.
5. What is the value of the b after the following statements?
byte b = 28;
b = b + 10;
Ans: Compilation Error
Any arithmetic operation with Integer variables will give an "int" result, which can't be assigned directly to "byte" here. We must use explicit casting.
6. How to create a immutable class (like String, Wrapper classes)?
a. Class should not have any setter methods
b. make all the fields private and final. private makes them not to be accessed by outside the class. final ensures even accidentally also the fields data wont be changed
c. Dont allow subclasses to override the class methods. So make the class as final.
d. If there is any field which is mutable, its getter should return a new object of that field. So that its value wont be changed in our actual immutable object.
7. Whats the output of the following program?
public class ThreadDemo {
static public void main(String[] args) {
Thread.currentThread().setDaemon(true);
System.out.println(Thread.currentThread().isDaemon());
}
}
Answer:
Runtime Exception: IllegalThreadStateException.
main() thread is already started and it is running state.
we cannot use setDaemon() on a thread which is already in running state.
8. Method Overloading/Overriding possibilities?
class A {
public void nonSt() { }
public static void st() { }
}
class B {
public void nonSt() { } //valid
public static void st() { } //valid
public static void nonSt() { } //invalid
public void st() { } //invalid
}