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:
    1. platform independence,
    2. object-oriented programming (OOP) support,
    3. automatic memory management (garbage collection),
    4. robustness,
    5. and a large standard library.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

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

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
  • 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:
    1. manages memory,
    2. handles garbage collection,
    3. 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 primitive data types in Java

A
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the reference data types in Java?

A
  1. classes
  2. interfaces
  3. arrays
  4. enumerations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

class

A

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

It specifies the:
1. properties (fields)
2. 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
9
Q

object

A
  • An object, on the other hand, is an instance of a 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
10
Q

inheritance

A
  • allows a class to inherit the 1. properties and 2. 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:
    1. reuse the code
    2. and extend the functionality of the superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A
  • used to declare members (fields or methods) that belong to the class itself rather than specific instances of the class.
  • Static members are:
    1. shared among all objects of the class
    2. 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
12
Q

How do you create and use an array in Java?

A
  • 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
13
Q

Method Overloading

A
  • 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
14
Q

Method Overriding

A
  • 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:
    1. name,
    2. return type,
    3. and parameters as the method in the superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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
16
Q

Checked Exceptions

A
  • exceptions that must be declared
    1. in the method signature using the “throws” keyword
    2. 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 its subclasses).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Unchecked Exceptions

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

abstract classes

A
  • An abstract class can have both:
    1. method declarations (without implementation)
    2. and concrete methods,
  • and it can be extended by other classes using the “extends” keyword
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

interface

A
  • an interface only contains method declarations,
  • and a class implements an interface using the “implements” keyword.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How many interfaces can a class implement?

A

multiple

21
Q

How many abstract classes can a class extend?

A

one

22
Q

What is a constructor?

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
  • it does not have a return type.
  • and use the “new” keyword to create an instance of the class.

For Example:

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }
}

Person person = new Person("John");
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.
  • it makes a variable a constant.
  • it prevents the method from being overridden in a subclass.
  • It prevents the class from being subclassed.
24
Q

What happens when you use the new keyword to create a string object (i.e. String str = new String(“Hello”))?

A
  • a new string object is created on the heap, (i.e. it doesn’t reference the same pointer in memory when you assign the same string value to a new variable).
  • and memory is allocated to store the characters “Hello”.
25
Q

What happens when you create a string using string literals (i.e. String str = “Hello”)?

A
  • In this case, the Java compiler checks if a string with the same value already exists in the string pool, which is a special area in the heap.
  • If a matching string exists, the reference to that string is assigned to the variable str.
  • If not, a new string object is created in the string pool and assigned to str.
26
Q

StringBuilder

A
  • not thread-safe
  • but provides better performance in single-threaded scenarios.
27
Q

StringBuffer

A
  • is thread-safe
  • but has slightly lower performance due to synchronization.
28
Q

What are the Java access modifiers

A
  1. Public
  2. Protected
  3. Default
  4. Private
29
Q

Public

A

Accessible from anywhere.

30
Q

Protected

A
  • Accessible within the same package
  • and subclasses (even if in different packages).
31
Q

Default (no modifier)

A

Accessible within the same package only.

32
Q

Private

A

Accessible within the same class only.

33
Q

Explain the concept of encapsulation in Java.

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

equals()

A
  • a method defined in the Object class (the superclass of all classes in Java)
  • 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.
35
Q

”==”

A
  • 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.
36
Q

What are the different types of Java operators?

A
  1. Arithmetic operators (+,-,*.etc…)
  2. Relational operators (==, >, <, etc…)
  3. Logical operators (&&, !!, !)
  4. Assignment operators (=)
37
Q

Explain the concept of method chaining in Java.

A
  • also known as cascading,
  • 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.

-results in more concise and readable code.

For Example:

StringBuilder builder = new StringBuilder()
    .append("Hello")
    .append(" ")
    .append("World")
    .append("!")
    .reverse();
38
Q

break

A

terminates the execution of a loop

39
Q

continue

A

skip remaining code in the current iteration of a loop and proceed to the next iteration.

40
Q

How do you sort elements in an array in Java?

A

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));
    }
}

NOTE: In this example, we have an array of integers called numbers. We use the Arrays.sort() method to sort the elements in ascending order. The sorted array is then printed using Arrays.toString().

41
Q

stack

A
  • The stack is used to store:
    1. local variables
    2. 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:
    1. parameters,
    2. local variables,
    3. 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.
42
Q

heap

A
  • he 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.
43
Q

Whats 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.
44
Q

What are different types of variables in Java?

A
  1. Local
  2. Instance
  3. Class
45
Q

Local Variables

A
  • declared within a:
    1. method,
    2. constructor,
    3. or block of code.
  • They are only accessible within the scope where they are declared.
  • They must be initialized before they are used.
46
Q

Instance Variables

A
  • 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.
47
Q

Class Variables

A
  • 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
  • They have the same value across different instances.
  • associated with the class itself and can be accessed using the class name.
48
Q

How do you handle multithreading in Java?

A
  • you can create and manage threads:
    1. using the Thread class
    2. 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
    }
}

NOTE: In this example, the MyThread class extends the Thread class and overrides the run() method with the code to be executed in the thread. In the Main class, we create an instance of MyThread and call the start() method to start the thread’s execution.

Alternatively, you can implement the Runnable interface and pass it to a Thread object for execution.

49
Q

What does encapsulation help with?

A
  1. achieving data integrity,
  2. code maintainability,
  3. and flexibility in changing the internal implementation without affecting other parts of the code.