Which process, a local variable has the same name as one of the instance variables?

21. Which of the following tool is used to generate API documentation in HTML format from doc comments in source code?

  1. javap tool
  2. javaw command
  3. Javadoc tool
  4. javah command

Answer : C
Explanation: The Javadoc is a tool that is used to generate API documentation in HTML format from the Java source files. In other words, it is a program (tool) that reads a collection of source files into an internal form.
The Javadoc command line syntax is,
Javadoc [options] [packagenames] [sourcefiles] [@files]

22. To successfully overload a method in Java, the method names must be

  1. Same
  2. Different
  3. Same or different based on the situation
  4. None

Answer : A
Explanation: To successfully overload a method in Java, the method names must be the same.

23. Which of the following is a garbage collection technique?

  1. Sweep model
  2. Space management model
  3. Mark and sweep model
  4. Cleanup model

Answer : C
Explanation: A mark and sweep garbage collection consists of two phases, the mark phase, and the sweep phase. I mark phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are garbage. In the sweep phase, the heap is traversed to find gaps between live objects, and the gaps are marked free lists used for allocating memory to new objects so option C is correct.

24. When finally block gets executed?

  1. Only when exception occurs in try block code
  2. Always when try block get executed, no matter exception occured or not
  3. Always when a method get executed, no matter exception occured or not
  4. Always when a try block get executed, if exception do not occur

Answer : B
Explanation: Finally block gets executed always when try block get executed, no matter exception occured or not.

25. In which process, a local variable has the same name as one of the instance variables?

  1. Variable Shadowing
  2. Serialization
  3. Abstraction
  4. Multi-threading

Answer : A
Explanation: Variable Shadowing is a process in which a local variable has the same name as one of the instance variables.

26. Which method must be implemented by all threads?

  1. run()
  2. stop()
  3. start()
  4. wait()

Answer : A
Explanation: All threads must implement the run() method.

27. Iterator returned by ConcurrentSkipListSet is

  1. Fail-fast
  2. Fail-safe
  3. Both of the above
  4. None of the above

Answer : B
Explanation: Iterator returned by ConcurrentSkipListSet is Fail-safe.

28. Which statement transfer execution to different parts of your code based on the value of an expression?

  1. if
  2. nested-if
  3. switch
  4. if-else-if

Answer : C
Explanation: switch statement transfer execution to different parts of program’s code based on the value of an expression.

29. Method Overriding is an example of

  1. Static binding
  2. Dynamic binding
  3. Both of the above
  4. None

Answer : B
Explanation: Method Overriding is an example of dynamic binding.

30. Which is a valid declaration of a String?

  1. String s1 = null;
  2. String s2 = ‘null’;
  3. String s3 = (String) ‘abc’;
  4. String s4 = (String) ‘\ufeed’;

Answer : A
Explanation: Option A is correct because it sets the String reference to null. Other options are wrong because null cannot be in single quotes, there are multiple characters between the single quotes (‘abc’), and can’t cast a char (primitive) to a String (object).

Pages: 1 2 3

Java allows us to declare a variable whenever we need it. We can categorize all our variables into three categories, which have different-different scopes:

  1. Instance variables: defined inside a class and have object-level scope.
  2. Class variables: defined inside a class with the static keyword. They have class-level scope common to all objects of the same class.
  3. Local variables: defined inside a method or in any conditional block. They have block-level scope and are only accessible in the block where they are defined.

What Is Variable Shadowing?

Variable shadowing happens when we define a variable in a closure scope with a variable name that is the same as one for a variable we've already defined in an outer scope.

In other words, when a local variable has the same name as one of the instance variables, the local variable shadows the instance variable inside the method block.

In the following example, there is an instance variable named x, and inside method printLocalVariable(), we are shadowing it with the local variable x.

class Parent {

    // Declaring instance variable with name `x`
    String x = "Parent`s Instance Variable";

    public void printInstanceVariable() {
        System.out.println(x);
    }

    public void printLocalVariable() {
        // Shadowing instance variable `x` with a local variable with the same name
        String x = "Local Variable";
        System.out.println(x);

        // If we still want to access the instance variable, we do that by using `this.x`
        System.out.println(this.x);
    }
}

