Java Flashcards

1
Q

How to create a char array from a String?

A

String str = “123445”;

char[] arr = str.toCharArray();

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

How to get an String from a value of a different type?

A

String str = String.valueOf(1234);
or
Integer.toString(int i, int radix); Integer.toString(int i);

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

What are the bitwise operations in Java?

A
& (bitwise and)
| (bitwise or)
^ (bitwise XOR)
~ (bitwise compliment)
>>> (Fill with zero left)
>> (fill with highest bit on the left - sign)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to isolate the lowerst bit that is 1 in X?

A

x &~(x-1)
x= 00101100 ;
y = x &~(x-1);
y = 0000100;

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

What are the primitive data types in java?

A

byte, short, int, long, float, double, boolean, char

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

What are the sizes of the primitive java types?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can integer literals be represented in java?

A
// 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can Floating-Point Literals be represented in java?

A
double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1  = 123.4f;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to separate long numbers in java 7?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to get the absolute number in java?

A

Math.abs(value)

It’s static and works for long, int, float and double

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

How to get a logarithm base 10?

A

Math.log10(double value)

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

How to get the maximum between 2 values?

A

Math.max(value)

It’s static and works for double, long, float and int

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

How to get the minimum between 2 values?

A

Math.min(value)

It’s static and works for double, long, float and int

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

How to raise a to a power in java?

A

Math.pow(double a, double b)

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

How to get the square root of a number in java?

A

Math.sqrt(double a)

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

Which collection classes are synchronized or thread-safe ?

A

Stack, Properties , Vector and Hashtable

17
Q

What is the difference between List and Set ?

A

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 .

18
Q

What is the difference between Map and Set ?

A

Map object has unique keys each containing some value, while Set contain only unique values.

19
Q

What do you understand by iterator fail-fast property?

A

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

20
Q

What is difference between fail-fast and fail-safe?

A

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.

21
Q

What is BlockingQueue

A

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.