1. Compare overloading with overriding.
Overloading refers to the case of having two methods of the same name but different properties; whereas, overriding occurs when there are two methods of the same name and properties, but one is in the child class and the other is in the parent class.
2. Explain the creation of a thread-safe singleton in Java using double-checked locking.
Singleton is created with the double-checked locking as before Java 5 acts as a broker and it’s been possible to have multiple instances of singleton when multiple threads create an instance of the singleton at the same time. Java 5 made it easy to create thread-safe singleton using Enum. Using a volatile variable is essential for the same.
3.
4.
Yes, we can execute any code, even before the main method. We will be using a static block of code in the class when creating the objects at load time of the class. Any statements within this static block of code will get executed at once while loading the class, even before the creation of objects in the main method.
5. Define JSON.
The expansion of JSON is ‘JavaScript Object Notation.’ It is a much lighter and readable alternative to XML. It is independent and easily parse-able in all programming languages. It is primarily used for client–server and server–server communication.
6. What are the advantages of JSON over XML?
The advantages of JSON over XML are:
7. What is JIT Compiler?
作者:lcksuper
链接:https://www.zhihu.com/question/21093419/answer/112968115
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set. A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently.
This is in contrast to a traditional compiler that compiles all the code to machine language before the program is first run.
To paraphrase, conventional compilers build the whole program as an EXE file BEFORE the first time you run it. For newer style programs, an assembly is generated with pseudocode (p-code). Only AFTER you execute the program on the OS (e.g., by double-clicking on its icon) will the (JIT) compiler kick in and generate machine code (m-code) that the Intel-based processor or whatever will understand.
再上个图,参考。
8. What is Classloader?
It is a part of the Java Runtime Environment which is used to dynamically load the class files into the Java Virtual Machine(JVM). The classloader triggers whenever a java file is executed.
9. Why is the main method static in Java?
The main method is static in Java because to call the static methods, there is no need for the object. The Java Virtual Machine(JVM) has to create an object to call the non-static main() method, which will result in extra memory allocation.
方法中使用静态(static)这个关键字,JVM 将会为这个方法开辟内存空间,你不需要对这个方法进行实例化,因此能够节省不必要的开销。
如果 main 方法不声明为静态的,JVM 就必须创建main类的实例,因为构造器可以被重载,JVM就没法确定调用哪个 main 方法。
因此,在这里这个 static 关键字是必须要有的,否则你的程序可以编译,但是无法运行。
10. Can we declare the static variables and methods in an abstract class?
Yes, we can declare the static variables and methods in an abstract class by inheriting the abstract class. In java, the static variables and methods can be accessed without creating an object. You can simply extend the abstract class and use the static context as mentioned below:
11.
关联指的是两个分开的类通过对象或实例建立的关系。关联可以是一对一,一对多,多对一,还有多对多,就好像数据库中的关联表,外键一样,我觉得可以结合数据库进行理解。
在面向对象的编程当中,一个对象通过调用另一个对象的方法和服务来进行交流合作。下面有一张图形象的表现出关联,组合,聚合的关系。
聚合是关联的一种特殊形式:主要体现在一下三个方面
组合是一种聚合的限制形式,其中两个实体高度相互依赖。也就是说,两个类高度耦合。有以下特征:
12. What is object cloning?
Object cloning means creating the exact copy of an existing object. In java programming, object cloning can be done using the clone() method. To create the object clone, you should implement the java.lang.Cloneable interface, otherwise the clone() method generates a CloneNotSupportException.
13.Can we overload the main method in Java?
Yes, you can overload the main() method in Java using the concept of method overloading.
Below is an example of main() method overloading:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
14.
The main difference between ‘==’ and equals() is that ‘==’ is an operator, while equal() is a method in java. The == operator is used for reference or addresses comparison, meaning it checks if both the objects or variables are pointing to the same memory location.
Whereas, equals() method compares the content of two objects or variables. It checks if the value of two objects is the same or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
16. Define content negotiation.
If we have visited a website to search for information, we will get the information in different languages and in different formats. When a client makes an HTTP request to a server, the client can also specify the media types here. The client can specify what it can accept back from the host, and on the basis of availability the host will return to the client. This is known as content negotiation because the client and the server negotiate on the language and format of the content to be shared.
17. Can we import the same package/class twice? Will the JVM load the package twice at runtime?
A package or class can be inherited multiple times in a program code. JVM and compiler will not create any issue. Moreover, JVM automatically loads the class internally once, regardless of times it is called in the program.
18.Distinguish between static loading and dynamic class loading.
1 |
|
In the Java programming language, you cannot accurately determine the exact size of an object located in the heap.
20.An enumeration (enum for short) in Java is a special data type which contains a set of predefined constants.
21.What is the difference between transient and volatile variables in Java?
Transient: In Java, it is used to specify whether a variable is not being serialized. Serialization is a process of saving an object’s state in Java. When we want to persist the object’s state by default, all instance variables in the object are stored. In some cases, we want to avoid persisting a few variables because we don’t have the necessity to transfer across the network. So, we declare those variables as transient.
If the variable is confirmed as transient, then it will not be persisted. The transient keyword is used with the instance variable that will not participate in the serialization process. We cannot use static with a transient variable as they are part of the instance variable.
Volatile: The volatile keyword is used with only one variable in Java, and it guarantees that the value of the volatile variable will always be read from the main memory and not from the thread’s local cache; it can be static.
The Map interface is not compatible with the Collection interface, because Map requires a key as well as a value, for example, if we want to add a key–value pair, we will use put(Object key, Object value).
There are two parameters required to add an element to HashMap object. In Collection interface, add(Object o) has only one parameter.
The other reasons are: Map supports valueSet, keySet, and other suitable methods that have just different views from the Collection interface.
23. What is the default size of the load factor in the hashing-based collection?
Default size = 0.75
Default capacity = initial capacity * load factor
24. What are wrapper classes in Java?
Wrapper Classes in Java offers a mechanism to convert the primitive data types into reference or object types and vice-versa. Each primitive data type in Java has a dedicated class known as the wrapper class, which wraps the primitive types into an object of that particular class. Converting primitive data types into objects is known as autoboxing and converting from an object to primitive data types called unboxing.
One of the major applications of wrapper classes is changing the value inside the methods. Java does not support the call by reference, which means changes done inside a method will not change the original value. So, after converting the types into objects, the original values will also change. Also, the wrapper classes help to perform serialization by converting objects into streams.
25. What is Polymorphism?
Polymorphism is the ability of an object to get processed in more than one way. It is mainly used when a base/parent class is used to refer to a child/derived class reference. There are two types of polymorphism in Java: