Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views11 pages

JAVAA (Entretien)

The document provides a comprehensive overview of Java concepts, including the differences between JDK, JRE, and JVM, features of Java, object-oriented programming principles, exception handling, and memory management. It explains key Java components such as classes, objects, constructors, inheritance types, and the Java Collections Framework. Additionally, it covers important keywords, memory types, and the implications of unhandled exceptions.

Uploaded by

Ikram Bec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

JAVAA (Entretien)

The document provides a comprehensive overview of Java concepts, including the differences between JDK, JRE, and JVM, features of Java, object-oriented programming principles, exception handling, and memory management. It explains key Java components such as classes, objects, constructors, inheritance types, and the Java Collections Framework. Additionally, it covers important keywords, memory types, and the implications of unhandled exceptions.

Uploaded by

Ikram Bec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

What is the difference between JDK, JRE, and JVM?

• Answer:

o JDK (Java Development Kit): A software development kit that includes tools for
developing Java applications, such the JRE (Java Runtime Environment), an
interpreter/loader (Java Virtual Machine), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), and other tools needed for development.

o JRE (Java Runtime Environment): Provides libraries and other components to


run Java applications. It includes the JVM but not the development tools.

o JVM (Java Virtual Machine): An engine that executes Java bytecode. It converts
bytecode into machine code and manages memory and runtime environment.

Explain the features of Java // Expliquez les fonctionnalités de Java.

• langage de haut niveau :Automatiquement Lorsqu'un objet n'est plus nécessaire,


le récupérateur de mémoire libère la mémoire .
• poo : En Java, tout est traité comme un objet, elle utilise l'héritage et le
polymorphisme.
• Platform-Independent: Write Once, Run Anywhere , thanks to JVM.
• Robuste : une gestion efficace de la mémoire et une gestion des exceptions rendent
les programmes Java stables.

What is a class and an object in Java?

• Answer:

o Class: Une classe est un modèle pour créer des objets. Elle définit un
ensemble de propriétés et de méthodes

o Une classe n'occupe pas de mémoire

o Object: Un objet est une instance d'une classe. C’est une entité concrète
créée à partir du plan de la classe.

What is the difference between == and .equals() in Java?

• Answer:

o == checks reference equality (whether two objects point to the same


memory location).

o .equals() checks content equality (whether two objects have the same
value).
What are constructors in Java, and how are they different from methods?

• Constructors: Special methods used to initialize objects. They have the same name
as the class and do not have a return type.

• Difference: Methods can have any name, can return values, and are used to define
behavior. Constructors are specifically for object creation and cannot return
anything.

What are the data types in Java?

• Primitive Data Types: byte, short, int, long, float, double, char, boolean

• Non-Primitive Data Types: Strings, arrays, classes, interfaces, etc.

The inheritance in java

Single Inheritance, where a class inherits from one superclass. It’s straightforward and
involves one parent and one child class.

Multilevel Inheritance, where a class inherits from a child class, forming a chain. For
example, a class can inherit from a parent, and that parent can also inherit from a
grandparent class.

Hierarchical Inheritance, where multiple classes inherit from a single superclass. It’s like
one parent class having multiple child classes.

Java does not support multiple inheritance with classes, Java achieves multiple
inheritance using interfaces, allowing a class to implement multiple interfaces.

What is the difference between an abstract class and an interface?

• Abstract Class: Can have both abstract (without implementation) and concrete
(with implementation) methods. It can have fields, constructors, and access
modifiers, you cannot create an object from it.

• Interface: Only contains abstract methods, Fields are implicitly public, static, and
final, Cannot have constructors, Supports multiple inheritance

What is method overloading and method overriding?

• Method Overloading: Defining multiple methods with the same name but different
parameters in the same class. It is resolved at compile-time.
• Method Overriding: Redefining a method from the parent class in the child class. It
is resolved at runtime.

the differences between a static variable, a static method, and a static class in Java

Static Variable (or Class Variable):

• A static variable is shared among all instances of the class. This means that any
change to the static variable will be reflected across all instances.

• Example: static int count = 0; – All instances of the class share the count variable.

Static Method:

• A static method belongs to the class rather than any specific instance. It can be
called without creating an object of the class. Static methods are used for
operations that do not depend on instance variables. However, they can only access
static data and cannot call instance methods directly.

• Example: static void display() {} – This method can be called using


ClassName.display().

Static Class (Nested Static Class):

