In this, you get the tcs pa answer of MCQ as well as coding question. this is helpful for the student which is placed in tcs and give to the pa.
Showing posts with label java code. Show all posts
Showing posts with label java code. Show all posts
Java Proctored Assessment Programming solution 13th February
Java Proctored Assessment Programming solution 13th February
Question 1.
Write a Program to print smallest vowel in the given line…(Ascii Values)
input:
matrix
output:
a
Solution:
import java.lang.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
s=s.toLowerCase();
int n=s.length();
int c=0;
char[] ch=s.toCharArray();
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
{
c++;
}
}
char[] c1=new char[c];
int k=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
{
c1[k++]=ch[i];
}
}
Arrays.sort(c1);
System.out.println(c1[0]);
}
}
Question 2.
Create a Sim Class with following Attributes
int simId;
String customerName;
double balance;
double ratePerSecond;
String circle;
create a public Class Solution in which take input of 5 object and then take 2 input (circle1, circle2) for circle of matches as circle1 and choose only those objects which are match with circle1.
Create a method named as transferCircle() and passed sim object and circle1, circle2 as parameter and arrange sim object in descending order as per ratePerSecond
Print the output as simId, customerName, circle, ratePerSecond.
Input:
1
raju
130
1.32
mum
2
sachin
120
2.26
ahd
3
ram
200
2.54
kol
4
shubham
640
3.21
ahd
5
kalpesh
150
1.8
ahd
ahd
kol
Output:
4 shubham kol 3.21
2 sachin kol 2.26
5 kalpesh kol 1.8
Solution:
import java.util.Scanner;
public class SimSolution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Sim1[] sim = new Sim1[5];
for (int i = 0; i < sim.length; i++) {
int simId = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
double balance = sc.nextDouble();
double ratePersecond = sc.nextDouble();
sc.nextLine();
String circle = sc.nextLine();
sim[i] = new Sim1(simId, name, balance,
ratePersecond, circle);
}
String circle1 = sc.nextLine();
String circle2 = sc.nextLine();
Sim1[] result = transferCircle(sim, circle1, circle2);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i].getSimId() + " " +
result[i].getName() + " " + result[i].getCircle() + " "
+ result[i].getRatePersecond());
}
sc.close();
}
public static Sim1[] transferCircle(Sim1[] sim, String
circle1, String circle2) {
Sim1[] temp;
int j = 0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1)) {
j++;
}
}
temp = new Sim1[j];
j = 0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1)) {
temp[j] = sim[i];
temp[j++].setCircle(circle2);
}
}
for (int i = 0; i < j - 1; i++) {
for (int k = 0; k < j - 1; k++) {
if (temp[k].getRatePersecond() < temp[k +
1].getRatePersecond()) {
Sim1 a = temp[k];
temp[k] = temp[k + 1];
temp[k + 1] = a;
}
}
}
return temp;
}
}
class Sim1 {
private int simId;
private String name;
private double balance;
private double ratePersecond;
private String circle;
public Sim1(int simId, String name, double balance, double
ratePersecond, String circle) {
this.simId = simId;
this.name = name;
this.balance = balance;
this.ratePersecond = ratePersecond;
this.circle = circle;
}
public int getSimId() {
return simId;
}
public void setSimId(int simId) {
this.simId = simId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getRatePersecond() {
return ratePersecond;
}
public void setRatePersecond(double ratePersecond) {
this.ratePersecond = ratePersecond;
}
public String getCircle() {
return circle;
}
public void setCircle(String circle) {
this.circle = circle;
}
}
Question 1.
Write a Program to print smallest vowel in the given line…(Ascii Values)
input:
matrix
output:
a
Solution:
import java.lang.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
s=s.toLowerCase();
int n=s.length();
int c=0;
char[] ch=s.toCharArray();
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
{
c++;
}
}
char[] c1=new char[c];
int k=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
{
c1[k++]=ch[i];
}
}
Arrays.sort(c1);
System.out.println(c1[0]);
}
}
Question 2.
Create a Sim Class with following Attributes
int simId;
String customerName;
double balance;
double ratePerSecond;
String circle;
create a public Class Solution in which take input of 5 object and then take 2 input (circle1, circle2) for circle of matches as circle1 and choose only those objects which are match with circle1.
Create a method named as transferCircle() and passed sim object and circle1, circle2 as parameter and arrange sim object in descending order as per ratePerSecond
Print the output as simId, customerName, circle, ratePerSecond.
Input:
1
raju
130
1.32
mum
2
sachin
120
2.26
ahd
3
ram
200
2.54
kol
4
shubham
640
3.21
ahd
5
kalpesh
150
1.8
ahd
ahd
kol
Output:
4 shubham kol 3.21
2 sachin kol 2.26
5 kalpesh kol 1.8
Solution:
import java.util.Scanner;
public class SimSolution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Sim1[] sim = new Sim1[5];
for (int i = 0; i < sim.length; i++) {
int simId = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
double balance = sc.nextDouble();
double ratePersecond = sc.nextDouble();
sc.nextLine();
String circle = sc.nextLine();
sim[i] = new Sim1(simId, name, balance,
ratePersecond, circle);
}
String circle1 = sc.nextLine();
String circle2 = sc.nextLine();
Sim1[] result = transferCircle(sim, circle1, circle2);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i].getSimId() + " " +
result[i].getName() + " " + result[i].getCircle() + " "
+ result[i].getRatePersecond());
}
sc.close();
}
public static Sim1[] transferCircle(Sim1[] sim, String
circle1, String circle2) {
Sim1[] temp;
int j = 0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1)) {
j++;
}
}
temp = new Sim1[j];
j = 0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1)) {
temp[j] = sim[i];
temp[j++].setCircle(circle2);
}
}
for (int i = 0; i < j - 1; i++) {
for (int k = 0; k < j - 1; k++) {
if (temp[k].getRatePersecond() < temp[k +
1].getRatePersecond()) {
Sim1 a = temp[k];
temp[k] = temp[k + 1];
temp[k + 1] = a;
}
}
}
return temp;
}
}
class Sim1 {
private int simId;
private String name;
private double balance;
private double ratePersecond;
private String circle;
public Sim1(int simId, String name, double balance, double
ratePersecond, String circle) {
this.simId = simId;
this.name = name;
this.balance = balance;
this.ratePersecond = ratePersecond;
this.circle = circle;
}
public int getSimId() {
return simId;
}
public void setSimId(int simId) {
this.simId = simId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getRatePersecond() {
return ratePersecond;
}
public void setRatePersecond(double ratePersecond) {
this.ratePersecond = ratePersecond;
}
public String getCircle() {
return circle;
}
public void setCircle(String circle) {
this.circle = circle;
}
}
Java Movie Management
Java Movie Management
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Movie[] mv = new Movie[4];
for(int i=0;i<4;i++)
{
int a = sc.nextInt();sc.nextLine();
String b = sc.nextLine();
int c = sc.nextInt();
int d = sc.nextInt();
mv[i] = new Movie(a,b,c,d);
}
sc.nextLine();
String nm = sc.nextLine();
int um = sc.nextInt();
int dm = sc.nextInt();
int res = findAvgBudgetByDirector(mv, nm);
if (res>0)
System.out.println(res);
else
System.out.println("Sorry - The given director has not yet directed any movie");
Movie temp = getMovieByRatingBudget(mv, um, dm);
if(temp == null)
System.out.println("Sorry - No movie is available with the specified rating and budget requirement");
else
System.out.println(temp.id);
}
public static int findAvgBudgetByDirector (Movie[] mv, String nm)
{
//method logic
int avg,s=0,j=0;
for(int i=0;i<4;i++)
{
if(nm.equalsIgnoreCase(mv[i].dt))
{
s = s+mv[i].bt;
j++;
}
}
if(j>0)
{
avg = s/j;
return avg;
}
else
return 0;
}
public static Movie getMovieByRatingBudget(Movie[] mv, int rating, int budget)
{
//method logic
Movie temp = new Movie();
for(int i=0;i<4;i++)
{
if((rating == mv[i].rt) && (budget == mv[i].bt) && (mv[i].bt % mv[i].rt == 0))
{
temp.id = mv[i].id;
return temp;
}
}
return null;
}
}
class Movie
{
//code to build Movie class
int id,rt,bt;
String dt;
Movie()
{
}
Movie(int id, String dt, int rt, int bt)
{
this.id = id;
this.dt = dt;
this.rt = rt;
this.bt = bt;
}
}
Java Proctored Assessment Programming solution 2nd march
Java Proctored Assessment Programming solution 2nd march
Question 1.
Reverse the given number
INPUT
12345
OUTPUT
Reverse of the number is 54321
Solution:
import java.util.Scanner;
public class Solution1000 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuffer sb = new
StringBuffer(sc.nextLine());
System.out.println("Reverse of the number is
"+sb.reverse());
}
}
Question 2.
Sort the medicines according to Medicine prices with
Respective to the disease
INPUT :
dolo650
batch1
fever
100
dolo990
batch2
headache
101
paracetemol
batch3
bodypains
102
almox500
batch4
fever
103
fever
OUTPUT :
100
103
Solution:
import java.util.Arrays;import java.util.Scanner;public class Solution100 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Medicine[] m = new Medicine[4];for(int i =0;i<m.length;i++) {String a = sc.nextLine(); String b = sc.nextLine(); String c = sc.nextLine(); int d = sc.nextInt(); sc.nextLine(); m[i] = new Medicine(a,b,c,d);
}String e = sc.nextLine(); Integer[] f = sortAccordingtoPrices(m,e); for(Integer g : f) { System.out.println(g); }}public static Integer[] sortAccordingtoPrices(Medicine[] m,String e) {int count =0 ;for(int i=0;i<m.length;i++) {if(m[i].getDisease().equals(e)) {count++;}}Integer[] j = new Integer[count];int k = 0;for(int i=0;i<m.length;i++) {if(m[i].getDisease().equalsIsIgnorecase(e)) {j[k++] = m[i].getPrice();Arrays.sort(j);}}return j;}}class Medicine{private String MedicineName;private String batch;private String disease;private int price;public String getMedicineName() {return MedicineName;}public void setMedicineName(String medicineName) {MedicineName = medicineName;}public String getBatch() {return batch;}public void setBatch(String batch) {this.batch = batch;}public String getDisease() {return disease;}public void setDisease(String disease) {this.disease = disease;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public Medicine(String medicineName, String batch, Stringdisease, int price) {this.MedicineName = medicineName;this.batch = batch;this.disease = disease;this.price = price;}}
Subscribe to:
Posts (Atom)