Skip to content

Commit b1d26ae

Browse files
Merge pull request amitshekhariitbhu#60 from karntrehan/master
Removing duplicates, Fixing ordering Fixes amitshekhariitbhu#59
2 parents f824ac4 + bb966a9 commit b1d26ae

File tree

1 file changed

+31
-217
lines changed

1 file changed

+31
-217
lines changed

README.md

Lines changed: 31 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -264,202 +264,11 @@
264264
- An interface is like a blueprint/contract of a class (or it may be thought of as a class with methods, but without their implementation). It contains empty methods that
265265
represent, what all of its subclasses should have in common. The subclasses provide the
266266
implementation for each of these methods. Interfaces are implemented.
267-
* What is serialization? How do you implement it?
268-
- Serialization is the process of converting an object into a stream of bytes in order to store
269-
an object into memory, so that it can be recreated at a later time, while still keeping the
270-
object's original state and data. In Android you may use either the Serializable, Externalizable (implements Serializable) or Parcelable interfaces.
271-
- While Serializable is the easiest to implement, Externalizable may be used if you need to insert custom logic into the process of serialization (although it is almost never used nowadays as it is considered a relic from early versions of Java). But it is highly recommended to use Parcelable in Android instead, as Parcelable was created exclusively for Android and it performs about 10x faster than Serializable, because Serializable uses reflection, which is a slow process and tends to create a lot of temporary objects and it may cause garbage collection to occur more often.
272-
- To use Serializable all you have to do is implement the interface:
273-
274-
```java
275-
/**
276-
* Implementing the Serializeable interface is all that is required
277-
*/
278-
public class User implements Serializable {
279-
280-
private String name;
281-
private String email;
282-
283-
public User() {
284-
}
285-
286-
public String getName() {
287-
return name;
288-
}
289-
290-
public void setName(final String name) {
291-
this.name = name;
292-
}
293-
294-
public String getEmail() {
295-
return email;
296-
}
297-
298-
public void setEmail(final String email) {
299-
this.email = email;
300-
}
301-
}
302-
```
303-
- Parcelable requires a bit more work:
304-
```java
305-
public class User implements Parcelable {
306-
307-
private String name;
308-
private String email;
309-
310-
/**
311-
* Interface that must be implemented and provided as a public CREATOR field
312-
* that generates instances of your Parcelable class from a Parcel.
313-
*/
314-
public static final Creator<User> CREATOR = new Creator<User>() {
315-
316-
/**
317-
* Creates a new USer object from the Parcel. This is the reason why
318-
* the constructor that takes a Parcel is needed.
319-
*/
320-
@Override
321-
public User createFromParcel(Parcel in) {
322-
return new User(in);
323-
}
324-
325-
/**
326-
* Create a new array of the Parcelable class.
327-
* @return an array of the Parcelable class,
328-
* with every entry initialized to null.
329-
*/
330-
@Override
331-
public User[] newArray(int size) {
332-
return new User[size];
333-
}
334-
};
335-
336-
public User() {
337-
}
338-
339-
/**
340-
* Parcel overloaded constructor required for
341-
* Parcelable implementation used in the CREATOR
342-
*/
343-
private User(Parcel in) {
344-
name = in.readString();
345-
email = in.readString();
346-
}
347-
348-
public String getName() {
349-
return name;
350-
}
351-
352-
public void setName(final String name) {
353-
this.name = name;
354-
}
355-
356-
public String getEmail() {
357-
return email;
358-
}
359-
360-
public void setEmail(final String email) {
361-
this.email = email;
362-
}
363-
364-
@Override
365-
public int describeContents() {
366-
return 0;
367-
}
368-
369-
/**
370-
* This is where the parcel is performed.
371-
*/
372-
@Override
373-
public void writeToParcel(final Parcel parcel, final int i) {
374-
parcel.writeString(name);
375-
parcel.writeString(email);
376-
}
377-
}
378-
```
379-
Note: For a full explanation of the <b>describeContents()</b> method see [StackOverflow](https://stackoverflow.com/questions/4076946/parcelable-where-when-is-describecontents-used/4914799#4914799).
380-
In Android Studio, you can have all of the parcelable code auto generated for you, but like with everything else, it is always a good thing to try and understand everything that is happening.
381-
* What are anonymous classes?[OracleDoc](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)
382-
* What is the difference between using `==` and `.equals` on a string?[GeeksForGeeks](http://www.geeksforgeeks.org/difference-equals-method-java/)
383-
* How is `String` class implemented? Why was it made immutable?
384-
- There is no primitive variant of `String` class in Java language - all strings are just wrappers around underlying array of characters, which is declared `final`. This means that, once a `String` object is instantiated, it cannot be changed through normal tools of the language (Reflection still can mess things up horribly, because in Java no object is truly immutable). This is why `String` variables in classes are the first candidates to be used, when you want to override `hashCode()` and `equals()` of your class - you can be sure, that all their required contracts will be satisfied.
385-
> Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. ([Official Java Documentation](https://docs.oracle.com/javase/tutorial/java/data/strings.html))
386-
387-
This class is also unique in a sense, that, when you create an instance like this:
388-
```java
389-
String helloWorld = "Hello, World!";
390-
```
391-
`"Hello, World!"` is called a *literal* and compiler creates a `String` object with its' value. So
392-
```java
393-
String capital = "Hello, World!".toUpperCase();
394-
```
395-
is a valid statement, that, firstly, will create an object with literal value "Hello, World!" and then will create and return another object with value "HELLO, WORLD!"
396-
- `String` was made immutable to prevent malicious manipulation of data, when, for example, user login or other sensitive data is being send to a server.
397-
* What does it means to say that a `String` is immutable?
398-
- It means that once created, `String` object's `char[]` (its' containing value) is declared `final` and, therefore, it can not be changed during runtime.
399-
* Do you think memory leak in java?
400-
* What is `String.intern()`? When and why should it be used?
401-
* What is the `hashCode()` and `equals()` used for?
402-
* Can you list 8 primitive types in java?
403-
* Why would you not call abstract method in constructor?
267+
404268
* What is the difference between iterator and enumeration in java?
405-
* Do you agree we use composition over inheritance? ([Composition vs Inheritance)] [https://www.journaldev.com/12086/composition-vs-inheritance])
406-
* When would you make an object value final?
407-
* What are these `final`, `finally` and `finalize` keywords?
408-
- `final` is a keyword in the java language. It is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed.
409-
```java
410-
class FinalExample {
411-
public static void main(String[] args) {
412-
final int x=100;
413-
x=200;//Compile Time Error because x is final
414-
}
415-
}
416-
```
417-
- `finally` is a code block and is used to place important code, it will be executed whether exception is handled or not.
418-
```java
419-
class FinallyExample {
420-
public static void main(String[] args) {
421-
try {
422-
int x=300;
423-
}catch(Exception e) {
424-
System.out.println(e);
425-
}
426-
finally {
427-
System.out.println("finally block is executed");
428-
}
429-
}
430-
}
431-
```
432-
- `Finalize` is a method used to perform clean up processing just before object is garbage collected.
433-
```java
434-
class FinalizeExample {
435-
public void finalize() {
436-
System.out.println("finalize called");
437-
}
438-
439-
public static void main(String[] args) {
440-
FinalizeExample f1=new FinalizeExample();
441-
FinalizeExample f2=new FinalizeExample();
442-
f1=null;
443-
f2=null;
444-
System.gc();
445-
}
446-
}
447-
```
448-
* What is garbage collector? How does it work?
449-
- All objects are allocated on the heap area managed by the JVM.
450-
As long as an object is being referenced, the JVM considers it alive.
451-
Once an object is no longer referenced and therefore is not reachable by the application code,
452-
the garbage collector removes it and reclaims the unused memory.
453-
* `Arrays` vs `ArrayLists`.
454-
* `HashSet` vs `TreeSet`.
455-
* Typecast in Java.
456-
- In Java, you can use casts to polymorph one class into another, compatible one. For example:
457-
```java
458-
long i = 10l;
459-
int j = (int) i;
460-
long k = j;
461-
```
462-
Here we see, that, while narrowing (`long i` -> `int j`) requires an explicit cast to make sure the programmer realizes, that there may be some data or precision loss, widening (`int j` -> `long k`) does not require an explicit cast, because there can be no data loss (`long` can take larger numbers than `int` allows).
269+
270+
* Do you agree we use composition over inheritance? [Composition vs Inheritance](https://www.journaldev.com/12086/composition-vs-inheritance)
271+
463272
* Difference between method overloading and overriding.
464273
<p align="center">
465274
<img alt="Overloading and Overriding" src="https://github.com/codeshef/android-interview-questions/blob/master/assets/overloading-vs-overriding.png">
@@ -573,28 +382,6 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
573382
- Iterator [Wikipedia](https://en.wikipedia.org/wiki/Iterator_pattern?oldformat=true)
574383
- Strategy [Wikipedia](https://en.wikipedia.org/wiki/Strategy_pattern?oldformat=true)
575384

576-
#### String
577-
578-
* How is `String` class implemented? Why was it made immutable?
579-
- There is no primitive variant of `String` class in Java language - all strings are just wrappers around underlying array of characters, which is declared `final`. This means that, once a `String` object is instantiated, it cannot be changed through normal tools of the language (Reflection still can mess things up horribly, because in Java no object is truly immutable). This is why `String` variables in classes are the first candidates to be used, when you want to override `hashCode()` and `equals()` of your class - you can be sure, that all their required contracts will be satisfied.
580-
> Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. ([Official Java Documentation](https://docs.oracle.com/javase/tutorial/java/data/strings.html))
581-
582-
This class is also unique in a sense, that, when you create an instance like this:
583-
```java
584-
String helloWorld = "Hello, World!";
585-
```
586-
`"Hello, World!"` is called a *literal* and compiler creates a `String` object with its' value. So
587-
```java
588-
String capital = "Hello, World!".toUpperCase();
589-
```
590-
is a valid statement, that, firstly, will create an object with literal value "Hello, World!" and then will create and return another object with value "HELLO, WORLD!"
591-
- `String` was made immutable to prevent malicious manipulation of data, when, for example, user login or other sensitive data is being send to a server.
592-
593-
* What does it means to say that a `String` is immutable?
594-
- It means that once created, `String` object's `char[]` (its' containing value) is declared `final` and, therefore, it can not be changed during runtime.
595-
596-
* What is `String.intern()`? When and why should it be used?
597-
598385
#### Collections and Generics
599386

600387
* `Arrays` vs `ArrayLists`.
@@ -617,6 +404,28 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
617404

618405
#### Objects and Primitives
619406

407+
#### String
408+
409+
* How is `String` class implemented? Why was it made immutable?
410+
- There is no primitive variant of `String` class in Java language - all strings are just wrappers around underlying array of characters, which is declared `final`. This means that, once a `String` object is instantiated, it cannot be changed through normal tools of the language (Reflection still can mess things up horribly, because in Java no object is truly immutable). This is why `String` variables in classes are the first candidates to be used, when you want to override `hashCode()` and `equals()` of your class - you can be sure, that all their required contracts will be satisfied.
411+
> Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. ([Official Java Documentation](https://docs.oracle.com/javase/tutorial/java/data/strings.html))
412+
413+
This class is also unique in a sense, that, when you create an instance like this:
414+
```java
415+
String helloWorld = "Hello, World!";
416+
```
417+
`"Hello, World!"` is called a *literal* and compiler creates a `String` object with its' value. So
418+
```java
419+
String capital = "Hello, World!".toUpperCase();
420+
```
421+
is a valid statement, that, firstly, will create an object with literal value "Hello, World!" and then will create and return another object with value "HELLO, WORLD!"
422+
- `String` was made immutable to prevent malicious manipulation of data, when, for example, user login or other sensitive data is being send to a server.
423+
424+
* What does it means to say that a `String` is immutable?
425+
- It means that once created, `String` object's `char[]` (its' containing value) is declared `final` and, therefore, it can not be changed during runtime.
426+
427+
* What is `String.intern()`? When and why should it be used?
428+
620429
* Can you list 8 primitive types in java?
621430

622431
* What is the difference between an Integer and int?
@@ -865,6 +674,10 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
865674
866675
* Why would you not call abstract method in constructor?
867676
677+
* What are anonymous classes?[OracleDoc](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)
678+
679+
* Can you list 8 primitive types in java?
680+
868681
* When would you make an object value `final`?
869682
870683
* What are these `final`, `finally` and `finalize` keywords?
@@ -909,6 +722,7 @@ It is also a good practice to annotate overridden methods with `@Override` to ma
909722
}
910723
```
911724

725+
912726
* What does the `static` word mean in Java?
913727
- In case of `static` variable it means that this variable (its' value or the object it references) spans across all instances of enclosing class (changing it in one instance affects all others), while in case of `static` methods it means that these methods can be invoked without an instance of their enclosing class. It is useful, for example, when you create util classes that need not be instantiated every time you want to use them.
914728

0 commit comments

Comments
 (0)