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

inheritance 1) Look at the following code. The method in line ________ will over

ID: 3605142 • Letter: I

Question

inheritance

1) Look at the following code. The method in line ________ will override the method in line ________.

Line 1 public class ClassA

Line 2 {

Line 3 public ClassA() {}

Line 4 public int method1(int a){}

Line 5 public int method2(int b){}

Line 6 }

Line 7 public ClassB extends ClassA

Line 8 {

Line 9 public ClassB(){}

Line 10 public int method1(int b){}

Line 11 public int method2(double c){}

Line 12 }

A) 4, 10 B) 5, 11 C) 10, 4 D) 11, 5 E) Both A and B

2) Look at the following code. Which line will cause a compiler error?

Line 1 public class ClassA

Line 2 {

Line 3 public ClassA() {}

Line 4 public int method1(int a){}

Line 5 public final int method2(double b){}

Line 6 }

Line 7 public ClassB extends ClassA

Line 8 {

Line 9 public ClassB(){}

Line 10 public int method1(int b){}

Line 11 public int method2(double c){}

Line 12 }

A) 4 B) 5 C) 10 D) 11

3) Protected members are:

A) not quite private B) not quite public C) both A and B D) neither A nor B

4) Look at the following code:

Line 1 public class ClassA

Line 2 {

Line 3 public ClassA() {}

Line 4 public void method1(int a){}

Line 5 }

Line 6 public class ClassB extends ClassA

Line 7 {

Line 8 public ClassB(){}

Line 9 public void method1(){}

Line 10 }

Line 11 public class ClassC extends ClassB

Line 12 {

Line 13 public ClassC(){}

Line 14 public void method1(){}

Line 15 }

Which method1 will be executed when the following statements are executed?

ClassA item1 = new ClassB(); item1.method1();

A) Line 4 B) Line 9 C) Line 14 D) This is an error and will cause the program to crash.

5) Look at the following code:

Line 1 public class ClassA

Line 2 {

Line 3 public ClassA() {}

Line 4 public void method1(int a){}

Line 5 }

Line 6 public class ClassB extends ClassA

Line 7 {

Line 8 public ClassB(){}

Line 9 public void method1(){}

Line 10 }

Line 11 public class ClassC extends ClassB

Line 12 {

Line 13 public ClassC(){}

Line 14 public void method1(){}

Line 15 }

Which method will be executed when the following statements are executed?

ClassC item1 = new ClassA(); item1.method1();

A) Line 4 B) Line 9 C) Line 14 D) This is an error and will cause the program to crash

6) What is wrong with the following code?

IntCalculator square = new IntCalculator() {

public int calculate(int number)

{

return number * number;

}}

A) The outer braces are not needed.

B) The inner braces are not needed.

C) The new key word is not needed.

D) The statement does not end with a semicolon.

7) Which of the following is an example of a lambda expression?

A) int x = x * factor;

B) IntCalculator = new divider(x, 2);

C) IntCalculator multiplier = x -> x * factor;

D) All of the above

TRUE OR FALSE

8) When an interface variable references an object, you can use the interface variable to call any and all of the methods in the class implementing the interface.

9) A compiler error will result if an anonymous inner class tries to use a variable that is not final, or not effectively final.

10) A functional interface is simply an interface that has one abstract method.

Explanation / Answer

1) Answer is option A:

Line 4 will override Line 10

method1(int) of class A have same signature

in method1(int) of class B.

2) Answer is option D:

Line 11 cause compiler error, because final method

cant be overridden in sub class.

3) Answer is Option C:

Because, Private variables will have scope and visibility

in the class itself, on the other side public has scope and

visibility in every package.

Unlike protected variables hav scope in class, current package

also other classes in other package only if they are inherited

to one another.

4) Answer is option B:

Line 9 will get executed, because ClassB is sub class of ClassA

and ClassA is instantiated with reference of classB, So, ClassB

instance is created and ClassB's method1() will be called. It is

called Widening.

5) Answer is option D:

This cause error at compile time, because super class ClassA is

created with reference to sub class ClassC. It is conceptually

wrong and not worthfull. It is called as narrowing.

6) Answer is option D:

The statement does not end with a semicolon. Since, we are

creating anonymous inner class type of either interface or

abstract class, the abstract methods are overridden inside

class. And the instance of inner type should be terminated

from with a semicolon.

7) Answer is option C:

IntCalculator multiplier = x -> x * factor;

Because, it is of format : {argument-list} -> {body}

8) Answer is True:

The implementation classes of interface can access

any methods and properties beacause it acts as super

class type.

9) Answer is True:

An anonymous inner class can access final variables only.

Example:

public class ClassC {

public static void main(String[] args) {

int x= 10; // Should be final

ClassA ca = new ClassA() {

@Override

public void method1() {

System.out.println(x); //Error raises

}

};

}

}

10) True : A functional interface will have one abstract method.

Which is represented as anotation @FunctionalInterface