120 Important Java Questions Codegan
120 Important Java Questions Codegan
(https://codegnan.com/)
There are endless questions asked in a Java interview depending on the job position and candidates with
different skills, experience, and background knowledge.
However, some common JAVA questions often get asked and answered repeatedly in Java interviews
everywhere.
This is why we have asked our Java experts and mentors to share their top interview questions from
beginner to experienced level. Also, we have shared the possible answers for each question so you can
prepare well for your next interview in 2024.
No matter what Java position you’re applying for, these 120 Java interview questions and answers might
help you showcase your expertise during the interview.
Before we dive into the interview questions, check out our one-on-one classroom and online Java courses:
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
Java interview questions for beginners
1. What is Java programming?
Java is the most popular and widely used programming language that is defined as a collection of objects.
With the use of Java, we can develop software, games, desktop applications, web applications, etc.
JRE, on the other hand, is an acronym for Java Runtime Environment. It comes with the installation
package of JDK as JRE provides libraries and JVM that are used to execute a Java program.
So it’s a completely autonomous language of the platform. The Java byte code is behind all this portability.
Java compiler-generated bytecode can be interpreted by any JVM. So writing programs in Java and
expecting them to run on any platform is much simpler. The Javac Java compiler compiles Java code, and
the Java JVM compiler runs that code.
Call Us WhatsApp
1. Bootstrap class loader: This is the first class loader. It loads classes from the rt.jar file.
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
2. Extension class loader: It loads class files from JRE/lib/ext location.
3. Application class loader: This class loader depends on CLASSPATH to find the location of class files. If
you specify your jars in CLASSPATH, then this class loader will load them.
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Encapsulation is also assisted by their languages. They are also regarded as languages that are Prototype-
oriented.
Without a constructor, there is no other way to create an object. By default, Java provides a default
constructor for every object. If we overload a constructor then we have to implement a default constructor.
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
14. What is the use of the ‘this’ keyword in the Java language?
In Java,’ this’ keyword corresponds to the object’s current case. It is helpful for discriminating between
local variables and instance variables. It can be used for calling constructors or you can use it to refer to an
instance. In the case of the overriding method, this is used to drop the existing class method.
So they’ve got various ways to do the same thing. What implementation does the children’s class pick
now?
In Multiple Inheritance, this ends in complexity. This is the key explanation for Java not promoting the
introduction of Multiple Inheritance.
Let’s say you’ve got a TV class and another AtomBomb class. They all have the switchOn) (function, but
only the TV has the switchOff) (function. If both classes are inherited from your class, then you have a
dilemma.
You should change all parents to switchOn), but switchOff can only switchOff TV. But in Java, you can
implement several interfaces.
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
19. Why is it not a good practice to create static variables in
Java?
For all the objects in a class, static variables are common. There is no need to validate the value of a static
variable whenever a new object is produced. Any code that uses a static variable can be in any state. It can
be inside a new object or at the level of a class.
In a Java class, the scope of the static variable is also open-ended. If we want better control on scope,
then variables should be generated at the object development stage. Even specifying static variables is not
a reasonable idea since they go against the ideals of Object-Oriented Programming.
As such, labeling the key static method in Java is recognized as a convention. But if we delete the static,
then the uncertainty will be there. The Java process could not understand which method to call a class to
start the program. In the Java process, this convention helps to define the starting code for a class
program that is transmitted as an argument to the Java process.
1. Number of parameters
2. The data type of parameters
3. The sequence of the data type of parameters
26) State the difference between method overloading and method overriding in Java history?
For Example:
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
30. Explain abstract class in Java?
In Java, an abstract class has one or more abstract techniques. In the abstract class, an abstract method
is only declared, but it is not implemented. In Java, an abstract class must be generalized and a child class
must follow its abstract methods. Java also does not make a new instance of the Abstract type.
An abstract class may have body methods (non-abstract techniques) applied. Only abstract
approaches provide an interface. The interface will provide static/default methods in an applied form
from Java 8 onwards.
An abstract class can have instance member variables. An interface cannot have instance variables. It
can only have constants.
An abstract class will have a constructor. The interface can’t provide a constructor. Another class
needs to execute it.
A class can extend only one abstract class. A class can implement more than one interface.
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
38. What is the motive or purpose of using packages in Java?
A package is used to encapsulate a group of classes, interfaces, and sub-packages. Often, it is a
hierarchical structure for storing information. It is easier to organize the related classes and sub-packages
in this manner.
A Package also provides access protection for classes and interfaces. A package also helps in removing
naming collisions.
Following are the constructors that support chained exceptions in Throwable classes:
40. What is the difference between JAR and WAR files in Java?
Following are the differences between the JAR file and the WAR file are the following:
JAR file stands for Java Archive file which allows us to combine many files into a single file. Whereas,
WAR files stand for Web Application Archive files that store XML, java classes, and JavaServer pages,
etc., for Web Application purposes.
JAR files hold Java classes in a library. Whereas, WAR files store the files in the ‘lib’ directory of the
web application.
All the enterprise Java Bean classes and EJB deployment descriptors present in the EJB module are
packed and stored in a JAR file with a .jar extension. Whereas, the WAR file contains the web modules
such as Servlet classes, GIFs, HTML files, JSP files, etc., with .war extension
Where there is a key, the value of the key is changed with the new value. Some significant features of a
HashMap are its ability, its load factor, and the resizing of the threshold.
It is up to the Java Virtual Machine (JVM), however, to start the garbage collector immediately or later in
time.
Memory allocation: Memory allocation is carried out to store all the class instance variables and the
object’s implementation-specific data.
Initialization: Initialization takes place to initialize the default values of the objects.
Constructor: For their parent classes, constructors invoke the constructors. This method continues
until the java.langObject function Object() { [native code] } is called. For all objects in Java, the
java.lang. Object class is the foundation class.
Execution: Before the body of the function Object() { [native code] } is executed, all instance variables
should be initialised and all initialization blocks must be executed. After that, the function Object() {
[native code] }’s body is executed.
For example,
String str = new String(“java”);
The above statement does not put the String object str in the String pool. We need to call the String.
intern() method to put the String objects into the String pool explicitly. It is only possible when we create a
String object as String literal.
For example,
String str1 = “java”;
Java automatically puts the String object into the String pool.
When you compile and run a Java program, the Java compiler automatically imports the java.lang package
by default, even if there is no import statement for java.lang.
Therefore, classes like String, System, and Thread are available by default without needing to be imported.
For example:
👉 If you want to expand your Java knowledge, download our Core Java syllabus
(https://codegnan.com/blogs/java-course-syllabus/) and prepare your learning roadmap
accordingly.
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
Pattern Programs in Java
51. Write a Java program to draw a pyramid.
*
* *
* * *
* * * *
* * * * *
public class Codegnan
{
public static void PyramidPattern(int n)
{
for (int i=0; i<n; i++) //outer loop for number of rows(n)
{ for (int j=n-i; j>1; j–) //inner loop for spaces
{
System.out.print(” “); //print space
}
for (int j=0; j<=i; j++ ) //inner loop for number of columns
{
System.out.print(“* “); //print star
}
System.out.println(); //ending line after each row
}
}
public static void main(String args[]) //driver function
{
int n = 5;
PyramidPattern(n);
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
52. Write a Java program to draw a right triangle star pattern.
*
* *
* * *
* * * *
* * * * *
public class Codegnan
{
public static void RTriangle(int n)
{
int i, j;
for(i=0; i<n; i++) //outer loop for number of rows(n)
{ for(j=2*(n-i); j>=0; j–) // inner loop for spaces
{
System.out.print(” “); // printing space
}
for(j=0; j<=i; j++) // inner loop for columns
{
System.out.print(“* “); // print star
}
System.out.println(); // ending line after each row
}
}
public static void main(String args[])
{
int n = 5;
RTriangle(n);
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
53. Write a Java program to draw a left triangle star pattern.
*
* *
* * *
* * * *
* * * * *
public class Codegnan
{
public static void printStars(int n)
{
int i, j;
for(i=0; i<n; i++) //outer loop for number of rows(n)
{ for(j=2*(n-i); j>=0; j–) // inner loop for spaces
{
System.out.print(” “); // printing space
}
for(j=0; j<=i; j++) // inner loop for columns
{
System.out.print(“* “); // print star
}
System.out.println(); // ending line after each row
}
}
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
54. Write a Java program to draw a diamond pattern with 5 rows
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
given.
*
***
*****
****************
*******
*****
***
*
import java.util.Scanner;
public class Codegnan
{
public static void main(String args[])
{
int n, i, j, space = 1;
System.out.print(“Enter the number of rows: “);
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n – 1;
for (j = 1; j<= n; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(” “);
}
space–;
for (i = 1; i <= 2 * j – 1; i++)
{
System.out.print(“*”);
}
System.out.println(“”);
}
space = 1;
for (j = 1; j<= n – 1; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(” “);
}
space++;
for (i = 1; i<= 2 * (n – j) – 1; i++)
{
System.out.print(“*”);
}
System.out.println(“”);
Call Us WhatsApp
}
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
56. Write a Java program to draw a reversed pyramid star
pattern with 5 rows given.
* * * * *
* * * *
* * *
* *
*
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j<=i; j++)
{
System.out.print(” “);
}
for (int k=0; k<=rows-1-i; k++)
{
System.out.print(“*” + ” “);
}
System.out.println();
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
57. Write a Java program to draw a right-down mirror star
pattern with 5 rows given.
*****
****
***
**
*
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // takes input
System.out.println(“Enter number of rows: “);
int rows = sc.nextInt();
for (int i= rows; i>= 1; i–)
{
for (int j=rows; j>i;j–)
{
System.out.print(” “);
}
for (int k=1;k<=i;k++)
{
System.out.print(“*”);
}
System.out.println(“”);
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
58. Write a Java program to draw a right Pascal’s triangle
star pattern with 5 rows given.
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j<=i; j++)
{
System.out.print(“*”+ ” “);
}
System.out.println(“”);
}
for (int i=rows-1; i>=0; i–)
{
for(int j=0; j <= i-1;j++)
{
System.out.print(“*”+ ” “);
}
System.out.println(“”);
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
59. Write a Java program to draw a left Pascal’s triangle star
pattern with 5 rows given.
*
**
***
*********
****
***
**
*
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i= 1; i<= rows ; i++)
{
for (int j=i; j <rows ;j++)
{
System.out.print(” “);
}
for (int k=1; k<=i;k++)
{
System.out.print(“*”);
}
System.out.println(“”);
}
for (int i=rows; i>=1; i–)
{
for(int j=i; j<=rows;j++)
{
System.out.print(” “);
}
for(int k=1; k<i ;k++)
{
System.out.print(“*”);
}
System.out.println(“”);
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
60. Write a Java program to print alphabet A.
import java.util.Scanner;
public class Codegnan
{
// Java program to print alphabet A
void display(int n)
{
// Outer for loop for number of lines
for (int i = 0; i<=n; i++)
{
// Inner for loop for logic execution
for (int j = 0; j<= n / 2; j++)
{
// prints two column lines
if ((j == 0 || j == n / 2) && i != 0 ||
// print first line of alphabet
i == 0 && j != n / 2 ||
// prints middle line
i == n / 2)
System.out.print(“*”);
else
System.out.print(” “);
}
System.out.println();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Edureka a = new Edureka(); a.display(7);
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
61. Write a Java program to print an empty triangle.
*
* *
* * *
* * * *
* * * * * *
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i=1; i<= rows ; i++)
{
for (int j = i; j < rows ; j++)
{
System.out.print(” “);
}
for (int k = 1; k <= (2*i -1) ;k++)
{ if( k==1 || i == rows || k==(2*i-1))
{
System.out.print(“*”);
}
else
{
System.out.print(” “);
}
}
System.out.println(“”);
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
62. Write a Java program to print an empty downward triangle.
*********
* *
* *
* *
*
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i=rows; i>= 1 ; i–)
{
for (int j = i; j < rows ; j++)
{
System.out.print(” “);
}
for (int k = 1; k <= (2*i -1) ;k++)
{
if( k==1 || i == rows || k==(2*i-1))
{
System.out.print(“*”);
}
else
{
System.out.print(” “);
}
}
System.out.println(“”);
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
63. Write a Java program to print an empty diamond pattern.
*
* *
*
* *
**
* *
* *
*
* *
*
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i=1; i<= rows ; i++)
{
for (int j = rows; j > i ; j–)
{
System.out.print(” “);
}
System.out.print(“*”);
for (int k = 1; k < 2*(i -1) ;k++)
{
System.out.print(” “);
}
if( i==1) { System.out.println(“”);
}
else
{
System.out.println(“*”);
}
}
for (int i=rows-1; i>= 1 ; i–)
{
for (int j = rows; j > i ; j–)
{
System.out.print(” “);
}
System.out.print(“*”);
for (int k = 1; k < 2*(i -1) ;k++)
Call Us WhatsApp
{
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
System.out.print(” “);
}
if( i==1)
System.out.println(“”);
else
System.out.println(“*”);
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
65. Write a Java program to print the following number pattern.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
int i, j, k = 1;
for (i = 1; i <= 5; i++){
for (j = 1; j< i + 1; j++){
System.out.print(k++ + ” “);
}
System.out.println();
}
}
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
int number = 1;
System.out.printf(“%” + (n – i) * 2 + “s”, “”);
for (int j = 0; j <= i; j++) {
System.out.printf(“%4d”, number);
number = number * (i – j) / (j + 1);
}
System.out.println();
}
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
67. Write a Java program to print the following number pattern.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Taking rows value from the user
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+” “);
}
System.out.println();
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
68. Write a Java program to print the following number pattern.
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Taking rows value from the user
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();for (int i = rows; i >= 1; i–)
{
for (int j = rows; j >= i; j–)
{
System.out.print(j+” “);
}
System.out.println();}sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
69. Write a Java program to print the following number pattern.
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j–)
{
System.out.print(j+” “);
}
System.out.println();
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
70. Write a Java program to print the following binary numbers
pattern.
10101
01010
10101
01010
10101
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++)
{
int num;
if(i%2 == 0)
{
num = 0;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
else
{
num = 1;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
System.out.println();
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
71. Write a Java program to print the following binary numbers
pattern.
1
10
101
1010
10101
import java.util.Scanner;
public class Codegnan
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}
System.out.println();
}
sc.close();
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
72. Write a Java program to print Floyd’s pattern.
1
2 3
4 5 6
7 8 9 10
public class Floyd
{
public static void main(String[] args)
{
int rows = 4, number = 1;
for(int i = 1; i <= rows; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(number + ” “);
++number;
}
System.out.println();
}
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
74. Write a Java program to swap two variables using the third
variable.
import java.util.Scanner;
public class SwapTwoNumbers
{
public static void main(String[] args)
{
int x, y, temp;
System.out.println(“Enter x and y”);
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println(“Before Swapping” + x + y);
temp = x;
x = y;
y = temp;
System.out.println(“After Swapping” + x + y);
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
76. Write a Java program to find a prime number.
import java.util.Scanner;
public class Prime
{
public static void main(String[] args)
{
int temp, num;
boolean isPrime = true;
Scanner in = new Scanner(System.in);
num = in.nextInt();
in.close();
for (int i = 2; i<= num/2; i++)
{
temp = num%i;
if (temp == 0)
{
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(num + “number is prime”);
else
System.out.println(num + “number is not a prime”);
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
77. Write a Java program to find whether a number is palindrome
or not.
import java.util.Scanner;
public class Palindrome
{
public static void main (String[] args)
{
String original, reverse = “”;
Scanner in = new Scanner(System.in);
int length;
System.out.println(“Enter the number or String”);
original = in.nextLine();
length = original.length();
for (int i =length -1; i>;=0; i–)
{
reverse = reverse + original.charAt(i);
}
System.out.println(“reverse is:” +reverse);
if(original.equals(reverse))
System.out.println(“The number is palindrome”);
else
System.out.println(“The number is not a palindrome”);
}
}
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
81. Explain the Armstrong number concept.
Armstrong number is the number which is the sum of the cubes of all its units, tens and hundreds digits for
three-digit numbers.
153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
If you have a four-digit number, for example:
1634 = 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1 + 1296 + 81 + 256 = 1634
Now, in this program, we have a temp and integers declared. We have initialized c with the value 0. Then,
we need to assign the integer value which we are going to check for Armstrong (in our case, let us say
153). Then we have assigned our temp variable with that number which we are going to check.
Thereafter, we used conditional check where the remainder is assigned to a and the number is divided by
10 and assigned to n. Now, our c variable which was set to zero initially is assigned with c+ (a*a*a).
Suppose we have to evaluate a four-digit number then c should be assigned with c + (a*a*a*a).
Finally, we have put an if-else statement for conditional checking where we have compared the value
contained in c against temp (which has the actual number stored at this point). If it matches, then the
number is Armstrong otherwise not.
82. Write a Java program to remove all spaces from the string
using replace() function.
class Codegnan
{
public static void main(String[] args)
{
String str1 = “Remove the white spaces from here.”;
//Using replaceAll() function
String str2 = str1.replaceAll(“\\s”, “”);
System.out.println(str2);
}
}
}
Here are some additional points you could discuss to elaborate on your example command for an
interview:
Note that this relationship goes only one way: for any o1, o2 of some class C, where none of o1 and o2 are
null, then it can happen that o1.hashCode() == o2.hashCode() is true BUT o1.equals(o2) is false.
A public no-arg constructor is needed while using an Externalizable interface. In Serialization, we need to
define serialVersionUID. If it is not explicitly defined it will be generated automatically based on all the
fields, and methods of the class.
As Java programs run on the JVM, objects are generated on the heap, which is the dedicated portion of the
program’s memory.
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
95. Write a program to give a simple example of garbage
collection in Java.
public class TestGarbage1
{
public void finalize()
{
System.out.println(“object is garbage collected”);
}
public static void main(String args[])
{
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}
To aid in the method of Java offers an automatic method called reclaiming memories, called Collector of
Garbage.
1. Strong Reference
2. Soft Reference
3. Weak Reference
4. Phantom Reference
Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
100. How many types of nested classes are supported in Java?
Java supports four types of nested classes:
101. What is the difference between nested class and inner class
in Java?
Java’s Inner Class is a non-static class. It is a type of nested class that is identified with a Static modifier in
another class but is not eligible.
A Nested class is either a Static Nested class or a non-Static Inner class that may be a class. An Inner
Class shall have access, even if declared confidential, to all members of the Enclosing Class. The other
members of the enclosing class cannot be accessed by a Static Nesting class.
Due to which String is considered Immutable. In this case, if one variable A changes the value to “real
data”, then a new String literal with “real data” is created and A will point to a new String literal. While B
will keep pointing to “TestData”.
110. What is the base class for Error and Exception classes in
Java?
Error, as well as the Exception class, is derived from the Throwable class in Java.
The statements present in this block will still execute regardless of whether an exception exists in a try
block or not such as closing a connection, stream, etc.
try {
// handle exception
try {
} catch (Exception e) {
// handle exception
try {
} catch (IOException e) {
Key
throw
throws
1. Call Us WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
Definition
Throws is a keyword used in the method signature used to declare an exception that might get thrown by
the function while executing the code.
The throw is a keyword that is used to throw an exception explicitly in the program inside a function or
inside a block of code.
2. Internal implementation
Internally, the throw is implemented as it is allowed to throw only a single exception at a time i.e we
cannot throw multiple exceptions with a throw keyword.
The throw is a keyword that is used to throw an exception explicitly in the program inside a function or
inside a block of code.
3. Type of exception
Throws is a keyword used in the method signature used to declare an exception that might get thrown by
the function while executing the code.
The throw is a keyword that is used to throw an exception explicitly in the program inside a function or
inside a block of code.
4. Syntax
Throws is a keyword used in the method signature used to declare an exception that might get thrown by
the function while executing the code.
The throw is a keyword that is used to throw an exception explicitly in the program inside a function or
inside a block of code.
5. Declaration
Throws is a keyword used in the method signature used to declare an exception that might get thrown by
the function while executing the code.
The throw is a keyword that is used to throw an exception explicitly in the program inside a function or
inside a block of code.
Stack
Properties
Call Us WhatsApp
Vector
(tel:08047759925) (https://wa.me/919966188862?text=Hello)
Hashtable
Blocking
Queue
ConcurrentMap
ConcurrentNavigableMap
119. What command will you use to package your Maven project?
To package a project into a distributable format we use the following command:
mvn -package
Sairam Uppugundla’s expertise spans Python, Software Development, Data Analysis, AWS, Big Data, Machine
Learning, Natural Language Processing (NLP) and more.
He previously worked as a Board Of Studies Member at PB Siddhartha College of Arts and Science. With expertise in data science,
he was involved in designing the Curriculum for the BSc data Science Branch. Also, he worked as a Data Science consultant for
Andhra Pradesh State Skill Development Corporation (APSSDC).
(https://www.youtube.com/@Codegnan)
Call Us (https://www.linkedin.com/in/sairam-uppugundla/)
WhatsApp
(tel:08047759925) (https://wa.me/919966188862?text=Hello)