Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer the Questions and write the explanation. Question 33 A non abstract subcl

ID: 3865895 • Letter: A

Question

Answer the Questions and write the explanation.

Question 33

A non abstract subclass extending an abstract superclass must provide the implementation for all direct abstract superclass abstract methods.

True

False

Question 37

True or False, a method from a subclass that overrides a superclass method having the same method signature cannot call that superclass method from within the overriding subclass method body.

True

False

2.5 points

Question 38

For Question 38 consider the following two classes:

     public abstract class C
     {
          private void foo1()
          {
               System.out.println( “Hello foo1” );
          }
          public abstract void foo2();
          public abstract int foo3();
          public void foo1Call()
          {
               foo1();
          }
     }
     public class D extends C
     {
          public void foo2()
          {
               System.out.println( “Hello foo2” );
          }
          public int foo3()
          {
               return 10;
          }
          private void foo4()
          {
               System.out.println( “Hello D foo4()” );
          }
     }

To instantiate an object of class C we could use the following statement(s):

C c2;
c2 = new C();

C c2 = new C();

a or b

none of the above

2.5 points

Question 39

For Question 39 consider the following two classes:

     public abstract class C
     {
          private void foo1()
          {
               System.out.println( “Hello foo1” );
          }
          public abstract void foo2();
          public abstract int foo3();
          public void foo1Call()
          {
               foo1();
          }
     }
     public class D extends C
     {
          public void foo2()
          {
               System.out.println( “Hello foo2” );
          }
          public int foo3()
          {
               return 10;
          }
          private void foo4()
          {
               System.out.println( “Hello D foo4()” );
          }
     }

Which of the following code sequences, if any, will successfully access private method foo1 in class C?

C c2 = new C();
c2.foo1();

C c2 ;
c2 = new D();
c2.foo1();

D d1 = new D();
d1.foo1();

C c1 = new D();
c1.foo1Call();

None of the above

2.5 points

Question 40

Assuming a StringIndexOutOfBoundsException exception class exists and is a subclass of IndexOutofBoundsException, what is the output of this code sequence?

try
{
     String word = new String("avaJ");
     System.out.println( word.charAt( 3 ) );
}

catch( StringIndexOutOfBoundsException e )
{
     System.out.println( “OOPS! ” );
}

     catch( IndexOutOfBoundsException ie )
{
     System.out.println( ie.getMessage() );
}

finally()
{
     System.out.println("I’d rather be sailing ");
}

OOPS!
A message indicating the cause of the exception thrown
I’d rather be sailing

J
I’d rather be sailing

OOPS!

I’d rather be sailing

J

a.

C c2;
c2 = new C();

b.

C c2 = new C();

c.

a or b

d.

none of the above

Explanation / Answer

33. a) True. Because abstract methods only have declaration in abstract class and everything that is declared needs to be defined also. So, abstract methods needs to be defined in child of abstract class.

37. b) False.. We can call super call method using super keyword.

38. d) None of these. Because abstract class are not concrete classes. They cannot be instantiated. We can just instantiate the class which extends the abstract class.

39. d) C c1 = new D();
c1.foo1Call();

This is because as C is abstract , we cannot instantiate it. Rather we need to instantiate the the D class using C. Futher foo1() is private so it cannot be accesed bu D. so it will call foo1Call() which calls foo1() function indirectly.

40. the output shoudl be b) J
I’d rather be sailing

As word.charAt(3) will return 4th character as characters start from 0. and 4th Character is J in the given string word. And finally block is executed always at the end of try or catch.

Note that there is one issue in code though .. finally is not a method its a block, So finally() should be changed to finally by removing ()