0% found this document useful (0 votes)
2 views

JAVA INTERVIEW QUESTION

The document outlines key Java interview questions and their answers, covering topics such as the definition of Java, differences between Java and C++, features of Java, and the roles of JDK, JRE, and JVM. It also explains memory management in Java, access specifiers, constructors, and the reasons Java does not support pointers. Overall, it serves as a comprehensive guide for understanding fundamental Java concepts and practices.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA INTERVIEW QUESTION

The document outlines key Java interview questions and their answers, covering topics such as the definition of Java, differences between Java and C++, features of Java, and the roles of JDK, JRE, and JVM. It also explains memory management in Java, access specifiers, constructors, and the reasons Java does not support pointers. Overall, it serves as a comprehensive guide for understanding fundamental Java concepts and practices.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JAVA INTERVIEW QUESTION

Q1. What is Java?


Ans. Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems in 1995
(now owned by Oracle), designed with a philosophy of "write once, run anywhere." It allows developers to build secure, robust,
and portable applications using a combination of the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java
Virtual Machine (JVM). Java supports features like automatic memory management, exception handling, multi-threading, and
extensive libraries (APIs), making it ideal for web, mobile, enterprise, and embedded applications across diverse platforms.

Q2. What are the Differences between C++ and Java?


Ans.
Aspect C++ Java
Platform-dependent (compiled to machine
Platform Dependency code) Platform-independent (runs on JVM)
Programming Paradigm Procedural and Object-Oriented Purely Object-Oriented
Memory Management Manual (using pointers and new/delete) Automatic (Garbage Collection)
Pointers Supports explicit pointers No direct support for pointers (for security)
Multiple Inheritance Supports multiple inheritance Not directly supported (uses interfaces)
Operator Overloading Supported Not supported
Compilation &
Execution Compiled to machine code Compiled to bytecode, executed on JVM
Templates/Generics Supports templates Supports generics
Mandatory (checked exceptions must be
Exception Handling Optional handled)
Libraries Standard Template Library (STL) Rich built-in libraries (Java API)
Execution Speed Faster (closer to hardware) Slower (JVM adds overhead)
More complex (manual memory
Syntax management) Simpler (automatic memory management)
Threading Depends on OS-level support Built-in threading support (via Thread class)
Web apps, mobile apps (Android), enterprise
Usage System programming, game development apps
File Extensions .cpp, .h .java

Q3. List the features of Java Programming language?


Ans. Features of Java Programming Language
1. Platform Independence
 Java’s “Write Once, Run Anywhere” (WORA) capability allows programs to run on any system with a Java Virtual
Machine (JVM).
2. Object-Oriented
 Java follows object-oriented principles, with everything organized around objects and classes (e.g., inheritance,
polymorphism, encapsulation).
3. Simple and Easy to Learn
 Java’s syntax is straightforward, inspired by C/C++, but without complex features like pointers and multiple
inheritance.
4. Robust and Secure
 Java provides strong memory management, exception handling, and a lack of direct pointer manipulation,
making applications safer and less prone to crashes.
5. Automatic Memory Management (Garbage Collection)
 Java automatically handles memory allocation and deallocation, freeing developers from manual memory
management.
6. Multithreading Support
 Java allows concurrent execution of multiple threads, enabling efficient CPU utilization and responsive
applications.
7. Distributed Computing Support
 Java supports distributed systems through APIs like RMI (Remote Method Invocation) and CORBA, allowing
applications to communicate over networks.
8. High Performance
 Though slower than C/C++, Java achieves high performance with Just-In-Time (JIT) compilation and
optimization techniques.
9. Dynamic and Extensible
 Java is adaptable to evolving environments by loading classes at runtime, and it supports dynamic linking of
new code libraries.
10. Rich Standard Library (Java API)
 Java comes with a vast collection of built-in classes and functions that simplify tasks like data structures,
networking, I/O, and GUI creation.
11. Security Features
 Java includes built-in security features such as access control, encryption, and sandboxing for executing
untrusted code safely.
12. Portability
 Java’s bytecode can run on any device with a compatible JVM, making it highly portable across different
platforms.
13. Network-Centric
 Java has comprehensive libraries for networking, making it easy to develop web and network-based
applications.
14. Open Source and Community Support
 Java is open-source, and its vast community offers extensive resources, frameworks, and tools like Spring and
Hibernate.
15. Backward Compatibility
 Java maintains backward compatibility, ensuring older programs can run on newer versions with minimal
changes.

Q4. what do you mean by Java Virtual Machine?


Ans. Java Virtual Machine (JVM) is a software-based engine that executes Java programs by converting platform-independent
Java bytecode into machine-specific instructions. It provides a runtime environment that ensures platform independence,
automatic memory management, and secure execution of Java applications across different operating systems.

Q5. Difference between JDK, JVM and JRE ?


