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
- class Person<T>(age: T){
- var age: T = age
- init {
- this.age= age
- println(age)
- }
- }
- fun main(args: Array<String>){
- var ageInt: Person<Int> = Person<Int>(30)
- var ageString: Person<String> = Person<String>("40")
- }
2) Write code for implementing constructor in kotlin
- class myClass(name: String, id: Int) {
- val e_name: String
- var e_id: Int
- init{
- e_name = name.capitalize()
- e_id = id
- println("Name = ${e_name}")
- println("Id = ${e_id}")
- }
- constructor(name: String, id: Int){
- println("Name = ${name}")
- println("Id = ${id}")
- }
- }
- fun main(args: Array<String>){
- val myclass = myClass ("Ashu", 101)
- }
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
Post a Comment