You are on page 1of 12

# .

Coding Questions For Interview

Q1.how to count occurrences of a character in a String

public class A {

public static void main(String[] args) {


String s = "sagar";
int a = s.length(); // total length
int b = s.replace("a", "").length();
//replace a with empty
int c = a - b;
System.out.println("Number of occurrence of a is:" + c);

}
}

Q2. frequency of letter or character in a string


import java.util.Scanner;

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)
}

}
}

Q4. swapping two numbers with using third variable

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);
}
}

Q4. swapping two numbers without using third variable


public class B {
public static void main(String[] args) {
int a = 10, b = 20;
a = a+b;
// here i adding the value A+B and storing in A, now A
value is 30
b = a-b;
// here i minusing the value A-B and storing in B, now B
value is 10
a = a-b;
// here i minusing the value A_B and storing in A, now
A value is in 20
System.out.println("a"+a);
System.out.println("b"+b);

}
}

Q5. Write a ArrayList programme.

import java.util.ArrayList;
import java.util.Iterator;

public class RemoveDuplicates{


public static void main(String[] args){
ArrayList<Integer> a=new ArrayList<Integer>();
a.add(100);
a.add(200);
a.add(400);
System.out.println("ArrayList before:"+a);

a.add(3,500);
System.out.println("ArrayList after adding element at
index:"+a);

ArrayList<Integer> a1=new ArrayList<Integer>();


a1.add(500);
a1.add(600);

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());
}
}
}

Q6. LinkedList programme


import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class RemoveDuplicates{


public static void main(String[] args){
List<Integer> a=new LinkedList<Integer>();
a.add(100);
a.add(200);
a.add(400);
System.out.println("LinkedList before:"+a);

a.add(3,500);
System.out.println("LinkedList after adding element at
index:"+a);

List<Integer> a1=new LinkedList<Integer>();


a1.add(500);
a1.add(600);

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;

public class even number {


public static void main(String[] args){
List<Integer> n=Arrays.asList(2,3,4,5,6,7,8,9);
//List object is created and it stores the array
List<Integer> evenNumber=n.stream().filter(x-
>x%2==0).collect(Collectors.toList()); //By using stream
api, Filtering the even numbers
System.out.println(evenNumber);
}
}

Q8. Programme to find odd number


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class odd number {


public static void main(String[] args){
List<Integer> n=Arrays.asList(2,3,4,5,6,7,8,9);
List<Integer> oddNumber=n.stream().filter
(x->x%2!=0).collect(Collectors.toList());
System.out.println(oddNumber);
}
}
Q9. Programme to square the numbers
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class square {


public static void main(String[] args){
List<Integer> n=Arrays.asList(2,3,4,5,6,7,8,9);
List<Integer>
square=n.stream().map(x>x*x).collect(Collectors.toList());

System.out.println(square);
}
}

Q9. number of times occuring the given string


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class RemoveDuplicates{


public static void main(String[] args){

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;

public class RemoveDuplicates{


public static void main(String[] args){

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);
}
}

Q11.Programme to get string ending with character "e"


public class RemoveDuplicates{
public static void main(String[] args){
List<String>
names=Arrays.asList("Pune","Bidar","Auarangabd","Bangalore","M
umbai","Goa");
List<String> newNames=names.stream().filter
(s->s.endsWith("e")).collect(Collectors.toList());
System.out.println(names);
System.out.println(newNames);
}
}

Q12.Programme to sort the given strings


public class RemoveDuplicates{
public static void main(String[] args){
List<String>
names=Arrays.asList("Pune","Bidar","Auarangabd","Bangalore","M
umbai","Goa");
List<String>
newNames=names.stream().sorted().collect(Collectors.toList());
System.out.println(names);
System.out.println(newNames);
}
}

Q13. Programme to sort the given numbers

public class RemoveDuplicates{


public static void main(String[] args){
List<Integer>
names=Arrays.asList(1,30,40,160,380,90,60,20,38);
List<Integer>
newNames=names.stream().sorted().collect(Collectors.toList());
System.out.println(names);
System.out.println(newNames);
}
}

Q14.Programme to convert given string to uppercase


public class RemoveDuplicates{
public static void main(String[] args){
List<String>
name=Arrays.asList("sangmesh","sagar","saurabh","swapnil","sun
il");
List<String> newNames=name.stream().map
(s->s.toUpperCase()).collect(Collectors.toList());
System.out.println(newNames);
}
}

Q15. Programme to convert given string to lowercase


public class RemoveDuplicates{
public static void main(String[] args){
List<String>
name=Arrays.asList("sANGMesh","sagar","saurabh","swapnil","sun
il");
List<String> newNames=name.stream().map
(s->s.toLowerCase()).collect(Collectors.toList());
System.out.println(newNames);
}
}

Q16. Programme to sort array using Treeset

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class RemoveDuplicates{


public static void main(String[] args){
ArrayList<Integer> l=new ArrayList<Integer>();
l.add(10);
l.add(20);
l.add(5);
l.add(30);
l.add(60);
l.add(40);
System.out.println(l);

TreeSet<Integer> hs=new TreeSet<Integer>();

for(int i=0;i<l.size();i++) {
hs.add(l.get(i));
}
System.out.println(hs);
}
}

Q17. Programme to sort hashset using stream api


import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class RemoveDuplicates{


public static void main(String[] args){
Set<String> s=new HashSet<String>();
s.add("Delhi");
s.add("Mumbai");
s.add("Bangalore");
s.add("Chennai");
s.add("Hyderabad");
System.out.println(s);
Set<String>
s1=s.stream().collect(Collectors.toCollection(TreeSet::new));
System.out.println(s1);
}
}
Q18.Sort the string characters in alphabetical order without
using any method.

public class RemoveDuplicates{


public static void main(String[] args){
String str="Sangmesh";
char[] arr=str.toCharArray(); //It is going to return charcter array to us.
char temp; //It is temporary array
for(int i=0;i<arr.length;i++) { //I have created nested loop
for(int j=i+1;j<arr.length;j++) {
if(arr[i]>arr[j]) { //For comparison purpose I have created if loop.If
arr[i]>arr[j] then, arr[i] it has to go to right side
temp = arr[i]; //If arr[i] is greater than arr[i+1] then it will store
in temp and
arr[i]=arr[j]; //After that arr[j] will be stored in arr[i]
arr[j]=temp; //Here temp will be stored in arr[j].
}
}
} //Once these two loops will get over then string will sort
System.out.println(); //It will print sorted string in alphabetical order.
}
}
Q19. Sort the string characters in alphabetical order using any method.
import java.util.Arrays;
import java.util.Scanner;

public class SortingString {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string value: ");
String str = sc.nextLine();
char charArray[] = str.toCharArray();
Arrays.sort(charArray);
System.out.println(new String(charArray));
}
}
Q20.Palindrome String
public class SortingString{
public static void main(String[] args){
String str="swapnil"; //Create string
having value "swapnil "
String rev="";
//It is string with empty value
for(int i=str.length()-1;i>=0;i--){
//for loop for reversing the string
rev=rev+str.charAt(i);
//It is used to get palindrome string (Logic for
palindrome)
}
if(str.equals(rev)) {
//If str equals to rev then if will execute
otherwise else will execute
System.out.println("It is a palindrome String");
}else {
System.out.println("It is not a palindrome String");
}
}
}
Q21. Sort the ArrayList

import java.util.ArrayList;
import java.util.Collections;

public class RemoveDuplicates{


public static void main(String[] args){
ArrayList<String> a=new ArrayList<String>();
a.add("pune");
a.add("aurangabad");
a.add("bidar");
a.add("nashik");
a.add("bangalore");
a.add("mumbai");
System.out.println(a);
Collections.sort(a);
System.out.println("Sorted arraylist:"+a);
}
}
Q22.Finding minimum And Maximum Salary
public class A {
public static void main(String[] args) {
int[] x = {34000,9000,36000,14000,1000, 5000};
for(int i=0; i<x.length-1; i++) {//for loop
for(int j=0; j<x.length-1;j++) {//to sorting value
if(x[j]>x[j+1]) {//For Comparing
int temp = x[j];
//we are swapping x[j] into temp
x[j] = x[j+1];
//we are swapping x[j+1] into x[j]
x[j+1] = temp;
// we are swapping x[j+1] into x[j]
}
}
}

for(int z:x) {//For Sorting the salary


System.out.println(z);
}
System.out.println("___");//For the pipercation
System.out.println(x[x.length-1]);//For first maximum salary
System.out.println(x[x.length-2]);//For second maximum salary
System.out.println(x[0]);//For first minimum salary
System.out.println(x[1]);//For second minimum salary``
}
}
Q23.Removing duplicate elements from an array
public class A {
public static void main(String[] args) {
int[]x = {0,1,2,2,3,3,4,4,5,6,7,7};
int[]temp = new int [x.length];
int j = 0;

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);
}
}
}

You might also like