QN 1- Jack is assigned to implement a class that has a published specification.
ID: 3829390 • Letter: Q
Question
QN 1- Jack is assigned to implement a class that has a published specification. He needed to add a parameter to one of its methods. What best describes this situation? (Note that this is a multiple answer question.)
a) Jack has violated the published contract.
b) Other programmers can easily add the parameter when they wish to use the updated method.
c) Jack should change the published specification.
d) Jack's change may cause other programs to fail to compile.
e) Jack may have found a bug in the published specification.
QN 2- Note: Multiple Answer
Which of the following will add 1 to each element of an appropriately declared, created and initialized int[] iVals?
a) for (int i: iVals)
{
iVals[i]++;
}
b) for (int i: iVals)
{
i = i + 1;
}
c) int i = 0;
while(i < iVals.length)
{
iVals[i++]++;
}
d) int i = iVals.length - 1;
while (i >= 0)
{
iVals[i]++;
i--;
}
e) for (int i = 0; i < iVals.length; i++)
{
iVals[i] += 1;
}
QN- 3 Assume the following classes are each defined in their own java files and compile
public class A {
public void display() {
System.out.println("A's display");
}
}
public class B extends A {
public void display() {
System.out.println("B's display");
}
}
public class C extends A {
public void display() {
System.out.println("C's display");
}
}
public class D {
public static void main(String[] args) {
A a = new C();
System.out.print("A" + (a instanceof A) + " B " + (a instanceof B) + " C " + (a instanceof C) + " Obj " + (a instanceof Object));
}
}
What is output when I run D?---------
Explanation / Answer
QN 1- Jack is assigned to implement a class that has a published specification. He needed to add a parameter to one of its methods. What best describes this situation? (Note that this is a multiple answer question.)
a) Jack has violated the published contract.
Yes, it is true. Signature of method has been already decided and other might be using this method
So changing the signature of method now violates the contract
b) Other programmers can easily add the parameter when they wish to use the updated method.
No, it is not easy to update method call every where in the program.
c) Jack should change the published specification.
No, he should let his client/other programs about this first.
d) Jack's change may cause other programs to fail to compile.
Yes, as described in (a)
e) Jack may have found a bug in the published specification.
Yes, it is also true thats why he want to add a parameter in this method
Ans: a, d, e
QN 2
Which of the following will add 1 to each element of an appropriately declared, created and initialized int[] iVals?
a)
for (int i: iVals)
{
iVals[i]++;
}
Yes, This for loop adding 1 in each element of iVals.
b) for (int i: iVals)
{
i = i + 1;
}
No, it is not changing the content of iVals.
c) int i = 0;
while(i < iVals.length)
{
iVals[i++]++;
}
No, it is also not adding 1 to each element.
d) int i = iVals.length - 1;
while (i >= 0)
{
iVals[i]++;
i--;
}
Yes, it is adding 1 to each element. Loop starts from last element of iVals.
e) for (int i = 0; i < iVals.length; i++)
{
iVals[i] += 1;
}
Yes, it is adding 1 to each element.
iVals[i] += 1 => iVals[i] = iVals[i] + 1;
Ans: a, d, e
Q3)
Ans: Atrue B false C true Obj true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.