Java is an object-oriented as well as a class-based programming language designed for fewer dependencies in implementation. It is an application development computing platform. Therefore, Java is fast, safe, and reliable. This software is commonly used to create Java apps in laptops, data centers, game consoles, science supercomputers, mobile phones, etc.
Java Platform is a series of programs that help programmers efficiently create and execute Java applications. It comprises a running generator, a compiler, and a variety of libraries. It is a compilation of applications and requirements for computers. 3RI Technologies provides Java Classes under the guidance of an industry expert.
Following are the 50 questions and answers, which are asked in interviews.
1. Explain an instance variable and a local variable?
Instance Variable: These variables are accessible in all the methods in the class. The declaration is done outside the methods and also inside the class. The instance variable usually describes an object’s properties, and they are completely bound to it at any cost.
For utilization purposes, all the objects of the class have their copy. Modification is done on local variables only has an impact only at that instance, while other class instances remain as it is.
Inside a block, function, or constructor, the local variables are present and can be accessed only inside them and can be utilized only inside this block, function, or constructor scope.
2. Explain how the contiguous memory locations are usually used for storing actual values in an array but not in ArrayList.
In the array, each element does not require any reference to the next element as the object or traditional data types like int, float, and others are stored in contiguous memory locations.
ArrayList’s case is different, and it does not follow data storing in the form of traditional data types, i.e., int, float, and others. Usually, ArrayList has references to the objects which are located at various places in the memory. Therefore, storing the actual objects or non-traditional data types like Integer, Double, and other placements are done at different memory locations.
3. In Java, we do not use pointers, whereas we used pointers in C/C++?
Pointer points to the address of the memory locations where variables are stored. So concerning security, it is risky as anyone can directly access the memory location with pointers’ help.
Pointer’s concept is not easy to understand; Java is simply looking for simplicity in coding, and with the use of a pointer, Java programming can become a bit complicated. With the help of a pointer, you may face errors while code compiling and debugging.
So to a certain level, pointers are not used in Java. Also, pointers’ usage causes the garbage collection process to be very slow and full of error.
4. Can you explain what the reasons behind making strings fixed in Java are?
String in Java is immutable or fixed because of
● String Pool
Programmers widely use the string data type. Java designers want to develop optimization in a string from the beginning. In this way, String Pool came into the picture; they store the String literals. By sharing the intended use of temporary StringString, the object is reduced a lot. It needs to facilitate sharing a lot as it is an immutable/fixed class, as sharing between mutable structures between two unknown parties is not possible. Thus, the String Pool concept is executed with the help of Java String.
Multithreading: The essential aspect in Java is the safety of threads. You don’t need external synchronization as String objects in Java are mutable. An exact and cleaner code can be written for sharing the String objects across different threads. With the help of this method, you achieve a very tedious process called concurrency.
Collections: String object is the key, especially for Hash Tables and HashMaps. As String objects are mutable, so you can easily edit it when it resides in the HashMaps. So mostly, you won’t be able to get the desired data. So it is risky because of these changes.
5. Explain why although inheritance is a popular OOPs concept, it is less advantageous than composition?
As multiple inheritances are not possible in Java, the classes in Java can only have one superclass. The composition pattern is preferred when various functionalities are required, e.g., to read and write in the file. It can be considered as a private member’s writer functionality and reader functionality.
Breaking encapsulation is avoided with the help of composition, and also, high flexibility is attained. With the help of composition, unit testing is possible, which we cannot do because of inheritance. When testing is needed for a class that is composing a different course, then a Mock Object is created to indicate a composed class that helps in testing; this is not possible with the help of inheritance as the superclass is needed for testing the derived class. The composition is loosely coupled by nature, whereas inheritance is compact or tightly coupled by nature.
6. What do you mean by constructor overloading?
When you need to create multiple constructors in the class with the same name, constructor parameters are different, which is called constructor overloading. Compilers can identify different types of constructors by the number of parameters and their corresponding classes.
7. What is method overloading and overriding?
Different methods in the same class with the same name, then only method overloading is possible. But still, there is a difference in the number or type of parameters. It is always inside a class, and it increases the readability of the program. Method overloading is not promoted when there is a difference in the return type method.
When there is an inheritance relationship present in two different classes having the same method signature, with the help of method overriding, a particular method is implemented for the derived class. Both the class methods have the same name and parameters. So it can easily override the base class method by the derived class.
8. Can you differentiate between a String, StringBuffer, and a StringBuilder?
Storage: The String pool is the storage space, whereas, for StringBuffer and StringBuilder, the heap memory is the storage space
Mutable: Only StringString is immutable, whereas the StringBuilder and StringBuffer are mutable.
Productivity: Programming using StringString is slow, whereas StringBuilder is the fastest in performing the operations. But the StringBuffer works at moderate speed and has speed in between StringBuilder and StringBuffer.
Threading: When threading is to be used, StringBuilder for single thread and StringBuffer for multiple threads are used, but not string.
9. Differences between interfaces and abstract classes?
Only abstract methods are available in interfaces, whereas non-abstract methods can be present along with abstract methods. Only static and final variables can be declared in interfaces, and abstract variables have non-static and non-final variables. Interfaces facilitate multiple inheritances, but abstract classes do not. In the interface, the class data members are accessed by the public, whereas in the abstract class, they are private. Abstract classes can quickly help build interfaces, whereas interfaces can’t help develop the abstract class.
10. Why in Java, static as well as private method overriding is possible?
In Java, static as well as private methods overriding is not possible. But it can be stated as method hiding and not method overriding. Method overriding is possible only for the parent class. The static methods do not have any relevance with the objects, and these methods are of the class level. When in a child class case, a static way with a method signature exactly like that is the parent class can exist without even throwing any compilation errors.
11. Can you tell us about Java?
Java was designed in June 1991 by James Gosling. It is a platform that has its own JRE and API. Java is
● High-level programming language.
● Object-oriented language.
● Robust and portable programming language.
● A very secure programming language.
● Java is a platform-independent programming language.
● Java gives high performance.
● Multi-threaded programming language
12. Explain the difference between Java and C++?
● C++ is a platform-dependent programming language, whereas Java is independent of the platform.
● Java is used for programming applications like mobile applications, web applications, and others, whereas C++ is used for system programming. C++ supports multiple inheritances, but Java does not support it.
● Operator overloading is supported in C++ but not in Java. In C + +, a pointer is used; in Java, pointers are not used.
● Compilers are used only in C++, but in Java, we need a compiler and interpreter.
● C++ call by value and reference both can be used, but only call by value is used in Java. C++ supports structure and unions, but Java does not support it.
● For threading, C++ needs to be dependent on third parties, but Java supports threading entirely.
● C++ can interact with hardware, and so it is very close to the hardware, but Java is not interactive with hardware.
13. What are the Java language’s attributes?
The features/attributes of the Java programming language are as follows:
● Easy to learn
● High-level programming language.
● Object-oriented language.
● Robust and portable programming language.
● A very secure programming language.
● Java is a platform-independent programming language.
● Java gives high performance.
● Multi-threaded programming language
● Dynamic programming language.
● Java programming languages have neutral architecture.
14. Explain Java virtual machine?
It acts like a run time engine which is the primary method in Java programs. It is recommended to have a Java virtual machine on your computer to run Java programs. The Java code is compiled or can run with a Java virtual machine to a Bytecode independent of the device but closer to native code.
15. Explain the difference between JDK, JRE, and JVM?
JVM stands for Java virtual machine. JVM is abstract machine that provides the runtime environment to execute Java Bytecode. This is how JVM works. The JVM implementation is known as the JRE. It is independent of hardware and software platforms. It creates a runtime instance when we run the Java code. JVM has three notions that are as follows
JRE means Java Runtime Environment. It is an implementation of JVM. The Java Runtime Environment consists of software tools that are used for developing a Java application. For Java code, it provides a runtime environment. JRE exists physically, unlike JVM. It contains sets of libraries and also other files that the Java virtual machine uses during runtime.
JDK stands for Java Development Kit. It is a software development environment that is used by Java applications and applets. JDK also exists physically, unlike JVM. It has all development tools, including JRE tools. JDK implements the Java platforms like Standard Edition Java Platform, Enterprise Edition Java Platform, Micro Edition Java Platform, and others.
16. What are all the areas allocated by JVM?
The areas that JVM allocates
● Class Area
● PCR(Program Counter Register)
● Native Method Stack
The Class Area stores each class structure. The stack holds frames. It includes local variables and partial results and leads to the invocation and return of methods. Each thread has its own JVM stack., which is created at the same time when the thread is created. PCR(Program counter register) consists of the JVM instruction address that is currently being executed. The Native Method stack has all the native methods that can be used in the application.
17. Explain what a JIT compiler is?
JIT is a Just-In-Time compiler that compiles that part of the Bytecode, which has similar functionality that also reduces the time needed for compiling the code. It is used to boost efficiency in general. It is the translator from the instruction set of a JVM to the instruction set of a particular CPU.
18. Explain what you mean by platform?
A Platform is of two types.
19. How is the Java platform different from other platforms?
The key difference between Java platform and another platform
Java is a software-based platform, whereas others can be software as hardware platforms. Java is executed at the topmost layer of the hardware platform, and it does not interact with the hardware. Still, other platforms interact with hardware and can only have hardware components.
20. What gives Java its ‘Write once and run anywhere feature’?
Java compiles and converts into Bytecode which is in between the source code and machine code. This Bytecode is not at all platform-specific and can be executed on any system, i.e., computer. That is why the Java code can be said ‘Write once and run anywhere in the future.’
21. What is a classloader?
Classloader helps to load the class files, and it is a subsystem of JVM. When a program is executed, it is loaded by the classloaders. There are three classloaders in Java.
It is the first classloader that is the superclass of Extension Classloader. It loads the rt.jar file that contains all class files of Java.
It is considered as a child classloader of Bootstrap ClassLoader and parent classloader of System ClassLoader. All the files inside the $JAVA_HOME/JRE/lib/ext directory
System/ Application ClassLoader:
System ClassLoader is a child classloader of Extension ClassLoader. All the files from the classpath that loads class files. Class files by default load the classpath. The classpath can be changed using the “-cp” or “-classpath” switch. This is Application Classloader
22. Which are the various access specifiers in Java?
The method’s access scope, class, variable, and others are defined using access specifiers, which are nothing but keywords. The access specifiers in Java are as follows:
The public access specifiers are the keywords used to define the method, class, variable, and another access scope defined as public and can be accessed by any class or method. Protected access specifiers can access the class of the same package, or by the sub-class of this class, or within the same class. Default is an access specifier which is accessible within the package only. The classes, methods, and variables come in default specifiers scope by default. Within class only, you can access methods or variables defined as private access specifiers
23. Can you explain the purpose of static methods and variables?
The static methods and variables are shared among all the objects of the class. The static is not considered a part of the object, but it is a part of the class. The static can be considered as a part of the class and not of the object. There is no need to create the object to access static variables, and they are stored in the class area. The static is used when the variables or methods are defined, common to all the class objects.
24. Why do we prefer Packages in Java?
Packages are preferred in Java because with the use of packages; name clashes can be avoided. Access control becomes easier because of packages. Hidden classes that are not visible can be used with the help of packages. The classes related are easily found with the help of packages.
25. Explain object-oriented paradigm?
The object-oriented paradigm is based on the objects with data and methods that are mainly defined in the class to which it belongs. The main objective of using an object-oriented paradigm is that it should integrate modularity and reusability. To design applications and programs, the objects are the instances of classes that interact with one another to develop applications and programs. The object-oriented paradigm follows a bottom-up way in program design. It focuses on the data, with methods to operate on the object’s data. The concepts like encapsulation and abstractions are included in this paradigm, which hides the user’s complexities but shows only functionality. The real-time approaches like inheritance, abstraction, and others are implemented using an object-oriented paradigm. C++, Simula, Smalltalk, Python, C#, and others are examples of the object-oriented paradigm.
26. Can you explain the difference between an object-oriented programming language and an object-based programming language?
OOP’s concept is followed by object-oriented languages, whereas OOP concepts like inheritance and polymorphism are not followed by object-based programming. Object-based programming languages have inbuilt objects, but object-oriented programming languages do not have inbuilt objects.
27. What is the constructor?
An object’s status may be initialized using the specific type of method, which is a builder; when the class is initiated, it is invoked, the object has the memory allocation.
The constructor can be defined as the special method used to initialize an object’s condition. When the class is instantiated, it is called, and the memory for the object is assigned. The default class constructor is named any time an object is generated with the new keyword. The name of the constructor shall be identical to the name of the class. The constructor shall not have a particular form of return.
28. Can you copy the constructor in Java?
You can only copy one object’s values to another object, but there is no such thing as a copy constructor in Java. There are many ways to copy objects with the help of a constructor by assigning one object’s values into another or by clone() method.
29. Explain how constructors are different from the method
The state of an object is initialized using a constructor. The constructor not have a return type. The constructor is found completely. A default constructor is provided by Java, if there is no constructor in a class. The constructor and class name should be the same.
The method is used to expose the behavior of the object. The method should always have a return type. The compiler does not provide a method. The method and class name may or may not be the same.
30. What does this keyword in Java is used for?
The current object’s behavior and state in Java are referred to using this keyword. In Java, this keyword has many uses; it refers to an instance method, variable, constructor, etc. This keyword can also be used to pass an argument into the methods or a constructor. It can also be a current class instance from the method.
31. Explain what inheritance is?
The object acquires all the properties and behavior of another object of another class is inheritance. Method Overriding and Code Reusability inheritance is used for. Upon the existence of the course, you can create a new class using inheritance. With the help of inheritance, you can reuse the methods and fields of the parent class. The parent-child relationship is also expressed by inheritance. We have a single level, multi-level, multiple, and hybrid inheritance we have in Java.
32. Explain composition in Java?
The reference to a class is known as a composition of any other class. If an object occupies the other object, the composition is called if the object contained cannot exist without container objects. In other words, the composition is the special case of aggregation, a more substantial relation of two objects.
33. Do super keywords have any uses?
Super may be used to refer to the instance variable of the immediate parent class. The immediate parent class method can be used to invoke super. The super() can be used to invoke the builder of the parent class automatically.
34. Can you explain how a super keyword is different from this keyword in Java?
The super keyword refers to the meaning of the parent class, while this keyword often points to the class’s actual context.
The super-keyword is mainly used to initialize the essential class variables in the derived class builder. In contrast, that keyword is primarily used to distinguish between the class builder’s local and instance variables.
The first statement inside the builder is super, and otherwise, the compiler would make an error.
35. Explain object cloning?
The cloning of the entity is used to construct the precise copy. The Object class clone() method is used to clone an object—the Java. Lang. A cloneable interface must be used by the class that we want to build for the object clone. Clone() produces a CloneNotSupportedException if we don’t implement a Cloneable interface.
36. Can you explain the difference between runtime polymorphism and compile-time polymorphism?
The polymorphism of compile-time offers less versatility, provided that everything in compile time is solved. It allows quick execution because, at compilation time, the type of object is specified. Overloading is a way to achieve compilation polymorphism in which multiple methods or builders with different signatures can be specified. It is often referred to as static attachment, precocious or overloading. Calling to a function is solved at compile-time polymorphism.
The call for an overridden procedure is resolved during runtime polymorphism. It is referred to as dynamic binding, late binding, overrides, or dynamic process delivery. Overriding is a way of achieving polymorphism over time, in which any process or variable of the derived class can be described again. By means of overriding the essential class properties of the derived class may be directly enforced. Compared to compile-time, it gives slower execution because, at runtime, the object type is defined. Polymorphism for runtime allows more versatility since everything is solved during runtime.
38. Explain what you mean by abstraction?
Abstraction is a process to cover the specifics of the implementation and just reveal the user features. It shows the user only the most essential things and hides the internal information, for instance, by sending text messages where you type text and send a reply. You don’t know the internal message distribution processing. Abstraction lets you concentrate more on what the object does. Abstraction helps you to focus more on what the object does. Two ways to attain conception are available in Java that is abstract class and interface.
39. Explain the interface?
The interface is a plan for a class with abstract and static constants. It can be used to achieve total abstraction and multiple legacies. It’s an abstraction mechanism. Only abstract methods, not method body, can exist in the Java interface. It is used for abstraction and various Java heritage. Interfaces can be defined as abstract methods and variables. The IS-A connection also stands for the Java interface. It cannot just like the abstract class be instantiated. However, to describe its processes, we need to implement them. We can have the predetermined, static, and private methods in an interface since Java 8.
40. What are the benefits of encapsulation in Java?
You can only read or write the class by supplying the setter or getter method. You can miss the trick or setter methods, in other words.
It provides you with data access. You can enter the setter method’s logic if you want to set the id value, which is more than 100 only. To avoid saving negative numbers in setters methods, you can write the rationale. It is a way to enter data concealed in Java, as other groups cannot access the data through the private data members.
It is easy to test to encapsulate class.
41. How many exceptions can be found in Java?
Two kinds of exceptions are mainly: regulated and unregulated. A mistake is treated here as an unregulated exception. There are three kinds of exceptions, according to Oracle:
Checked Exception: The checked exceptions are the ones checked during compilation time. For instance, ClassNotFoundException, SQLException etc.
Unchecked Exception: The unchecked exceptions are treated during runtime because they cannot be checked during compilation periods.
42. What is the objective of the Java toString() method?
The method toString() returns an object’s string representation. When any object is printed, Java’s compiler calls the toString() method of your object internally. If the toString() method is crossed, the desired result will be returned; it can be the state of an object, etc. By overriding the Object Class toString(), we can replace the object values, so there is not much code to write.
43. Why is CharArray() preferable to password storage over StringString?
The StringString stays until the waste is collected in the string pool. If the password is saved in a string, the password remains in memory for a longer time, and anyone with a memory dump can extract the password as an exact text. Using CharArray, on the other hand, allows us to blank it when the password is finished. By allowing us to control the memory, the threat to security by means of the string.
44. How are the inner classes of Java beneficial?
Java Inner Classes have two kinds of advantages. Nested classes represent a particular relationship that allows access by all outer class members, including by private individuals (data members and methods). Nesting classes serve for developing more readable and maintainable code since classes and interfaces are logically only grouped in one spot. Optimization of Code: less code is needed to write.
45. So what is a nested class?
the nested class is defined as the class defined within another class or interface. We use the nested class to groups and interfaces logically to be easier to read and maintain in one location. All outer class data members, including private data and methods, can be accessed by a nesting class.
46. What are the drawbacks of the inner class?
The main inconveniences of using inner classes are as follows.
Internal classes increase the total number of classes the developer uses, thus increasing the workload of JVM because some routine operations for extra courses have to be performed, leading to slower performance. IDEs favor the inner classes less than the top courses, so it angers the developers when dealing with the internal classes.
47. What are anonymous inner classes?
The classes that are declared and instanced within the expression are anonymous internal classes. Different access changes cannot be implemented. The unknown type cannot be static, and no static fields, method, or class can be defined. We can assume that it is a class that has no name and can only have one entity that is defined.
48. What’s the nested interface?
A brokered interface is called an interface that is declared within the interface or class. By chance, it’s static—the interfaces used for nested interfaces group linked interfaces to make maintenance simple. The outside interface or class must refer to the interface that has been nested. It is not directly available. If declared in the interface, the nested interface shall be public if declared inside the class.
49. Explain Garbage Collection?
Collecting garbage is a retrieval mechanism for unused execution items. It is done for the management of memories. That is to say; it is the process of removing unused items from memory to free up space and provide Java virtual machines with this space. The garbage collection java gives 0 to a variable with no value, i.e., the variable is specified but not initialized. We used free() in the language C and delete() in the language C++ for this reason. It is automatically executed in Java. Java, therefore, allows better control of the memory.
50. What does serialization mean?
Serialization in Java is a byte stream writing process for the status of an object. The key applications are Hibernate, RMI, JPA, EJB, and JMS. It is primarily used on the network to travel items (which is known as marshaling). For serialization, the Serializable interface is used. It is useful to save the state of a storage program, like the file. At a later date, deserializations can be used to restore the content of this file. The implementation of RMI is also essential (Remote Method Invocation). Using RMI, the method for a Java object on one computer can be invoked into another.