java interview questions Flashcards
List some important features of Java.
Ease of learning: Java is considered to be an easy-to-learn and understand language.
Security: Java is a secure language. The applications developed using this language are virus and tamper-proof.
OOP: OOP means Object-Oriented Programming Language. While Java is not considered to be purely object-oriented, the inclusion of features such as data abstraction, inheritance, and data-encapsulation, makes Java an object-oriented programming language. Thus, as such, everything in Java is considered an object.
Independent Platform: This is an important Java feature. Java does not depend on any specific platform and as long as the system has JRE installed, Java bytecode will be interpreted.
why java is secure?
Security API’s
Security manager
Void of Pointers
Memory management(garbage collector)
Compile-time checking
Cryptographic Security
Java Sandbox
Exception Handling
Java Class Loader
More details
Name the types of memory allocations in Java.
tAnother popular Java interview question is about memory allocation. Five important types of memory allocations in Java are - Heap memory, Class memory, Native method stock memory, Stack memory, and Program-counter memory.
what is stack memory in java?
Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects referred from the method that are in a heap.
Access to this memory is in Last-In-First-Out (LIFO) order. Whenever we call a new method, a new block is created on top of the stack which contains values specific to that method, like primitive variables and references to objects.
When the method finishes execution, its corresponding stack frame is flushed, the flow goes back to the calling method, and space becomes available for the next method.
source
what are key features of stack memory in java?
Some other features of stack memory include:
- It grows and shrinks as new methods are called and returned, respectively.
- Variables inside the stack exist only as long as the method that created them is running.
It’s automatically allocated and deallocated when the method finishes execution. - If this memory is full, Java throws java.lang.StackOverFlowError.
- Access to this memory is fast when compared to heap memory.
- This memory is threadsafe, as each thread operates in its own stack.
source
what is heap space in java?
Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory.
These objects have global access and we can access them from anywhere in the application.
We can break this memory model down into smaller parts, called generations, which are:
- Young Generation – this is where all new objects are allocated and aged. A minor Garbage collection occurs when this fills up.
- Old or Tenured Generation – this is where long surviving objects are stored. When objects are stored in the Young Generation, a threshold for the object’s age is set, and when that threshold is reached, the object is moved to the old generation.
- Permanent Generation – this consists of JVM metadata for the runtime classes and application methods.
what are some key features of java heap memory?
- It’s accessed via complex memory management techniques that include the Young Generation, Old or Tenured Generation, and Permanent Generation.
- If heap space is full, Java throws java.lang.OutOfMemoryError.
- Access to this memory is comparatively slower than stack memory
- This memory, in contrast to stack, isn’t automatically deallocated. It needs Garbage Collector to free up unused objects so as to keep the efficiency of the memory usage.
- Unlike stack, a heap isn’t threadsafe and needs to be guarded by properly synchronizing the code.
source
what is JVM?
Java Virtual Machine (JVM) is an implementation of a virtual machine which executes a Java program.
The JVM first interprets the bytecode. It then stores the class information in the memory area. Finally, it executes the bytecode generated by the java compiler.
It is an abstract computing machine with its own instruction set and manipulates various memory areas at runtime.
Components of the JVM are:
Class Loaders
Run-Time Data Areas
Execution Engine
source
what is classloader in JVM?
We know that Java Program runs on Java Virtual Machine (JVM). When we compile a Java Class, JVM creates the bytecode, which is platform and machine-independent. The bytecode is stored in a .class file. When we try to use a class, the ClassLoader loads it into the memory.
source
what are built-in classloader in java?
There are three types of built-in ClassLoader in Java.
- Bootstrap Class Loader – It loads JDK internal classes. It loads rt.jar and other core classes for example java.lang.* package classes.
- Extensions Class Loader – It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
- System Class Loader – This classloader loads classes from the current classpath. We can set classpath while invoking a program using -cp or -classpath command line option.
source
what is JRE?
Java Runtime Environment (JRE) is a bundle of software components used to run Java applications.
Core components of the JRE include:
An implementation of a Java Virtual Machine (JVM)
Classes required to run the Java programs
Property Files
source
What is JDK?
Java Development Kit (JDK) provides environment and tools for developing, compiling, debugging, and executing a Java program.
Core components of JDK include:
JRE
Development Tools
source
what is class area of memory in java?
The class method area is the memory block that stores the class code, variable code(static variable, runtime constant), method code, and the constructor of a Java program. (Here method means the function which is written inside the class). It stores class-level data of every class such as the runtime constant pool, field and method data, the code for methods.
source
what is program counter register memory in java?
Each JVM thread that carries out the task of a specific method has a program counter register associated with it. The non-native method has a PC that stores the address of the available JVM instruction whereas, in a native method, the value of the program counter is undefined. PC register is capable of storing the return address or a native pointer on some specific platform.
source
what are native method stacks in java?
Also called C stacks, native method stacks are not written in Java language. This memory is allocated for each thread when it’s created And it can be of a fixed or dynamic nature.
source
As a language, Java is considered platform-independent. Why?
Java is considered to be platform-independent because it does not depend on any hardware or software. When the Java programmer compiles Java code, the compiler converts the code into bytecode that can be run on different platforms. The only constraint is that the platform must have JRE (runtime environment) or Java Virtual Machine (JVM) installed on it.
source
What is data-encapsulation in Java?
Data-encapsulation in Java is the process of hiding the variables (data) and the code applied to the data in a single unit. It is a significant feature of object-oriented programming. This helps Java developers to isolate different objects from each other and to create modules for each. By this, all objects would have their own behaviors, attributes, and functions. It prevents data or object mixing, hides data, enhances security.
source
What are wrapper classes in Java?
Java, as a programming language, supports primitive data types. Wrapper classes help convert these primitive data types into objects or reference types and vice versa. Thus, the wrapper classes are so named because they ‘wrap’ these data types into identifiable objects of the specific data classes the primitive data types belong to. This Java interview question can help the interviewer assess whether you are comfortable using both old and new data types.
source
What are constructors in Java?
In Java, constructors can be understood as a block of codes applied to an object to initialize it. It can be understood as a special method for initializing an object. Constructors are called when a class instance is created such as creating an object using the new() keyword. When a class does not contain a constructor, a default constructor, provided by the Java compiler, is automatically called.
There are two types of Java constructors - default and parameterized. When no parameters are defined, the constructor is called a default constructor. When specific parameters are defined for a constructor, it is called a parameterized constructor. While you may be using the Java programming language for some time, this Java interview question shows the interviewer that you know the technicalities too.
source
Why is Java not considered to be purely object-oriented?
Java supports primitive data types such as boolean, int, byte, long, float, etc. An object-oriented language must only support objects. However, the primitive data types are not objects, hence Java is not considered to be purely object-oriented.
source
how use of pointers slows down garbage collection ?
because, it may happen that you hold some reference to the object somewhere, and GC only collects objects which have no references left.
source
Does Java use pointers? If not, why?
No, unlike C++, Java doesn’t use pointers. Java’s main focus is simplicity. The use of pointers complicates the process, especially for new java programmers. The use of pointers can also lead to possible errors. Moreover, pointers grant direct access to memory and thus, compromise security. By excluding pointers from Java, a certain amount of abstraction is achieved. Java has automatic Garbage Collectors, the use of pointers can slow down the garbage collection process. This advanced Java interview question deals with the intricacies of the language and answering it can help set you apart from other candidates.
source
why do you need to use synchronization in java?
To prevent thread interleaving or interference
To provide consistency to the program
what is the difference between pass-by-value and pass-by-reference?
- Pass-by-reference: When a method is called, the method arguments reference the same variable in memory as the caller.
- Pass-by-value: When a method is called, the caller passes a copy of the argument variables to the method resulting in two values in memory.
source