Java Basics Flashcards

Learn Java Basics

1
Q

What is Java?

A

A common Object Oriented Programming language.

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

What are advantages of Java?

A
  • Write once, run anywhere
  • Large developer community
  • Huge amount of documentation
  • Great exception handling
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does a Java file compile into?

A

bytecode

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

What is the difference between JDK, JRE, JVM?

A
  • Java Development Kit contains compiler and debugger tools to compile the Java source code, also includes Java Runtime Environment and Java Virtual Machine
  • The JRE provides an environment and in that environment the JVM runs the compiled byte code. It contains both the library and the JVM
  • The JVM creates a layer of abstraction from the Java Code and the computer environment. It is used to convert the java bytecode into machine readable code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

State vs Behavior

A
  • States are properties of an object.

- Behavior are actions an object can take

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

What is a class?

A

A class is the blueprint that provides the states and behaviors its instantiated objects will have.

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

What is an object?

A

An object is an instantiation of a class.

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

What are the class members?

A
  • Instance variables
  • Methods
  • Constructors
  • Inner class
  • static/instance blocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the types of variables?

A
  • Instance variable
  • Local variable
  • Static variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the variable scopes?

A
  • Class - every object shares the class variable
  • Instance - every object has their own variable
  • Method - inside method & passed in as parameters
  • Block - inside for or if statements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the access modifiers and what access do they allow?

A
  • Public - globally accessible
  • Protected - accessible within the package and its
    subclasses
  • Default - accessible only within the package
  • Private - accessible only within that class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are non-access modifiers and what do they do?

A
  • Static - variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves
  • Final - define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value.
  • Abstract - purpose of extending
  • Synchronized - used for threads; accessed by only one thread at a time
  • Volatile - used for threads; lets JVM know that a thread accessing a variable must merge its own private copy with the master copy in memory
  • Transient - used in serialization; field that is transient won’t be sent at all and will appear as null on the other end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a constructor?

A

A method within a class that tells an instantiated object what its default values and methods will be. Can have parameters passed into it.

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

What are the types of constructors?

A
  • Default
  • No args
  • Args
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When do you get a default constructor?

A

If you don’t explicitly create your own constructor for the class.

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

What are the 4 pillars of OOP?

A

A PIE - Abstraction, Polymorphism, Inheritance, Encapsulation

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

What is Abstraction?

A

Its main goal is to handle complexity by hiding unnecessary details from the user.

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

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

A
  • Typically, a programmer would use an abstract class to denote a kind of hierarchy between the abstract class and the classes that extend it. An interface is just implemented, so the interface can be used across a variety of classes that may not be related at all.
  • Can only extend one abstract class
  • Can implement as many interfaces as you would like
  • If a programmer wants to have method definitions or contain variables that aren’t static and final, they would use an abstract class. The methods in an abstract class can also be overridden, while in an interface, all the methods must be implemented by the class that implements the interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is polymorphism?

A

Objects of different types can be accessed through the same interface, each type can provide its own, independent implementation of this interface

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

What are the types of polymorphism?

A
  • Compile time(static) using method overloading

- Runtime(dynamic) using method overriding

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

What is inheritance?

A

A new class is derived from an existing class by acquiring the properties and methods of other classes.

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

How do you inherit?

A

extend a class

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

What is encapsulation?

A

The mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.

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

What is a POJO?

A

A plain old java object

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

What is a bean?

A

A bean is like a pojo but it must have a no args constructor, must have private instance variables, must have getters and setters

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

What is the stack?

A

Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order.

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

What is the heap?

A

The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap is created on virtual machine start-up.

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

What is the difference between final, finally, and finalize?

A
  • Final is a non-access modifier that marks your variable as the value that it is initialized at forever, so a constant. Can also be applied to a class to make it so that no one can extend that class or a method to prevent subclasses from overriding that method.
  • Finally refers to the finally clause in a try, catch, finally block. Finally will always execute, even if there is an exception.
  • Finalize - a method that the garbage collector invokes when the JVM figures out that a particular instance should be garbage collected. The main purpose of using finalize is to release resources used by objects before they’re removed from the memory.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What does static mean?

A
  • Static is a non-access modifier that means you can use that method or variable from a class without instantiating that class. Static also means that it belongs to the class itself and not an instance of a class. For static variables there’s 1 shared copy of the variable among all instances of the class and static methods can only use static variables.
30
Q

What is an exception?

A

Exceptions are events in execution which deviate or interrupt from the normal flow of execution.

31
Q

What class does exception extend?

A

Exception extends Throwable, which has Exception and Errors as subclasses and RuntimeException, which is a subclass of Exception.

32
Q

Throw vs Throws

A
  • Throw - method body to invoke an exception; act of throwing an exception explicitly; throw is followed by an instance variable
  • Throws - method signature; used to declare an exception; throws is followed by exception class names
33
Q

Checked vs Unchecked Exceptions

A
  • Checked Exceptions - fall under Exceptions; must be thrown or surrounded in a try/catch block otherwise the JVM won’t compile the code. Outside resource failing.
    Exception, IOException, FileNotFoundException, SQLException
  • Unchecked Exceptions - fall under RuntimeExceptions, which is a subclass of Exception. Unchecked Exceptions can be compiled and will throw an exception at runtime. Unchecked exceptions can be caught or handled with logic.
    RuntimeException, ArrayIndexOutOfBoundsException, ArithmeticException
34
Q

What is the difference between String, String Builder, and String Buffer?

A
  • String - Immutable (cannot be changed or extended); stored in string pool; thread safe
  • StringBuilder - Mutable; for strings changing rapidly; do not extend string because string is final
  • StringBuffer - Mutable; synchronized, much slower; do not extend string because string is final; thread safe, useful for threads/strings being manipulated by multiple threads
