Project Management Professional: Core Java Interview Questions!

Tuesday, July 21, 2020

Core Java Interview Questions!

32.

Explain the scenerios to choose between String , StringBuilder and StringBuffer ?


or


What is the difference between String , StringBuilder and StringBuffer ? 

Core Java

If the Object value will not change, use String Class because a String object is immutable.


If the Object value can change and will only be modified from a single thread, use StringBuilder because StringBuilder is unsynchronized(means faster).


If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized).


33.Why don't we use pointers in Java?

Answer: Pointers are considered to be unsafe, and increase the complexity of the program, add

ing the concept of pointers can be contradicting. Also, JVM is responsible for implicit memory allocation; thus, to avoid direct access to memory by the user, pointers are discouraged in Java.

34.What is multiple inheritance? Does Java support multiple inheritance? If not, how can it be achieved?

Answer: If a subclass or child class has two parent classes, that means it inherits the properties from two base classes, it is multiple inheritances. Java does not multiple inheritances as in case if the parent classes have the same method names, then at runtime, it becomes ambiguous, and the compiler is unable to decide which method to execute from the child class.

35.Please explain what do you mean by an Abstract class and an Abstract method?

Answer: An abstract class in Java is a class that can't be instantiated. Such a class is typically used for providing a base for subclasses to extend as well as implementing the abstract methods and overriding or using the implemented methods defined in the abstract class. To create an abstract class, it needs to be followed by the abstract keyword. Any abstract class can have both abstract as well as non-abstract methods. A method in Java that only has the declaration and not implementation is known as an abstract method. Also, an abstract method name is followed by the abstract keyword. Any concrete subclass that extends the abstract class must provide an implementation for abstract methods.

36.What is String Pool in Java?

Answer: The collection of strings stored in the heap memory refers to the String pool. Whenever a new object is created, it is checked if it is already present in the String pool or not. If it is already present, then the same reference is returned to the variable else new object is created in the String pool, and the respective reference is returned.

37.Could you draw a comparison between Array and ArrayList?

Answer: An array necessitates for giving the size during the time of declaration, while an array list doesn't necessarily require size as it changes size dynamically. To put an object into an array, there is the need to specify the index. However, no such requirement is in place for an array list. While an array list is parameterized, an array is not parameterized.

38.What are the default values for local variables?

Answer: The local variables are not initialized to any default value, neither primitives nor object references.


39.Explain different types of typecasting?

The concept of assigning a variable of one data type to a variable of another data type. It is not possible for the boolean data type.


Different types of typecasting are:

  • Implicit/Widening Casting: Storing values from a smaller data type to the larger data type. It is automatically done by the compiler.

  • Explicit/Narrowing Casting: Storing the value of a larger data type into a smaller data type. This results in information loss:

  1. Truncation

  2. Out of Range

Let us see code samples to further understand this:

int i = 10;

long l = i;

long l = 10,000;

int i = (int) l;

float f = 3.14f

int i = (int) f;

i=3;

long l = 123456789;

byte b = (byte) l;








No comments: