Java Flashcards

1
Q

What is java ?

A

Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language.

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

What is platform ?

A

Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

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

There are 4 platforms or editions of Java:

A
  • Java SE: java programming platform
  • Java EE: It is an enterprise platform that is mainly used to develop web and enterprise applications
  • JAVA ME: micro platform that is dedicated to mobile applications.
  • JAVA FX: used to develop rich internet applications. It uses a lightweight user interface API.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Features of Java

A
Simple
Object-Oriented
Portable
Platform independent
Secured
Robust
Architecture neutral
Interpreted
High Performance
Multithreaded
Distributed
Dynamic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why java is simple ?

A
  • No pointers
  • Automatic garbage collector
  • syntax is simple and undestandable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why java is platform independent ?

A

Java is a write once, run anywhere language

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

why java is secured ?

A

Java is secured because:

No explicit pointer

Java Programs run inside a virtual machine sandbox

Classloader: Classloader in Java is a part of the Java Runtime Environment (JRE) which is used to load Java classes into the Java Virtual Machine dynamically. It adds security by separating the package for the classes of the local file system from those that are imported from network sources.

Bytecode Verifier: It checks the code fragments for illegal code that can violate access rights to objects.

Security Manager: It determines what resources a class can access such as reading and writing to the local disk.

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

why java is secured ?

A

Java is secured because:

No explicit pointer

Java Programs run inside a virtual machine sandbox

Classloader: Classloader in Java is a part of the Java Runtime Environment (JRE) which is used to load Java classes into the Java Virtual Machine dynamically. It adds security by separating the package for the classes of the local file system from those that are imported from network sources.

Bytecode Verifier: It checks the code fragments for illegal code that can violate access rights to objects.

Security Manager: It determines what resources a class can access such as reading and writing to the local disk.

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

other features for java ?

A

Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn’t require any implementation.

High-performance
Java is faster than other traditional interpreted programming languages because Java bytecode is “close” to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc.

Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.

Java supports dynamic compilation and automatic memory management (garbage collection).

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

other features for java ?

A

Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn’t require any implementation.

High-performance
Java is faster than other traditional interpreted programming languages because Java bytecode is “close” to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc.

Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.

Java supports dynamic compilation and automatic memory management (garbage collection).

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

other features for java ?

A

Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn’t require any implementation.

High-performance
Java is faster than other traditional interpreted programming languages because Java bytecode is “close” to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc.

Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.

Java supports dynamic compilation and automatic memory management (garbage collection).

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

What is JVM ?

A

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn’t physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode.

The JVM performs the following main tasks:

Loads code
Verifies code
Executes code
Provides runtime environment

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

What is JRE ?

A

The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

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

What is JDK ?

A

The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools.

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

FInal attribute

A

if variable final , you can’t reassign it
i method, you can t override it
if class , you can t extend it

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

Can we declare a constructor final?

A

No, because constructor is never inherited.

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

What is final parameter?

A

If you declare any parameter as final, you cannot change the value of it.

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

static blank final variable

A

A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

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

What is covariant ?

A

The covariant return type specifies that the return type may vary in the same direction as the subclass.

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

Some benefis of covariant return type ?

A

Covariant return type helps in preventing the run-time ClassCastExceptions on returns.

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

Usage of Java super Keyword

A
super can be used to refer immediate parent class instance variable.
super can be used to invoke immediate parent class method.
super() can be used to invoke immediate parent class constructor.

Note: super() is added in each class constructor automatically by compiler if there is no super() or this().

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

STATIC BINDING

A

f there is any private, final or static method in a class, there is static binding.²

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

Java clonning

A
Why use clone() method ?
The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing time to be performed that is why we use object cloning.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Wrapper classes

A

Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
Synchronization: Java synchronization works with objects in Multithreading.
java.util package: The java.util package provides the utility classes to deal with objects.
Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.

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

What is encapsulation ?

A

Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.

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

CharSequence Interface

A

The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in Java by using these three classes.

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

immutability String

A

The Java String is immutable which means it cannot be changed. Whenever we change any string, a new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes.

25
Q

Java string constant pool

A

Each time you create a string literal, the JVM checks the “string constant pool” first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn’t exist in the pool, a new string instance is created and placed in the pool. For example:

26
Q

Why String objects are immutable in Java?

A
27
Q

Why String class is Final in Java?

A

The reason behind the String class being final is because no one can override the methods of the String class. So that it can provide the same features to the new String objects as well as to the old ones.

28
Q

String comparison

A

There are three ways to compare String in Java:

By Using equals() Method
By Using == Operator
By compareTo() Method

29
Q

String buffer

A

Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer class in Java is the same as String class except it is mutable i.e. it can be changed.

30
Q

String builder

A

Java StringBuilder class is used to create mutable (modifiable) String. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.

StringBuffer is synchronized i.e. thread safe. It means two threads can’t call the methods of StringBuffer simultaneously. StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously.

StringBuilder is more efficient than StringBuffer.

31
Q

immutable class

A

There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members as the example given below:

32
Q

STRING TOKENIZER

A

The java.util.StringTokenizer class allows you to break a String into tokens. It is simple way to break a String. It is a legacy class of Java.

33
Q

Type of java exceptions

A

There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely:

Checked Exception
Unchecked Exception
Error

33
Q

Type of java exceptions

A

There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely:

Checked Exception
Unchecked Exception
Error

34
Q

Java Regex

A
Java Regex API provides 1 interface and 3 classes in java.util.regex package.
Matcher class
It implements the MatchResult interface. It is a regex engine which is used to perform match operations on a character sequence.
35
Q

Java inner class or nested class is a class that is declared inside the class or interface.

A

Nested classes represent a particular type of relationship that is it can access all the members (data members and methods) of the outer class, including private.
Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
Code Optimization: It requires less code to write.

36
Q

types of nested classes

A

There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.

Non-static nested class (inner class)
Member inner class
Anonymous inner class
Local inner class
Static nested class
37
Q

Rules for Java Local Inner class

A
1) Local inner class cannot be invoked from outside the method.
Local inner class cannot access non-final local variable till JDK 1.7. Since JDK 1.8, it is possible to access the non-final local variable in the local inner class.
38
Q

Nest static class

A

A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name.

It can access static data members of the outer class, including private.
The static nested class cannot access non-static (instance) data members or
39
Q

nested interfaces

A

There are given some points that should be remembered by the java programmer.

The nested interface must be public if it is declared inside the interface, but it can have any access modifier if declared within the class.
Nested interfaces are declared static

40
Q

Can we define a class inside the interface?

A

Yes, if we define a class inside the interface, the Java compiler creates a static nested class. Let’s see how can we define a class within the interface:

41
Q

Advantages of Java Multithreading

A

It doesn’t block the user because threads are independent and you can perform multiple operations at the same time.

2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.

42
Q

Multitasking

A

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved in two ways:

Process-based Multitasking (Multiprocessing)
Thread-based Multitasking (Multithreading)

43
Q

1) Process-based Multitasking (Multiprocessing)

A

Each process has an address in memory. In other words, each process allocates a separate memory area.
A process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another requires some time for saving and loading registers, memory maps, updating lists, etc.

44
Q

Thread-based Multitasking (Multithreading)

A

Threads share the same address space.
A thread is lightweight.
Cost of communication between the thread is low.

45
Q

Java Thread class

A

Java provides Thread class to achieve thread programming. Thread class provides constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface.

46
Q

Life cycle of a Thread

A
New
Active
Blocked / Waiting
Timed Waiting
Terminated
47
Q

Explanation of Different Thread States

A

New: Whenever a new thread is created, it is always in the new state. For a thread in the new state, the code has not been run yet and thus has not begun its execution.
Active: When a thread invokes the start() method, it moves from the new state to the active state. The active state contains two states within it: one is runnable, and the other is running.

Blocked or Waiting: Whenever a thread is inactive for a span of time (not permanently) then, either the thread is in the blocked state or is in the waiting state.
Timed Waiting: Sometimes, waiting for leads to starvation. For example, a thread (its name is A) has entered the critical section of a code and is not willing to leave that critical section. In such a scenario, another thread (its name is B) has to wait forever, which leads to starvation. To avoid such scenario, a timed waiting state is given to thread B. Thus, thread lies in the waiting state for a specific span of time, and not forever. A real example of timed waiting is when we invoke the sleep() method on a specific thread. The sleep() method puts the thread in the timed wait state. After the time runs out, the thread wakes up and start its execution from when it has left earlier.

Terminated: A thread reaches the termination state because of the following reasons:

When a thread has finished its job, then it exists or terminates normally.
Abnormal termination: It occurs when some unusual events such as an unhandled exception or segmentation fault.

48
Q

Examplke of thread and state

A

Whenever we spawn a new thread, that thread attains the new state. When the method start() is invoked on a thread, the thread scheduler moves that thread to the runnable state. Whenever the join() method is invoked on any thread instance, the current thread executing that statement has to wait for this thread to finish its execution, i.e., move that thread to the terminated state. Therefore, before the final print statement is printed on the console, the program invokes the method join() on thread t2, making the thread t1 wait while the thread t2 finishes its execution and thus, the thread t2 get to the terminated or dead state. Thread t1 goes to the waiting state because it is waiting for thread t2 to finish it’s execution as it has invoked the method join() on thread t2.

