Java SE 8 OCA exam
ArrayList
extends AbstractList<E> implements List<E>. Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. This class is roughly equivalent to Vector, except that it is unsynchronized.
HashMap
extends AbstractMap<K,V> , implements Map<K,V>. HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
Tree Set
extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used. This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
List and explain the 8 control statements
if - Determines whether a code should be executed based on the specified condition. •if..else - In this statement, if the condition specified is true, the if block is executed. Otherwise, the else block is executed. •while loop- Known as the most common loop, it evaluates a certain condition. If the condition is true, the code is executed. This process is continued until the specified condition turns out to be false. The condition to be specified in the while loop must be a Boolean expression. • do..while loop - similar to the while loop, the only difference being that the condition in the do-while loop is evaluated after the execution of the loop body. This guarantees that the loop is executed at least once. •for - The for loop in java is used to iterate and evaluate a code multiple times. When the number of iterations is known by the user, it is recommended to use the for loop. •for..each - The traversal of elements in an array can be done by the for-each loop. The elements present in the array are returned one by one. It must be noted that the user does not have to increment the value in the for-each loop. •switch - A switch statement in java is used to execute a single statement from multiple conditions. The switch statement can be used with short, byte, int, long, enum types, etc
HashTable
implements Map<K,V>. This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.
Thread methods
static Thread.StatevalueOf(String name): Returns the enum constant of this type with the specified name. static Thread.State[]values(): Returns an array containing the constants of this enum type, in the order they are declared.
Thread States
- new: not yet started - runnable: executing - blocked: waiting for a lock - waiting: waits for another thread - timed_waiting: waits for specified time - terminated: has exited
What is Gradle?
A Build Automation Tool
Comparable vs Comparator
A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface to compare its instances. Unlike Comparable, Comparator is external to the element type we are comparing. It's a separate class. We create multiple separate classes (that implement Comparator) to compare by different members.
Arrays, varargs
A method may use a vararg parameter (variable argument) as if it is an array. It differs from an array because a vararg parameter must be the last element in a method's parameter list. This implies you are only allowed to have one vararg parameter per method.
What are Gradle Build Scripts?
A script file for handling two things, one is projects and another one is tasks. Every Gradle build represents one or more projects. A project represents a library JAR or a web application or it might represent a ZIP that assembled from the JARs produced by other projects.
Lambdas
A short block of code which takes in parameters and returns a value. Lambda similar to methods, but they do not need a name and they can be implemented right in the body of a method.
What is POM?
An XML representation of a Maven project held in a file named pom.xml. It contains information about the project and various configuration details used by Maven to build the project(s). It always resides in the base directory of the project as pom.xml.
What is a dependency?
An artifact that must be included in the build for the application to function properly
Producer-Consumer Problem
An example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue. The producer's job is to generate data, put it into the buffer, and start again. At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one piece at a time.
Garbage collection, finalize
An object is eligible for garbage collection when one of two situations occurs: 1.)The object no longer has any references pointing to it. 2.) All references to the object have gone out of scope. Java allows objects to implement the finalize() method, which is called right before an object is garbage collected.
Gradle build scripts
Build scripts describe your build by configuring projects. A project is an abstract concept, but you typically map a Gradle project to a software component that needs to be built, like a library or an application. Each build script you have is associated with an object of type Project and as the build script executes, it configures this Project
checked vs unchecked exceptions
Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword (fileNotFound, IOException) Unchecked: are the exceptions that are not checked at compiled time (outOfMemory, nullPointer, indexOutOfBounds)
List and explain the 4 reference types
Class, Interface, Array, Enum
How does a Collection differ from a Map?
Collection inherits from iterable, whereas Map does not. Collections have a type whereas Maps have key-value pairs. A Collection has an iterator whereas a Map does not.
What are Generics?
Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods.
Reflection
Commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. Allows programmers to create objects that are classes.
Deadlock/starvation
Deadlock: a situation when two threads are waiting for each other and the waiting never ends. Here both threads cant completes their tasks. Starvation: describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads.
True or False: An array is a type of Collection in Java
False
True or False: Protected variables cannot have their values changed once set.
False
True or false: A Hashtable extends Set to store key/value pairs.
False
True or false: A List extends Collection to handle sets, which must contain unique elements.
False
True or false: RuntimeException and its subclasses are checked exceptions.
False
True or false: An enhanced for loop can be used to traverse a String.
False: Enhanced for loops can only be used with arrays or iterables.
True or False: An unchecked exception must be dealt with a try/catch block.
False: Remember, handling an unchecked Throwable is optional.
Functional vs marker interfaces
Functional: An interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. It can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional interfaces. Marker: It is an empty interface (no field or methods). Examples of marker interface are Serializable, Cloneable and Remote interface. All these interfaces are empty interfaces.
Generics
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. provide compile-time type safety that allows programmers to catch invalid types at compile time.
Iterable vs. Iterator
Iterable: Implementing this interface allows an object to be the target of the "for-each loop" statement Iterator: Interface is member of the Java Collections framework. public interface Iterator<E> An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework
JRE vs JDK
Java Runtime Environment: contains the Java class libraries, the Java class loader, and the Java Virtual Machine. Java Development Kit: allows developers to create Java programs that can be executed and run by the JVM and JRE. a package of tools for developing Java-based software.
Serialization
Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object
List vs. Set vs. Queue vs. Deque
List: extends Collection<E> An ordered collection (also known as a sequence). Set: extends Collection<E> A collection that contains no duplicate elements. Queue: extends Collection<E> A collection designed for holding elements prior to processing Deque: extends Queue<E> A linear collection that supports element insertion and removal at both ends
What is Maven?
Maven is a project management and comprehension tool. Maven provides developers a complete build lifecycle framework. Development team can automate the project's build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.
Multithreading
Multithreaded execution is an essential feature of the Java platform. Each application starts with just one main thread, which has the ability to create multiple threads.
What is the default value of String variable?
Null
Platform Independence: Write Once, Run Anywhere
One of the main benefits of Java. This means that any code written with java only needs to be compiled once, rather than needing to be recompiled for different operating systems
Wrapper classes
Provide a way to use primitive types as objects
Can a java interface have methods that are not abstract?
Recently, this became possible in java. Interfaces can have default and static methods, which both have bodies. Static methods cannot be overridden in implementation classes.
IndexOutOfBoundsException is what type of Exception?
RunTimeException
Runnable Interface vs. Thread Class
Runnable interface: should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. Thread Class: implements Runnable.
Stack vs Heap
Stack: the part of the computer's memory where the temporary variables live, and memory allocation in the Stack happens at compile time. Heap: somewhat like a global memory pool. Objects and classes are stored in memory in the Heap. Memory allocation in the Heap happens at runtime.
List and explain the 4 variable scopes
Static (Class), Instance (Object), Method (Parameter), Local (Block)
StringBuilder vs StringBuffer
StringBuffer is similar to StringBuilder except one difference that StringBuffer is thread safe, i.e., multiple threads can use it without any issue. The thread safety brings a penalty of performance.
Collection vs. Collections
The important methods of Collection interface are add(), remove(), size(), clear() etc. and Collections class contains only static methods like sort(), min(), max(), fill(), copy(), reverse() etc.
public static void main(String[] args)
The main() method is a special method in Java Programming that serves as the externally exposed entrance point by which a Java program can be run.
Default methods
This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8
Synchronization
Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.
Dynamic Binding
When the compiler is not able to resolve a call at compile time, so it must be done at runtime. Method overriding is a common example
List and explain the 4 levels of testing
Unit Testing : checks if software components are fulfilling functionalities or not. Integration Testing : checks the data flow from one module to other modules. System Testing : evaluates both functional and non-functional needs for the testing. Acceptance Testing : checks the requirements of a specification or contract are met as per its delivery.
Verbose, Clear Syntax
Used to tell the JVM which kind of information to see. The three verbose options supported by JVM are verbose: class, verbose: garbage collection, and verbose: Java Native Interface.
String pool
Where string literals are stored
How do you force garbage collection in java?
YOU CANNOT. You can only suggest garbage collection with the method System.gc()
Anonymous class
a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator
Try with resources
a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement
Abstraction (Interfaces vs. abstract classes)
abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. Interfaces have all abstract methods, and abstract classes must have at least one abstract method.
List and explain the 8 primitive types
• byte - 1 byte, 8-bit signed two's complement integer. Stores whole numbers valuesmin imum of -128 and a maximum value of 127 (inclusive). •short - 2 bytes, 6-bit signed two's complement integer. Stores whole numbers values minimum of -32,768 and a maximum value of 32,767 (inclusive). •int - 4 bytes, 32-bit signed two's complement integer, Stores whole numbers values minimum of -2,147,483,648 (-2 ^31) and a maximum value of 2,147,483,647 (2^31 -1). •long - 8 bytes, 64-bit signed two's complement integer. Stores whole numbers values minimum of -9,223,372,036,854,775,808 (-2 ^63) and a maximum value of 9,223,372,036,854,775,807 (2 ^63) •float - 4 bytes, single-precision 32-bit IEEE 754 floating point. Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits •double - 8 bytes, a double-precision 64-bit IEEE 754 floating point. Stores fractional numbers. Sufficient for storing 15 decimal digits •boolean - 1 bit, has only two possible values: true and false • char - 2 bytes, single 16-bit Unicode character. Stores a single character/ letter or ASCII values. It has a minimum value of '\u0000' (or 0) and a maxi - mum value of '\uffff' (or 65,535 inclusive)