35
Q

What is the difference between == and equals()?

A

== operator - Strict comparison by value and identity; used for both primitive and objects comparison
equals() Method - Comparison by value only; method can be overridden ; used for objects comparison

36
Q

What is fileI/O?

A
  • File Input Output
  • Java uses the concept of a stream to speed I/O. streams can be used to read/write from and to the console, files, data, and more.
37
Q

What is serialization?

A

Serialization is the process where java objects are converted to byte streams

38
Q

What is a wrapper class?

A

A Wrapper Class is meant to encapsulate primitive data types to turn them into objects using an operation of the JVM called autoboxing. You can also unbox an object to a primitive.

39
Q

What is a collection?

A

A set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework, it works in a manner of a library. Iterable.

40
Q

What is a set?

A

An interface which extends collections. A set is an unordered collection of objects which can only contain unique values.

41
Q

What are the types of sets?

A
  • HashSet
  • LinkedSet
  • TreeSet
42
Q

What is a hashset?

A

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.

43
Q

What is a sortedset?

A

The order of the sorting is either the natural sorting order of the elements (if they implement java.lang.Comparable ), or the order determined by a Comparator that you can give to the SortedSet .

44
Q

What is a treeset?

A

Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements NavigableSet interface. The objects of TreeSet class are stored in ascending order.

45
Q

What is a list?

A

List is an ordered collection of objects in which duplicate values can be stored.

46
Q

What are the types of lists?

A
  • ArrayList
  • LinkedList
  • Vector
  • Stack
47
Q

What is an arraylist?

A

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed

48
Q

What is a linkedlist?

A

Linked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node.

49
Q

What is a vector?

A

VECTOR is a resizable data structure. It allows duplicate elements to be inserted and preserves insertion order. VECTOR is synchronized, so it is thread safe. Vector implements RandomAccess interface, so its retrieval methods are very fast.

50
Q

What is a stack?

A

STACK is a data structure which extends Vector, and it stores objects in a last-in-first-out matter. The last object to be added will be the first one to be removed.

51
Q

What is a queue?

A

It represents an ordered list of objects just like a List , but its intended use is slightly different. A queue is designed to have elements inserted at the end of the queue, and elements removed from the beginning of the queue.

52
Q

What is a thread?

A

A thread, in the context of Java, is the path followed when executing a program.

53
Q

What is a singleton?

A

A single instance of a class no matter how many references are created.

54
Q

What is reflection?

A

Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.

55
Q

Collection vs Collections

A
  • Collection is the interface that implements Iterable. It marks a class as a Collection and therefore also Iterable.
  • Collections is the java util package that provides static methods available to Collection classes.
56
Q

What datatypes are supported in Java?

A

Boolean, char, byte, short, int, long, double, and float.

57
Q

What is casting?

A

Casting is the process of taking an object of a particular type and turning it into another type, which gives you access to that type’s methods. Casting is used when going from a more general to specific type.

58
Q

What is an interface?

A

An interface is a class that other classes can implement. It is a contract which includes method signatures (but no method definitions) and sometimes static final variables which must all be implemented by classes that use that interface.

59
Q

Comparable vs Comparator

A
  • Comparable is an interface that compares an object based on natural ordering (numbers and letters). It uses a convention of positive number for greater than the compared element, negative number for less than the compared element, and 0 for equal.
  • Comparator is an interface that implements Comparable and implements its compareTo method for custom ordering. Comparator allows you to implement your own rules.
60
Q

How do you start a thread with runnable?

A
  • Invoke the run method to start a thread implementing Runnable.
  • The Runnable object needs to be passed through the thread constructor and have invoke start.
61
Q

What are the methods in thread and what do they do?

A
  • Run - Executes starts the thread of execution.
  • Start - Creates a new thread and calls run().
  • Sleep - static method of a thread, doesn’t release resource locks.
  • Wait - method of an object, releases resource locks.
  • Notify - wakes up only a single thread from a wait state to use a resource.
  • NotifyAll - wakes up all threads waiting for a resource.
  • Join - causes the current thread to wait for another thread to finish.
62
Q

What is the difference between the run and start methods?

A
  • Run is a method of an object calling runnable
  • Start is a method of an object inheriting from the Thread class
  • When you call run it executes the run method.
  • When you call start it instantiates a new thread and calls run.
63
Q

What is a deadlock?

A

When two threads are waiting for resources the other has.

64
Q

What is shadowing?

A
  • Shadowing is when two variables or objects with the same name and type have overlapping scopes.
  • When this happens the field with the larger scope is hidden.
65
Q

Where are variable references stored?

A
  • Depends what type of variables they are, and their scope, local variables or variables with block scope are stored in the stack.
  • Objects and their instance variables are stored in the heap.
66
Q

What is a short circuit operator?

A
  • In java there are two (&& and ||)
  • && will evaluate to false if the first half of the expression evaluates to false
  • || will evaluate to true if the first half of the expression evaluates to true
67
Q

What are some collection methods?

A
  • add
  • contain
  • isEmpty
  • size
68
Q

What are some iterator methods?

A
  • has
  • hasNext
  • remove
69
Q

What is a map?

A

An object that maps keys to values. One key to one value with no duplicates.

70
Q

What are some map methods?

A
  • containsKey
  • containsValue
  • size
  • get
71
Q

What is a hashmap?

A
  • HashMap is a data structure which stores data in the form of key-value pair.
  • HashMap doesn’t allow duplicate keys but allows duplicate values. That means A single key can’t contain more than 1 value but more than 1 key can contain a single value.
  • HashMap allows null key also but only once and multiple null values.
  • This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. It is roughly similar to HashTable but is unsynchronized.