1. JAVA PRACTICE QUESTIONS AND SOLUTIONS
WAP to calculate and print the Total and Average marks scored by a student.
public class third {
public static void main(String[] args) {
String n = args[0];
double m1 = Double.parseDouble(args[1]);
double m2 = Double.parseDouble(args[2]);
double m3 = Double.parseDouble(args[3]);
System.out.println("Name : " + n);
System.out.println("Marks 1: " + m1);
System.out.println("Marks 2: " + m2);
System.out.println("Marks 3: " + m3);
System.out.println("Total : " + (m1 + m2 + m3));
System.out.println("Avg : " + (m1 + m2 + m3) / 3);
}
}
implement if-then-else statement to print Can Vote, if the provided age is greater than or
equal to 18 and print Cannot Vote otherwise
public class fourth {
public static void main(String[] args) {
int age = Integer.parseInt(args[0]);
if (age < 18) {
System.out.println("Cannot Vote");
} else if (age >= 18) {
System.out.println("Can vote");
}
}
}
Use If-else statement such that if the argument passed to the main method is equal to
Sunday the program should print Holiday, otherwise it should print Working Day.
public class sixth {
public static void main(String[] args) {
String s = args[0];
if (s.equals("Sunday")) {
System.out.println("Holiday");
} else {
2. System.out.println("Working Day");
}
}
}
four integers represent the marks in Maths, Science, Social and English. The program
should print the passCount. The passCount should reflect the count of subjects in which
the marks scored is greater than or equal to 50.
public class eight {
public static void main(String[] args) {
int passcount = 0;
int arr[] = new int[4];
for (int i = 0; i < 4; i++) {
arr[i] = Integer.parseInt(args[i]);
if (arr[i] >= 50) {
passcount++;
}
}
System.out.println(passcount);
}
}
Switch statement. Working Day - when the arg[0] is equal to Monday or Tuesday or
Wednesday or Thursday is a Semi-Working Day - when the arg[0] is equal to Friday
is a Happy Day! - when the arg[0] is equal to either Saturday or a Sunday
is not a valid day of week - when the text in arg[0] does not represent a day in
a week
public class ninth {
public static void main(String[] args) {
String day = args[0];
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
System.out.println("Working day");
break;
case "Friday":
System.out.println("Semi-Working Day");
break;
3. case "Saturday":
case "Sunday":
System.out.println("Happy Day");
break;
default:
System.out.println("not a valid day");
break;
}
}
}
Switch case. If 1 print One, Two, Three. If 2 print Two Three. If 3 print Three. If 4 print
Four. If 10 print Ten.
public class tenth {
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
switch (num) {
case 1:
System.out.println("One");
System.out.println("Two");
System.out.println("Three");
break;
case 2:
System.out.println("Two");
System.out.println("Three");
break;
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
case 10:
System.out.println("Ten");
break;
default:
System.out.println("Some other number");
break;
}
}
}
4. Write a logic to print all the English alphabets from A to Z.
public class eleventh {
public static void main(String[] args) {
for (int i = 65; i <= 90; i++) {
System.out.print((char) i);
}
}
}
2-dimensional array where it ask the user input number of students, number of exams,
marks of student1, marks of student 2 etc., and calculate the grade of the students
import java.util.Scanner;
public class thirteenth {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("number of students");
int a = sc.nextInt();
System.out.println("number of subjects");
int b = sc.nextInt();
int[][] arr = new int[a][b];
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
System.out.println("student " + (i + 1) + "subject " + (j + 1));
arr[i][j] = sc.nextInt();
}
}
for (int i = 0; i < a; i++) {
int sum = 0;
for (int j = 0; j < b; j++) {
sum += arr[i][j];
}
int percent = sum / b;
System.out.println(percent);
if (percent >= 90) {
System.out.println("O");
} else if (percent >= 80) {
System.out.println("A+");
} else if (percent >= 70) {
5. System.out.println("A");
} else if (percent >= 60) {
System.out.println("B+");
} else if (percent >= 50) {
System.out.println("B");
} else if (percent >= 45) {
System.out.println("C");
} else if (percent >= 40) {
System.out.println("D");
} else if (percent <= 40) {
System.out.println("E");
}
}
}
}
Create an array and it should ask the user no of subjects, enter the marks out of 100 and it
needs to calculate sum and percentage.
import java.util.Scanner;
public class fourteenth {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int arr[] = new int[a];
for (int i = 0; i < a; i++) {
arr[i] = sc.nextInt();
}
int sum = 0;
for (int i = 0; i < a; i++) {
sum += arr[i];
}
int avg = sum / a;
System.out.println(avg);
System.out.println(sum);
}
}
6. A class contains the data members name (String), age (int), designation (String), salary
(double) and the methods setData(), displayData(). Call setData() with arguments and
finally call the method displayData() to print the output.
public class nineteenth {
private String name;
private int age;
private String desg;
private double salary;
public void setdata(String n, int a, String d, double sa) {
name = n;
age = a;
desg = d;
salary = sa;
}
public void getdata() {
System.out.println(name + age + desg + salary);
}
public static void main(String[] args) {
String name = args[0];
int age = Integer.parseInt(args[1]);
String desg = args[2];
double salary = Double.parseDouble(args[3]);
nineteenth str1 = new nineteenth();
str1.setdata(name, age, desg, salary);
str1.getdata();
}
}
A class contains the data members id (int), name (String) which are initialized with a
parameterized constructor and the method show(). Create an object to the class with
arguments id and name within the main(), and finally call the method show() to print the
output.
public class twentieth {
private int id;
private String name;
twentieth(int id, String name) {
this.id = id;
7. this.name = name;
}
void show() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
twentieth t1 = new twentieth(1, "Hemanth");
t1.show();
}
}
public class twentytwo {
private String name;
private int age;
void set(String name, int age) {
this.name = name;
this.age = age;
}
void get() {
System.out.println(name);
System.out.println(Integer.toString(age));
}
public static void main(String[] args) {
twentytwo t1 = new twentytwo();
t1.set("Hemanth", 21);
t1.get();
}
}
String palindrome or not.
import java.util.Scanner;
public class twentythree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n = sc.nextLine().toUpperCase();
String rev = "";
8. for (int i = n.length() - 1; i >= 0; i--) {
rev = rev + n.charAt(i);
}
if (n.equals(rev)) {
System.out.println("palindrome");
} else {
System.out.println("not");
}
}
}
Put inside Tags. Put the passed string into the set of symmetrical pair of brackets. Both of
which provided as input.
public class twentyfour {
public static void main(String[] args) {
StringBuilder s = new StringBuilder(args[0]);
StringBuilder s1 = new StringBuilder(args[1]);
int l = s1.length();
s1.insert(l / 2, s);
System.out.println(s1);
}
}
String Reversal
public class twentyfive {
public static void main(String[] args) {
String n = args[0];
for (int i = n.length() - 1; i >= 0; i--) {
System.out.print(n.charAt(i));
}
}
}
9. Middle Two chars
public class twentysix {
public static void main(String[] args) {
String s = args[0];
int l = s.length() / 2;
System.out.print(s.charAt(l - 1));
System.out.print(s.charAt(l));
}
}
WAP to check if the string ends with given input.
public class twentyseven {
public static void main(String[] args) {
String n = args[0];
String m = args[1];
String o = "";
for (int i = n.length() - m.length(); i <= n.length() - 1; i++) {
o = o + n.charAt(i);
}
if (m.equals(o)) {
System.out.println("true");
} else {
System.out.println("Fals");
}
}
}
Append “*” to make the string length 10.
public class twentynine {
public static void main(String[] args) {
String n = args[0];
if (n.length() < 10) {
System.out.print(n);
for (int i = 0; i < 10 - n.length(); i++) {
System.out.print("*");
}
}
}
}
10. Remove the character ‘x’ from the string. Without using print, but using println.
public class thirty {
public static void main(String[] args) {
String s = args[0];
String s1 = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != 'x') {
s1 = s1 + s.charAt(i);
}
}
System.out.println(s1);
}
}
The method receives one command line argument. If the argument has the same prefix
and suffixes up to 3 characters, remove the suffix and print the argument.
Example:
Cmd Args : systemsys
system
public class thirtyone {
public static void main(String[] args) {
String s = args[0];
String rev = "";
for (int i = s.length() - 1; i >= 0; i--) {
rev = rev + s.charAt(i);
}
int ind = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == rev.charAt(i)) {
ind = i;
} else {
break;
}
}
ind++;
if (ind <= 3) {
for (int i = 0; i < s.length() - ind; i++) {
System.out.print(s.charAt(i));
}
}
}
}
11. The program should remove the first two characters from the argument and print the
output, except in one condition. The program should skip removal of x or y if it encounters
them in the first two positions.
public class thirtytwo {
public static void main(String[] args) {
StringBuilder s = new StringBuilder(args[0]);
if (s.charAt(1) != 'y') {
s.deleteCharAt(1);
}
if (s.charAt(0) != 'x') {
s.deleteCharAt(0);
}
System.out.println(s);
}
}
Counting the occurrence frequency of the character ‘o’ in the given string.
public class thirtythree {
public static void main(String[] args) {
String s = args[0];
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'o') {
count++;
}
}
System.out.println(count);
}
}
Using StringBuffer. Consider a string "Hello India" and delete 0 to 6 characters in that and
print the result.
Consider another string "Hello World", delete characters from position 0 to length of the
entire string and print the result. Consider another string "Hello Java", remove 0th
character and then print the result.
public class thirtyfour {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("Hello India");
s.delete(0, 6);
12. System.out.println(s);
StringBuffer s1 = new StringBuffer("Hello World");
s1.delete(0, s1.length());
System.out.println(s1);
StringBuffer s2 = new StringBuffer("Hello Java");
s2.deleteCharAt(0);
System.out.println(s2);
}
}
Create a class that contains the data members id of int data type, javaMarks, cMarks and
cppMarks of float data type write a method setMarks() to initialize the data members
write a method displayMarks() which will display the given data Create another class
Result which is derived from the class Marks contains the data members total and avg of
float data type write a method compute() to find total and average of the given marks
write a method showResult() which will display the total and avg marks Write a class
SingleInheritanceDemo with main() method it receives four arguments as id, javaMarks,
cMarks and cppMarks. Create object only to the class Result to access the methods.
class Marks {
int id;
float javamarks;
float cMarks;
float cppMarks;
void setmarks(int id, float javamarks, float cMarks, float cppMarks) {
this.id = id;
this.javamarks = javamarks;
this.cMarks = cMarks;
this.cppMarks = cppMarks;
}
void display() {
System.out.println("Java" + javamarks + "cMarks" + cMarks + "cppMarks" + cppMarks);
}
}
class Result extends Marks {
float total;
float average;
void compute() {
total = super.javamarks + super.cMarks + super.cppMarks;
13. System.out.println(total);
}
void average() {
average = total / 3;
System.out.println(average);
}
}
public class thirtyfive {
public static void main(String[] args) {
Result r = new Result();
r.setmarks(Integer.parseInt(args[0]), Float.parseFloat(args[1]), Float.parseFloat(args[2]),
Float.parseFloat(args[3]));
r.display();
r.compute();
r.average();
}
}
Default values of few data types.
public class second {
static byte b;
static short s;
static int i;
static long l;
static boolean bo;
static double d;
static float f;
public static void main(String[] args) {
System.out.println("byte default value" + b);
System.out.println("short default value" + s);
System.out.println("int default value" + i);
System.out.println("long default value" + l);
System.out.println("boolean default value" + bo);
System.out.println("double default value" + d);
System.out.println("float default value" + f);
}
}