Basic Java Interview Questions Answers for 2024

Here, we have listed topic-wise the most important 50+ basic Core Java interview questions with the best possible answers for 2024. These basic Java interview questions are often asked from freshers and beginners in any Java technical interview round.

We have also covered the most important coding programming questions with answers that will boost up your knowledge to crack the interview round.

Basic Java Class and Object Interview Questions


1. What is an object in Java?

Ans: An object in Java is any real-world thing that has properties and actions. In other words, an entity that has state, behavior, and identity is known as object.

2. What are the characteristics of an object in Java?

Ans: An object has three characteristics that are as follows:

a) State: It represents the properties of an object. It is represented by instance variable/attribute of an object.

b) Behavior: It represents functionality or actions. It is represented by methods/functions in Java.

c) Identity: It represents the unique name of an object. It differentiates one object from the other. The unique name of an object is used to identify the object.

3. What is a class in Java?

Ans:  A class is a user-defined data type that acts as a template for creating objects of the identical type. It represents the common properties and actions (functions) of an object.

4. Why do we need a class in Java?

Ans: We need a class for packing together a group of logical related data items (fields) and methods (functions) that work on them.

5. What is an attribute?

Ans: A variable that is defined in a class is called attribute.


6. What is instantiating of an object?

Ans: The process of creating an object of a particular class is called instantiating of an object.

7. Why is an object called an instance of a class?

Ans: An object is called an instance of a class because every object created from a class gets its own instances of variables defined inside a class. Multiple objects can be constructed from the same class.

8. Is a class a data type in Java?

Ans: Yes, a class is also considered a user-defined data type because a user defines a class.

9. Can a class represent an object?

Ans: No, a class never represents an object. It represents data and actions that an object will have.

10. Which of the following are valid class names?

Student, _Student, class, true, _, CollegeStudent, schoolName, null, _class, Class99

Ans: Student, _Student, _, CollegeStudent, _class, Class99 are the valid class names. class, true, null, schoolName are not valid class names.


11. What is the difference between class and object in Java?

Ans: The differences between class and object are as follows:

  • A class is a user-defined data type, whereas an object is an instance of class data type.
  • A class represents the type of objects, whereas an object represents particular instances of things.
  • Classes do not occupy memory location, but objects occupy memory location.
  • Classes cannot be manipulated due to not available in the memory location, but objects can be manipulated.

12. What is the meaning of creating an object in Java?

Ans: Creating an object means allocating memory to store the data of variables temporarily. i.e. we create an object of a class to store data temporarily.


13. How to create an object in Java?

Ans: In Java, an object of a class is created using the new keyword in three steps. They are as follows.

  • Declaration of a reference variable.
  • Creation of an object.
  • Linking the object and reference variable.

14. What is the general syntax to create an object of class?

Ans: The general syntax to create an object of a class is as follows:

Classname object_reference_variable = new Classname();

15. Consider a class whose name is College and object reference variable myCollege. It is declared like this:

College myCollege;

Answers the following questions:

a) Is an object of class College created?

b) What is the meaning of this statement: College myCollege;?

Ans a. No, an object of class College is not created still. It only refers to an object.

Ans b. The statement College myCollege; tells the JVM to allocate memory space for a reference variable and names that reference variable myCollege. The reference variable is of type College.


16. What is an object reference in Java?

Ans: An object reference is a unique hexadecimal number that represents a memory address of the object. When an object is created, a new reference number is allocated to it.


17. Where the address of an object is stored in the memory?

Ans: The address of an object is stored in the object reference variable in the stack memory.

18. What is an object reference variable in Java?

Ans: Object reference variable is a variable that stores the address of an object in the stack memory.

19. Suppose we have created an object of a class Student like this: new Student();

Answers the following questions:

a) Is an object of class Student created?

b) What is the meaning of this statement: new Student();

Ans a: Yes, an object of class Student is created.

Ans b: The statement new Student(); tells the JVM to allocate memory space for a new Student object on the heap.

20. For which purpose we create an object of class in Java?

Ans: We create an object of a class in java to store data temporarily in Java application and to access members of any particular class.


21. How many ways to create an object in Java?

Ans: There are several ways to create an object of class in Java. They are as follows.

  • Using the new keyword
  • Using Class.forName
  • Using Clone.
  • Using Object Deserialization.
  • Using ClassLoader.

22. What is new in Java?

Ans: In Java, a new is a special keyword that is used to create an object of the class. It allocates the memory to store an object during runtime and returns a reference to it.


23. What is void in Java?

Ans: Void is a keyword that indicates that this method does not return any data back to the class of an object.

24. What is the difference between reference variable and variable?

