java - Basic Flashcards

1
Q

What is Java and what are its main features?

A

Java is a high-level, general-purpose programming language that was developed by Sun Microsystems (now owned by Oracle Corporation).

Its main features include platform independence, object-oriented programming (OOP) support, automatic memory management (garbage collection), robustness, and a large standard library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Java SE

A

Java SE (Standard Edition) is the core Java platform that provides the basic set of APIs and libraries for general-purpose development.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Java EE

A

Java EE (Enterprise Edition) is a set of APIs and libraries built on top of Java SE, specifically designed for developing enterprise applications, such as web applications and distributed systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Java ME

A

Java ME (Micro Edition) is a subset of Java SE, optimized for resource-constrained devices like mobile phones and embedded systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the JVM (Java Virtual Machine) and its role in Java programming.

A

The JVM is a crucial component of the Java platform.

It is responsible for executing Java bytecode, which is the compiled form of Java source code.

The JVM acts as an abstraction layer between the Java program and the underlying hardware and operating system.

It provides platform independence by translating bytecode into machine-specific instructions at runtime.

The JVM also manages memory, handles garbage collection, and provides various runtime services to Java programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the different types of data types in Java?

A

primitive

reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the primitive data types in Java?

A

byte

short

int

long

float

double

char

boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the reference types in Java?

A

classes

interfaces

arrays

enumerations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a class?

A

a class is a blueprint or template that defines the structure and behavior of objects.

It specifies the properties (fields) and actions (methods) that objects of that class can possess.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an object?

A

An object, on the other hand, is an instance of a class.

It represents a specific entity created from the class and can hold its own unique state and behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain the concept of inheritance

A

Inheritance is a fundamental feature of object-oriented programming in Java.

It allows a class to inherit the properties and methods of another class, known as the superclass or base class.

The class that inherits from the superclass is called a subclass or derived class.

The subclass can reuse the code and extend the functionality of the superclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give an example of inheritance

A

consider a superclass called “Vehicle” and a subclass called “Car.”

The Car class can inherit attributes and methods from the Vehicle class, such as “numberOfWheels” and “startEngine()”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the “static” keyword in Java?

A

The “static” keyword in Java is used to declare members (fields or methods) that belong to the class itself rather than specific instances of the class.

Static members are shared among all objects of the class and can be accessed directly using the class name, without creating an instance of the class.

Static methods are commonly used for utility functions or operations that don’t require access to instance-specific data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you create and use an array in Java?

A

In Java, an array is a fixed-size, ordered collection of elements of the same type.

To create an array, you specify the element type and the size of the array.

For Example:

int[] numbers = new int[5];

or

int[] numbers = {1, 2, 3, 4, 5};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is method overloading?

A

Method overloading refers to the ability to have multiple methods in the same class with the same name but different parameters.

The methods are distinguished by their parameter types, number, or order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is method overriding?

A

Method overriding, on the other hand, occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.

The overridden method in the subclass must have the same name, return type, and parameters as the method in the superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you handle exceptions in Java?

A

The try-catch-finally block is used to handle exceptions.

The code that might throw an exception is placed in the try block, and the corresponding exception handlers are placed in catch blocks.

If an exception occurs in the try block, the catch block that matches the type of the thrown exception is executed.

The finally block, if present, is executed regardless of whether an exception occurred or not and is commonly used for cleanup tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a checked exception?

A

Checked exceptions are exceptions that must be declared in the method signature using the “throws” keyword or caught using a try-catch block.

They represent expected error conditions that the calling code must handle explicitly.

checked exceptions are subclasses of Exception (excluding RuntimeException and it’s subclasses)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is an unchecked exception?

A

Unchecked exceptions, on the other hand, do not require explicit handling or declaration.

They usually represent unexpected runtime errors or programming mistakes.

Unchecked exceptions are subclasses of RuntimeException and its subclasses.

20
Q

What’s an abstract class?

A

An abstract class can have both method declarations (without implementation) and concrete methods, and it can be extended by other classes using the “extends” keyword.

A class can only extend one abstract class.

21
Q

What is an interface?

A

an interface only contains method declarations, and a class implements an interface using the “implements” keyword. A class can implement multiple interfaces

22
Q

How do you create and use a constructor in Java?

A

A constructor in Java is a special method that is used to initialize objects of a class.

It has the same name as the class and does not have a return type.

To create a constructor, you define a method with the same name as the class and use the “new” keyword to create an instance of the class.

Constructors can have parameters to initialize object fields.

23
Q

What is the purpose of the “final” keyword in Java?

A

In Java, the “final” keyword is used to restrict certain entities from being modified.

When applied to a variable, it makes the variable a constant, meaning its value cannot be changed once assigned.

When applied to a method, it prevents the method from being overridden in a subclass.

When applied to a class, it prevents the class from being subclassed.