What Is variable Hiding?

Variable hiding happens when we define a variable in a child class with the same name as one we defined in the parent class. A child class can declare a variable with the same name as an inherited variable from its parent class, thus hiding the inherited variable.

In other words, when the child and parent classes both have a variable with the same name, the child class' variable hides the parent class' variable.

In the following example, we are hiding the variable named x in the child class while it is already defined by its parent class.

class Child extends Parent {

    // Hiding the Parent class's variable `x` by defining a variable in the child class with the same name.
    String x = "Child`s Instance Variable";

    @Override
    public void printInstanceVariable() {
        System.out.print(x);

        // If we still want to access the variable from the super class, we do that by using `super.x`
        System.out.print(", " + super.x + "\n");
    }
}

Variable Hiding Is Not the Same as Method Overriding

While variable hiding looks like overriding a variable (similar to method overriding), it is not. Overriding is applicable only to methods while hiding is applicable to variables.

In the case of method overriding, overridden methods completely replace the inherited methods, so when we try to access the method from a parent's reference by holding a child's object, the method from the child class gets called. You can read more about overriding on Everything About Method Overloading vs Method Overriding, Why We Should Follow Method Overriding Rules, and How Does the JVM Handle Method Overloading and Overriding Internally.

But in variable hiding, the child class hides the inherited variables instead of replacing them, so when we try to access the variable from the parent's reference by holding the child's object, it will be accessed from the parent class.

When an instance variable in a subclass has the same name as an instance variable in a super class, then the instance variable is chosen from the reference type.
public static void main(String[] args) throws Exception {

    Parent parent = new Parent();
    parent.printInstanceVariable(); // Output - "Parent's Instance Variable"
    System.out.println(parent.x); // Output - "Parent's Instance Variable"

    Child child = new Child();
    child.printInstanceVariable();// Output - "Child's Instance Variable, Parent's Instance Variable"
    System.out.println(child.x);// Output - "Child's Instance Variable"

    parent = child; // Or parent = new Child();
    parent.printInstanceVariable();// Output - "Child's Instance Variable, Parent's Instance Variable"
    System.out.println(parent.x);// Output - Parent's Instance Variable

    // Accessing child's variable from parent's reference by type casting
    System.out.println(((Child) parent).x);// Output - "Child's Instance Variable"
}

In above example, when we call the overridden method printInstanceVariable()onparent while holding the child's object in it, we can see the output is:

Child's Instance Variable, Parent's Instance Variable

Because in the child class, the method is printing the child class' x variable and super.x.

But when we call System.out.println(parent.variable); on the same parent reference that is holding the child's object, it prints Parent's Instance Variable because the new Child() object keeps the parent's x as well as the child's x and hides the parent's x. So, in this case, x is chosen from the class that is the reference type.

But if we wanted to access the child's variable even if we are using a parent reference, we can do that by using (Child) parent).variable.

When our variables are private or in another package and have default access, such variables are not visible outside of that class, and the child class cannot access them. So there no confusion — and that is why we should always stick to General Guidelines to create POJOs and declare our variables with private access (and also provide proper get/set methods to access them).

Do you want to know more about variable hiding?, In the article Why Instance Variable Of Super Class Is Not Overridden In SubClass, I have discussed why variables do not follow overriding, why variable hiding is not designed same as method overriding and why instance variable is chosen from reference type instead of the object? Please go ahead and read it.

You can find the complete code on this GitHub Repository and please feel free to provide your valuable feedback.

In which process a local variable has the same name as one of the instance?

17) In which process, a local variable has the same name as one of the instance variables? Explanation: There are following reasons for considering a variable shadowing, they are listed below: When we define a variable in a local scope with a variable name same as the name of a variable defined in an instance scope.

Are local variables and instance variables the same?

Local Variable: These variables are declared within a method but do not get any default value. ... Difference between Instance Variable and Local Variable..

Can method and instance variable have same name?

Yes it's fine, mainly because, syntactically , they're used differently.

When a local variable in an instance method has the same name as an instance field the instance field hides the local variable?

A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field.