Java Flashcards
(21 cards)
How to create a char array from a String?
String str = “123445”;
char[] arr = str.toCharArray();
How to get an String from a value of a different type?
String str = String.valueOf(1234);
or
Integer.toString(int i, int radix); Integer.toString(int i);
What are the bitwise operations in Java?
& (bitwise and) | (bitwise or) ^ (bitwise XOR) ~ (bitwise compliment) >>> (Fill with zero left) >> (fill with highest bit on the left - sign)
How to isolate the lowerst bit that is 1 in X?
x &~(x-1)
x= 00101100 ;
y = x &~(x-1);
y = 0000100;
What are the primitive data types in java?
byte, short, int, long, float, double, boolean, char
What are the sizes of the primitive java types?
byte = 8 bit signed short = 16 bit signed int = 32 bit signed (Unsigned too in Java 8) long = 64 signed (Unsigned too in Java 8) float = 32 bit IEEE 754 precisions double = 64 bit IEEE 754 precisions boolean = Not defined char = 16 bit unicode
How can integer literals be represented in java?
// The number 26, in decimal int decVal = 26; // The number 26, in hexadecimal int hexVal = 0x1a; // The number 26, in binary int binVal = 0b11010;
How can Floating-Point Literals be represented in java?
double d1 = 123.4; // same value as d1, but in scientific notation double d2 = 1.234e2; float f1 = 123.4f;
How to separate long numbers in java 7?
You can use underscore:
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
How to get the absolute number in java?
Math.abs(value)
It’s static and works for long, int, float and double
How to get a logarithm base 10?
Math.log10(double value)
How to get the maximum between 2 values?
Math.max(value)
It’s static and works for double, long, float and int
How to get the minimum between 2 values?
Math.min(value)
It’s static and works for double, long, float and int
How to raise a to a power in java?
Math.pow(double a, double b)
How to get the square root of a number in java?
Math.sqrt(double a)
Which collection classes are synchronized or thread-safe ?
Stack, Properties , Vector and Hashtable
What is the difference between List and Set ?
Set contain only unique elements while List can contain duplicate elements.
Set is unordered while List is ordered . List maintains the order in which the objects are added .
What is the difference between Map and Set ?
Map object has unique keys each containing some value, while Set contain only unique values.
What do you understand by iterator fail-fast property?
terator fail-fast property checks for any modification in the structure of the underlying collection everytime we try to get the next element. If there are any modifications found, it throws ConcurrentModificationException. All the implementations of Iterator in Collection classes are fail-fast by design except the concurrent collection classes like ConcurrentHashMap and CopyOnWriteArrayL
What is difference between fail-fast and fail-safe?
Iterator fail-safe property work with the clone of underlying collection, hence it’s not affected by any modification in the collection. By design, all the collection classes in java.util package are fail-fast whereas collection classes in java.util.concurrent are fail-safe.
Fail-fast iterators throw ConcurrentModificationException whereas fail-safe iterator never throws ConcurrentModificationException.
What is BlockingQueue
java.util.concurrent.BlockingQueue is a Queue that supports operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.
BlockingQueue interface is part of java collections framework and it’s primarily used for implementing producer consumer problem. We don’t need to worry about waiting for the space to be available for producer or object to be available for consumer in BlockingQueue as it’s handled by implementation classes of BlockingQueue.
Java provides several BlockingQueue implementations such as ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue etc.