Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Capgemini Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Practice Questions for Capgemini Exceller Coding

Round
Question 1
Problem Statement –

You have write a function that accepts, a string which length is


“len”, the string has some “#”, in it you have to move all the
hashes to the front of the string and return the whole string
back and print it.
char* moveHash(char str[],int n);
example :-
Input:
Move#Hash#to#Front
Output:
###MoveHashtoFront

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 –

Capgemini in its online written test have a coding question,


wherein the students are given a string with multiple
characters that are repeated consecutively. You’re supposed to
reduce the size of this string using mathematical logic given as
in the example below :
For Example:
Input :
abbccccc
Output:
ab2c5
import java.util.*;
public class CharacterCount
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int i, j, k = 0, count = 0;
String uni = new String("");
for(i=0; i<str.length(); i++)
{
count = 0;
for(j=0; j<=i; j++)
{
if(str.charAt(i)==str.charAt(j))
{
count++;
}
}
if(count==1)
{
uni = uni + str.charAt(i);
}
}
for(i=0; i<uni.length(); i++)
{
count = 0;
for(j=0; j<str.length(); j++)
{
if(uni.charAt(i)==str.charAt(j))
{
count++;
}
}
if(count==1)
{
System.out.printf("%c",uni.charAt(i));
}
else
{
System.out.printf("%c
%d",uni.charAt(i),count);
}
}
}
}

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 –

Write a function to solve the following equation a 3 + a2b + 2a2b


+ 2ab2 + ab2 + b3.

Write a program to accept three values in order of a, b and c


and get the result of the above equation.

You might also like