MUST BE IN JAVA Question 3: (8 points) <?xml:namespace prefix = o ns = \"urn:sch
ID: 3553500 • Letter: M
Question
MUST BE IN JAVA
Question 3:(8 points) <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
What is up-casting and down-casting?
Answer: Upcasting is when a subclass object is treated as a superclass object. Downcasting is when a superclass object is treated as a subclass object.
Assume the base class has methods A() and B(), and the sub class has methods C() and D(). When up-casting is used, can the reference access the methods in the subclass?
Answer:
Question 4: What is the result of the code segments below? (5 points)
for (int i = 0; i <= 10; i++) {
if (i == 2) {
break;
}
System.out.println(i);
}
Result:
for (int i = 0; i <= 10; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
Result:
Question 5. Check whether there is any error in the program below, if yes, please fix it in each line. If no, simply write "correct". (5 points)
class TestConversion {
public static voidmain(String[] args) {
int i1 = 12; // line 1
int i2 = 5.6; // line 2
double d1 = (i1 - i2) * 1.0; // line 3
float f1 = (i1 + i2) * 1.0; // line 4
short s1 = 1; // line 5
byte b1 = 2; // line 6
short s2 = s1 + b1; // line 7
float f2 = 1.2; // line 8
float f3 = 2.2f; // line 9
f3 = s1 + i1; // line 10
}
}
Answer:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Question 6: Please print out the value of a, b,c, i, j, k. (6 points)
int a = 10;
int b = 5;
int i = a++;
int j = --b;
Answer:
a = b =
i = j =
Explanation / Answer
3.) No, they can't access methods C() and D()
Attributes cant be overloaded like methods. For example
Here method invocation depends on the type of actual object. Here object is of type SubClass so method of SubClass is invoked.
While accessing attributes,it depends on the type of reference variable. Here reference variable ie testis of type SuperClass so attribute from SuperClass is accessed.
4. )Result:
0
1
0
1
3
4
5
6
7
8
9
10
int s2 = s1 + b1;
line 8:
double f2 = 1.2;
6.)a =11
b =4
i =10
j =4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.