24
Q

What is the difference between String, StringBuilder, and StringBuffer in Java?

A

String represents an immutable sequence of characters, meaning once created, it cannot be changed.

StringBuilder and StringBuffer are mutable counterparts of String.

StringBuilder is not thread-safe but provides better performance in single-threaded scenarios.

StringBuffer, on the other hand, is thread-safe but has slightly lower performance due to synchronization.

StringBuilder and StringBuffer allow modification of the underlying character sequence.

25
Q

What are the access modifiers in Java?

A

Public

Protected

Private

26
Q

Public

A

Accessible from anywhere.

27
Q

Protected

A

Accessible within the same package and subclasses (even if in different packages).

28
Q

Default (no modifier)

A

Accessible within the same package only.

29
Q

Private

A

Accessible within the same class only.

30
Q

Explain the concept of encapsulation in Java.

A

Encapsulation is one of the fundamental principles of object-oriented programming.

It refers to the bundling of data (fields) and methods (behaviors) within a class and controlling access to them through visibility modifiers (e.g., private, public).

Encapsulation provides data abstraction, hiding the internal implementation details of an object and allowing access to the object’s state only through defined methods.

It helps in achieving data integrity, code maintainability, and flexibility in changing the internal implementation without affecting other parts of the code.

31
Q

What are the different control flow statements in Java?

A

if-else

for loop

while loop

switch

32
Q

equals()

A

The “equals()” method is a method defined in the Object class (the superclass of all classes in Java) and is used to compare the contents or values of two objects for equality.

It is typically overridden in custom classes to provide a meaningful comparison.

33
Q

”==”

A

The “==” operator, on the other hand, is used for reference equality comparison.

It checks if two variables or expressions refer to the same memory location.

For objects, the “==” operator compares the memory addresses, not the actual contents.

34
Q

What are the different types of operators in Java?

A

arithmetic operators

relational operators

logical operators

assignment operators

35
Q

Explain the concept of method chaining in Java.

A

Method chaining, also known as cascading, is a programming technique where multiple methods are called sequentially on the same object instance in a single statement.

Each method call returns the object itself, allowing subsequent methods to be invoked on the returned object.

36
Q

What is the purpose of the “break” statement in Java?

A

the break statement is used to terminate the execution of a loop or a switch statement prematurely.

When encountered, the break statement causes the program to exit the loop or switch and continue with the next statement after the loop or switch block.

37
Q

What is the purpose of the “continue” statement in Java?

A

The continue statement, on the other hand, is used to skip the remaining code in the current iteration of a loop and proceed to the next iteration.

When encountered, the continue statement jumps to the loop’s conditional expression or the loop’s increment/decrement statement, bypassing any code following the continue statement within the current iteration.

38
Q

How do you sort elements in an array in Java?

A

To sort elements in an array in Java, you can use the Arrays class’s sort() method or the Collections class’s sort() method for arrays of objects.

For example:

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1, 3};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}

39
Q

stack

A

The stack is used to store local variables and method call information.

Each thread in a Java program has its own stack, and it operates in a Last-In-First-Out (LIFO) manner.

When a method is called, a new stack frame is created to store the method’s parameters, local variables, and return address.

When the method execution completes, the stack frame is removed.

The stack is generally smaller in size compared to the heap and has a limited memory allocation.

40
Q

heap

A

The heap is used to dynamically allocate memory for objects.

All objects and arrays in Java are allocated on the heap.

The heap memory is shared among all threads and managed by the Java runtime environment’s garbage collector.

Objects in the heap have a longer lifespan and can be accessed from different parts of the program.

The heap size is generally larger and can grow as needed.

41
Q

What is the difference between the stack and the heap?

A

The stack is used for efficient memory allocation and deallocation for method calls, while the heap provides a more flexible and dynamic memory allocation for objects.

42
Q

What are the different types of variables in Java?

A

Local variables

Instance variables

Class variables

43
Q

Local variables

A

Local variables are declared within a method, constructor, or block of code.

They are only accessible within the scope where they are declared. Local variables must be initialized before they are used.

44
Q

Instance variables

A

Instance variables are declared within a class but outside any method, constructor, or block of code.

They are associated with an instance of the class and have different values for each instance.

Instance variables are accessible to all methods and blocks within the class.

45
Q

Class variables

A

Class variables are declared with the static keyword within a class but outside any method, constructor, or block of code.

They are shared among all instances of the class and have the same value across different instances.

Class variables are associated with the class itself and can be accessed using the class name.

46
Q

How do you handle multithreading in Java? Explain the use of threads.

A

To handle multithreading in Java, you can create and manage threads using the Thread class or by implementing the Runnable interface.

For example:

public class MyThread extends Thread {
@Override
public void run() {
// Code to be executed in the thread
}
}

public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Start the thread
}
}