Interview Questions 2 Flashcards

1
Q

Could you differentiate an Interface and an Abstract class?

A

An abstract class may have instance methods, which can implement a default behavior. On the other hand, an interface can’t implement any default behavior. However, it can declare different constants and instance methods. While an interface has all the public members, an abstract class contains only class members like private, protected and so on.

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

What are the different ways you can use “Static”?

A

Static can be used in four ways: static variables, static methods, static classes and it can be used across a block of code in any class in order to indicate code that runs when a virtual machine starts and before the instances are created.

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

Do you think all property of Immutable Object needs to be final?

A

Not compulsory as we can easily achieve the same by making member as non-final but private and not changing them except in the constructor. Also, avoid providing setter methods for them. If it is a mutable object, then prevent leaking any reference for that member.

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

Can you brief me about Singleton Class?

A

It basically controls object creation, restricting the number to one while allowing the flexibility to create objects if the scenario changes.

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

In Java, what is the default value of Float and Double?

A

Default value of Float is 0.0f while 0.0d for Double.

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

Explain Dot Operator.

A

It is used to access the instance variables and methods of class objects. Also, we can use it to access classes and sub-packages from a package.

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

What do you mean by Serialization in Java?

A

Well, serialization is nothing but a process of transforming objects into a stream of bytes.

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

Can you tell me the reason why String class is considered immutable?

A

It is because to avoid change in String object once it is created. As String is immutable, you can share it between different threads in a safe way. This is quite crucial in multithreaded programming.

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

What is a Map?

A

Map is an interface

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.

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

What is a HashMap?

A

HashMap is a class that implements a Map. It is unsynchronized and supports null values and keys

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

What is a Hashtable?

A

Hashtable is a synchronized version of HashMap

Hashtable is synchronized, which means Hashtable is thread-safe and can be shared between multiple threads

Don’t use them unless you will be sharing them between threads (if not just use HashMap or ArrayList).

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

What is a TreeMap

A

TreeMap is similar to HashMap but uses Tree to implement Map

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

What is Mulit- threading?

A

Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class.

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

Can you explain when and why Getters and Setters important?

A

We can put setters and getters within interfaces, which can hide implementation details. This allows us to make member variables public in Java.

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

What is difference between Throw and Throws?

A

While Throw is used to trigger an exception, Throws is used in the declaration of exception. It is not possible to handle checked exception without Throws.

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

What is the significance of the order in which catch statements for FileNotFoundException and IOException are written?

A

It is crucial to consider the order as the FileNoFoundException is inherited from the IOException. Therefore, it is important that exception’s subclasses caught first.