Ans: Reference variable holds the address of an object whereas a variable holds data.

25. What is object declaration in Java?

Ans: The process of defining a variable along with its data type and name is called the declaration of state of an object or simply object declaration.


26. What is object initialization in Java?

Ans: The process of assigning a value of the variable is called initialization of state of an object or simply object initialization.

27. How to initialize the state of object in Java?

Ans: There are three ways by which we can initialize the state of an object. In other words, we can initialize value of variables in Java by using three ways. They are as follows:

  • By using constructor
  • By using a reference variable
  • By using a method.

28. What is an anonymous object in Java?

Ans: An object which has no reference variable is called anonymous object in Java

29. Write the syntax to create an anonymous object in Java?

Ans: The general syntax to create an anonymous object is as follows:

new Class_name();

30. What is an anonymous class in Java?

Ans: A class that does not have a name is called anonymous class in Java.


31. When to use an anonymous object?

Ans: Anonymous object creation is useful when it is not used more than once.

32. Define concrete class in Java.

Ans: A class whose object can be created and whose all methods have body is called concrete class.

Basic Java Data types and Variables Interview Questions


1. What is a statement in Java?

Ans: A statement in Java consists of variables, constants, operators, comments, keywords, identifiers, punctuators, etc.

2. What are Java tokens?

And: Tokens in Java are the various elements in java program that are identified by the compiler. It is the smallest element of a program that is meaningful to the compiler.

For example: final double p = 3.14; //  constant. This statement consists of six tokens: ‘final’, ‘double’, ‘p’, ‘=’, ‘3.14’, and ‘;’.

3. What are keywords in Java?

Ans: Keywords in Java are predefined code (or words) that has a specific meaning and that meaning can be changed. Some commonly used keywords are: class, new, null, void, return, abstract, package, etc.

4. Is main keyword in Java?

Ans: No, main is not a keyword in Java.

5. What is data type in Java?

Ans: A data type in java is a term that specifies memory size and type of values that can be stored into the memory location. In other words, a data type defines different values that a variable can take.


6. What are the types of data types in Java?

Ans: There are two types of data types in java that are as follows:

  • Primitive data types
  • Non-primitive data types

7. What is a Primitive data type in Java?

Ans: A data type whose variable can store only one value at a time is called primitive data type. Primitive data types are predefined in Java and cannot be developed by programmers. It can be used by programmers when creating variables in the program.

8. What are the different types of primitive data types supported by Java programming language?

Ans: Java defines eight primitive data types: boolean, char, byte, short, int, long, char, float, and double.

9. What are the different types of Integer data types?

Ans: Java defines four integer data types: byte, short, int, and long.

10. Which is the smallest integer data type?

Ans:  Byte is the smallest integer data type that has the least memory size allocated.


11. What is the memory size of byte data type?

Ans: The memory size of a byte is 8 bits.

12. What is the default value of byte?

Ans:  The default value of byte is 0.

13. What is the range value of byte data type?

Ans: The range value of byte is from -128 (2^7) to 127 (inclusive)(2^7 – 1).

14. What is the storage capacity of short integer data type?

Ans: The default memory size (storage capacity) of short is 16 bits (i.e. 4 bytes).

15. What is the default value of short?

Ans: The default value of short is 0.


16. Which data type is mostly used for integer values in Java programming?

Ans: int data type.

17. What are the memory size and default value of int data type?

Ans: The memory size of int is 32 bits (4 bytes). The default value of int is 0.

18. What is the default memory size and value of long data type?

Ans: The default memory size of long data type is 64 bits (8 bytes) and default value is 0.

19. Will the code compile successfully?

public class Test {
public static void main(String[] args) {
  byte num = 129;
  System.out.println(num);
  }
}

Output:

Compile-time error: Type mismatch: cannot convert from int to byte

This error occurs because the value is out of the range of byte type. The range of byte is -128 to +127.

20. Will the code compile successfully? If yes, what will be the output of the below program?

public class Test {
public static void main(String[] args) {
  short num = 12_9_10;
  System.out.println(num);
  }
 }

Answer: Yes, the code will be compiled successfully. The output will be 12910.


21. Which of the following lists of integer (numeric) primitive types is presented in order from smallest to largest data type?

a) byte, short, int, long

b) int, short, byte, long

c) short, byte, int, long

d) short, int, byte, long

Ans: a (byte, short, int, long)

22. Suppose we declare int x = 10; Is this declaration advisable?

Ans: No, it is not advisable. We should use a byte variable instead of using int variable because byte will take less memory size and improve the speed of execution of the program.

Whenever possible, you should use smaller data types because wider data types take more time for manipulation.

23. What will be the output of the following program?

