Java Flashcards

1
Q

Final

A
final class can't be inherited
final method can't be overridden
final variable value can't be changed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Finally

A

code will be executed whether exception is handled or not.

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

Finalize

A

used to perform clean up processing just before object is garbage collected

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

static

A

modifier associated with the class rather than with any object.

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

Generics

A

“a type or method to operate on objects of various types while providing compile-time type safety”

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively.

Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.

type erasure:

  • all the extra information added using generics into source code will be removed from byte code generated from it
  • inside byte code will be the old java syntax without generics. This helps in generating and executing code written prior to Java 5 (before generics)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

StringBuilder vs. StringBuffer

A

StringBuffer is synchronized
StringBuilder is not synchronized (faster)

Both are mutable (String is immutable)

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

ArrayList vs. Vector

A

ArrayList is synchronized
Vector is not synchronized

ArrayList grows by 50%
Vector doubles in size

ArrayList can only use Iterator
Vector can use Iterator and Enumerator

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

Checked exceptions vs. Unchecked exceptions

A

Checked exceptions:

  • checked at compile time
  • can anticipate and try to recover from
  • should try to catch
  • if a method throws a checked exception, then the method must either handle the exception, or it must specify the exception using throws keyword.
  • parent class is Exception
  • IOException
  • SQLException
  • DataAccessException
  • ClassNotFoundException
  • InvocationTargetException
  • MalformedURLException

Unchecked exceptions:

  • handling is NOT verified during compile time.
  • occur because of bad programming.
  • parent class is RuntimeException.
  • NullPointerException
  • ArrayIndexOutOfBound
  • IllegalArgumentException
  • IllegalStateException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Error

A

a subclass of Throwable

  • indicates serious problems that a reasonable application should not try to recover from.
  • most are abnormal conditions.
  • some of the common Errors are OutOfMemoryError and StackOverflowError.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

static binding

A

a binding that occurs during compilation.

also called early binding because it happens before a program runs.

actual object is not used for binding.

Example: method overloading.

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

volatile

A

variables are stored in main memory (not CPU cache)

changes to them are visible across threads

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

synchronized

A

keyword used in method declarations and code blocks

instance methods are synchronized over the instance of the class owning the method. Only one thread per instance of the class can execute this method.

static methods are synchronized on the Class object associated with the class. Only one Class object exists per JVM per class, so only one thread can execute inside a static synchronized method per class, irrespective of the number of instances it has.

all synchronized blocks synchronized on the same object can only have one thread executing inside them at the same time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.

synchronized block within method:
public void performSynchronizedTask() {
    synchronized (this) {
        setCount(getCount()+1);
    }
}

synchronized block within static method:
public static void performStaticSyncTask(){
synchronized (SynchronizedBlocks.class) {
setStaticCount(getStaticCount() + 1);
}
}

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

atomic

A

operation can be safely performed in parallel on multiple threads without using the synchronized keyword or locks

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

thread contention

A

a condition where one thread is waiting for a lock/object that is currently being held by another thread. Therefore, this waiting thread cannot use that object until the other thread has unlocked that particular object.

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

race condition

A

occurs when two or more threads can access shared data, and they try to change it at the same time.
Because the thread scheduling algorithm can swap between threads at any time, you don’t know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are “racing” to access/change the data.

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

sleep vs. wait

A

sleep:
- sleep() doesn’t releases any lock or monitor while waiting
- used to hold the process for few seconds or the time you wanted
- used to introduce pause on execution, generally

wait:
- wait() releases the lock or monitor
- thread goes in waiting state
- won’t come back automatically until we call the notify() or notifyAll()
- used for inter-thread communication

17
Q

process vs. thread

A

process:
- has a self-contained execution environment
- generally has a complete, private set of basic run-time resources
- each process has its own memory space.

thread:
- sometimes called lightweight processes
- both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.
- threads exist within a process — every process has at least one.
- threads share the process’s resources, including memory and open files.

18
Q

abstract class vs. interface

A

A class must be declared abstract when it has one or more abstract methods.

An interface differs from an abstract class because an interface is not a class. An interface is essentially a type that can be satisfied by any class that implements the interface.

19
Q

Memoization

A

an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

20
Q

inheritance vs. composition

A

inheritance:

  • “is-a” relationship
  • tightly coupled
  • no access control
  • use when superclass will not be changed
public class Animal {
    // variables, methods etc.
}
public class Cat extends Animal{
}

composition:

  • “has-a” relationship
  • uses instance variables of other objects
  • loosely coupled
  • access can be restricted
  • unit testing is easy - we know all the methods used from the other class
  • generally should be used over inheritance
public class Job {
    // variables, methods etc.
}
public class Person {
    //composition has-a relationship
    private Job job;
    //variables, methods, constructors
}
21
Q

access modifiers

A

Default:

  • no access modifier is specified
  • accessible to the classes in the same package only

Public:
-can be accessed from anywhere

Private:
-accessible only inside the same class

Protected:
-accessible only to the classes in the same package and to the subclasses (can be different packages

22
Q

polymorphism

A
  • perform a single action in different ways
  • the capability of a method to do different things based on the object that it is acting upon
  • allows you define one interface and have multiple implementations
public class Animal {
   public void sound(){
      System.out.println("Animal is making a sound");   
   }
}
public class Horse extends Animal{
     @Override
     public void sound(){
          System.out.println("Neigh");
     }
}
public class Cat extends Animal{
    @Override
    public void sound(){
        System.out.println("Meow");
    }
}
public static void main(String args[]){
    Animal obj = new Horse();
    obj.sound();
}

which sound() method will be called is determined at runtime

23
Q

method overloading

A
  • two or more methods in one class have the same method name but different parameters
  • compile-time (reference type determines which overloaded method will be used)
24
Q

method overriding

A
  • two methods have the same method name and parameters (i.e., method signature).
  • one of the methods is in the parent class and the other is in the child class.
  • allows a child class to provide a specific implementation of a method that is already provided its parent class.
  • run-time (real object type determines which overridden method will be used at runtime)
  • polymorphism applies to overriding, not overloading
25
Q

powers of 2

A

Power of Two Binary Hexadecimal Decimal Value
2⁰ 0001 1 1
2¹ 0010 2 2
2² 0100 4 4
2³ 1000 8 8
2⁴ 0001 0000 10 16
2⁵ 0010 0000 20 32
2⁶ 0100 0000 40 64
2⁷ 1000 0000 80 128
2⁸ 0001 0000 0000 100 256
2⁹ 0010 0000 0000 200 512
2¹⁰ 0100 0000 0000 400 1,024