Diese Präsentation wurde erfolgreich gemeldet.

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
×

1 von 2 Anzeige

# part 1 Consider the following piece of code- Person p - null- p-speak(.docx

part 1 Consider the following piece of code: Person p = null;
p.speak();
a) Does it compile? Explain.
b) Does it execute? Explain.
part 2 You are given three variables p, q, and r of type Person. The three variables have been correctly initialized. Write a piece of code that writes the initial value of p in q, the initial value of q in r, and the initial value of r in p. Try to make your code as compact as possible.
Solution
Question 1:
a) Yes. it will compile.
b) No. It will not execute. During execution we will get runtime exception becuase p object is not initialized. So object p value is null. we can not call any functon by object which is havnig null. if we try to call then will get NullPointerException.
Question 2:
Person p =new Person();
Person q =new Person();
Person r =new Person();
Person temp =new Person();
temp = r;
r = q;
q = p;
p = temp;
.

part 1 Consider the following piece of code: Person p = null;
p.speak();
a) Does it compile? Explain.
b) Does it execute? Explain.
part 2 You are given three variables p, q, and r of type Person. The three variables have been correctly initialized. Write a piece of code that writes the initial value of p in q, the initial value of q in r, and the initial value of r in p. Try to make your code as compact as possible.
Solution
Question 1:
a) Yes. it will compile.
b) No. It will not execute. During execution we will get runtime exception becuase p object is not initialized. So object p value is null. we can not call any functon by object which is havnig null. if we try to call then will get NullPointerException.
Question 2:
Person p =new Person();
Person q =new Person();
Person r =new Person();
Person temp =new Person();
temp = r;
r = q;
q = p;
p = temp;
.

Anzeige
Anzeige

## Weitere Verwandte Inhalte

Anzeige

### part 1 Consider the following piece of code- Person p - null- p-speak(.docx

1. 1. part 1 Consider the following piece of code: Person p = null; p.speak(); a) Does it compile? Explain. b) Does it execute? Explain. part 2 You are given three variables p, q, and r of type Person. The three variables have been correctly initialized. Write a piece of code that writes the initial value of p in q, the initial value of q in r, and the initial value of r in p. Try to make your code as compact as possible. Please help me with Java question. Thank you! Solution Question 1: Answer: a) Yes. it will compile. b) No. It will not execute. During execution we will get runtime exception becuase p object is not initialized. So object p value is null. we can not call any functon by object which is havnig null. if we try to call then will get NullPointerException. Question 2: Answer: Person p =new Person(); Person q =new Person(); Person r =new Person(); Person temp =new Person();
2. 2. temp = r; r = q; q = p; p = temp;