DEV Community

velvizhi Muthu
velvizhi Muthu

Posted on

Module-01--Interview Question _JAVA

01. How many data types are in JAVA?
Java has two categories of data types:

  • Primitive Data Types (8 total): byte, short, int, long, float, double, char, boolean
  • Non-Primitive (Reference) Data Types: Classes, Interfaces, Arrays, Enums, etc.

02. What is the default value of Char in the data type?
The default value of a char is \u0000 (null character), which is not visible.

03. What command are you using to compile and run the Java program using the command?
Compile: javac ClassName.java
Run: java ClassName

04. What is JDK?
JDK stands for Java Development Kit.
It is a software package provided by Oracle.
JDK convert from .java file into .class(Bytecode) file.
That .class file is unreadable file.
That .class file we can run anywhere like Mac, Windows, Linux.
So Bytecode(.class) is platform-independent because it does not depend on OS.
JDK includes:
JRE (Java Runtime Environment).
JVM (Java Virtual Machine).
To develop Java applications → Without JDK, we cannot compile Java code.

05. What is JRE?
JRE stands for Java Runtime Environment.
It is a software package that provides everything required to run Java programs.
JRE helps to us for run the .class file.
JRE platform-dependent.
JRE contains:
JVM (Java Virtual Machine)
JRE contains Predefined Java packages and classes like java.lang, java.util, java.io.

06. Diff b/w static variable and Non-static variable?
Static Variable:
Belongs to - The class, not to any specific object.
Memory Allocation -Only once, at class loading time. Shared among all instances.
Accessed by - Can be accessed using the class name or an object.
Use Case - When you want a common value shared across all objects (e.g., a counter for the number of objects created).
Keyword - Declared with the static keyword.

Non-static Variable:
Belongs to - Each object of the class.
Memory Allocation - Every time a new object is created.
Accessed by - Must be accessed through an object.
Use Case - When each object needs to maintain its own state (e.g., name, age, etc.).
Keyword - No keyword needed (default behavior).

07. How to call the static variable and non-static variable?
Static Variable: ClassName.variableName or object.variableName (not recommended)
Non-static Variable: Accessed using an object: object.variableName

08. What is JIT?
JIT stands for Just-In-Time Compiler.
JIT convert form bytecode(.class) into binary code.

09. What is an Object?
Object is physical entity.
Syntax of object :
ClassName referenceName = new ClassName();
MobileShop mobile = new MobileShop();
Object is combination of state and behaviour.
new is a java keyword.
If we mention a new keyword in object, JVM will be allocated one memory in heap at runtime.

Definition for state:

The data or information an object holds.
Variables (also called fields or attributes).

Definition for behaviour:
The actions or functions the object can perform.

Defined by: Methods inside the class.

10. What is the Class?
Class is template / blueprint.
Class is logical entity.
Class is a java keyword.
Syntax of class :
class MobileShop
{

    }
First letter should be capital in class name.
Class name should allow these special char like $ and _
Class name should allow numeric values in last & intermediary.
Enter fullscreen mode Exit fullscreen mode

11. Can we create the object without a class?
No, you cannot create an object without a class in Java.

12. Can we create the object without the "new" keyword?
Yes, in Java, you can create an object without using the new keyword, but it depends on the context and how the object is being created.

13. Who will allocate the object memory in the heap?
The Java Virtual Machine (JVM) allocates memory for objects in the heap at runtime.

14. Why do we use the operator in JAVA?
Operators are used to perform operations on variables and values (e.g., arithmetic, logical, relational).

15. How many types of operators are available in JAVA?
Arithmetic Operators
Relational (Comparison) Operators
Logical Operators
Bitwise Operators
Assignment Operators
Unary Operators
Ternary Operator
Instance of Operator

16. What is the Variable?
A variable is the name of a memory location.
A variable holds a value.
Variable names should start with a small letter.
Variable names can start with these special characters, like $ and _ symbol.
Ex :
int tamilMark = 60;
int ---> data type
tamilMark ---> variable name
= ---> assignment operator
60 ---> value
; ---> end of statement

17. How many primitive data types are in Java?
There are 8 primitive data types:
byte, short, int, long, float, double, char, boolean

18. Why is JDK independent?
JDK is not truly platform-independent, but the Java language is, because the code you write is compiled to bytecode, which runs on any platform with a JVM.

19. What is JVM?
JVM stands for Java Virtual Machine.
JVM helps us run the .class file.
JVM is platform-dependent.
JVM manages memory(Heap, Stack, String constant pool) and garbage collection.
JVM contains:
JIT (Just In Time).

20. What are static variables and non-static variables?
Static Variable: Shared across all instances; belongs to the class.
Declared with the static keyword.
Common for all objects of the class.
Can be accessed with ClassName.variableName or variableName.

Non-static Variable: Each object has its copy; it belongs to the instance.

21. What are local variables and global variables?
Local Variable :
Declared inside a method, constructor, or block.
Scope: Only available within that method/block.
Must be initialised before use.
Not accessible outside the method.
Global variables or Fields :
These are variables declared outside methods, but inside the class.
Instance(object) Variable (Non-static)
* Declared without static inside a class.
* The default value is set if not initialised.

22. Is it really an instance variable stored in the heap?
Yes, instance variables are stored in the heap as part of the object.

23. What is an operator?
In Java, an operator is a special symbol or keyword used to perform operations like add, sub, div etc...
Types of operators:
Assignment Operators : =
Arithmetic Operators : +, -, *, /, %
Unary Operators :
++ -> Increment operator
-- -> Decrement operator
no++ --> post increment
++no --> Pre increment
no-- --> post Decrement
--no --> Pre Decrement
Equality and Relational Operators : ==, !=, <, >, <=, >=
Conditional Operators : &&, ||
&& = AND operator
|| = OR operator
Operator: symbol
Operand: Value

Top comments (0)