49
Q

create a thread

A

There are two ways to create a thread:

By extending Thread class
By implementing Runnable interface.
50
Q

Starting a thread:

A

The start() method of Thread class is used to start a newly created thread. It performs the following tasks:

A new thread starts(with new callstack).
The thread moves from New state to the Runnable state.
When the thread gets a chance to execute, its target run() method will run.

51
Q

Thread Scheduler in Java

A

A component of Java that decides which thread to run or execute and which thread to wait is called a thread scheduler in Java
n Java, a thread is only chosen by a thread scheduler if it is in the runnable state. However, if there is more than one thread in the runnable state, it is up to the thread scheduler to pick one of the threads and ignore the other ones. There are some criteria that decide which thread will execute first. There are two factors for scheduling a thread i.e. Priority and Time of arrival.

52
Q

priority and time of arrival

A

Priority: Priority of each thread lies between 1 to 10. If a thread has a higher priority, it means that thread has got a better chance of getting picked up by the thread scheduler.

Time of Arrival: Suppose two threads of the same priority enter the runnable state, then priority cannot be the factor to pick a thread from these two threads. In such a case, arrival time of thread is considered by the thread scheduler. A thread that arrived first gets the preference over the other threads.

53
Q

First Come First Serve Scheduling:

A

In this scheduling algorithm, the scheduler picks the threads thar arrive first in the runnable queue.

54
Q

Time-slicing scheduling:

A

Usually, the First Come First Serve algorithm is non-preemptive, which is bad as it may lead to infinite blocking (also known as starvation). To avoid that, some time-slices are provided to the threads so that after some time, the running thread has to give up the CPU. Thus, the other waiting threads also get time to run their job.

55
Q

Preemptive-Priority Scheduling:

A

The name of the scheduling algorithm denotes that the algorithm is related to the priority of the threads.

56
Q

how tread schelder works?

A

Let’s understand the working of the Java thread scheduler. Suppose, there are five threads that have different arrival times and different priorities. Now, it is the responsibility of the thread scheduler to decide which thread will get the CPU first.

The thread scheduler selects the thread that has the highest priority, and the thread begins the execution of the job. If a thread is already in runnable state and another thread (that has higher priority) reaches in the runnable state, then the current thread is pre-empted from the processor, and the arrived thread with higher priority gets the CPU time.

When two threads (Thread 2 and Thread 3) having the same priorities and arrival time, the scheduling will be decided on the basis of FCFS algorithm. Thus, the thread that arrives first gets the opportunity to execute first.

57
Q

sleep thread

A

The Java Thread class provides the two variant of the sleep() method. First one accepts only an arguments, whereas the other variant accepts two arguments. The method sleep() is being used to halt the working of a thread for a given amount of time. The time up to which the thread remains in the sleeping state is known as the sleeping time of the thread. After the sleeping time is over, the thread starts its execution from where it has left.

58
Q

Important things to remember in sleep

A

Whenever the Thread.sleep() methods execute, it always halts the execution of the current thread.

Whenever another thread does interruption while the current thread is already in the sleep mode, then the InterruptedException is thrown.

If the system that is executing the threads is busy, then the actual sleeping time of the thread is generally more as compared to the time passed in arguments. However, if the system executing the sleep() method has less load, then the actual sleeping time of the thread is almost equal to the time passed in the argument.

59
Q

Can we start a thread twice

A

No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

60
Q

What if we call Java run() method directly instead start() method?

A

Each thread starts in a separate call stack.
Invoking the run() method from the main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.

61
Q

the join method

A

The join() method in Java is provided by the java.lang.Thread class that permits one thread to wait until the other thread to finish its execution. Suppose th be the object the class Thread whose thread is doing its execution currently, then the th.join(); statement ensures that th is finished before the program does the execution of the next statement. When there are more than one thread invoking the join() method, then it leads to overloading on the join() method that permits the developer or programmer to mention the waiting period. However, similar to the sleep() method in Java, the join() method is also dependent on the operating system for the timing, so we should not assume that the join() method waits equal to the time we mention in the parameters. The following are the three overloaded join() methods.²

62
Q

Daemon Thread in Java

A

Daemon thread in Java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically.

Points to remember for Daemon Thread in Java
It provides services to user threads for background supporting tasks. It has no role in life than to serve user threads.
Its life depends on user threads.
It is a low priority thread.

If you want to make a user thread as Daemon, it must not be started otherwise it will throw IllegalThreadStateException.

63
Q

Jaba thread pool

A

Java Thread pool represents a group of worker threads that are waiting for the job and reused many times.

Advantage of Java Thread Pool
Better performance It saves time because there is no need to create a new thread.