Chapter 10 - Identifying Errors Flashcards

1
Q
  1. Where is the error in this code sequence?

C c1 = new C( );

A

Correction:

D d1 = new D();

Error:

◊ The error is with instantiating abstract class object.
◊ C class is abstract, So cannot instantiate.
◊ To correct this , instantiate D object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Where is the error in this code sequence?
D d1 = new D( );
d1.foo1( );
A

The correct statement is follows:
d1.foo2();

Error:
◊ The error is with calling different class method.
◊ foo1() method is not available in D class.
◊ To correct this , call foo2 method.
◊ The foo1 method of class C is private and is not inherited by D.
◊ a D object
reference cannot call foo1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Is there an error in this code sequence? Why or why not?
C c2;
c2 = new D( );
A

The program doesn’t contain any errors.

Reason for no errors:

◊ C is abstract class so we can instantiate using through child class D.
◊ With this instance we can call abstract class C methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Where is the error in this new class?
public class E extends D
{
 public void foo4( )
 {
  super.foo4( );
  System.out.println( "Hello E foo4()" );
 }
}
A

The correct statement is follows:

super.foo3(); // Line 5

Error:

◊ In Parent class D the foo4() method declared as private.
◊ Private methods have scope within class. So cannot use from other class.
◊ To correct this , call super.foo3() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Where is the error in this class?
public class J extends I
{
}
A

The correct statement is follows:

Public class J implements I { } // Line 1

Error:

◊ Interface cannot be extended.
◊ Interface must be implemented.
◊ To correct this, instead of extends keyword use implements keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Where is the error in this class?
public class K
{
 public void foo( );
}
A

The foo method does not have a method body; it must be declared abstract. The K class must be declared abstract as well.

The correct statement is follows:

Public void foo() {} // Line 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly