Coding Questions For Interview
Coding Questions For Interview
public class A {
}
}
public class B {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
// by using scanner i will get user input
System.out.println("Enter string");
// it will print statement enter string
String s=in.nextLine();
// input will stored in s
s=s.toLowerCase();
// it will convert all characters in lower case
System.out.println("character|Frequency");
// to count each letter frequency
for (char cha = 'a'; cha <='z'; cha++) {
// create loop for different alphabet
int c = 0;
for (int i = 0; i <s.length(); i++) {
if(cha==s.charAt(i))
// here we check that cha value is equal or not to character
of string
c++; // it increments count
}
System.out.println(cha + "\t" +c);
}
}
}
Q3. Program to print Fibonacci Series in java
public class B {
public static void main(String[] args) {
int a=0, b=1; // here i taking a fix number
int c; // take a variable
for (int i = 1; i < 10; i++) {
// this for loop for how much number i have to print
c = a+b;
// here i giving the c value
System.out.println(" "+c);
a=b; //here i putting the b value in a
b=c; //in b i putting c value(swapping)
}
}
}
public class B {
public static void main(String[] args) {
int a = 10, b = 20;// to swipe the element i taken two
variable
int t; // here i take t variable
t = a; // here i putting the A value in T
a = b; // here i putting B value in A
b = t; // here i putting T value in B
System.out.println("a"+a);
System.out.println("b"+b);
}
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
a.add(3,500);
System.out.println("ArrayList after adding element at
index:"+a);
a.addAll(2,a1);
System.out.println("ArrayList after inserting another
arraylist:"+a);
if(a.contains(400)) {
System.out.println("Yes present");
}else {
System.out.println("Not present");
}
a.remove(1);
System.out.println("ArrayList after removing element:"+a);
Iterator itr=a.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
a.add(3,500);
System.out.println("LinkedList after adding element at
index:"+a);
a.addAll(2,a1);
System.out.println("LinkedList after inserting another
LinkedList:"+a);
if(a.contains(400)) {
System.out.println("Yes present");
}else {
System.out.println("Not present");
}
a.remove(1);
System.out.println("LinkedList after removing
element:"+a);
Iterator itr=a.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Q7. Programme to find even number
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
System.out.println(square);
}
}
List<String>names=Arrays.asList("sagar","sangmesh","swapnil","
saurabh","sunil");
List<String> newNames=names.stream().filter
(s->s.equals("sagar")).collect(Collectors.toList());
System.out.println(names);
System.out.println(newNames);
}
}
Q10. Programme to get string starting with character "B"
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
List<String>names=Arrays.asList("Pune","Bidar","Auarangabd","B
angalore","Mumbai","Goa");
List<String> newNames=names.stream().filter
(s->s.startsWith("B")).collect(Collectors.toList());
System.out.println(names);
System.out.println(newNames);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
for(int i=0;i<l.size();i++) {
hs.add(l.get(i));
}
System.out.println(hs);
}
}
import java.util.ArrayList;
import java.util.Collections;
for(int i=0;i<x.length-1;i++) {
if(x[i]!=x[i+1]) {
temp[j] = x[i];
j++;
}
}
temp[j] = x[x.length-1];
for(int z:temp) {
System.out.println(z);
}
}
}
Q24.For Sorted Array using For loop
public class A {
public static void main(String[] args) {
int[]x = {0,1,2,2,3,3,4,4,5,6,7,7};//consider as i
int[]temp = new int [x.length];//consider as j and
getting the size of array
int j = 0;//For copying the value of X in temp
for(int i=0;i<x.length-1;i++) {
//For comparing the value between i and j and handeling
ArrayIndexOufOfBoundsException
if(x[i]!=x[i+1]) {
// which means when i is 0 then x[0] is 1 then 2,3,4,5,
temp[j] = x[i]; //Copied the value
j++; //For incrementing
}
}
temp[j] = x[x.length-1];//For getting the last value
for(int z:temp) { //For showing default value
System.out.print(z);
}
}
}