# Java Interview question-6

**Question 51: Write a program to find the first occurrence of a character in a string in java.**

```java
package learning;
public class FindFirstOccurence {
    public static void main(String[] args) {
        System.out.println(findFirstOccurence("Hello World!!", 'l'));
        System.out.println(findFirstOccurence("Hello World!!", '!'));
    }
    private static int findFirstOccurence(String str, char c) {
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == c) {
                return i;
            }
        }
        return -1;
    }
}
/**
 * Output:
 * 2
 * 11
 */
```

**Question 52: Find all employees who live in ‘Pune’ city, sort them by their name, and print the names of employees.**

```java
employeeList.stream()
.filter(e -> e.getCity().equalsIgnoreCase("Pune"))
.sorted(Comparator.comparing(Employee::getName))
.forEach(e -> System.out.println(e.getName()));
```

**Question 53: Find the Book id from a map whose name is “java” using stream API java-8.**

```java
Map<String, String> books = new HashMap<>();
books.put("123-A", "DS");
books.put( "324-A", "c++");
books.put("213-B", "Java");
Ans -To create stream for map use entrySet and then stream.
Optional<String> result = books.entrySet().
 stream().
 filter(e -> e.getValue().equalsIgnoreCase("Java")).
 map(Map.Entry::getKey).findFirst();
System.out.println(result);
```

**Question 54: Find the Output**

```java
package learning;
public class FindOutput {
    public static void main(String[] args) {
        SuperC c = new SubCla();
        c.methodToOverride1();
        c.methodToOverride2();
    }
}
class SuperC {
    public void methodToOverride1() {
        System.out.println("in methodToOverride1");
    }
    public void methodToOverride2() {
        System.out.println("in methodToOverride2");
    }
}
class SubCla extends SuperC {
    public void methodToOverride1() {
        System.out.println("in methodToOverride1-sub");
    }
    public void methodToOverride2() {
        System.out.println("in methodToOverride2-sub");
    }
}
/**
 * Output:
 * in methodToOverride1-sub
 * in methodToOverride2-sub
 */
```

**Question 55: Find the occurrence of names of employees from the List&lt;Employee&gt;, and find the frequency of each name.**

```java
public class Test {
    public static void main(String[] args) {
        Employee emp1 = new Employee(1,"Ajay",100);
        Employee emp2 = new Employee(1,"name",100);
        Employee emp3 = new Employee(1,"Ajay",100);
        Employee emp4 = new Employee(1,"name",100);
        Employee emp5 = new Employee(1,"Ajay",100);
        List<Employee> empList = Arrays.asList(emp1,emp2,emp3,emp4,emp5);
        Map<String,Long> answer = empList.stream()
                .collect(Collectors.groupingBy(Employee::getName,
                          Collectors.counting()));
        System.out.println(answer);
    }
}
```

**Question 56: Query to find number of employees each department.**

```java
SELECT emp_dept, COUNT(*) FROM emp_details GROUP BY emp_dept;
```

**Question 57: Find the output below code. Will this compile? if not, what can be done to compile it?**

```java
public class MainTest {
    public static void main(String[] args) {
        Parent parent = new Child();
        //Will this below line compile
        parent.foo();
        //Will this below line compile
        parent.bar();
    }    
}
class Parent {
    public void foo() {
        System.out.println("Parent :: foo()");
    }
}
class Child extends Parent {
    @Override
    public void foo() {
        System.out.println("Child :: foo()");
    }
    public void bar() {
        System.out.println("Child :: bar()");
    }
}

// Since bar function is not in Parent class so it will not even compile
// Either we have to create a fucntion inside parent as well or
// we will have to use Child child = new Child()
```

**Question 58: What would be the size of the HashSet? What can we do to stop HashSet from adding duplicate students with the same name? please tell us about the workaround.**

```java
public class HashSetTest {
    public static void doSomethingWithHashSet() {
        HashSet<Student> student = new HashSet();
        student.add(new Student("Ajay"));
        student.add(new Student("Ajay"));
        System.out.println("sizze of hashset : "+student.size());
    }
    public static void main(String[] args) {
        doSomethingWithHashSet();
    }
}
class Student {
    String name;
    public Student(String name) {
        this.name = name;
    }
}
// we must to override equals method of Students class to stop adding
// duplicate Students. And to improve performance, we will need to 
// overirde hashCode function.
```

**Question 59: Types of fault tolerance mechanisms in Spring microservices?**

There are a few ways we can use it.

* Timeouts
    
* Retries
    
* Circuit Breaker
    
* Deadlines
    
* Rate limiters
    

Hystrix and Resiliancy4j are one of the java libraries which help with the above options.

**Question 60: Java 8 changes in memory optimization?**

PermGen was removed and replaced with MetaSpace.Class and metadata moved to MetaSpace. MetaSpace is not contiguous with Java Heap and allocated out of Native memory. This means it can use available memory from the system upto max memory.

* MetaSpace size max limit can be configured using JVM options.
    
* G1 starts supporting the concurrent unloading of Classes.
    
* Code Cache is introduced. It stores compiled code by the JIT compiler.
    
* Compressed Class Space is also introduced.
