1) Write a program that accepts 10 student records (roll number and score) and prints them in decreasing
order of scores. In case there are multiple records pertaining to the same student, the program should
choose a single record containing the highest score. The program should be capable of accepting a multi-line
input. Each subsequent line of input will contain a student record, that is, a roll number and a score
(separated by a hyphen). The output should consist of the combination of roll number and corresponding
score in decreasing order of score.
INPUT to program
1001-40
1002-50
1003-60
1002-80
1005-35
1005-55
1007-68
1009-99
1009-10
1004-89
OUTPUT from program
1009-99
1004-89
1002-80
1007-68
1003-60
1005-55
1001-40
Note: In case of input data being supplied to the question, it should be assumed to be a console input.
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
/*
package com.sajadhaja.training;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Student {
int id;
int score;
public static void main(String args[]){
try{
Scanner in = new Scanner(System.in);
ArrayList<Student> stud= new ArrayList<Student>();
int c=0;
while(c<10){
String k=in.nextLine();
StringTokenizer stk =new StringTokenizer(k,"-");
Student st=new Student();
st.id=Integer.parseInt(stk.nextToken());
st.score=Integer.parseInt(stk.nextToken());
if(c>1){
Student stDup=findStudentByid(st.id,stud);
if(stDup!=null){
if(st.score >stDup.score) {
stud.add(st);
stud.remove(stDup);
}
}else{
System.out.println("stDup Null"+st.id+" "+st.score);;
stud.add(st);
}
}else stud.add(st);
c++;
}
Collections.sort (stud, new Comparator<Student>() {
public int compare(Student o1, Student o2) {
return o2.score - o1.score;
}
});
for(Student stt:stud){
System.out.println(">>>>"+stt.id+" and "+stt.score);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
static Student findStudentByid(int id,ArrayList<Student> list){
for(Student s:list){
if(s.id==id)
return s;
}
return null;
}
}
2) Sam wants to select a username in order to register on a website.
The rules for selecting a username are:
1. The minimum length of the username must be 5 characters and the maximum may be 10.
2. It should contain at least one letter from A-Z
3. It should contain at least one digit from 0-9
4. It should contain at least one character from amongst @#*=
5. It should not contain any spaces
Write a program which accepts 4 usernames (one username per line) as input and checks whether each of them satisfy the above mentioned conditions.
If a username satisfies the conditions, the program should print PASS (in uppercase)
If a username fails the conditions, the program should print FAIL (in uppercase)
Suppose the following usernames are supplied to the program:
1234@a
ABC3a#@
1Ac@
ABC 3a#@
Then the output should be:
FAIL
PASS
FAIL
FAIL
IMPORTANT NOTES - READ CAREFULLY:
1. Your solution should assume console input
2. Your solution should contain class name as Main, as the solution will be compiled as Main.java
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/ package com.sajadhaja.training;
import java.util.Scanner;
public class Pyramid {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int arr[]=new int[3];
arr[0]=in.nextInt();
arr[1]=in.nextInt();
arr[2]=in.nextInt();
for(int i=0;i<arr.length;i++){
int y=arr[i];
int z=y+1;
for(int j=1;j<=y;j++){
System.out.println();
System.out.format("%"+z+"s","");
for(int k=1;k<=j;k++){
System.out.print(j);
System.out.format("%1s","");
}
z--;
}
}
}
}
3) Kermit, a frog hops in a particular way such that:
1. He hops 20cm in the first hop, 10cm in the second hop and 5cm in the third hop.
2. After three hops Kermit rests for a while and then again follows the same hopping pattern.
Calculate the total distance travelled by Kermit (in centimeters) for the provided number of hops. Exactly 4 numbers of hops will be provided to the program (one number per line) as per the below example.
Suppose the following number of hops is provided to the program:
4
6
3
5
Then the total distance covered should be displayed as follows:
55
70
35
65
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
public class MonkeyHop {
public static void main(String[] args) {
int a,b,c,d;
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
d = in.nextInt();
int[] array = {a,b,c,d};
int k = 0;
for(int i=0; i<array.length; i++){
switch(array[i]%3){
case 0: k = 0 + (array[i]/3)*35; break;
case 1: k = 20 + (array[i]/3)*35; break;
case 2: k = 30 + (array[i]/3)*35; break;
case 3: k = 35 + (array[i]/3)*35; break;
}
System.out.println(k);
}
}
}
4) Write a program which will print the below structures according to the input provided to the program. The program should accept 3 inputs in the form of numbers between 1 and 9, both inclusive (one number per line) and then generate the corresponding structures based on the input.
Suppose the following sequence of numbers is supplied to the program:
3
2
4
Then the output should be:
1
2 2
3 3 3
1
2 2
1
2 2
3 3 3
4 4 4 4
IMPORTANT NOTES - READ CAREFULLY:
1. Your solution should assume console input
2. Your solution should contain class name as Main, as the solution will be compiled as Main.java
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
public class Pyramid {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int arr[]=new int[3];
arr[0]=in.nextInt();
arr[1]=in.nextInt();
arr[2]=in.nextInt();
for(int i=0;i<arr.length;i++){
int y=arr[i];
int z=y+1;
for(int j=1;j<=y;j++){
System.out.println();
System.out.format("%"+z+"s","");
for(int k=1;k<=j;k++){
System.out.print(j);
System.out.format("%1s","");
}
z--;
}
}
}
}
5) Ross is an event organizer. He has received data regarding the participation of employees in two different events. Some employees have participated in only one event and others have participated in both events. Ross now needs to count the number of employees who have taken part in both events. The records received by Ross consist of employee ids, which are unique. Write a program that accepts the employee ids participating in each event (the first line relates to the first event and the second line relates to the second event). The program should print the number of common employee ids in both the events.
Suppose the following input is given to the program, where each line represents a different event:
1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112
Now the common employee ids are 1003 and 1005, so the program should give the output as:
2
IMPORTANT NOTES - READ CAREFULLY:
1. Your solution should assume console input
2. Your solution should contain class name as Main, as the solution will be compiled as Main.java
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Rose {
public static void main(String args[]){
Scanner in= new Scanner(System.in);
String strarr[] = {"",""};
strarr[0]=in.nextLine();
strarr[1]=in.nextLine();
ArrayList<Integer> list[] = new ArrayList[2];
for(int i=0;i<strarr.length;i++){
StringTokenizer stk=new StringTokenizer(strarr[i],",");
list[i]=new ArrayList<Integer>();
while(stk.hasMoreTokens()){
list[i].add(Integer.parseInt(stk.nextToken()));
}
}
ArrayList<Integer> listcommon = new ArrayList<Integer>(list[1]);
listcommon.retainAll(list[0]);
System.out.println(listcommon.size());
}
}
6) Write a program which will accept a single pair of strings separated by a comma; the program should calculate the sum of ascii values of the characters of each string. The program should then subtract the sum of the ascii values of the second string from the sum of the ascii values of the first string.
Suppose the following input is given to the program:
123ABC,456DEF
Then the sum of the ascii values of the characters in '123ABC' is 348 and in '456DEF' it is 366. The Difference between these numbers is 348 – 366 = -18
The corresponding output to be printed by the program is:
-18
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
public class AsciiOperation {
public static void main(String[] args) {
boolean condition = false;
Scanner scanner = new Scanner(System.in);
String value = scanner.nextLine();
condition = value.equalsIgnoreCase("exit");
if(!condition && value.contains(",")){
calculate(value);
}
}
private static void calculate(String value){
final String[] event1 = value.split(",");
int ss = 0;
for ( int i = 0; i < event1[0].length(); ++i ) {
char c = event1[0].charAt( i );
ss += (int) c;
}
int sd = 0;
for ( int i = 0; i < event1[1].length(); ++i ) {
char c = event1[1].charAt( i );
sd += (int) c;
}
System.out.println(ss-sd);
}
}
7) Write a program which will take the year (yyyy) and the numeric sequence of the month (0-11) as its input. The program will return the day on which the 28th of that particular month and year falls. The input can consist of two year-month combinations, one combination per line.
The numeric sequence of months is as follows:
0 – Jan
1 – Feb
2 – March
and so on......
The format for supplying the input is:
1999-5
Where 1999 is the year and 5 is the numeric sequence of the month (corresponding to June). The program should display the day on which June 28, 1999 fell, and in this case the output will be MONDAY.
The output should be displayed in uppercase letters.
Suppose the following INPUT sequence is given to the program:
1999-5
1998-6
Then the output should be:
MONDAY
TUESDAY
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Scanner;
public class YearMonthDay {
public static void main(String args[]){
Scanner in =new Scanner(System.in);
String a=in.next();
String b=in.next();
getDay(a);
getDay(b);
}
public static void getDay(String in){
String yearmon[]= in.split("-");
Calendar cal=Calendar.getInstance();
cal.set(Integer.parseInt(yearmon[0]),Integer.parseInt(yearmon[1]),28);
int val = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(new DateFormatSymbols().getWeekdays()[val].toUpperCase() );
}
}
8) Write a program which will accept three sentences (one sentence per line) and print the words having Initial Caps within the sentences. Below is an example.
Here is an example. If the below three sentences are given to the program as input,
This is a Program
Coding test of Initial Caps
the program Will Test You
Then, the output would look like:
This
Program
Coding
Initial
Caps
Will
Test
You
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class StringExamp {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String li[]=new String[3];
li[0]=in.nextLine();
li[1]=in.nextLine();
li[2]=in.nextLine();
capsOut(li[0]);
capsOut(li[1]);
capsOut(li[2]);
}
public static void capsOut(String in){
String words[]= in.split(" ");
for(String word:words){
Pattern pat = Pattern.compile("^[A-Z]");
Matcher match = pat.matcher(word);
if(match.find())
System.out.println(word);
}
}
}
order of scores. In case there are multiple records pertaining to the same student, the program should
choose a single record containing the highest score. The program should be capable of accepting a multi-line
input. Each subsequent line of input will contain a student record, that is, a roll number and a score
(separated by a hyphen). The output should consist of the combination of roll number and corresponding
score in decreasing order of score.
INPUT to program
1001-40
1002-50
1003-60
1002-80
1005-35
1005-55
1007-68
1009-99
1009-10
1004-89
OUTPUT from program
1009-99
1004-89
1002-80
1007-68
1003-60
1005-55
1001-40
Note: In case of input data being supplied to the question, it should be assumed to be a console input.
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
/*
package com.sajadhaja.training;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Student {
int id;
int score;
public static void main(String args[]){
try{
Scanner in = new Scanner(System.in);
ArrayList<Student> stud= new ArrayList<Student>();
int c=0;
while(c<10){
String k=in.nextLine();
StringTokenizer stk =new StringTokenizer(k,"-");
Student st=new Student();
st.id=Integer.parseInt(stk.nextToken());
st.score=Integer.parseInt(stk.nextToken());
if(c>1){
Student stDup=findStudentByid(st.id,stud);
if(stDup!=null){
if(st.score >stDup.score) {
stud.add(st);
stud.remove(stDup);
}
}else{
System.out.println("stDup Null"+st.id+" "+st.score);;
stud.add(st);
}
}else stud.add(st);
c++;
}
Collections.sort (stud, new Comparator<Student>() {
public int compare(Student o1, Student o2) {
return o2.score - o1.score;
}
});
for(Student stt:stud){
System.out.println(">>>>"+stt.id+" and "+stt.score);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
static Student findStudentByid(int id,ArrayList<Student> list){
for(Student s:list){
if(s.id==id)
return s;
}
return null;
}
}
2) Sam wants to select a username in order to register on a website.
The rules for selecting a username are:
1. The minimum length of the username must be 5 characters and the maximum may be 10.
2. It should contain at least one letter from A-Z
3. It should contain at least one digit from 0-9
4. It should contain at least one character from amongst @#*=
5. It should not contain any spaces
Write a program which accepts 4 usernames (one username per line) as input and checks whether each of them satisfy the above mentioned conditions.
If a username satisfies the conditions, the program should print PASS (in uppercase)
If a username fails the conditions, the program should print FAIL (in uppercase)
Suppose the following usernames are supplied to the program:
1234@a
ABC3a#@
1Ac@
ABC 3a#@
Then the output should be:
FAIL
PASS
FAIL
FAIL
IMPORTANT NOTES - READ CAREFULLY:
1. Your solution should assume console input
2. Your solution should contain class name as Main, as the solution will be compiled as Main.java
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/ package com.sajadhaja.training;
import java.util.Scanner;
public class Pyramid {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int arr[]=new int[3];
arr[0]=in.nextInt();
arr[1]=in.nextInt();
arr[2]=in.nextInt();
for(int i=0;i<arr.length;i++){
int y=arr[i];
int z=y+1;
for(int j=1;j<=y;j++){
System.out.println();
System.out.format("%"+z+"s","");
for(int k=1;k<=j;k++){
System.out.print(j);
System.out.format("%1s","");
}
z--;
}
}
}
}
3) Kermit, a frog hops in a particular way such that:
1. He hops 20cm in the first hop, 10cm in the second hop and 5cm in the third hop.
2. After three hops Kermit rests for a while and then again follows the same hopping pattern.
Calculate the total distance travelled by Kermit (in centimeters) for the provided number of hops. Exactly 4 numbers of hops will be provided to the program (one number per line) as per the below example.
Suppose the following number of hops is provided to the program:
4
6
3
5
Then the total distance covered should be displayed as follows:
55
70
35
65
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
public class MonkeyHop {
public static void main(String[] args) {
int a,b,c,d;
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
d = in.nextInt();
int[] array = {a,b,c,d};
int k = 0;
for(int i=0; i<array.length; i++){
switch(array[i]%3){
case 0: k = 0 + (array[i]/3)*35; break;
case 1: k = 20 + (array[i]/3)*35; break;
case 2: k = 30 + (array[i]/3)*35; break;
case 3: k = 35 + (array[i]/3)*35; break;
}
System.out.println(k);
}
}
}
4) Write a program which will print the below structures according to the input provided to the program. The program should accept 3 inputs in the form of numbers between 1 and 9, both inclusive (one number per line) and then generate the corresponding structures based on the input.
Suppose the following sequence of numbers is supplied to the program:
3
2
4
Then the output should be:
1
2 2
3 3 3
1
2 2
1
2 2
3 3 3
4 4 4 4
IMPORTANT NOTES - READ CAREFULLY:
1. Your solution should assume console input
2. Your solution should contain class name as Main, as the solution will be compiled as Main.java
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
public class Pyramid {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int arr[]=new int[3];
arr[0]=in.nextInt();
arr[1]=in.nextInt();
arr[2]=in.nextInt();
for(int i=0;i<arr.length;i++){
int y=arr[i];
int z=y+1;
for(int j=1;j<=y;j++){
System.out.println();
System.out.format("%"+z+"s","");
for(int k=1;k<=j;k++){
System.out.print(j);
System.out.format("%1s","");
}
z--;
}
}
}
}
5) Ross is an event organizer. He has received data regarding the participation of employees in two different events. Some employees have participated in only one event and others have participated in both events. Ross now needs to count the number of employees who have taken part in both events. The records received by Ross consist of employee ids, which are unique. Write a program that accepts the employee ids participating in each event (the first line relates to the first event and the second line relates to the second event). The program should print the number of common employee ids in both the events.
Suppose the following input is given to the program, where each line represents a different event:
1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112
Now the common employee ids are 1003 and 1005, so the program should give the output as:
2
IMPORTANT NOTES - READ CAREFULLY:
1. Your solution should assume console input
2. Your solution should contain class name as Main, as the solution will be compiled as Main.java
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Rose {
public static void main(String args[]){
Scanner in= new Scanner(System.in);
String strarr[] = {"",""};
strarr[0]=in.nextLine();
strarr[1]=in.nextLine();
ArrayList<Integer> list[] = new ArrayList[2];
for(int i=0;i<strarr.length;i++){
StringTokenizer stk=new StringTokenizer(strarr[i],",");
list[i]=new ArrayList<Integer>();
while(stk.hasMoreTokens()){
list[i].add(Integer.parseInt(stk.nextToken()));
}
}
ArrayList<Integer> listcommon = new ArrayList<Integer>(list[1]);
listcommon.retainAll(list[0]);
System.out.println(listcommon.size());
}
}
6) Write a program which will accept a single pair of strings separated by a comma; the program should calculate the sum of ascii values of the characters of each string. The program should then subtract the sum of the ascii values of the second string from the sum of the ascii values of the first string.
Suppose the following input is given to the program:
123ABC,456DEF
Then the sum of the ascii values of the characters in '123ABC' is 348 and in '456DEF' it is 366. The Difference between these numbers is 348 – 366 = -18
The corresponding output to be printed by the program is:
-18
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
public class AsciiOperation {
public static void main(String[] args) {
boolean condition = false;
Scanner scanner = new Scanner(System.in);
String value = scanner.nextLine();
condition = value.equalsIgnoreCase("exit");
if(!condition && value.contains(",")){
calculate(value);
}
}
private static void calculate(String value){
final String[] event1 = value.split(",");
int ss = 0;
for ( int i = 0; i < event1[0].length(); ++i ) {
char c = event1[0].charAt( i );
ss += (int) c;
}
int sd = 0;
for ( int i = 0; i < event1[1].length(); ++i ) {
char c = event1[1].charAt( i );
sd += (int) c;
}
System.out.println(ss-sd);
}
}
7) Write a program which will take the year (yyyy) and the numeric sequence of the month (0-11) as its input. The program will return the day on which the 28th of that particular month and year falls. The input can consist of two year-month combinations, one combination per line.
The numeric sequence of months is as follows:
0 – Jan
1 – Feb
2 – March
and so on......
The format for supplying the input is:
1999-5
Where 1999 is the year and 5 is the numeric sequence of the month (corresponding to June). The program should display the day on which June 28, 1999 fell, and in this case the output will be MONDAY.
The output should be displayed in uppercase letters.
Suppose the following INPUT sequence is given to the program:
1999-5
1998-6
Then the output should be:
MONDAY
TUESDAY
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Scanner;
public class YearMonthDay {
public static void main(String args[]){
Scanner in =new Scanner(System.in);
String a=in.next();
String b=in.next();
getDay(a);
getDay(b);
}
public static void getDay(String in){
String yearmon[]= in.split("-");
Calendar cal=Calendar.getInstance();
cal.set(Integer.parseInt(yearmon[0]),Integer.parseInt(yearmon[1]),28);
int val = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(new DateFormatSymbols().getWeekdays()[val].toUpperCase() );
}
}
8) Write a program which will accept three sentences (one sentence per line) and print the words having Initial Caps within the sentences. Below is an example.
Here is an example. If the below three sentences are given to the program as input,
This is a Program
Coding test of Initial Caps
the program Will Test You
Then, the output would look like:
This
Program
Coding
Initial
Caps
Will
Test
You
SOLUTIONS:-
/* Author:- Mohammed Sajadh NA
Blog:- http://sajadhaja.blogspot.in
*/
package com.sajadhaja.training;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class StringExamp {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String li[]=new String[3];
li[0]=in.nextLine();
li[1]=in.nextLine();
li[2]=in.nextLine();
capsOut(li[0]);
capsOut(li[1]);
capsOut(li[2]);
}
public static void capsOut(String in){
String words[]= in.split(" ");
for(String word:words){
Pattern pat = Pattern.compile("^[A-Z]");
Matcher match = pat.matcher(word);
if(match.find())
System.out.println(word);
}
}
}