Answer: Java is:
A Singleton class ensures that only one instance of the class is created and provides a global point of access to it. It's used to represent a single instance of an object.
The transient
keyword is used to indicate that a field should not be serialized.
Garbage collection automatically removes objects that are no longer reachable or referenced, freeing memory in Java.
Java has four access modifiers:
Multithreading allows the execution of two or more threads in parallel, improving the efficiency of the CPU by performing multiple tasks concurrently.
Java Reflection is an API that allows inspection of classes, methods, fields, and other properties of objects at runtime.
Method overloading occurs when multiple methods with the same name have different parameters (either in type, number, or both).
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
The super
keyword is used to refer to the immediate parent class of a class. It can be used to access parent class methods and constructors.
An exception is an event that disrupts the normal flow of the program. It is an object that describes an abnormal condition in a program's flow.
IOException
).NullPointerException
).The try-catch
block is used to handle exceptions. The code that may throw an exception is placed inside the try
block, and the catch
block handles the exception.
A constructor is a special method used to initialize objects. It has the same name as the class and no return type.
The this
keyword refers to the current object instance, used to differentiate between class members and method parameters.
==
: Checks if two references point to the same object..equals()
: Compares the content of the two strings.A default
method in an interface allows you to define a method with a body in the interface, which can be optionally overridden by implementing classes.
Java collections include classes like ArrayList
, HashMap
, LinkedList
, TreeSet
, HashSet
, etc., used for storing and manipulating groups of objects.
Iterator
is used to traverse through a collection and safely remove elements while iterating.
compareTo()
method).compare()
method).The volatile
keyword ensures that the value of a variable is always read from and written to the main memory, ensuring visibility across threads.
Java has four access modifiers:
import java.util.HashSet;
public class LongestSubstring {
public static void main(String[] args) {
String str = "abcabcbb";
int length = lengthOfLongestSubstring(str);
System.out.println("Length of longest substring: " + length);
}
public static int lengthOfLongestSubstring(String s) {
HashSet<Character> set = new HashSet<>();
int left = 0, right = 0, maxLength = 0;
while (right < s.length()) {
if (!set.contains(s.charAt(right))) {
set.add(s.charAt(right));
maxLength = Math.max(maxLength, right - left + 1);
right++;
} else {
set.remove(s.charAt(left));
left++;
}
}
return maxLength;
}
}
Explanation: The program uses a sliding window approach with two pointers (left
and right
). The window expands by adding characters and shrinks when a repeating character is found.
public class GCD {
public static void main(String[] args) {
int a = 56, b = 98;
int gcd = findGCD(a, b);
System.out.println("The GCD of " + a + " and " + b + " is: " + gcd);
}
public static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
Explanation: The program uses the Euclidean algorithm to compute the GCD. It continues to reduce b
by finding the remainder of a
divided by b
until b
becomes zero.
import java.util.Stack;
public class BalancedParentheses {
public static void main(String[] args) {
String str = "{[()]}";
boolean isBalanced = isBalanced(str);
System.out.println("Is the string balanced? " + isBalanced);
}
public static boolean isBalanced(String str) {
Stack<Character> stack = new Stack<>();
for (char ch : str.toCharArray()) {
if (ch == '(' || ch == '{' || ch == '[') {
stack.push(ch);
} else {
if (stack.isEmpty()) {
return false;
}
char top = stack.pop();
if ((ch == ')' && top != '(') || (ch == '}' && top != '{') || (ch == ']' && top != '[')) {
return false;
}
}
}
return stack.isEmpty();
}
}
Explanation: The program uses a Stack
to keep track of opening parentheses. It checks whether each closing parenthesis matches the most recent opening parenthesis. If the stack is empty at the end, the string is balanced.