Java Interview Questions and Programs

1) Transient Variable in java?

2) Vector vs ArrayList in java?

3) Which version in java?

4) Concurrent hashmap in java?

5) Volatile keyword in java?

6) Comparable and toCompare() method in java?


1) Write syntax of Coroutines, Sealed Classes, Extension Function, Generics in kotlin

fun main() = runBlocking { // this: CoroutineScope

    launch { // launch a new coroutine and continue

        delay(1000L) // non-blocking delay for 1 second (default time unit is ms)

        println("World!") // print after delay

    }

    println("Hello") // main coroutine continues while a previous one is delayed

}


Generic in Kotlin

  1. class Person<T>(age: T){  
  2. var age: T = age  
  3. init {  
  4. this.age= age  
  5. println(age)  
  6.     }  
  7. }  
  8. fun main(args: Array<String>){  
  9. var ageInt: Person<Int> = Person<Int>(30)  
  10. var ageString: Person<String> = Person<String>("40")  
  11. }  


2) Write code for implementing constructor in kotlin

  1. class myClass(name: String, id: Int) {  
  2. val e_name: String  
  3. var e_id: Int  
  4. init{  
  5. e_name = name.capitalize()  
  6. e_id = id    
  7. println("Name = ${e_name}")  
  8. println("Id = ${e_id}")  
  9.     } 
  1. constructor(name: String, id: Int){  
  2. println("Name = ${name}")  
  3. println("Id = ${id}")  
  4.     }  

  1. }  
  2. fun main(args: Array<String>){  
  3. val myclass = myClass ("Ashu"101)  
  4.   
  5. }  

3) Write code for a thread-safe singleton in java?

public class ASingleton { private static volatile ASingleton instance; private static Object mutex = new Object(); private ASingleton() { } public static ASingleton getInstance() { ASingleton result = instance; if (result == null) { synchronized (mutex) { result = instance; if (result == null) instance = result = new ASingleton(); } } return result; } }



public class Singleton {
private static class LoadSingleton {
      static final Singleton INSTANCE = new Singleton();
   }
   private Singleton() {}

   public static Singleton getInstance() {
      return LoadSingleton.INSTANCE;
   }
}


public final class SomeSingleton {
   public static final SomeSingleton INSTANCE;

   private SomeSingleton() {
      INSTANCE = (SomeSingleton)this;
      System.out.println("init complete");
   }

   static {
      new SomeSingleton();
   }
}


4) Write down logic in Kotlin for removing similar repeated characters with alternate case

Comments

Popular posts from this blog

Google Assistant Implementation in Android application with app actions