Interview Questions : PART 3
Question 14: What is content negotiation? Explain with example.
Suppose we want to visit a site for any kind of recipe, a recipe can be represented in different languages like English, German or may be other and their format for presentation can also differ from HTML to PDF or may be Plain text
In this case when an client makes an HTTP request to a server client can also specify the media types here client can specify what it can accept back from host and on the basis of availability the host will return to the client so that’s why it is called as content negotiation.
In terms of language you can see content negotiation while changing language preference in google.com.
Note: This content negotiation plays a vital role when developer deal with web services. A piece of code written in Java script can make request to server and ask for a JSON representation. Meanwhile a piece of code written in C++ can make a request to same server to same URL and ask for an XML representation of a resource. And in both case host will satisfy the request and information will arrive at client in ideal format for parsing.
Question 15: Encapsulation
1) Encapsulation helps to bind all variables and methods into single unit called as class
2) Writing a simple class with public instance variable and methods is also called as encapsulation but in oops we need to focus on the proper encapsulation.
3) Encapsulation provides an ability to make changes in our implementation code without breaking the code of others who use our code.
4) Encapsulation helps to write more flexible and maintainable code.
5) Encapsulation solves the problem at implementation level and hide the implementation and protect the data from outer world (which can modify our code) using access modifier. Opposite of that abstraction solves the problem at design level.
Consider a scenario:
Suppose you create a library and release that, then there will be client who will be using that library and will integrate in his application and will make bigger library. Now if we will not focus on the proper encapsulation then the client code can access our code and bring changes.
Next time when we will be distributing our library to other client, then client will not get what we have created that and that alter code can restrict the client to get the functionality for what you have created your library.
So proper encapsulation means the client should not access the internal implementation of our code and could not bring changes.
And being a library developer we can bring changes in our code without worrying about how it will affect the client code.
Question 16: One liner for some design principle:
1) Encapsulate what varies:
from your application perspective identify those aspects which can vary and take out varying components and encapsulate them as separate entity.It will allow you to alter and extends those varying parts without affecting other parts.
2) Program to interface instead of implementation
try to use the concept of Interface and use polymorphism concept for the supertype so that actual runtime object could not get locked in the implementation code
3) Favor composition instead of composition
this technique will lead to change the behavior at runtime
4) Always bend your coding style towards loose coupling
loosely coupled designs allow you to build flexible applications that can handle change because they minimize the inter-dependency between objects.
5) The hollywood principle
Don’t call us , we will call you, this is initiation of dependency injection principle.this principle allow low-level components to attach themselves into a system, but the high-level components determine when they are needed, and how.
6) Single responsibility principle
you should design your classes so that each has a single responsibility. When a class has multiple responsibilities, then there can be chances that it will need to be changed increases.
7) Open closed principle
Classes should be open for extension, but closed for modification. class should allow its behavior to be modified without altering its source code.
8) Interface segregation principle.
Clients should not be forced to depend upon interfaces that they don’t use.
9) Dependency inversion.
Depend upon abstractions. Do not depend upon concrete classes. high-level modules should not depend on low-level modules, both should depend on abstractions. Abstractions should not depend on details, details should depend on abstractions.
Question 17: When we write any Class in eclipse, most of times eclipse generates a warning in terms of serialVersionUID. So what that means? In which scenario that is useful?
- When we write a class then may be scenario where we can serialize that in that case serialVersionUID comes into light.
- For classes that implement java.io.Serializable, the serialVersionUID plays a major role in versioning the class.
- This stream-unique identifier is a 64-bit hash of the class name, interface class names, methods, and fields. Technically speaking, if the above aspects match, the classes are of “same version.
- For example, you create an Employee class with a first name, last name, and employee ID, and then serialize a copy of an employee to a file. If you add a “number of dependants” attribute to the class, you will not be able to read the saved file back into the new Employee class, even though all saved attributes are still part of the new class.
- If you try to read a previously stored employee object into the preceding modified Employee class, you will receive a java.io.InvalidClassException and be told that the classes are incompatible.
- The reason for this is that all classes have serial numbers, and the serial number for the Employee class is stored with a serialized object. When you read the object in, the JVM detects you are trying to populate a class with a different serial number, and it throws the exception.
— this is scenario where class version plays a great role and this is scenario which explains uses of serialVersionUID.
Question 18: How data types are handled by JVM or how data types are linked with jvm?
- As there are 8 different primitive data types are present in java and besides that there is also a reference type . The Java virtual machine computes by performing operations on certain types of data. Both the data types and operations are strictly defined by the Java virtual machine specification.
- All the primitive types of the Java programming language are primitive types of the Java virtual machine.The Java virtual machine specification defines the range of values for each of the data types, but does not define their sizes.
- Some points about boolean primitive type:
- a) When a compiler translates Java source code into bytecodes, it uses ints or bytes to represent booleans.
- b) In the Java virtual machine, false is represented by integer zero and true by any non-zero integer.
- c) Operations involving boolean values use ints. Arrays of boolean are accessed as arrays of byte, though they may be represented on the heap as arrays of byte or as bit fields.
- The primitive types of the Java virtual machine have the same range everywhere. A long in the Java virtual machine always acts like a 64-bit signed twos complement number, independent of the underlying host platform.
- The Java virtual machine works with one other primitive type that is unavailable to the Java programmer: the return Address type. This primitive type is used to implement finally clauses of Java programs.
- Values of type reference come in three flavors: the class type, the interface type, and the array type. All three types have values that are references to dynamically created objects. The class type’s values are references to class instances.
- The array type’s values are references to arrays, which are full-fledged objects in the Java virtual machine. The interface type’s values are references to class instances that implement an interface.
- The basic unit of size for data values in the Java virtual machine is the word–a fixed size chosen by the designer of each Java virtual machine implementation.
- The word size must be large enough to hold a value of type byte, short, int, char, float, return Address, or reference. Two words must be large enough to hold a value of type long or double. The word size is often chosen to be the size of a native pointer on the host platform.