Java Capgemini Questions
Java Capgemini Questions
Java Capgemini Questions
Round
Question 1
Problem Statement –
import java.util.*;
public class MoveHash
{
public static void moveHash(String str ,int n)
{
String str1= new String("");
String str2= new String("");
int i=0;
for(i=0;i<n;i++)
{
if(str.charAt(i)=='#')
str1=str1 + str.charAt(i);
else
str2 = str2 + str.charAt(i);
}
String result = str1.concat(str2);
System.out.println(result);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
int len= a.length();
moveHash(a, len);
}
}
Question 2
Problem Statement –
Question 3
Problem Statement –
You’re given an array of integers, print the number of times
each integer has occurred in the array.
Input :
1233414512
Output :
1 occurs 3 times
2 occurs 2 times
3 occurs 2 times
4 occurs 2 times
5 occurs 1 times
import java.util.Scanner;
public class ArrayFrequency
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int array[] = new int[n]; // taking
value of integer
int count = 0;
int k = 0;
for(int i=0; i<n; i++)
{
array[i] = sc.nextInt(); //elements of
array
}
//step - 1
int newarray[] = new int[n];
for(int i=0; i<n; i++)
{
count = 0;
for(int j=0; j<=i; j++)
{
if(array[i]==array[j])
{
count++;
}
}
if(count == 1)
{
newarray[k] = array[i];
k++;
}
}
//step 2;
for(int i=0; i<k; i++)
{
count = 0;
for(int j=0; j<n; j++)
{
if(newarray[i] == array[j])
{
count++;
}
}
System.out.printf("%d occurs %d times\
n",newarray[i],count);
}
}
}
Question 4
Problem Statement –