Skip to content

Instantly share code, notes, and snippets.

@sam43
Last active June 15, 2019 11:18
Show Gist options
  • Save sam43/dcb7ab38a8147496c706ad1a42373732 to your computer and use it in GitHub Desktop.
Save sam43/dcb7ab38a8147496c706ad1a42373732 to your computer and use it in GitHub Desktop.
/**
*
* @author scode
*/
public class CountOnes4mString {
// Returns count of n length binary
// strings with consecutive 1's
static int countStrings(int n) {
int a[] = new int[n], b[] = new int[n];
a[0] = b[0] = 1;
for (int i = 1; i < n; i++) {
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
return (1 << n) - a[n - 1] - b[n - 1];
}
public static void main(String[] args) {
String s = "110011111101111011111";
int op = 0;
String subs[] = s.split("0");
for (String s1 : subs) {
op = s1.length() > op ? s1.length() : op;
}
System.out.println(op);
}
}
// link: https://android.jlelse.eu/android-interview-questions-cheat-sheet-96ea01c88def
**1. Define a constructor?
A constructor is a method used to initialize the state of an object, and it gets invoked at the time of object creation. Rules forconstructor are:
Constructor Name should be same as class name.
A constructor must have no return type.
**2. Define Destructor?
A destructor is a method which is automatically called when the object is made of scope or destroyed. Destructor name is also same as class name but with the tilde symbol before the name.
**3. What is an Inline function?
An inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code.
**4. What is dynamic or run time polymorphism?
Dynamic or Run time polymorphism is also known as method overriding in which call to an overridden function is resolved during run time, not at the compile time. It means having two or more methods with the same name, same signature but with different implementation.
**5. Why bytecode cannot be run in Android?
Android uses DVM (Dalvik Virtual Machine ) rather using JVM(Java Virtual Machine).
**6. What is a Sticky Intent?
A Sticky Intent is a broadcast from sendStickyBroadcast() method such that the intent floats around even after the broadcast, allowing others to collect data from it.
**7. What’s the difference between onCreate() and onStart()?
The onCreate() method is called once during the Activity lifecycle, either when the application starts, or when the Activity has been destroyed and then recreated, for example during a configuration change.
The onStart() method is called whenever the Activity becomes visible to the user, typically after onCreate() or onRestart().
**8. Difference between Service, Intent Service, AsyncTask & Threads
-> Android service is a component that is used to perform operations on the background such as playing music. It doesn’t has any UI (user interface). The service runs in the background indefinitely even if application is destroyed.
-> AsyncTask allows you to perform asynchronous work on your user interface. It performs the blocking operations in a worker thread and then publishes the results on the UI thread, without requiring you to handle threads and/or handlers yourself.
-> IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
-> A thread is a single sequential flow of control within a program. Threads can be thought of as mini-processes running within a main process.
**9. What is the relationship between the life cycle of an AsyncTask and an Activity? What problems can this result in? How can these problems be avoided?
An AsyncTask is not tied to the life cycle of the Activity that contains it. So, for example, if you start an AsyncTask inside an Activity and the user rotates the device, the Activity will be destroyed (and a new Activity instance will be created) but the AsyncTask will not die but instead goes on living until it completes.
Then, when the AsyncTask does complete, rather than updating the UI of the new Activity, it updates the former instance of the Activity (i.e., the one in which it was created but that is not displayed anymore!). This can lead to an Exception (of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity).
There’s also the potential for this to result in a memory leak since the AsyncTask maintains a reference to the Activity, which prevents the Activity from being garbage collected as long as the AsyncTask remains alive.
For these reasons, using AsyncTasks for long-running background tasks is generally a bad idea . Rather, for long-running background tasks, a different mechanism (such as a service) should be employed.
Note: AsyncTasks by default run on a single thread using a serial executor, meaning it has only 1 thread and each task runs one after the other.
**10. Why is it recommended to use only the default constructor to create a Fragment?
The reason why you should be passing parameters through bundle is because when the system restores a fragment (e.g on config change), it will automatically restore your bundle. This way you are guaranteed to restore the state of the fragment correctly to the same state the fragment was initialised with.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment