Accenture Training:
2 Questions 45 Mins, 1 Complete O/P, 1 Partial O/P.
1) Rat Problem – Food required for the rat?
2) Binary Number System, With Binary Operations.
3) Password Check
4) Max. Number in an array and it’s index.
5) Distance Calculation using formula.
6) The function accepts 3 integer: a,b,c
a. If c = 1: a+b
b. If c = 2: a-b
c. If c = 3: a*b
d. If c = 4: a/b
Answer: use switch () or if statement.
public class Main{
public static void main(String[] args){
int a,b,c,ans;
a = 5; // Generally input is taken
b = 4;
c = 3;
ans = 0;
switch(c){
case 1:
ans = a+b;
break; // It is important don’t miss
case 2:
ans = a-b;
break;
case 3:
ans = a*b;
break;
case 4:
ans = a/b;
break;
}
System.out.print(ans);
}}
7) Question:
a. Input Array
b. Split it as Odd, even
c. Find the second element
d. Sum of those
Code:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner obj = new Scanner(System.in);
int n = obj.nextInt();
int[] arr = new int[n];
for (int i = 0; i<n; i++){
arr[i] = obj.nextInt();
}
int odd = 0;
int eve = 0;
for (int i = 0; i<n; i++){
if (i % 2 == 0){
eve++;
}
else{
odd++;
}
}
int[] odd_arr = new int[odd];
int[] eve_arr = new int[eve];
int odd_ind = 0;
int eve_ind = 0;
for (int i = 0; i < n; i++){
if(arr[i]%2 != 0){
odd_arr[odd_ind] = arr[i];
odd_ind++;
}
else{
eve_arr[eve_ind] = arr[i];
eve_ind++;
}
}
odd_arr.sort();
eve_arr.sort();
System.out.println("Odd:");
for (int i = 0; i < odd; i++){
System.out.println(odd_arr[i]);
}
System.out.println("Even:");
for (int i = 0; i < eve; i++){
System.out.println(eve_arr[i]);
}
System.out.print("Second odd: ");
System.out.println(odd_arr[1]);
System.out.print("Second even: ");
System.out.println(eve_arr[1]);
System.out.print("Sum of Those:");
System.out.println(odd_arr[1] + eve_arr[1]);
}
}
Above code is not given the sorted array.
8) Anagram Question
a. “listen” = “silent”