Top Java Interview Questions Flashcards

1
Q

Why Java is platform independent?

A

Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.

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

Why Java is not 100% Object-oriented?

A

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

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

What are wrapper classes in Java?

A

Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.

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

What is the importance of reflection in Java?

A

Java Reflection allows you to analyze classes, interfaces, fields, and methods during runtime without knowing what they are called at compile time.

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

Contiguous memory locations are usually used for storing actual values in an array but not in ArrayList. Explain

A

An array generally contains elements of the primitive data types such as int, float, etc. In such cases, the array directly stores these elements at contiguous memory locations. While an ArrayList does not contain primitive data types. An arrayList contains the reference of the objects at different memory locations instead of the object itself. That is why the objects are not stored at contiguous memory locations.

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

Why is synchronization necessary?

A

Java allows multiple threads to execute. They may be accessing the same variable or object. Synchronization helps to execute threads one after another. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

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

Why is it said that the length() method of String class doesn’t return accurate results?

A

length() simply takes into account the number of characters within in the String (and ignores characters outside BMP - U+10000 or above).

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

What are the differences between Heap and Stack Memory in Java?

A

Memory Stack memory is used only by one thread of execution. Heap memory is used by all the parts of the application.
Access Stack memory can’t be accessed by other threads. Objects stored in the heap are globally accessible.
Memory Management Follows LIFO manner to free memory. Memory management is based on the generation associated with each object.
Lifetime Exists until the end of execution of the thread. Heap memory lives from the start till the end of application execution.
Usage Stack memory only contains local primitive and reference variables to objects in heap space. Whenever an object is created, it’s always stored in the Heap space.

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

What is a package in Java? List down various advantages of packages.

A

Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below I have listed down a few of its advantages:

Packages help in avoiding name clashes
They provide easier access control on the code
Packages can also contain hidden classes which are not visible to the outer classes and only used within the package
Creates a proper hierarchical structure which makes it easier to locate the related classes

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

Why pointers are not used in Java?

A
  • Increases complexity of code

- JVM is responsible for implicit memory allocation; don’t want users to have direct access to memory

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

What is JIT compiler in Java?

A

It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute.

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

Inheritance

A

Inheritance is a process where one class acquires the properties of another.

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

Encapsulation

A

Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.

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

Abstraction

A

Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.

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

Polymorphism

A

Polymorphism is the ability of a variable, function or object to take multiple forms.

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

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

A

A local variable is bound to its local scope (i.e., its code block).
An instance variable is bound to an object (or instance thereof).

17
Q

Differentiate between the constructors and methods in Java?

A

Methods Constructors

  1. Used to represent the behavior of an object 1. Used to initialize the state of an object
  2. Must have a return type 2. Do not have any return type
  3. Needs to be invoked explicitly 3. Is invoked implicitly
  4. No default method is provided by the compiler 4. A default constructor is provided by the compiler if the class has none
  5. Method name may or may not be same as class name 5. Constructor name must always be the same as the class name
18
Q

What is final keyword in Java?

A
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.

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

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.
19
Q

Difference between String, StringBuilder, and StringBuffer.

A

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

20
Q

What are the common interfaces of the Collection class

A

List, Queue, Set

21
Q

What is multiple inheritance? Is it supported by Java?

A

If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes.

The problem with multiple inheritance is that if multiple parent classes have the same method name, then at runtime it becomes difficult for the compiler to decide which method to execute from the child class.

Therefore, Java doesn’t support multiple inheritance.

22
Q

What is a marker interface?

A

A Marker interface can be defined as the interface having no data member and member functions. In simpler terms, an empty interface is called the Marker interface

23
Q

Explain Bean in Spring

A

A bean is an object that is instantiated, assembled, and managed by a Spring IoC container.

24
Q

What is autowiring in Spring?

A

Autowiring enables the programmer to inject the bean automatically. We don’t need to write explicit injection logic.

25
Q

What are some of the important Spring annotations which you have used?

A

@Controller – for controller classes in Spring MVC project.
@PathVariable – for mapping dynamic values from the URI to handler method arguments.
@Autowired – for autowiring dependencies in spring beans.
@Service – for service classes.

26
Q

What is a Thread?

A

A thread is the smallest piece of programmed instructions which can be executed independently by a scheduler. In Java, all the programs will have at least one thread which is known as the main thread.

27
Q

What are the two ways to create a thread?

A

In Java, threads can be created in the following two ways:-

By implementing the Runnable interface.
By extending the Thread

28
Q

What is garbage collection?

A

Garbage collection in Java a program which helps in implicit memory management. Since in Java, using the new keyword you can create objects dynamically, which once created will consume some memory. Once the job is done and there are no more references left to the object, Java using garbage collection destroys the object and relieves the memory occupied by it.

29
Q

How would you try all combinations or permutations of an input?

A

Breadth first search: Start with an empty set, then for each element of the input, copy all existing sets, add the element, then add the new sets.

30
Q

What are some disadvantages of dependency injection?

A
  • Compile time issues can be pushed to runtime.

- Many DI frameworks use reflection which can hinder IDE automation.

31
Q

How would you approach a problem with a sorted input (e.g. array, list, matrix)?

A

Use a variation of binary search or two pointers approach.

32
Q

What are some advantages of dependency injection?

A
  • Helps with unit testing.
  • Reduces boilerplate code.
  • Easier to extend applications.
  • Helps enable loose-coupling.
33
Q

What is livelock?

A

Threads reacting to conditions or events generating by themselves (overwhelmed with useless work created by each other).

34
Q

What is starvation?

A

A thread is unable to acquire a resource that another thread has occupied for too long, or has a higher priority.

35
Q

What is the difference between a process and a thread?

A

A process is an independent piece of software in its own memory space.
A thread is part of an application that shares common memory with other threads (common memory allows for faster exchange of data).

36
Q

What is the difference between Runnable and Callable?

A

Runnable has run(): No return value, cannot throw unchecked exceptions.
Callable has call(): Represents a task with a value to be returned. Can throw exceptions.

37
Q

When does the async model perform best?

A
  • Large numbers of task (where at least one can always progress)
  • Tasks involved with a lot of I/O (i.e., easily blocked)
  • Tasks that are largely independent of one another
38
Q

What is JIT?

A

Just In Time compiler - coverts bytecode to machinecode upon method call.

39
Q

What is the String pool?

A
Collection of strings stored in heap memory.
When a string is created (without new String()) the string pool is checked - if it exists, return a reference to that string, else create the object and return a reference to it.