<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ekta Dass on Medium]]></title>
        <description><![CDATA[Stories by Ekta Dass on Medium]]></description>
        <link>https://medium.com/@ekta.dass01?source=rss-91b3c2ca2927------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tBx0splbsD_eeUY7xixAEA.jpeg</url>
            <title>Stories by Ekta Dass on Medium</title>
            <link>https://medium.com/@ekta.dass01?source=rss-91b3c2ca2927------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:57:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ekta.dass01/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Most commonly discussed interview questions about Android— Part II]]></title>
            <link>https://medium.com/@ekta.dass01/most-commonly-discussed-interview-questions-about-android-part-ii-f8ff01d29b5c?source=rss-91b3c2ca2927------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8ff01d29b5c</guid>
            <category><![CDATA[dispatcher]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[android-interviews]]></category>
            <category><![CDATA[kotlin-interview-question]]></category>
            <dc:creator><![CDATA[Ekta Dass]]></dc:creator>
            <pubDate>Mon, 07 Aug 2023 05:56:08 GMT</pubDate>
            <atom:updated>2023-08-07T05:56:08.522Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cDZPtr0Koy1vompN7dCeQg.png" /></figure><blockquote><strong>In Kotlin coroutines, what are the ways to switch between threads?</strong></blockquote><p>We can use coroutines to switch between threads using a feature called <strong><em>Dispatchers</em></strong>. Coroutines allow you to write asynchronous code in a more sequential and readable manner. To switch between threads, we can change the dispatcher context in which the coroutine runs.</p><p>The available dispatchers are:</p><p><strong>1)</strong><em>Dispatchers.Default</em>: This is used for CPU-intensive tasks, like sorting or parsing JSON.<br><strong>2)</strong> <em>Dispatchers.IO</em>: This is used for IO-bound tasks, like reading or writing to files or making network requests. <em>for eg:</em> when we getting the data from API/Repo/DB<br><strong>3)</strong> <em>Dispatchers.Main</em>: This is typically used for UI-related tasks in Android applications. <em>for eg: </em>Once we update the receive data from API/Repo/DB on RecycleView/Layout.</p><blockquote><strong>Concept of Dispatchers in the context of Kotlin coroutines with examples.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*g3Xol6h4dRIJKeGy2ProTA.png" /></figure><p>The <strong><em>runBlocking </em></strong>function is used here to create a coroutine in the main thread for demonstration purposes. In a project application, we might create coroutines in a more structured way using launch or async functions.</p><p>By using <strong><em>withContext</em></strong>, we can switch to a different dispatcher temporarily and then return to the original dispatcher after the block of code completes.</p><p>The <strong><em>Dispatchers.Main</em></strong> dispatcher is typically used in Android applications. If you are running this code outside an Android application, you may not have access to the Main dispatcher. In such cases, you can use the runBlocking function without specifying a dispatcher to run the coroutine in the main thread.</p><blockquote><strong>How do you ensure that coroutines are properly canceled or cleared to avoid memory leaks?</strong></blockquote><p>To ensure coroutines are properly canceled or cleared to avoid memory leaks, we should use structured concurrency with functions like <strong><em>lifecycleScope</em></strong> or <strong><em>viewModelScope</em></strong>. These scopes automatically cancel all child coroutines when the associated lifecycle is destroyed (e.g., when the Activity or Fragment is destroyed). It helps prevent leaking coroutines and resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*Pqr5fc768Sm4oDI6VASSow.png" /><figcaption>cancel job</figcaption></figure><p>We can call the <strong><em>cancel() </em></strong>function on a coroutine to manually cancel it. <br><strong>Note:</strong> This method can lead to potential resource leaks if not handled properly. If the coroutine is performing any IO or other asynchronous operations, it won’t be automatically canceled, and We need to handle the cancellation appropriately in our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*7YMn4lKMmLSE1LLqDeqQaA.png" /></figure><p>We also need to override<strong><em> onDestroy()</em></strong> method.</p><p>In this approach, We don’t need to manually cancel the coroutine. When the associated lifecycle (in this case, when <strong><em>onDestroy</em></strong> is called), the coroutine will be automatically cancelled by the lifecycle-aware scope (<em>lifecycleScope</em>). Similarly, We can use <em>viewModelScope</em> in a ViewModel to achieve the same structured concurrency behavior.</p><blockquote><strong>What is the purpose of runBlocking and when would you use it?</strong></blockquote><p>runBlocking is a coroutine builder in Kotlin that allows us to run a new coroutine and block the current thread until the coroutine completes. It is mainly used for writing simple test cases, bridging between coroutine-based and synchronous code, and for starting the main coroutine in applications. It should be used judiciously and with care since blocking the thread can lead to performance issues and defeat the purpose of using coroutines for asynchronous programming.</p><p>The ‘<em>runBlocking</em>’ function takes a ‘<em>block</em>’ of code, which is a lambda with a suspend modifier. The ‘<em>block</em>’ represents the code that will be executed within the coroutine. The function returns the result of the ‘<em>block</em>’ when it completes.</p><blockquote><strong>What is the purpose of the </strong><strong>init block in Kotlin classes? How does it differ from the constructor?</strong></blockquote><p>The init block in Kotlin is used to initialize properties and execute code that needs to be run when an instance of a class is created. It is a part of the primary constructor and is called during the object initialization phase.</p><p><em>Differences from the constructor:</em></p><ul><li>The primary constructor can initialize properties directly as part of the parameter list, whereas the init block allows you to perform additional initialization logic.</li><li>If there are multiple constructors, the init block is always executed regardless of which constructor is called. It consolidates common initialization code.</li></ul><pre>class Person(name: String, age: Int) {<br>    val name: String<br>    val age: Int<br><br>    init {<br>        this.name = name<br>        this.age = age<br>        println(&quot;Person object created: $name, $age&quot;)<br>    }<br>}</pre><blockquote><strong>Explain the concepts of companion objects and how they are used in Kotlin.</strong></blockquote><p>In Kotlin, <strong><em>a companion object</em></strong> is a special object that is associated with the class in which it is declared. It is similar to static members in other programming languages but with more capabilities. The companion object is shared among all instances of the class, and its members can be accessed directly using the class name.</p><pre>class MyClass {<br>    companion object {<br>        const val CONSTANT_VALUE = 42<br><br>        fun create(): MyClass {<br>            return MyClass()<br>        }<br>    }<br>}<br><br>// Accessing companion object members<br>val constant = MyClass.CONSTANT_VALUE<br>val instance = MyClass.create()</pre><blockquote><strong>Explain the concept of extension functions in Kotlin and provide an example of how they can be used?</strong></blockquote><p>Extension functions in Kotlin allow us to add new functions to existing classes without modifying their source code. They are powerful tools for adding utility functions to classes from external libraries or standard classes.</p><pre>// Extension function for the String class<br>fun String.isPalindrome(): Boolean {<br>    val cleanString = this.toLowerCase().replace(Regex(&quot;[^a-zA-Z0-9]&quot;), &quot;&quot;)<br>    return cleanString == cleanString.reversed()<br>}<br><br>fun main() {<br>    val text = &quot;A man, a plan, a canal, Panama!&quot;<br>    println(text.isPalindrome()) // Output: true<br>}</pre><p>In this example, we added an isPalindrome extension function to the String class. The function checks if the string is a palindrome (reads the same backward as forward).</p><blockquote><strong>Explain the difference between the </strong><strong>let, </strong><strong>apply, </strong><strong>run, </strong><strong>also, and </strong><strong>with scope functions in Kotlin.</strong></blockquote><p>These functions allows us to execute a block of code in the context of an object.</p><p><strong>let</strong>: It is used to perform a block of code on a non-null object and return the result of the last expression within the block. It is commonly used for safe null checks and chaining operations.</p><p><strong>apply</strong>: It is used to configure an object by calling multiple methods on it and returns the object itself. It is useful when you need to set properties on an object during its initialization.</p><p><strong>run</strong>: It is used to execute a block of code on an object, similar to let, but it returns the result of the last expression within the block or the entire object if no return value is specified.</p><p><strong>also</strong>: It is used to perform additional actions on an object and returns the original object. It is typically used for logging or side effects without altering the original object.</p><p><strong>with</strong>: It is used to call multiple methods on an object without the need to repeat the object reference. It does not return anything and is useful when you need to perform several operations on the same object.</p><pre>data class Person(var name: String, var age: Int)<br><br>fun main() {<br>    val person = Person(&quot;Ekta&quot;, 28)<br><br>    val resultLet = person.let {<br>        it.age + 5<br>    }<br><br>    val resultApply = person.apply {<br>        name = &quot;Bob&quot;<br>        age = 25<br>    }<br><br>    val resultRun = person.run {<br>        &quot;Person: $name, Age: $age&quot;<br>    }<br><br>    val resultAlso = person.also {<br>        println(&quot;Person name: ${it.name}&quot;)<br>    }<br><br>    val resultWith = with(person) {<br>        &quot;Person: $name, Age: $age&quot;<br>    }<br><br>    println(&quot;Result Let: $resultLet&quot;)<br>    println(&quot;Result Apply: $resultApply&quot;)<br>    println(&quot;Result Run: $resultRun&quot;)<br>    println(&quot;Result Also: $resultAlso&quot;)<br>    println(&quot;Result With: $resultWith&quot;)<br>}</pre><blockquote><strong>What is the concept of inline functions in Kotlin and when they are used?</strong></blockquote><p>In Kotlin, an inline function is a higher-order function modifier that suggests the compiler to perform &quot;inlining&quot; of the function&#39;s code at the call site. When you mark a function as inline, the function&#39;s bytecode is copied directly into the calling code, which can lead to improved performance in certain situations.</p><p>Use case of inline functions:</p><ul><li>Reducing the overhead of function calls</li></ul><pre>inline fun executeOperation(a: Int, b: Int, operation: (Int, Int) -&gt; Int): Int {<br>    return operation(a, b)<br>}<br><br>fun main() {<br>    val result = executeOperation(5, 3) { x, y -&gt; x + y }<br>    println(&quot;Result: $result&quot;)<br>}</pre><blockquote><strong>Explain the concept of smart casts in Kotlin. How do they work, and what benefits do they provide?</strong></blockquote><p>Smart casts in Kotlin are automatic type casts that the compiler performs based on certain conditions. When the compiler can guarantee that a variable is of a specific type within a certain code block, it will automatically cast the variable to that type, allowing you to use its properties and functions without the need for explicit type checks or casts.</p><p>Smart casts work with the is operator and !is operator, which are used for type checks.</p><pre>fun printLength(any: Any) {<br>    if (any is String) {<br>        // Smart cast to String<br>        println(&quot;Length: ${any.length}&quot;)<br>    } else {<br>        println(&quot;Not a String&quot;)<br>    }<br>}</pre><p>Benefits of smart casts:</p><ul><li>Increased type safety: Smart casts eliminate the need for manual type checks and casting, reducing the chances of runtime type errors.</li><li>Cleaner code: They simplify code and make it more concise and readable by removing redundant type checks and casts.</li><li>Improved developer productivity: Smart casts save developers from writing boilerplate code, improving productivity.</li></ul><p>Happy coding adventures!</p><p>Thanks for reading,</p><p>Ekta</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8ff01d29b5c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android’s most commonly asked interview questions.]]></title>
            <link>https://medium.com/@ekta.dass01/androids-most-commonly-asked-interview-questions-3e9448cff7fc?source=rss-91b3c2ca2927------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e9448cff7fc</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[dagger]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-interviews]]></category>
            <dc:creator><![CDATA[Ekta Dass]]></dc:creator>
            <pubDate>Tue, 20 Sep 2022 11:56:08 GMT</pubDate>
            <atom:updated>2022-09-20T11:56:08.488Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*_VnwMkTlz8cwXX3c_gig9w.jpeg" /></figure><blockquote><strong>What is the difference between a val and const?</strong></blockquote><p>Both are immutable once the value is assigned to them we can not modify it. However, we have to assign the value to <strong>const </strong>during the initialization point and <strong>val </strong>during run time.</p><blockquote><strong><em>What is the output of the below code?</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*ZD8HxcAdmvhI52xrY8Nlig.jpeg" /></figure><p>There will be a compile time error in this line “lateinit var string: String” because we can not assign null. We will see an error to remove the “initializers from the property”<br>Also, If the lateinit is not initialized then you will receive a <strong>UninitializedPropertyAccessException</strong></p><blockquote><strong><em>How many ways we can bind data in MVVM?</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*OPfOArXlMEStBg1iTzQf_g.jpeg" /><figcaption>enable data binding in build.gradle(:app)</figcaption></figure><p>We can do this by using data binding to bind data. Data bindings can both be <em>OneWay </em>or <em>TwoWay</em> to flow statistics from side to side between the View and ViewModel.<br>The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format, rather than programmatically.</p><p><strong><em>Advantages of using the Data Binding Library?</em></strong></p><p>-&gt; Reduce findViewById calls and enhance your app’s performance<br>-&gt; Helps get rid of memory leaks or nullPointerExceptions<br>-&gt; Uses declarative layout, which is more adaptable<br>-&gt; Data and Views are separated from each other</p><blockquote><strong><em>What are the sealed classes?</em></strong></blockquote><p><strong>Enum</strong>: allow a single instance of each value and can’t encode more information on each type. for eg. in an error case of success, or failure we can not have an associate exception.</p><p><strong>Sealed class</strong>: like abstract classes, sealed classes allows us to represent hierarchies. The child class can be any type of class like a data class, an object, a regular class, or even <br>another sealed class. But we have to define hierarchies in the same class or nested classes. If we try to extend a sealed class outside the file cause compile time error.</p><blockquote><strong><em>How ViewModel retains the object?</em></strong></blockquote><p>There are various advantages of the ViewModel class such as handling configuration changes, data sharing, and lifecycle awareness to name a few.</p><p><a href="https://blog.mindorks.com/android-viewmodels-under-the-hood"><em>But, how is ViewModel still alive when the activity itself is killed?</em></a><em> (Check the link to deep dive)</em></p><p>It is ViewModelStore that stores the ViewModel and is retained when the rotation occurs and which returns the same view model instance in the new activity instance.</p><p>ViewModel store is linked to activity/fragment and when in the process of rotation current instance is destroyed and the new instance is created</p><p>ViewModelStoreOwner : it is an interface.It has just one method which returns the ViewModelStore.</p><p>By default Activity and Fragment are always ViewModel store owner because their parent classes in Android SDK implement the ViewModelStoreOwner interface. (ComponentActivity.java and Fragment.java )</p><blockquote><strong><em>Responsibilities of Viewmodel.</em></strong></blockquote><p>ViewModel is the primary factor of MVVM software. The number one responsibility of ViewModel is to offer data to the view, so that view can put that records on the display screen.<br>ViewModel provides loose coupling because ViewModel doesn’t references about the views but the views have references about ViewModel which is why there is a loose coupling. views keep observing the viewmodel using live data observable pattern and it depends upon views which type of data they want to fetch. because of loose coupling, it is easy to write unit test cases as well.</p><blockquote><strong><em>What is coroutine in Kotlin?</em></strong></blockquote><p>Coroutine is nothing but a lightweight thread that executes code asynchronously. They simply simplify asynchronous programming. It is used to manage long-running operations in the background to avoid blocking the main i.e UI thread. It is recommended because of its cheap, lightweight, and built-in cancellation properties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*FXEQUYM4a_XXqrK48R8FWQ.jpeg" /><figcaption>Suspend function</figcaption></figure><p>We just need to add suspend modifier to tell the kotlin compiler that this function needs to be executed within the coroutine. The kotlin compiler writes the callbacks to show data under the hood for us when the computation can suspend. A suspend function must be called from another suspend function. When a suspend function returns it means it has completed all work.</p><p>In the above example, the network request is nothing but just another suspend function as you can see below and it returns data as the synchronous version of it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*RVD_O1Pj4YgX6mOTGwdcNw.jpeg" /></figure><p><em>withContext</em> used to switch between <em>Dispatchers</em> nothing just a suspend function from the coroutine that takes dispatchers as parameters.</p><p><em>Dispatcher</em> is nothing but just to run a task on a particular thread such as IO, Main, or Default dispatchers.</p><p><em>What happens if I </em><strong><em>launch </em></strong><em>the coroutine in this way:- There will a compiler error on launch that is because the launch must be called within a </em><strong><em>scope</em></strong><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*vaXjSfKD9Sw6dbZqoxPF2w.jpeg" /></figure><p><em>CoroutineScope</em></p><ul><li>Keep track of coroutines</li><li>Ability to cancel them</li><li>Is notified of the failure</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*NWenKMMgbAmTPEr0NEZl0g.jpeg" /><figcaption>1st way</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*3dSwLb9f8TGPmbYtQb5Vyg.jpeg" /><figcaption>2nd way with job</figcaption></figure><p>Job is going to define the lifecycle of either the scope and also the coroutines. Whenever we are passing a job to a scope that means it is going to handle exceptions in a specific way.</p><p><strong>Scope with a job: </strong>When a child fails, it propagates the cancellation to the other children. When the failure is notified, the scope will cancel itself and propagates the execution up. ( crash the app in case of UI-related scope)</p><p><strong>Scope with a SupervisorJob:</strong> The failure of the child doesn’t affect the other children. When the failure is notified the scope doesn&#39;t do anything.</p><blockquote><strong><em>Difference between launch and async in coroutine?</em></strong></blockquote><p>Both create a new coroutine. With the <em>launch,</em> we don&#39;t receive any value.</p><p>With <em>async</em>, we get a return value. It returns a deferred object and we can call await with the object. await will suspend the execution of the coroutine until async finished its work and it will return the value of the coroutine</p><p><em>launch</em> throws the exception and <em>async</em> hold on to an exception until await is called.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*Sde-1Hwu4v329nKfSKSGoQ.jpeg" /><figcaption>async example</figcaption></figure><blockquote><strong>Difference between Hilt vs Dagger?</strong></blockquote><p>Dagger creates a graph to find where to get dependencies when needed in your project. For Dagger to do this we need to add the <a href="http://twitter.com/Component">@Component</a> annotation.</p><p>In Dagger-Android, we have to create a component class with a builder/factory, including every module and we should inject the application context into the Application class after building our project. Here is the Dagger-Android way to construct a component.</p><p>However, we do not need to create this component in Hilt. Hilt already does this for us when we add the <a href="http://twitter.com/HiltAndroidApp">@HiltAndroidApp</a> annotation. Also, the instance of the application can be <a href="http://twitter.com/Injected">@Injected</a> by Hilt into other modules.</p><p>But in <em>Hilt</em>, we don’t need to create a component, include every module, and build for generating DaggerAppComponent class.</p><p>In <em>Dagger</em>, we create scope annotations such as ActivityScope, and FragmentScope to specify the lifecycle, but hilt provides us with core components such as Application, Activity, Fragment, Service, and View.</p><p>In <em>Dagger</em>, <a href="http://twitter.com/ContributesAndroidIn">@ContributesAndroidIn</a>jector is used to automatically generate the codes of the subcomponents and remove the common codes. This causes us to write too many <a href="http://twitter.com/Scope">@Scope</a> annotations and <a href="http://twitter.com/Inject">@Inject</a> annotations in large projects.</p><p>However, we do not need to write these annotations in Hilt. Instead, Hilt provides the <a href="http://twitter.com/AndroidEntryPoint">@AndroidEntryPoint</a> annotation. In this way, instances are <a href="http://twitter.com/Injected">@Injected</a> into the required fields by Hilt.</p><blockquote><strong><em>Difference between Room vs SQlite?</em></strong></blockquote><p>In the case of <em>SQLite</em>, There is no compile time verification of raw SQLite queries. But in Room, there is SQL validation at compile time.<br>As your schema changes, you need to update the affected SQL queries manually. Room solves this problem. You need to use lots of boilerplate code to convert between SQL queries and Java data objects. But, Room maps our database objects to Java Object without boilerplate code. Room is built to work with LiveData and RxJava for data observation, while SQLite does not.<br>Room annotations and main components:</p><p><a href="http://twitter.com/Entity">@Entity</a> — Define our database tables<br><a href="http://twitter.com/DAO">@DAO</a> — Provide an API for reading and writing data<br><a href="http://twitter.com/Database">@Database</a> — Represent a database holder</p><blockquote><strong><em>What is Google analytics?</em></strong></blockquote><p>Google App Engine is a cloud-based computing service developed to host web applications in the existing Google infrastructure. It is offered as a Platform as a Service (PaaS) product that gives Web app developers and enterprises access to Google’s scalable hosting and tier 1 Internet service. Google App Engine boasts scalability and easy management as well as free use of predefined consumed resources and comes with Java support.</p><blockquote><strong><em>What is Jetpack Compose?</em></strong></blockquote><p>Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs.</p><p>You won’t be editing any XML layouts or using the Layout Editor. Instead, you will call composable functions to define what elements you want, and the Compose compiler will do the rest.</p><p>Will post more questions later.. Good Luck !! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e9448cff7fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>