Interview Questions 2 Flashcards
Could you differentiate an Interface and an Abstract class?
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.
What are the different ways you can use “Static”?
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.
Do you think all property of Immutable Object needs to be final?
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.
Can you brief me about Singleton Class?
It basically controls object creation, restricting the number to one while allowing the flexibility to create objects if the scenario changes.
In Java, what is the default value of Float and Double?
Default value of Float is 0.0f while 0.0d for Double.
Explain Dot Operator.
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.
What do you mean by Serialization in Java?
Well, serialization is nothing but a process of transforming objects into a stream of bytes.
Can you tell me the reason why String class is considered immutable?
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.
What is a Map?
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.
What is a HashMap?
HashMap is a class that implements a Map. It is unsynchronized and supports null values and keys
What is a Hashtable?
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).
What is a TreeMap
TreeMap is similar to HashMap but uses Tree to implement Map
What is Mulit- threading?
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.
Can you explain when and why Getters and Setters important?
We can put setters and getters within interfaces, which can hide implementation details. This allows us to make member variables public in Java.
What is difference between Throw and Throws?
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.