ANS.
JRE (Java Runtime
Aspect JDK (Java Development Kit) Environment) JVM (Java Virtual Machine)
Used for developing, compiling, Provides an environment to Executes Java bytecode and
Purpose and debugging Java programs. run Java applications. converts it to machine code.
Includes JVM and core
Includes JRE, compiler (javac), libraries needed to run Java Part of JRE, handles bytecode
Components debugger, and tools. programs. execution.
Contains tools like compiler,
Development debugger, and documentation Does not include No development tools; only
Tools generator. development tools. executes programs.
Bytecode Compiles source code into
Execution bytecode. Runs bytecode using JVM. Directly executes bytecode.
Used by developers to write and Used by end-users to run Internal component responsible
User Role build applications. Java applications. for execution.
Ensures platform independence
Platform Enables cross-platform Supports platform- through bytecode
Independence development. independent execution. interpretation.
Memory Provides tools but relies on JVM Handles memory through Manages memory and garbage
Management for execution. JVM's garbage collection. collection.
Installation Needed to write and compile Needed only to run Java Comes with JRE, not installed
Requirement Java programs. applications. separately.
Summary:
 JDK: For developing Java programs (includes tools + JRE).
 JRE: For running Java programs (includes JVM + libraries).
 JVM: For executing Java bytecode (inside JRE).

Q6. How many Types of memory area are allocated by JVM?


Ans. Types of Memory Areas Allocated by JVM
JVM allocates five key memory areas for executing Java programs:
1. Method Area
 Stores class-level data such as class structures, method code, static variables, and constant pool.
 Shared among all threads.
2. Heap
 Stores objects and instance variables.
 Managed by Garbage Collector for automatic memory deallocation.
 Shared among all threads.
3. Stack
 Stores method-local variables, partial results, and the method call stack.
 Each thread has its own stack.
 Memory is allocated and deallocated in a Last In, First Out (LIFO) manner.
4. Program Counter (PC) Register
 Holds the address of the currently executing bytecode instruction for each thread.
 Each thread has its own PC register.
5. Native Method Stack
 Holds native method (non-Java) calls, typically written in C/C++.
 Each thread has its own native method stack.

Summary:
 Shared by all threads: Method Area, Heap.
 Thread-specific: Stack, Program Counter Register, Native Method Stack.

Q7. What If write static public void instead of Public static void ?
Ans. Both public static void and static public void will work the same way when declaring the main method in Java. The
program will run without issues.

Q8. Explain public static void main?


Ans. The public static void main method is the entry point for any Java application. It is where the program begins execution
when you run a Java program.
Here's a detailed breakdown of each part:
1. public
 Access Modifier: The public keyword makes the main method accessible from anywhere, meaning it can be called by the
Java runtime system when starting the program.
 Why public?: Since the main method is the entry point for the program, it must be accessible to the Java Virtual
Machine (JVM) from outside the class, regardless of where the class is located.
2. static
 Static Keyword: The static modifier means that the main method belongs to the class itself, rather than to instances of
the class.
 Why static?: The JVM calls the main method without creating an instance of the class. Therefore, main must be static, so
it can be executed directly by the JVM when the program starts.
3. void
 Return Type: The void keyword means the main method does not return any value. It simply performs the tasks and
terminates.
 Why void?: The main method doesn’t need to return any value because its purpose is to start the program and allow
the JVM to execute the code.
4. main
 Method Name: main is the name of the method that the JVM looks for to begin execution of a Java application. It is a
conventionally reserved method name.
 Why main?: The JVM looks specifically for this method to start the program. The name is predefined and fixed in Java.
5. String[] args
 Parameter: args is an array of String objects that can hold command-line arguments passed to the program.
 Why String[] args?: The JVM allows passing additional parameters (like arguments) from the command line to the
program when it is executed. These are received as a String array.

Q9. what are the various access specifier in java?


Ans. In Java, access specifiers (also known as access modifiers) define the visibility or accessibility of classes, methods, and
variables. There are four types of access specifiers in Java:
1. public
 Visibility: Accessible from anywhere (within the same package, from other packages, or from other classes).
 Usage: Used when you want to allow any class or package to access the method, variable, or class.
Example:
java
Copy code
public class Example { public int number; }
2. private
 Visibility: Accessible only within the same class. Not accessible from outside the class, even by subclasses.
 Usage: Used for variables or methods that should not be accessed directly from outside the class to
