Consider the following code: list1 = [1,2,3,4,5] list2 = list1 list2 = 1 print(list1) print(list2) What will be the result? And why?
The result will be [1,1,3,4,5] [1,1,3,4,5] The reason is that the variable stands for a list is actually a value that points to the block of memory of the list. When list2=list1 gets executed, the "pointer" value get assigned to list2. Thus they stands for the same memory of the list. When we change the element in either list1 or list2, we are changing the same list. That's why the two print statement give the same result, [1,1,3,4,5].
What are the differences between abstract class and interface in Java?
As we know, both abstract class and interface in Java are used as blueprints to achieve abstraction. They look similar due to the fact that there are usually more declarations than implementation in both cases. For abstract class, it can have abstract and non-abstract methods, doesn't support multiple inheritance, can have final, non-final, static and non-static variables, can provide the implementation of interface. However, for the interface, it can have only abstract methods. Since Java 8, it can have default and static methods also. And it supports multiple inheritance, has only static and final variables.
What do public, protected and privated inheritance do in C++ Inheritance?
In C++ public inheritance, the child class will inherite the members with the same accessor from the parent's. In the protected inheritance, the child class will inherite the public members in the parent class as protected members. The protected members will be kept the same way. At last, in the private inheritance, the child class will inherite the public and protected members in the parent class as private members. In all cases, the private members will always be kept inside its own class and cannot be access from outside the class.