Part 3 Flashcards

1
Q

How the declaration of abstract method looks like? What do you know about implementation of abstract method?

A

Abstract method has only declaration and it doesn’t have implementation. (note it is declared without curly braces {} )
public abstract int getNoWheels();
Child classes are responsible to provide implementation.

abstract class Wheel {
  public abstract void methodOne();
  public abstract void methodTwo(); 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What if a class extends any abstract class?

A
If a class extends any abstract class then compulsory we should provide implementation for every abstract method of the parent class otherwise we have to declare child class as abstract. 
If we declare child as abstract then the code compiles fine but child of child is responsible to provide implementation for all abstract methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which modifiers can’t be used together with abstract modifier and why?

A

final, static, syncronised, native, strictfp, private

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

What is the difference between final and abstract with respect to methods and classes?

A
For abstract methods we should override it in the child class to provide implementation. Whereas for final methods we can't override it hence abstract final combination is illegal for methods. 
For abstract classes we should create child class to provide implementation whereas for final class we can't create child class. Hence final abstract combination is illegal for classes. 
Final class cannot contain abstract methods whereas abstract class can contain final methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

From where the member is visible if it is declared as public. What we need to pay attention when we are trying to access the member from another class, package?

A

Before checking whether member is accessible outside of package, be sure that class is also accessible outside of package. Even if member is declared as public but class is visible only from the same package, you will take compile time error when you will try to use that member inside anothor package.

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

What if memeber is declared as private? What kind of methods can’t be marked with private?

A

It is accessible only within the same class.
Private methods are not visible in child classes where abstract methods should be visible in child classes to provide implementation, hence private - abstract combination is illegal for methods.

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

From where is member visible if it is declared as protected?

A

Within current package or in child classes from outside
We can access protected members within the current package anywhere either by child reference or by parent reference.
But from outside package we can access protected members only in child classes and should be by child reference only that is we can’t use parent reference to call protected members from outside package.

package pack1; 
public class A {
  protected void methodOne() {
     System.out.println("methodOne is executed");
  }
}
package pack2; 
class B extends A {
   public static void main(String[] args) {
      A a = new A(); 
      a.methodOne(); // compile error because protected method can't be                           
                                    // used in parent reference in child in outside package  
      B b = new B();
      b.methodOne(); // this is ok 
      A a1 = new B(); 
      a1.methodOne(); // compile time error due to usage of protected member in parent type reference 
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which access modifiers we can use with local variable (declared inside the method)?

A

For local variable (declared within method) only the final modifier is available - you can’t use public, private, protected modifiers on local variable, it will give you error in such case.
When you declare local variable with final, you can’t change it then (like constant).

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

What kind of objects in java could be declared with strictfp modifier?

A

strictfp is the modifier applicable for methods and classes but not for variables

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

What is the strictfp modifier use for?

A

usually the result of floating point of arithmetic is varing from platform to platform, to overcome this porblem we should use stritfp modifier
if a method declare as the strictfp then all the floating point calculations in that method has follow IEEE754 standard, so that we will get platform independent results

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

What is it mean when class is declared with strictfp modifier?

A

If a class is declared as strictfp then every concrete method(which has body) of that class has to follow IEEE754 standard for floating point arithmetic.

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

What is the difference between abstract and strictfp? When we can use the abstract strictfp combination and when not?

A
strictfp method talks about implementation where as abstract method never talks about implementation hence abstract, strictfp combination is illegal for the methods. 
But we can declare a class with abstract and strictfp modifiers simultaneously. That abstract strictfp combination is legal for classes but illegal for methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can we declare a formal parameters of methods as final?

A

The formal parameters of a method simply acts as local variables of that method, hence it is possible to declare formal parameters as final. If we declare formal parameters as final then we can’t change its value within the method.

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

What kind of objects can be declared with static modifier?

A

static is the modifier applicable for methods, variables and blocks. We can’t declare a class with static but inner classes can be declaring as the static

For instance and static variables JVM will provide default values but if instance and static declared as final JVM won’t provide default value compulsory we should perform initialization whether we are using or not . For the local variables JVM won’t provide any default values we have to perform explicitly before using that variables , this rule is same whether local variable final or not.

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

Which variables can be declared as static and what is it mean?

A
In the case of instance variables for every object a separate copy will be created but in the case of static variables a single copy will be created at class level and shared by all objects of that class. 
Instance variables can be accessed only from instance area directly and we can't access from static area directly. But static variables can be accessed from both instance and static areas directly.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can be combination of static abstract use for methods?

A

static abstract combination is illegal for methods

17
Q

Can we overload the static main(String[] args) method?

A
Overloading concept is applicable for static method including main method also.But JVM will always call String[] args main method . The other overloaded method we have to call explicitly then it will be executed just like a normal method call . 
Inheritance concept is applicable for static methods including main() method hence while executing child class, if the child doesn't contain main() method then the parent class main method will be executed.
18
Q

Which kinds of objects can be declared with native modifier?

A

native is a modifier applicable only for methods but not for variables and classes

19
Q

What is it mean when the native modifier is used?

A
native is a modifier applicable only for methods but not for variables and classes. The methods which are implemented in non java are called native methods or foreign methods
For native methods implementation is already available where as for abstract methods implementation should not be available child class is responsible to provide that, hence abstract native combination is illegal for methods. 

We can’t declare a native method as strictfp because there is no guaranty whether the old language supports IEEE754 standard or not. That is native strictfp combination is illegal for methods.
For native methods inheritance, overriding and overloading concepts are applicable.

20
Q

What kinds of object can be declared with synchronised modifier?

A

Synchronized is the modifier applicable for methods and blocks but not for variables and classes.

21
Q

What is it mean when method is declared as synchronised?

A

If a method or block declared with synchronized keyword then at a time only one thread is allow to execute that method or block on the given object.

22
Q

Can we use abstract and synchronised modifiers simultaneously?

A

For syncronized methods compulsory implementation should be available, but for abstract methods implementation won’t be available, Hence abstract - syncronized combination is illegal for methods.

23
Q

Which kind of objects can be declared with transient modifier?

A

Transient is the modifier applicable only for variables but not for methods and classes

24
Q

What is it mean when variable is declared as transient?

A

At the time of serialization if we don’t want to serialize the value of a particular variable to meet the security constraints then we should declare that variable with transient modifier.
At the time of serialization jvm ignores the original value of the transient variable and save default value that is transient means “not to serialize”.
Static variables are not part of object state hence serialization concept is not applicable for static variables duo to this declaring a static variable as transient there is no use.
Final variables will be participated into serialization directly by their values due to this declaring a final variable as transient there is no impact.

25
Q

Which kinds of objects can be declared with volatile modifier?

A

Volatile is the modifier applicable only for variables but not for classes and methods.

26
Q

What is it mean when variable is declared with volatile modifier?

A

If the value of variable keeps on changing such type of variables we have to declare with volatile modifier.
If a variable declared as volatile then for every thread a separate local copy will be created by the jvm, all intermediate modifications performed by the thread will takes place in the local copy instead of master copy.
Once the value got finalized before terminating the thread that final value will be updated in master copy.
The main advantage of volatile modifier is we can resolve data inconsistency problems, but creating and maintaining a separate copy for every thread increases complexity of the Programming and effects performance of the system. Hence if there is no specific requirement never recommended to use volatile modifier and it’s almost outdated.
Volatile means the value keep on changing where as final means the value never changes hence final volatile combination is illegal for variables.

27
Q

Which are the modifiers that are applicable for inner classes but not for outer classes?

A

private, protected, static

28
Q

Which are the modifiers that are applicable only for methods?

A

native

29
Q

Which are the modifiers applicable only for variables?

A

transient, volatile

30
Q

Which are the modifiers applicable for constructors?

A

public, private, protected, default

31
Q

Which are the modifiers applicable for local variable?

A

final

32
Q

Which are the modifiers that are available for classes, but not for enums?

A

final, abstract