• Only nested classes can be static. A static nested class is associated with the outer
class, but it cannot access the non-static members of the outer class directly. It is
often used to group classes that are only relevant to the outer class.

• Example: static class NestedClass {} – This class can be created without needing an
instance of the outer class.

Overload (surcharge)

when multiple methods in the same class have the same name but different parameters
(different type, number, or order of parameters). It allows the same method to perform
different tasks based on the method signature.

• Happens within the same class.

• The methods have the same name but different parameters (by type, number, or
order).

• Return type can be the same or different.

• It's resolved at compile-time (also known as compile-time polymorphism or


static polymorphism).
Override (redifinition)

when a subclass (child class) provides a specific implementation of a method that is


already defined in its superclass (parent class). The subclass method should have the
same name, same parameters, and same return type as the method in the superclass.

• Happens between a superclass and a subclass (inheritance).


• The method in the subclass has the same name, parameters, and return type as the
method in the superclass.
• It's resolved at runtime (also known as runtime polymorphism or dynamic
polymorphism).
• The method in the subclass overrides the method in the superclass, providing its
own implementation.

The overridden method in the parent class can still be accessed using
super.methodName().
1-LTS (Long Term Support) in java

There’re 2 types

LTS versions are released every three years, with updates and support continuing for
several years after that. Like 8 11 17, ,(the last version => 21 )

Non-LTS versions receive support for a shorter period (6 months) 9 10 12 13 14 15 16


18 19 20 ,(the last version => 23)

2-Pointers in JAVA

It doesn’t use pointers, In Java, you don’t have direct access to memory addresses or
pointers. It is a high-level programming language.

Java uses references to objects, A reference points to an object stored in the heap
memory, but the actual memory address is managed by the Java Virtual Machine (JVM)

it uses the Garbage Collector (GC) is part of the Java Virtual Machine (JVM) that manages
memory

Java Virtual Machine (JVM)

The JVM handles where objects are stored in memory, including the heap where objects
live. And GC is a part of JVM.

The Garbage Collector


Automatically tracks which objects are no longer in use. When an object is no longer
needed, the Garbage Collector frees up the memory allocated to that object.

Access modifiers / Modificateur d'acces

• public: it can be accessed by all classes in all packages.( Il accessible depuis par
tout les classe et tout les packages )

Example: public int number ;