maintain encapsulation and data hiding.
Example:
java
Copy code
public class Example { private int number; // Only accessible within this class }
3. protected
 Visibility: Accessible within the same package and by subclasses (even if they are in different packages).
 Usage: Used when you want a method or variable to be accessible to classes in the same package and to
subclasses outside the package.
Example:
java
Copy code
public class Example { protected int number; // Accessible in subclasses and same package }
4. Default (No Modifier)
 Visibility: Also known as package-private. It is accessible only within the same package.
 Usage: Used when you want to allow access to a method or variable only within classes in the same package. If
no access modifier is specified, the default access modifier is applied.
Example:
java
Copy code
public class Example { int number; // Default access modifier, accessible within the same package }

Summary of Access Levels:


Access Modifier Class Package Subclass World (Other Packages)
public Yes Yes Yes Yes
private Yes No No No
protected Yes Yes Yes No
Default (No Modifier) Yes Yes No No
When to Use Each Modifier:
 public: Use when you want your class, method, or variable to be accessed universally.
 private: Use to restrict access to your class members, ensuring they are only used within the same class.
 protected: Use for members that should be visible within the same package and to subclasses.
 Default: Use when you only need access within the same package, without the need for external access.
Let me know if you need further details!

Q10. What gives Java in Write 1 run anywhere nature?


Ans. The "Write Once, Run Anywhere" (WORA) capability of Java is primarily due to the use of Java Bytecode and the Java Virtual
Machine (JVM). Here's how it works:

Q11. What are the advantage of package of Java?


Ans.
1. Namespace Management: Packages help avoid naming conflicts by grouping related classes into unique namespaces.
2. Access Control: Packages provide access control, allowing classes and members to be accessed only within the same
package or by subclasses.
3. Code Organization: Packages logically organize related classes, making code easier to manage and maintain.
4. Reusability: Packages enable easy reuse of related classes across different projects.
5. Simplifies Maintenance: Packages isolate code, making updates and maintenance more manageable.
6. Avoiding Clutter: Packages reduce clutter in the global namespace by organizing classes into distinct groups.
7. Protection and Security: Packages control visibility and access to classes and members, enhancing security.
8. Easier Collaboration: Packages allow different teams to work independently on different parts of the project.

Q12. What is an object?


Ans. An object in Java is an instance of a class that represents real-world entities with both state (attributes/fields)
and behavior (methods/functions). An object is created from a class blueprint and can hold data and perform actions defined
within the class.

Q13. What is constructor and types of constructor?


Ans. A constructor is a special method in Java used to initialize objects. It is called when an object of a class is created. A
constructor has the same name as the class and does not have a return type.
Key Characteristics:
 It is used to set the initial values for object attributes.
 A constructor is called automatically when an object is created using the new keyword.
 It can have parameters to initialize object fields with specific values.
Types of Constructors:
1. Default Constructor:
 A constructor with no parameters.
 If no constructor is defined in a class, Java provides a default constructor that initializes the object with default
values (e.g., 0 for numeric types, null for objects).
 Example:


2. Parameterized Constructor:
 A constructor that takes parameters to initialize an object with specific values.
 Useful when you want to provide initial values at the time of object creation.
 Example:


3. Copy Constructor:
A copy constructor is a constructor used to create a new object by copying the values of an existing object of the same
class. It takes an object of the same class as a parameter.

Summary:
1. Default Constructor: No parameters, sets default values.
2. Parameterized Constructor: Takes parameters to initialize the object with specific values.
3. Copy Constructor: A constructor that creates a new object by copying the values from an existing object of the same
class.
Example of usage:

Q14. what are the difference between Constructor and methods?


Ans. Here are the key differences between Constructors and Methods:
Aspect Constructor Method
Purpose Used to initialize an object when it is created. Used to define behavior or actions of an object.
Name Has the same name as the class. Can have any name (except the class name).
Return Type Does not have a return type (not even void). Must have a return type (e.g., void, int).
Invocation Called automatically when an object is created. Called explicitly using an object reference.
Can be overloaded (multiple constructors with Can be overloaded (multiple methods with
Overloading different parameters). different parameters).
Default If no constructor is defined, a default constructor is If no method is defined, there is no default
Behavior provided by Java. method behavior.
Used to perform actions or computations on
Purpose of Use Used to set initial values for object attributes. object data.
Example Car(String model) void start() { }
Summary:
 Constructors are used to initialize objects and are automatically invoked during object creation, while Methods define
the behavior or actions of objects and are explicitly called to perform operations.

Q15. why does java not support pointer?


Ans. Java does not support pointers directly for several important reasons, which are primarily related to safety, simplicity, and
security. Here's why Java avoids using pointers:
1. Memory Safety: Java eliminates pointer-related errors like memory corruption and segmentation faults by not allowing
direct memory access.
2. Simpler Memory Management: Java uses automatic garbage collection, removing the need for manual memory
management.
3. Security: By restricting direct memory access, Java reduces the risk of security vulnerabilities like buffer overflow
attacks.
4. Ease of Use: Java simplifies coding by using object references instead of pointers, making it easier and safer for
developers.
5. Object References: Java uses references, which act like pointers but are managed safely by the JVM, avoiding manual
memory handling.

You might also like