Java Impo Ques
Java Impo Ques
1. Class
A blueprint or template for creating objects. It defines properties (attributes) and behaviors
(methods).
2. Object
An instance of a class. It has state (data) and behavior (methods).
3. Encapsulation
Wrapping data and methods into a single unit (class) and restricting direct access to some of
the object's components. This is often done using access specifiers like private, public, and
protected.
4. Abstraction
Hiding complex implementation details and showing only the essential features. This helps
reduce complexity and increase efficiency.
5. Inheritance
The mechanism by which one class (child or subclass) inherits the properties and behaviors of
another class (parent or superclass), promoting code reusability.
6. Polymorphism
The ability of a function, object, or method to behave differently based on the context. It is of
two types:
o Compile-time Polymorphism (Method Overloading)
o Runtime Polymorphism (Method Overriding)
1. Data Hiding: Prevents external access to internal object details, improving security.
2. Control: Provides control over how data is accessed or modified.
3. Maintenance: Makes the code easier to maintain or modify without affecting other parts of the
program.
4. Reusability: Encapsulated code is easier to reuse and understand.
5. Increased Flexibility: You can change internal implementation without changing the interface
exposed to users.
What is polymorphism?
The word polymorphism means having many forms, and it comes from the Greek words poly
(many) and morph (forms), this means one entity can take many forms.
1.Compile-time Polymorphism
a. Method overloading
We can have one or more methods with the same name that are solely distinguishable by argument
numbers, type, or order.
Method Overloading occurs when a class has many methods with the same name but different
parameters. Two or more methods may have the same name if they have other numbers of
parameters, different data types, or different numbers of parameters and different data types.
b. Operator Overloading
An operator is said to be overloaded if it can be used to perform more than one function other than
the one its pre-defined for. Operator overloading is a mechanism through which we can change the
meaning of a pre-defined operator and make it work for user-defined objects. In this example, we'll
try to achieve the same by the use of method as Java does NOT support operator overloading.
2. Runtime Polymorphism
Runtime Polymorphism in Java known as Dynamic Method Dispatch. It is a process in which a
function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved
by Method Overriding. Method overriding, on the other hand, occurs when a derived class has a
definition for one of the member functions of the base class. That base function is said to be
overridden.
Method Overriding
Method overriding in Java means when a subclass provides a specific implementation of a method
that is already defined in its superclass. The method in the subclass must have the same name,
return type, and parameters as the method in the superclass. Method overriding allows a subclass to
modify or extend the behavior of an existing method in the parent class. This enables dynamic
method dispatch, where the method that gets executed is determined at runtime based on the
object's actual type.
Characteristics of Constructors:
Same Name as the Class: A constructor has the same name as the class in
which it is defined.
No Return Type: Constructors do not have any return type, not even void.
The main purpose of a constructor is to initialize the object, not to return a
value.
Automatically Called on Object Creation: When an object of a class is
created, the constructor is called automatically to initialize the object’s
attributes.
Used to Set Initial Values for Object Attributes: Constructors are primarily
used to set the initial state or values of an object’s attributes when it is
created.
What is JDK?
The Java Development Kit (JDK) is a cross-platformed software development environment that offers a
collection of tools and libraries necessary for developing Java-based software applications and applets. It is a
core package used in Java, along with the JVM (Java Virtual Machine) and the JRE (Java Runtime
Environment).
Contents of JDK
The JDK has a private Java Virtual Machine (JVM) and a few other resources
necessary for the development of a Java Application.
JDK contains:
Java Runtime Environment (JRE),
An interpreter/loader (Java),
A compiler (javac),
An archiver (jar) and many more.
The Java Runtime Environment in JDK is usually called Private Runtime
because it is separated from the regular JRE and has extra content.
What is JVM?
A Java Virtual Machine (JVM) is a virtual machine that allows Java programs to
run on different platforms and operating systems. It acts as an interpreter,
translating Java bytecode into machine code that the computer can understand.
The JVM is a crucial part of the Java Runtime Environment (JRE).
What is JRE?
A Java Virtual Machine (JVM) is a virtual machine that allows Java programs to
run on different platforms and operating systems. It acts as an interpreter,
translating Java bytecode into machine code that the computer can understand.
The JVM is a crucial part of the Java Runtime Environment (JRE).
In Java, what are the differences between heap and stack memory?
1. Stack follows the LIFO order. Heap does not follow any order because here, the
pattern of memory allocation is not fixed.
2. It stores the entities that have a short life, like Heap stores entities like objects.
variables and methods.
3. It is not flexible as we cannot make any changes It is flexible as we can make changes here even
once the memory allocation is done. after the allocation of memory.
4. It is faster than heap in terms of allocation and It is slower than stack in terms of allocation and
deallocation. deallocation.
5. The size of stack memory is small. The size of heap memory is large.
6. Through the JVM option -Xss, we can improve Through the JVM options -Xmx and -Xms, we can
the size of a stack. change the size of a stack.
8. The implementation part is easy here. The implementation part is tough here.
9. In stack, the memory allotment is continuous. In heap, the memory allotment is random.
10. The allocation and deallocation are automatically Here the allocation and deallocation are done
performed by the compiler. manually.
1. Public
It is an Access modifier, which specifies from where and who can access the
method. Making the main() method public makes it globally available. It is made
public so that JVM can invoke it from outside the class as it is not present in the
current class.
2. Static
It is a keyword that is when associated with a method, making it a class-
related method. The main() method is static so that JVM can invoke it
without instantiating the class. This also saves the unnecessary wastage of
memory which would have been used by the object declared only for calling
the main() method by the JVM.
3. Void
It is a keyword and is used to specify that a method does not return
anything. As the main() method does not return anything, its return type is void.
As soon as the main() method terminates, the Java program terminates too.
Hence, it doesn't make any sense to return from the main() method as JVM
can't do anything with its return value of it.
4. main
It is the name of the Java main method. It is the identifier that the JVM looks
for as the starting point of the Java program. It's not a keyword.
5. String[] args
It stores Java command-line arguments and is an array of
type java.lang.String class. Here, the name of the String array is args but it is
not fixed and the user can use any name in place of it.
The Java String Pool (also known as the String Intern Pool) is a special memory region in the Java heap
where String literals are stored to optimize memory usage and improve performance.
📝 Note: Numeric types (int, float, etc.) are signed and store both positive and negative values.
What are the default values assigned to variables and instances in Java?
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
boolean false
A static variable in Java is a variable that belongs to the class, not to any specific object. It is shared across
all instances of the class and is initialized only once when the class is first loaded into memory.
What is Inheritance? Explain types of inheritance.
Inheritance is one of the four fundamental OOP concepts (along with encapsulation, polymorphism, and
abstraction). It allows a class to acquire the properties and behaviors (fields and methods) of another class.
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the
derived class also acts as the base class for other classes. In the below image, class A
serves as a base class for the derived class B, which in turn serves as a base class for the
derived class C. In Java, a class cannot directly access the grandparent’s members if they
are private.
Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one
subclass. In the below image, class A serves as a base class for the derived classes B, C,
and D.
* : Multiplication
/ : Division
% : Modulo
+ : Addition
- : Subtraction
2. Unary Operators
Unary Operators need only one operand. They are used to increment,
decrement, or negate a value.
- , Negates the value.
+ , Indicates a positive value (automatically converts byte, char,
or short to int).
++ , Increments by 1.
o Post-Increment: Uses value first, then increments.
o Pre-Increment: Increments first, then uses value.
-- , Decrements by 1.
o Post-Decrement: Uses value first, then decrements.
o Pre-Decrement: Decrements first, then uses value.
! , Inverts a boolean value.
3. Assignment Operator
'=' Assignment operator is used to assign a value to any variable. It has right-
to-left associativity, i.e. value given on the right-hand side of the operator is
assigned to the variable on the left, and therefore right-hand side value must be
declared before using it or should be a constant.
The general format of the assignment operator is:
variable = value;
In many cases, the assignment operator can be combined with others to create
shorthand compound statements. For example, a += 5 replaces a = a + 5.
Common compound operators include:
+= , Add and assign.
-= , Subtract and assign.
*= , Multiply and assign.
/= , Divide and assign.
%= , Modulo and assign.
4. Relational Operators
Relational Operators are used to check for relations like equality, greater than,
and less than. They return boolean results after the comparison and are
extensively used in looping statements as well as conditional if-else statements.
The general format is ,
variable relation_operator value
Relational operators compare values and return Boolean results:
== , Equal to.
!= , Not equal to.
< , Less than.
<= , Less than or equal to.
> , Greater than.
>= , Greater than or equal to.
5. Logical Operators
Logical Operators are used to perform "logical AND" and "logical OR"
operations, similar to AND gate and OR gate in digital electronics. They have a
short-circuiting effect, meaning the second condition is not evaluated if the first
is false.
Conditional operators are:
&&, Logical AND: returns true when both conditions are true.
||, Logical OR: returns true if at least one condition is true.
!, Logical NOT: returns true when a condition is false and vice-versa
6. Ternary operator
The Ternary Operator is a shorthand version of the if-else statement. It has
three operands and hence the name Ternary. The general format is,
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute
the statements after the '?' else execute the statements after the ':'.
7. Bitwise Operators
Bitwise Operators are used to perform the manipulation of individual bits of a
number and with any of the integer types. They are used when performing
update and query operations of the Binary indexed trees.
& (Bitwise AND): returns bit-by-bit AND of input values.
| (Bitwise OR): returns bit-by-bit OR of input values.
^ (Bitwise XOR): returns bit-by-bit XOR of input values.
~ (Bitwise Complement): inverts all bits (one's complement).
8. Shift Operators
Shift Operators are used to shift the bits of a number left or right, thereby
multiplying or dividing the number by two, respectively. They can be used when
we have to multiply or divide a number by two. The general format ,
number shift_op number_of_places_to_shift;
<< (Left shift): Shifts bits left, filling 0s (multiplies by a power of two).
>> (Signed right shift): Shifts bits right, filling 0s (divides by a power of
two), with the leftmost bit depending on the sign.
>>> (Unsigned right shift): Shifts bits right, filling 0s, with the leftmost bit
always 0.
What is scanner class? How it works? Also explain its working with one example.