Categories: Java

Q.1. What is the difference between a local variable and an instance variable?

Ans. In Java, a local variable is typically used inside a method, constructor, or a block and has only local scope. Thus, this variable can be used only within the scope of a block. The best benefit of having a local variable is that other methods in the class won’t be even aware of that variable.

 

Q.2. What is final keyword in Java?

Ans. final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:

 

(i) final variable:

When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

 

(ii) final method:

When a method is declared final then it can’t be overridden by the inheriting class.

 

(iii) final class:

When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.

 

Q.3. What is Java String Pool?

 

Ans. Java String pool refers to a collection of Strings which are stored in heap memory. In this, whenever a new object is created, String pool first checks whether the object is already present in the pool or not. If it is present, then the same reference is returned to the variable else new object will be created in the String pool and the respective reference will be returned.

 

Q.4. Differentiate between the constructors and methods in Java?

Ans.

Methods:

  1. Used to represent the behavior of an object
  2. Must have a return type
  3. Needs to be invoked explicitly
  4. No default method is provided by the compiler
  5. Method name may or may not be same class name

 

Constructors:

  1. Used to initialize the state of an object
  2. Do not have any return type
  3. Is invoked implicitly
  4. A default constructor is provided by the compiler if the class has none
  5. Constructor name must always be the same as the as class name

 

Q.5. What is constructor chaining in Java?

 

Ans. In Java, constructor chaining is the process of calling one constructor from another with respect to the current object. Constructor chaining is possible only through legacy where a subclass constructor is responsible for invoking the superclass’ constructor first. There could be any number of classes in the constructor chain. Constructor chaining can be achieved in two ways:

  1. Within the same class using this()
  2. From base class using super()

 

Q.6. What is the difference between break and continue statements?

Ans.

break

  1. Can be used in switch and loop (for, while, do while) statements
  2. It causes the switch or loop statements to terminate the moment it is executed
  3. It terminates the innermost enclosing loop or switch immediately

continue

  1. Can be only used with loop statements
  2. It doesn't terminate the loop but causes the loop to jump to the next iteration
  3. A continue within a loop nested with a switch will cause the next loop iteration to execute

 

Q.7. What is the difference between this() and super() in Java?

Ans. In Java, super() and this(), both are special keywords that are used to call the constructor.

 

this()

  1. this() represents the current instance of a class
  2. Used to call the default constructor of the same class
  3. Used to access methods of the current class
  4. Used for pointing the current class instance
  5. Must be the first line of a block

 

super()

  1. super() represents the current instance of a parent/base class
  2. Used to call the default constructor of the parent/base class
  3. Used to access methods of the base class
  4. Used for pointing the superclass instance
  5. Must be the first line of a block

 

Q.8. Differentiate between static and non-static methods in Java.

Ans.

Static Method

  1. The static keyword must be used before the
  2. It is called using the class (className.methodName)
  3. They can't access any non-static instance variables or methods

 

Non-Static Method

  1. No need to use the static keyword before method name the method name
  2. It is can be called like any general method
  3. It can access any static method and any static variable without creating an instance of the class

 

Q.9. Difference between String, StringBuilder, and StringBuffer.

Ans.

Factor

String

StringBuilder

StringBuffer

Storage Area

Constant String Pool

Heap Area

Heap Area

Mutability

Immutable

Mutable

Mutable

Thread Safety

Yes

No

Yes

Performance

Fast

More efficient

Less efficient

 

Q.10. Why Java Strings are immutable in nature?

 

Ans. In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.

R4R.in Team
The content on R4R.in website is created by expert teams.
|| Blockchain || Cisco CCNA || Selenium || Internet security || Thermostat || Fashion || Online Games || Working From Home(WFH) || COVID-19 || Games || Cryptocurrency || Digital economy || Property || Sundarban National Park || NEET Course || Investment || Java || Inverter battery || PDF Drive || Online Library || Yoga || Career || Sports || SEO || Software Engineering || APPs || Digital Marketing || Certified True Copy || Mobiles || Software Testing || Books || TAX || Foreign Currency || AutoCAD || Online Course || Machine || Python || Banks || Bitcoin || Education || German Language || Festivals || Health ||