Interview Questions 2023 - Java
Interview Questions 2023 - Java
Concept.
In contrast, because the byte code generated by the Java compiler is platform-
independent, it can run on any machine, Java programs are written once and run
everywhere.
Languages Compatibility.
It can access the native system libraries directly in C++. As a result, it’s better
for programming at the system level.
Java’s native libraries do not provide direct call support. You can use Java Native
Interface or access the libraries.
Characteristics.
C++ distinguishes itself by having features that are similar to procedural and
object-oriented languages. The characteristic that sets Java apart is automatic
garbage collection. Java doesn’t support destructors at the moment.
Primitive and object types in C++ have the same kind of semantics. The primitive
and object and classes of Java, on the other hand, are not consistent.
Easy: Java is a language that is considered easy to learn. One fundamental concept
of OOP Java has a catch to understand.
Secured Feature: Java has a secured feature that helps develop a virus-free and
tamper-free system for the users.
OOP: OOP stands for Object-Oriented Programming language. OOP signifies that, in
Java, everything is considered an object.
We get two major things along with the Java Download file.
In C++, the source program is compiled into an object code that is further executed
to produce an output.
4. What is a ClassLoader?
Class Memory
Heap Memory
Stack Memory
Program Counter-Memory
Native Method Stack Memory
6. What are the differences between Heap and Stack Memory in Java?
Yes, the program will successfully execute if written so. Because, in Java, there
is no specific rule for the order of specifiers
Neither the Local Variables nor any primitives and Object references have any
default value stored in them.
9. Explain the expected output of the following code segment?
{
public static void main (String args[])
200Simplilearn
E-Learning Company100100
The term aggregation refers to the relationship between two classes best described
as a “whole/part” and “has-a” relationship. This kind is the most specialized
version of an association relationship. It contains the reference to another class
and is said to have ownership of that class.
12. Define Copy Constructor in Java
A Paradigm that is based on the concepts of “Objects.” It contains data and code.
Data that is in the form of fields, and regulation, that is in the form of
procedures. The exciting feature of this paradigm is that the object’s procedures
can access and often modify the data fields themselves.
17. Define Wrapper Classes in Java.
In Java, when you declare primitive datatypes, then Wrapper classes are responsible
for converting them into objects(Reference types).
18. What is a singleton class in Java? And How to implement a singleton class?
A class that can possess only one object at a time is called a singleton class. To
implement a singleton class given steps are to be followed:
Make sure that the class has only one object
Give global access to that object
The package is a collective bundle of classes and interfaces and the necessary
libraries and JAR files. The use of packages helps in code reusability.
20. Can you implement pointers in a Java Program?
Java Virtual Machine takes care of memory management implicitly. Java's primary
motto was to keep programming simple. So, accessing memory directly through
pointers is not a recommended action. Hence, pointers are eliminated in Java.
21. Differentiate between instance and local variables.
For instance, variables are declared inside a class, and the scope of variables in
javascript is limited to only a specific object.
A local variable can be anywhere inside a method or a specific block of code. Also,
the scope is limited to the code segment where the variable is declared.
22. Explain Java String Pool.
The term final is a predefined word in Java that is used while declaring values to
variables. When a value is declared using the final keyword, then the variable's
value remains constant throughout the program's execution.
25. What happens when the main() isn't declared as static?
When the main method is not declared as static, then the program may be compiled
correctly but ends up with a severe ambiguity and throws a run time error that
reads "NoSuchMethodError."
26. Why is Java a platform independent language?
One of the most well-known and widely used programming languages is Java. It is a
programming language that is independent of platforms. Java doesn't demand that the
complete programme be rewritten for every possible platform. The Java Virtual
Machine and Java Bytecode are used to support platform independence. Any JVM
operating system can run this platform-neutral byte code. The application is run
after JVM translates the byte code into machine code. Because Java programmes can
operate on numerous systems without having to be individually rewritten for each
platform, the language is referred to as "Write Once, Run Anywhere" (WORA).
27. Why is the main method static in Java?
The main function is called by the JVM even before the objects are created, thus
even if the code correctly compiles, there will still be an error at runtime.
33. Can we make the main() thread a daemon thread?
This technique designates whether the active thread is a user thread or a daemon
thread. For instance, tU.setDaemon(true) would convert a user thread named tU into
a daemon thread. On the other side, executing tD.setDaemon(false) would convert a
Daemon thread, tD, into a user thread.
34. What happens if there are multiple main methods inside one class in Java?
There is no limit to the number of major approaches you can use. Overloading is the
ability to have main methods with different signatures than main (String []), and
the JVM will disregard those main methods.
35. How does an exception propagate in the code?
In the event that an exception is not caught, it is initially thrown from the top
of the stack and then moves down the call stack to the preceding method. The
runtime system looks for a way to handle an exception that a method throws. The
ordered list of methods that were called to get to the method where the error
occurred is the collection of potential "somethings" that can be used to manage the
exception. The call stack is the list of methods, and exception propagation is the
search technique.
36. How do exceptions affect the program if it doesn't handle them?
If you don't deal with an exception once it occurs, the programme will end abruptly
and the code after the line where the exception occurred won't run.
37. Is it mandatory for a catch block to be followed after a try block?
Each attempt block does not necessarily have to be followed by a catch block.
Either a catch block or a final block ought to come after it. Additionally, any
exceptions that are expected to be thrown should be mentioned in the method's
throws clause.
38. Can you call a constructor of a class inside another constructor?
Yes, a class may include any number of constructors, and each function Object ()
{[native code] } may call the others using the this() function Object() { [native
code] } call function [please do not mix the this() function Object() { [native
code] } call function with this keyword]. The constructor's first line should be
either this () or this(args). Overloading of constructors is what this is called.
39. Contiguous memory locations are usually used for storing actual values in an
array but not in ArrayList. Explain.
Primitive data types like int, float, and others are typically present in an array.
In such circumstances, the array immediately saves these elements at contiguous
memory regions. While an ArrayList does not contain primitive data types. Instead
of the actual object, an ArrayList includes the references to the objects' many
locations in memory. The objects are not kept in consecutive memory regions because
of this.
40. Why does the java array index start with 0?
The distance from the array's beginning is just an offset. There is no distance
because the first element is at the beginning of the array. Consequently, the
offset is 0.
41. Why is the remove method faster in the linked list than in an array?
List is an interface in the Java Collections Framework. The add() and addAll()
methods are the main methods at the List interface. The add() method is used
to add an element to the list, while the addAll() method is used to add a
collection of elements to the list.
The List interface contains two overloaded versions of the add() method:
The first add() method accepts a single argument of type E, the element to be added
to the list.
The second add() method accepts a variable number of arguments of type E, which are
the elements to be added to the list.
The List interface also contains two overloaded versions of the addAll() method:
The first addAll() method accepts a single argument of type Collection<? Extends
E>, which is the collection of elements to be added to the list.
The second addAll() method accepts a variable number of arguments of type E, which
are the elements to be added to the list.
43. How does the size of ArrayList grow dynamically? And also state how it is
implemented internally?
Aggregation (HAS-A) and composition are its two forms (Belongs-to). In contrast to
composition, which has a significant correlation, the aggregation has a very modest
association. Aggregation can be thought of as a more confined version of the
composition. Since all compositions are aggregates but not all aggregates are
compositions, aggregate can be thought of as the superset of composition.
46. How is the creation of a String using new() different from that of a literal?
The new () operator always produces a new object in heap memory when creating a
String object. The String pool may return an existing object if we build an object
using the String literal syntax, such as "Baeldung," on the other hand.
47. How is the ‘new' operator different from the ‘newInstance()' operator in java?
Both the new operator and the newInstance() method are used to create objects in
Java. If we already know the kind of object to create, we can use the new operator;
however, if the type of object to create is supplied to us at runtime, we must use
the newInstance() function.
48. Is exceeding the memory limit possible in a program despite having a garbage
collector?
Yes, even with a garbage collector in place, the programme could still run out of
memory. Garbage collection aids in identifying and removing programme objects that
are no longer needed in order to release the resources they use. When an object in
a programme cannot be reached, trash collection is executed with respect to that
object. If there is not enough memory available to create new objects, a garbage
collector is used to free up memory for things that have been removed from the
scope. When the amount of memory released is insufficient for the creation of new
objects, the program's memory limit is exceeded.
49. Why is synchronization necessary? Explain with the help of a relevant example.
System.out.println() in Java outputs the argument that was supplied to it. On the
monitor, the println() method displays the findings. An objectname is typically
used to call a method.