Java Questions Srithar
Java Questions Srithar
package Dumps;
import java.util.StringTokenizer;
public class Prog1 {
static int op;
public static void abc(String s) {
StringTokenizer st2 = new StringTokenizer(s, "/");
String d = st2.nextToken();
String m = st2.nextToken();
String y = st2.nextToken();
if ((Integer.parseInt(d) <= 31) && (Integer.parseInt(d) >= 1)
&& (Integer.parseInt(m) >= 1) && (Integer.parseInt(m) <= 12)
&& y.length() == 4 && d.length() == 2 && m.length() == 2)
op = 1;
else
op = 0;
}
public static void main(String[] args) {
String d = "12/06/1987";
abc(d);
System.out.println(op);
}
}
2.
package Dumps;
import java.util.StringTokenizer;
public class Prog2 {
static String str1;
public static void abc(String str) {
StringTokenizer st = new StringTokenizer(str, " ");
st.nextToken();
String a = st.nextToken();
char b = str.charAt(0);
str1 = a + "," + b;
}
public static void main(String[] args) {
String str = "Jessica Miller";
abc(str);
System.out.println(str1);
}
}
3.
package Dumps;
import java.util.HashMap;
import java.util.Set;
public class Prog3 {
static Integer op = 0;
public static void abc(HashMap<Integer, String> m) {
Set<Integer> s = m.keySet();
for (Integer i : s) {
if (i % 4 != 0)
op++;
}
}
public static void main(String[] args) {
HashMap<Integer, String> m = new HashMap<Integer, String>();
m.put(2, "hi");
m.put(8, "Hello");
m.put(12, "Welcome");
abc(m);
System.out.println(op);
}
}
4.
package Dumps;
public class Prog4 {
static int op = 0;
public static void abc(int n) {
int temp, rem;
temp = n;
while (temp > 0) {
rem = temp % 10;
op = op + rem * rem;
temp = temp / 10;
}
}
public static void main(String[] args) {
int n = 321;
abc(n);
System.out.println(op);
}
}
5.
package Dumps;
import java.util.ArrayList;
package Dumps;
public class Prog6 {
static int op = 0;
public static void abc(String str, String str1, String str2) {
int n1 = str.indexOf(str1);
int n2 = str.indexOf(str2);
if (n2 > n1)
op = 1;
else
op = 0;
}
public static void main(String[] args) {
String str = "geniousRajKumarDev";
String str1 = "Raj";
String str2 = "Dev";
abc(str, str1, str2);
System.out.println(op);
}
}
7.
package Dumps;
public class Prog7 {
static int op = 0;
public static void abc(String str) {
int i, n, sum = 0;
String s;
for (i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
s = str.valueOf(c);
n = Integer.parseInt(s);
sum = sum + n;
}
}
op = sum;
}
public static void main(String[] args) {
String str = "good23bad4";
abc(str);
System.out.println(op);
}
}
8.
package Dumps;
import java.util.ArrayList;
public class Prog8 {
static ArrayList<Integer> al1;
public static void abc(ArrayList<Integer> al) {
al1 = new ArrayList<Integer>();
for (int i = 0; i < al.size(); i++) {
int j = i + 1;
int n = al.get(i);
if (j % 3 != 0)
al1.add(n);
}
}
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(3);
al.add(1);
al.add(11);
al.add(19);
al.add(17);
al.add(19);
abc(al);
System.out.println(al1);
}
9.
package Dumps;
public class Prog9 {
static int op = 0;
public static void abc(String str) {
int n = str.length();
if (n == 8) {
if (str.matches("[A-Z]{3}[0-9]{4}[A-Z]{1}"))
op = 1;
else
op = 2;
} else
op = 2;
}
public static void main(String[] args) {
String str = "ALD3245A";
abc(str);
System.out.println(op);
}
}
10.
package Dumps;
import java.util.StringTokenizer;
public class Prog10 {
static int op = 0;
public static void abc(String str1, String str2) {
int count = 0, c = 0;
String temp = null, str;
StringTokenizer st = new StringTokenizer(str2, " ");
StringTokenizer st1 = new StringTokenizer(str1, " ");
while (st.hasMoreTokens()) {
str = st.nextToken();
count++;
if (count == 2) {
temp = str;
}
}
while (st1.hasMoreTokens()) {
str = st1.nextToken();
if (str.compareTo(temp) == 0)
c++;
}
op = c;
}
public static void main(String[] args) {
String str1 = "Hi this is cognizant academy";
String str2 = "Hello this is a trainee";
abc(str1, str2);
System.out.println(op);
}
}
11.
package Dumps;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Prog11 {
static int op = 0;
public static void abc(ArrayList<String> al, String str) {
Comparator com = Collections.reverseOrder();
Collections.sort(al, com);
12.
package Dumps;
public class Prog12 {
static String op;
public static void abc(String str1, String str2) {
int n = str1.length();
int n1 = str2.length();
String str3 = null, str4 = null;
if (n == n1)
op = str1 + str2;
else if (n > n1) {
str3 = str1.substring(n - n1);
str4 = str2;
op = str3 + str4;
} else {
str3 = str2.substring(n1 - n);
str4 = str1;
op = str4 + str3;
}
}
public static void main(String[] args) {
String str1 = "Hi";
String str2 = "Delhi";
abc(str1, str2);
System.out.println(op);
}
}
13.
package Dumps;
public class Prog13 {
static int op = 0;
public static void abc(int[] a, int num) {
int rem, temp = 0, sum = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > num)
sum = sum + a[i];
}
while (sum != 0) {
rem = sum % 10;
temp = temp * 10 + rem;
sum = sum / 10;
}
op = temp;
}
public static void main(String[] args) {
int a[] = { 10, 15, 20, 25, 30, 100 };
int num = 15;
abc(a, num);
System.out.println(op);
}
}
14.
package Dumps;
public class Prog14 {
static int op;
public static void abc(String s1) {
if (s1.matches("[0-9]{2}[./-]{1}[0-9]{2}[./-]{1}[0-9]{4}||[0-9]
{2}"))
op = 1;
else
op = 0;
}
public static void main(String[] args) {
String s1 = "15.07.2014";
abc(s1);
System.out.println(op);
}
}
15.
package Dumps;
import java.util.ArrayList;
public class Prog15 {
static int op = 0;
public static void abc(ArrayList<Integer> a1, ArrayList<Integer> a2) {
int i, sum = 0, b;
a1.retainAll(a2);
for (i = 0; i < a1.size(); i++) {
b = a1.get(i);
sum = sum + b;
}
if (sum == 0)
op = -1;
else
op = sum;
}
public static void main(String[] args) {
ArrayList<Integer> a1 = new ArrayList<Integer>();
ArrayList<Integer> a2 = new ArrayList<Integer>();
a1.add(1);
a1.add(3);
a1.add(4);
a1.add(7);
a1.add(9);
a2.add(2);
a2.add(5);
a2.add(7);
a2.add(8);
abc(a1, a2);
System.out.println(op);
}
}
16.
package Dumps;
import java.util.StringTokenizer;
public class Prog16 {
static String op;
public static void abc(String str, int start, int end) {
StringBuffer sb = new StringBuffer(str);
sb.reverse();
end = start + end;
op = sb.substring(start, end);
}
public static void main(String[] args) {
String str = "rajasthan";
int start = 2;
int len = 3;
17.
package Dumps;
import java.util.Date;
import java.util.StringTokenizer;
public class Prog17 {
static String op;
public static void abc(String date) {
StringTokenizer st = new StringTokenizer(date, "/");
String d, m, y;
int y1;
d = st.nextToken();
m = st.nextToken();
y = st.nextToken();
y1 = Integer.parseInt(y);
y1 = y1 + 1;
String new_date = d + "/" + m + "/" + y1;
Date d1 = new Date(new_date);
int day = d1.getDay();
if (day == 0)
op = "sunday";
else if (day == 1)
op = "monday";
else if (day == 2)
op = "tuesday";
else if (day == 3)
op = "wednesday";
else if (day == 4)
op = "thursday";
else if (day == 5)
op = "friday";
else if (day == 6)
op = "saturday";
}
public static void main(String[] args) {
String date = "16/07/2014";
abc(date);
System.out.println(op);
}
}
18.
package Dumps;
import java.util.StringTokenizer;
public class Prog18 {
static String op;
public static void abc(String str) {
String d, m, y;
StringTokenizer st = new StringTokenizer(str, "/");
d = st.nextToken();
m = st.nextToken();
y = st.nextToken();
op = d + "-" + m + "-" + y.substring(2, 4);
}
public static void main(String[] args) {
String str = "12/11/1998";
abc(str);
System.out.println(op);
}
}
19.
package Dumps;
import java.util.ArrayList;
public class Prog19 {
static ArrayList<Integer> op;
20.
package Dumps;
public class Prog20 {
static int op;
public static void abc(String str) {
if (str.startsWith("-")) {
int n = Integer.parseInt(str);
n = Math.abs(n);
op = n;
} else
op = -1;
}
public static void main(String[] args) {
String str = "-94923";
abc(str);
System.out.println(op);
}
}
21.
package Dumps;
public class Prog21 {
static int op = 0;
public static void abc(int n, int r) {
int i, n1 = 1, r1 = 1, d = 1, temp;
for (i = 1; i <= n; i++) {
n1 = n1 * i;
}
for (i = 1; i <= r; i++) {
r1 = r1 * i;
}
temp = n - r;
for (i = 1; i <= temp; i++) {
d = d * i;
}
op = n1 / (r1 * d);
}
public static void main(String[] args) {
int n = 5, r = 3;
abc(n, r);
System.out.println(op);
}
}
22.
package Dumps;
import java.util.StringTokenizer;
public class Prog22 {
static String op;
public static void abc(String str1, String str2) {
int max = 0;
String n;
StringTokenizer st = new StringTokenizer(str1, str2);
while (st.hasMoreTokens()) {
n = st.nextToken();
if (n.length() > max) {
max = n.length();
op = n;
}
}
}
public static void main(String[] args) {
String str1 = "delhi-pune-patna";
String str2 = "-";
abc(str1, str2);
System.out.println(op);
}
}
23.
package Dumps;
import java.util.ArrayList;
public class Prog23 {
static int op;
public static void abc(String str) {
ArrayList<Character> al = new ArrayList<Character>();
ArrayList<Character> al1 = new ArrayList<Character>();
int i, j, count = 0;
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ') {
al.add(str.charAt(i));
}
}
for (i = 0; i < al.size(); i++) {
count = 0;
if (al.get(i) != '*') {
for (j = i + 1; j < al.size(); j++) {
if (al.get(i) == al.get(j)) {
al.set(j, '*');
count++;
}
}
if (count == 0)
al1.add(al.get(i));
}
}
op = al1.size();
}
public static void main(String[] args) {
String str = "Hello World";
abc(str);
System.out.println(op);
}
}
24.
package Dumps;
public class Prog24 {
static String op = "";
public static void abc(String str) {
int i;
char c, c1 = 0;
if (str.length() % 2 == 0) {
for (i = 0; i < str.length(); i += 2)
op = op + str.charAt(i + 1) + str.charAt(i);
} else {
for (i = 0; i < str.length() - 1; i += 2)
op = op + str.charAt(i + 1) + str.charAt(i);
op = op + str.charAt(str.length() - 1);
}
}
public static void main(String[] args) {
String str = "New York";
abc(str);
System.out.println(op);
}
}
25.
package Dumps;
import java.util.ArrayList;
public class Prog30 {
static ArrayList<Integer> op;
public static void abc(ArrayList<Integer> al1, ArrayList<Integer> al2) {
op = new ArrayList<Integer>();
int i, j, temp;
al1.addAll(al2);
for (i = 0; i < al1.size(); i++) {
for (j = i + 1; j < al1.size(); j++) {
if (al1.get(i) > al1.get(j)) {
temp = al1.get(i);
al1.set(i, al1.get(j));
al1.set(j, temp);
}
}
for (i = 0; i < al1.size(); i++) {
if (i == 2 || i == 6 || i == 8)
op.add(al1.get(i));
}
26.
package Dumps;
public class Prog26 {
static double op;
public static void abc(int[] n) {
int i, max = 0, min;
for (i = 0; i < n.length; i++) {
if (n[i] > max)
max = n[i];
}
min = max;
for (i = 0; i < n.length; i++) {
if (n[i] < min)
min = n[i];
}
op = (max + min) / 2.0;
}
public static void main(String[] args) {
int n[] = { 3, 6, 9, 4, 2, 5 };
abc(n);
System.out.println(op);
}
}
27.
28.
package Dumps;
public class Prog28 {
static int op[] = new int[20], j = 0;
public static void abc(int[] n, int n1) {
int i, count = 0;
for (i = 0; i < n.length; i++) {
if (n[i] != n1)
op[j++] = n[i];
else
count++;
}
for (i = 0; i < count; i++)
op[j++] = 0;
}
29.
package Dumps;
import java.util.StringTokenizer;
public class Prog29 {
static String op;
public static void abc(String str) {
int i, count, max = 0;
String s, s1;
char c;
StringTokenizer st = new StringTokenizer(str, " ");
while (st.hasMoreTokens()) {
s = st.nextToken();
s1 = s.toLowerCase();
count = 0;
for (i = 0; i < s1.length(); i++) {
c = s1.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c
== 'u')
count++;
}
if (count > max) {
max = count;
op = s;
}
}
}
30.
package Dumps;
import java.util.ArrayList;
public class Prog30 {
static ArrayList<Integer> op;
public static void abc(ArrayList<Integer> al1, ArrayList<Integer> al2) {
op = new ArrayList<Integer>();
int i, j, temp;
al1.addAll(al2);
for (i = 0; i < al1.size(); i++) {
for (j = i + 1; j < al1.size(); j++) {
if (al1.get(i) > al1.get(j)) {
temp = al1.get(i);
al1.set(i, al1.get(j));
al1.set(j, temp);
}
}
for (i = 0; i < al1.size(); i++) {
if (i == 2 || i == 6 || i == 8)
op.add(al1.get(i));
}