Java is a high-level object-oriented programming language that represents the real real-world objects which implements the class of attributes containing data type variables and methods. It is a platform-independent language which can run on any operating system. The java runtime environment interprets the byte code into machine language and tells the operating system what it has to do.
In this article, you can go through the set of Java interview questions most frequently asked in the interview panel. This will help you crack the interview as the topmost industry experts curate these at HKR training.
Let us have a quick review of the Java interview questions.
Ans:
A local variable is a variable that can be defined inside a method and the scope of that variable is accessible only to its method but not outside the method. An
instance variable is a variable which is defined inside a class and outside a method and the scope of that variable is accessible all throughout the class.
Ans: Pointers increases the complexity of a program and becomes unsafe. Hence the java code is designed with simplicity by avoiding the pointers that will become contradicting. JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.
Ans: The features are.
The concepts of OOPs that include
Ans: The concepts of OOPs are:
Inheritance: It is a process by which one class acquires the properties of another class.
Encapsulation: It is a mechanism of wrapping the code and data as a single unit.
Abstraction: It is a methodology for hiding the implementation details and proving only the functionality to the users.
Polymorphism: It is the ability of a variable, function or object to take multiple forms.
Ans: Members are the methods and instance variables.
Public: These members are visible in the same package as well as in the outside packages of other packages.
Here the public members in Class A are visible to Class B of the same package 1 as well as Class C of a different package 2.
Private: These members are visible only in the same class but not in the other classes of the same package or classes in the outside packages.
Here the private members in class A are visible only in that class. It is invisible for class B of same package 1 as well as class C of different package 2.
Ans:
Default: The default specifiers are those in which the methods and variables are declared in a class without any access specifiers.
Default members in Class A are visible to the other classes which are inside the same package 1 and it is invisible to the classes which are outside the package 2. The members in Class A are visible to the Class B and invisible to the Class C.
Protected: The protected specifier is the same as a default specifier but when a class is extended than it is also visible even in a class which is outside the package.
Here the members in class A are visible in Class B of the same Package 1. For Class C of different Package 2, these members are invisible but if Class C extends Class A than these members becomes visible in Class C even outside the package 2.
Ans:
Break:
Example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
System.out.println(i);
}
Continue:
Example:
for (int i = 0; i < 10; i++)
{
if(i == 5)
{
continue;
}
System.out.println(i);
}
Ans: The final keyword is used as a non-access modifier. A final variable is used in different contexts of a class, method and a variable.
Ans:
Ans:
Methods:
Constructors:
Ans: Every number system uses positional notation, i.e., each position in which a digit is written has a different positional value. Each position is power of the base, which is 2 for binary number system, and these powers begin at 0 and increase by 1.
The value of a binary number is based on the presence of 1 bits and their positional value. So, the value of a given binary number is:
1 + 2 + 4 + 8 +16 + 32 + 64 + 128 = 255
which is same as 28 - 1.
Ans:
Ans: Assembly language is dependent upon the instruction set and the architecture of the processor. In this tutorial, we focus on Intel-32 processors like Pentium. To follow this tutorial, you will need :
Ans: If you select "Development Tools" while installing Linux, you may get NASM installed along with the Linux operating system and you do not need to download and install it separately. For checking whether you already have NASM installed, take the following steps −
This should install NASM on your system. Alternatively, you can use an RPM distribution for the Fedora Linux. This version is simpler to install, just double-click the RPM file.
Ans: An assembly program can be divided into three sections −
Ans: The data section is used for declaring initialized data or constants. This data does not change at runtime. You can declare various constant values, file names, or buffer size, etc., in this section.
The syntax for declaring data section is: section.data
Ans: The bss section is used for declaring variables. The syntax for declaring bss section is : section.bss
Ans: The text section is used for keeping the actual code. This section must begin with the declaration global _start, which tells the kernel where the program execution begins.
The syntax for declaring text section is:
section.text
global _start
_start:
Ans: Assembly language programs consist of three types of statements −
The executable instructions or simply instructions tell the processor what to do. Each instruction consists of an operation code (opcode). Each executable instruction generates one machine language instruction.
The assembler directives or pseudo-ops tell the assembler about the various aspects of the assembly process. These are non-executable and do not generate machine language instructions.
Macros are basically a text substitution mechanism.
Ans: A segmented memory model divides the system memory into groups of independent segments referenced by pointers located in the segment registers. Each segment is used to contain a specific type of data. One segment is used to contain instruction codes, another segment stores the data elements, and a third segment keeps the program stack.
In the light of the above discussion, we can specify various memory segments as −
Ans: There are ten 32-bit and six 16-bit processor registers in IA-32 architecture. The registers are grouped into three categories −
The general registers are further divided into the following groups −
Ans: The three basic modes of addressing are −
Register Addressing
In this addressing mode, a register contains the operand. Depending upon the instruction, the register may be the first operand, the second operand or both.
For example,
MOV DX, TAX_RATE ; Register in first operand
MOV COUNT, CX ; Register in second operand
MOV EAX, EBX ; Both the operands are in registers
As processing data between registers does not involve memory, it provides fastest processing of data.
Immediate Addressing
An immediate operand has a constant value or an expression. When an instruction with two operands uses immediate addressing, the first operand may be a register or memory location, and the second operand is an immediate constant. The first operand defines the length of the data.
For example,
BYTE_VALUE DB 150 ; A byte value is defined
WORD_VALUE DW 300 ; A word value is defined
ADD BYTE_VALUE, 65 ; An immediate operand 65 is added
MOV AX, 45H; Immediate constant 45H is transferred to AX
Direct Memory Addressing
When operands are specified in memory addressing mode, direct access to main memory, usually to the data segment, is required. This way of addressing results in slower processing of data. To locate the exact location of data in memory, we need the segment start address, which is typically found in the DS register and an offset value. This offset value is also called effective address.
In direct addressing mode, the offset value is specified directly as part of the instruction, usually indicated by the variable name. The assembler calculates the offset value and maintains a symbol table, which stores the offset values of all the variables used in the program.
In direct memory addressing, one of the operands refers to a memory location and the other operand references a register.
Ans: The EQU directive is used for defining constants. The syntax of the EQU directive is as follows −
CONSTANT_NAME EQU expression
For example: TOTAL_STUDENTS equ 50
You can then use this constant value in your code, like −
mov ecx, TOTAL_STUDENTS
cmp eax, TOTAL_STUDENTS
The operand of an EQU statement can be an expression −
LENGTH equ 20
WIDTH equ 10
AREA equ length * width
Above code segment would define AREA as 200.
Ans:
Abstract class:
Interface:
The interfaces have no constructors and couldn’t be instantiated.
Only the abstract methods alone must be declared.
The classes which implement the interface should provide the implementation for all methods.
Interfaces contain only constants.
Ans: Taking thread dump is easier than taking heap dump because you don't need to remember tool. In Linux, you can just use the kill command to take the thread dump e.g.
$ kill -3 PID
will print the thread dump in the log file or where System.out is redirected. Similarly, in Windows, you can use Ctrl + Break from the command prompt. Alternatively, you can also use jConsole and VisualVM to take the thread dump of Java application in both Windows and Linux. You can also read Java Performance The Definitive Guide By Scott Oaks to learn more about thread dump and heap dump.
Ans: No. You cannot override private or static methods. If a similar method is created with the same return type and the same method arguments in child class then it hides the superclass method which is also called as method hiding. Similarly, the private method is not overridable in sub-class which is not accessible there. You can create another private method with the same name in the child class as in this example.
Example:
class Base_class {
private static void display() {
System.out.println("Static or class method from Base class");
}
public void print() {
System.out.println("Non-static or instance method from Base class");
}
class Derived_class extends Base_class {
private static void display() {
System.out.println("Static or class method from Derived class");
}
public void print() {
System.out.println("Non-static or instance method from Derived class");
}
public class test {
public static void main(String args[])
{
Base obj= new Derived_class();
obj.display();
obj.print();
}
}
Ans:
Ans:
Serialization:
Deserialization:
Ans: When a class is needed for extending some other classes other than a thread then it is recommended to implement the runnable interface because java can extend only one class. If there is no need for extending any class than at the time it is recommended to extend the properties of a thread class.
Ans:
HashMap:
HashTable:
Ans:
HashSet:
TreeSet:
Ans: Collections are the framework which is designed to store and manipulate the objects. The collections perform the following operations.
A group of objects are known as collections. All the collection classes and interfaces are available in the util package.
Ans:
Ans:
ArrayList:
Vector:
Ans:
Array:
ArrayList:
Ans:
Get method:
Post method:
Ans: Session is a state of a conversation between client and server and consists of multiple requests and responses. As HTTP and Web Server are stateless, the session maintains the unique information as session-id that is passed between client and server in every request and response. The methods of session management in servlets are:
Ans: JDBC is a software component which enables the application to interact with the database. There are mainly four types of JDBC drivers.
Ans: These are the statements which are used to send SQL commands to the database and also retrieve the data from the database. The methods like execute(), executeUpdate() and executeQuery() are provided by JDBC to interact with the database.
JDBC supports three types of statements.
Ans: There are three lifecycle methods in JSP
Ans: JSP is a technology of the server’s side programming with simple content generation. JSP’s are document-centric while Servlets are programs. A JSP page contains the fragments of java programming code which executes and instantiates Java classes. However, they occur inside an HTML template file. It provides the framework for the development of a Web Application.
Ans: Synchronization is about handling only one thread for accessing a block of code at a time. While multiple threads access the block of code then there are chances for inaccurate results, in the end, to avoid this the synchronization is provided for the sensitive block of code. The keyword “synchronized” means that a thread requires a key to access the synchronized code.
It locks as per object. Each Java object has a lock which has only one key. A thread can access a synchronized method only if the thread can get a key to the objects to lock. Towards this, the “Synchronization” keyword will be used.
Example:
public class ExampleThread implements Runnable{
public static void main (String[] args){
Thread t = new Thread ();
t.start ();
}
public void run(){
synchronized(object){
{
}
}
Ans: The modules of spring framework include.
Ans:
Ans:
Ans:
Ans: Data movement instructions move data from one location to another. The source and destination locations are determined by the addressing modes, and can be registers or memory. Some processors have different instructions for loading registers and storing to memory, while other processors have a single instruction with flexible addressing modes.
Ans: Assemblies are of two types:
Ans: Assemblies are made up of IL code modules and the metadata that describes them. Although programs may be compiled via an IDE or the command line, in fact, they are simply translated into IL, not machine code. The actual machine code is not generated until the function that requires it is called.
Ans:
Ans :
Ans:
The main internal hardware of a PC consists of processor, memory, and registers. Registers are processor components that hold data and address. To execute a program, the system copies it from the external device into the internal memory. The processor executes the program instructions.
The fundamental unit of computer storage is a bit; it could be ON (1) or OFF (0). A group of nine related bits makes a byte, out of which eight bits are used for data and the
last one is used for parity. According to the rule of parity, the number of bits that are ON (1) in each byte should always be odd.
So, the parity bit is used to make the number of bits in a byte odd. If the parity is even, the system assumes that there had been a parity error (though rare), which might have been caused due to hardware fault or electrical disturbance.
The processor supports the following data sizes −
Word: a 2-byte data item
Doubleword: a 4-byte (32 bit) data item
Quadword: an 8-byte (64 bit) data item
Paragraph: a 16-byte (128 bit) area
Kilobyte: 1024 bytes
Megabyte: 1,048,576 bytes
Ans: These are parameters to specify heap size in Java. The -Xms defines the size of the heap when JVM starts up and -Xmx is used to specify the maximum heap size for Java application i.e. your heap cannot grow beyond that and JVM will die by throwing OutOfMemoryError if your heap doesn't have enough space to create new objects. See here to learn more about heap memory in Java.
Ans: The Java virtual machine throws java.lang.OutOfMemoryError when there is not enough memory to run the application e.g. no more memory to create new objects, no more memory to create new threads etc. The most common OutOfMemoryError is the java.lang.OutOfMemoryError: java heap space, which comes when there is no more memory left to create a new object.
Ans: The main differences between 32-bit and 64-bit JVM are that later is designed for 64-bit operating system e.g. Windows 8 or later versions of Linux. From Java developer's perspective, the main difference between them comes from heap size. A 64-bit JVM virtually has unlimited heap memory as compared to 4GB of the theoretical limit of 32-bit JVM. If your program needs more memory, better run it on 64-bit JVM with large heap space. See here to learn more about 32-bit and 64-bit JVM
Batch starts on 11th Mar 2021, Weekday batch
Batch starts on 15th Mar 2021, Weekday batch
Batch starts on 19th Mar 2021, Fast Track batch