• private: It cannot be accessed from outside the class.( Il n'est pas accessible depuis
l'extérieur de la classe)

Example: private String name;

• protected: The member is accessible within the same class, subclasses, and other
classes in the same package. It allows access from derived classes even in different
packages.( il est accessible par le m class et les class qui hérite de la m class.)

Example: protected void display();

• default: It is called package-private, meaning that members (fields, methods,


constructors) with no explicit access modifier are accessible only within the same
package. Example: int age; (without any access modifier)

Inner class and Subclass (classe interne/sous classe)

• An inner class is a class that is defined inside another class.


outerclass.innerclass inner = new outerclass.innerclass():
• A subclass (also known as a derived class or child class) is a class that inherits
from another class (the superclass or parent class).

Constructor’s types

• Default constructor
• Parameterized Constructor

Exception

Uses exception handling mechanisms to manage errors so we ensure that the program
doesn’t crash unexpectedly.

• all exception classes are part of the java.lang.Exception

The types of exceptions in Java


Answer: "In Java, exceptions are classified into three main types: Checked Exceptions,
Unchecked Exceptions, and Errors.

Checked Exceptions: These are checked at compile-time, and you must handle them with
try-catch or declare them with throws. Examples include IOException and SQLException.

Unchecked Exceptions: These occur at runtime and do not need to be explicitly handled.
Examples are NullPointerException and ArithmeticException.

Errors: These represent serious issues, like OutOfMemoryError, and are generally not
handled by the application.

Oreder of the execution

1. Write the source code (.java file).


2. Compile the source code to bytecode using javac (produces .class file).
3. Class loading by the JVM.
4. Bytecode verification by the JVM for security and correctness.
5. Execution of bytecode by the JVM (using interpretation or JIT compilation).
6. Execution of the main() method, where the actual program logic starts.
7. Memory management (Garbage Collection) is handled during execution.
8. Program termination after all tasks are complete

Error vs exception

• Error: represent the errors occurs by the execution environment of the application
• Exception: represent the errors occurs by the application itself

How does exception handling work in Java using try, catch, throw, throws, and finally?

• Answer:

o try: Code that might throw an exception is placed inside a try block.

o catch: Handles exceptions thrown by the try block.

o throw: Used to explicitly throw an exception.

o throws: Declares that a method may throw an exception.

o finally: Executes code after try-catch, whether an exception was thrown or


not.
Immutable object is an object that cannot be changed after it's created, If you try to
modify it, a new object is created instead.

String / StringBuilder/StringBuffer

String: is an immutable sequence of characters in Java. Once a String object is created.


• Whenever you modify a String a new String object is created.
• Strings are slower for repeated modifications due to immutability and the need
to create new objects each time you change the string.

StringBuilder: is a mutable sequence of characters.

• allows for modifications (like appending, inserting, or deleting characters)


without creating new objects.
• making it more memory-efficient.

StringBuffer: is a mutable sequence of characters.

• allows modifications without creating new objects


• is slower than StringBuilder

Objet immutable

• String is an immutable object.


• Integer, Boolean, LocalDateTime

-------OOP----------------------------------

principles of OOP

Encapsulation: Bundling data and methods that operate on the data into a single unit
(class) and restricting access using access modifiers.
Abstraction: Hiding complex implementation details and exposing only essential features
of an object or method.

Inheritance: A mechanism where one class inherits fields and methods from another
class, promoting code reusability.

Polymorphism: The ability of an object to take many forms, either through method
overloading (compile-time) or method overriding (runtime).

What is the this keyword in Java?

• Answer: The this keyword refers to the current instance of the class. It is used to
differentiate instance variables from parameters with the same name, call
constructors, or pass the current object as a parameter.

What is the super keyword in Java?

• Answer: The super keyword refers to the parent class object. It is used to call the
parent class constructor or access parent class methods and fields when they are
overridden or hidden by the child class.

What is the purpose of the final keyword in Java?

• Answer:

o final variable: When you declare a variable as final, you cannot change its
value after it has been assigned.

o final method A final method cannot be overridden by subclasses. This is


useful when you want to ensure that the method's behavior remains
consistent and is not altered by child classes.

o final class: A class declared as final cannot be subclassed. This means no


other class can extend or inherit from it. It is used to prevent inheritance and
to ensure that the class's functionality is not altered.

Inner Class: A class defined within another class that has access to all members of the
enclosing class, even private ones.

What is the stack and heap memory in Java?

• Answer:

o Stack Memory: Used for the execution of threads. It stores method calls and
local variables. Memory is allocated and deallocated in a last-in, first-out
manner.
o Heap Memory: Used for dynamic allocation of objects. Memory is managed
by the garbage collector.

What is the difference between throw and throws?

• Answer:

o throw: Used to explicitly throw an exception from a method or block of code.

o throws: Declares that a method may throw exceptions, indicating what


exceptions can be thrown to the calling method.

What is the Java Collections Framework?

• Answer: The Java Collections Framework is a set of classes and interfaces that
provides an architecture for storing and manipulating groups of data. It includes
interfaces like List, Set, and Map and classes like ArrayList, HashSet, HashMap, etc.

List Interface:

2. ArrayList

▪ A resizable array implementation of the List interface. It is best for


frequent access and traversal operations but slower for insertions and
deletions.

3. LinkedList

▪ A doubly linked list implementation of the List and Deque interfaces.


It is better for frequent insertions and deletions but slower for access
operations.

4. Vector

▪ A synchronized, resizable array. It is thread-safe but generally slower


than ArrayList.

5. Stack

▪ A subclass of Vector that implements a Last-In-First-Out (LIFO) stack


of elements. It provides methods like push(), pop(), and peek().

Set Interface

6. HashSet
▪ A set backed by a HashMap. It does not maintain any order of
elements and allows null values.

7. LinkedHashSet

▪ A HashSet implementation that maintains the insertion order of


elements. It is slightly slower than HashSet.

8. TreeSet

▪ A set backed by a TreeMap. It maintains elements in a sorted order


(natural or custom comparator).

9. EnumSet

▪ A specialized set for use with enum types. It is highly efficient and
implemented as a bit vector.

What are memory leaks in Java, and how can you avoid them?

• Answer: Memory leaks occur when objects are no longer needed but are still
referenced, preventing garbage collection. They can be avoided by:

o Nullifying references when objects are no longer needed.

o Using weak references when appropriate.

o Being mindful of static references and properly managing collections.

What happens if an exception is not caught?

• Answer: If an exception is not caught, it propagates up the call stack. If it remains


uncaught, it eventually reaches the JVM, which terminates the program and prints a
stack trace.

You might also like