public class Test {
public static void main(String[] args)
{
   long x = (long) 200000.255;
   System.out.println(x);
  }
}

Output: 200000

24. What is the use of float and double data types in Java programming language?

Ans: Float data type is used to represent the decimal number which can hold 6 to 7 decimal digits. Double data type is also used to represent decimal numbers up to 15 decimal digits accurately.

25. What are the default memory size and default value of float data type?

Ans: The default memory size allotted for float data type is 32 bits (i.e. 4 bytes). The default value for float data type is 0.0f.


26. What are the default memory size and default value of double data type?

Ans:  The default memory size allotted for double is 64 bits (i.e. 8 bytes). The default value is 0.0d.

27. What is the use of char data type in Java programming language?

Ans: A char data type is mainly used to store a single character like P, a, b, z, x, etc.

28. What are the default memory size and default value of char data type?

Ans: The default memory size allotted for char data type is 2 bytes. The default value is u0000.

29. What is the use of boolean data type in Java?

Ans: Boolean data type is generally used to test a particular conditional statement during the execution of the program. It represents one bit of information as either true or false.

30. What is the default memory size and default value for boolean data type in Java?

Ans: The default memory size allotted for boolean data type is 1 bit. The default value is false.


31. Why Java takes 2 bytes of memory for storing character?

Ans: Java supports more than 18 international languages. To handle more than 18 international languages, 1 byte of memory is not sufficient for storing all characters and symbols present in 18 languages.

32. Why take boolean data types zero bytes of memory?

Ans: Boolean data type takes zero bytes of memory space because boolean data type in Java is implemented by Sun Micro System using the concept of a flip-flop.

A flip-flop is a general-purpose register that stores one bit of information (one for true and zero for false).

33. Which of the following lists of primitive data types are presented in order from largest to smallest data type?

a) byte, char, float, double

b) byte, char, double, float

c) double, float, byte, char

d) char, double, float, int

Ans: c (double, float, byte, char)

34. Which of the following declarations will give compile-time error?

a) double d1 = 5.50f;

b) double d2 = 5.0;

c) float f1 = 10f;

d) float f2 = 10.00;

Ans: d

35. Which of the following declarations will not compile?

a) int num1 = 999;

b) int num2 = _999;

c) int num3 = 9_9_9;

d) All of the above compile

Ans: b


36. Which of the following declarations will give compile time error?

a) double x, int y = 0;

b) int x, y;

c) int x, y = 0;

d) int x = 0, y = 0;

Ans: a

37. What is the difference between byte and char data types in Java?

Ans: The main difference between byte and char is that a byte can store raw binary data whereas a char stores characters or text data.

38. What will happen when we assign a variable of primitive data type to another variable of same type?

Ans: When a variable of primitive data type is assigned to another variable, a copy is created.

39. What is non-primitive data type in Java?

Ans: A data type that is used to store a group of values (either of same type or different type or both) is called non primitive data type or user-defined data type. It is created by programmers in their program. A non-primitive data type is also known as advanced data types in java.

40. Why non-primitive data type is also called reference data type in Java?

Ans: When we declare a variable of non-primitive data type, it references a memory location where data is stored in the heap memory. Therefore, the variable of a non-primitive data type is also called referenced data type in Java.


41. What is the default value of any reference variable?

Ans: null

42. What are types of non-primitive data types in Java?

Ans: There are five types of non-primitive data types in Java. They are:

  • Class
  • Object
  • String
  • Array
  • Interface

43. What is the difference between primitive data types and non-primitive data types in Java?

Ans: Refer to this tutorial: Non-primitive data types in Java

44. In which memory location JVM stores primitive type values?

Ans: Stack memory.

45. What will happen when we assign a variable of reference data type to another variable of same reference type?

Ans: When a variable of reference type is assigned to another reference variable, both points to the same object.


46. If a value to variable of reference data type is not assigned, what does it contain?

Ans: It contains a null default value when it is unassigned.

47. What is an identifier in Java?

Ans: An identifier is a sequence of characters that are made up of letters, digits, underscores(_), and dollar signs ($). It represents a name that identifies elements such as variables, methods, classes, and other items in the program. For example, Student, num1, num2, main, etc.

48. Which of the following identifiers are valid?

student, Test, area, a++, –a, 5#R, $5, #77, class, public, int, x, y, radius.

Ans: Test, student, area, radius, $5, x, y, radius are valid identifiers because they follow rules.


Related Java Interview Questions on Topic Wise


In this tutorial, we have covered almost all the important basic Core Java interview questions and answers for 2024. Hope that you will have understood all the answers to the basic Java interview questions.
All the best!!!

Please share your love