SlideShare a Scribd company logo
2
Most read
10
Most read
11
Most read
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 {
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;
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;
}
}
}
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) {
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);
}
}
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;
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 = "";
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));
}
}
}
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("*");
}
}
}
}
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));
}
}
}
}
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);
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;
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);
}
}
JAVA PRACTICE QUESTIONS v1.4.pdf

More Related Content

PPTX
Chapter 7 ooad
PDF
Plsql lab mannual
PPTX
Date and time functions in mysql
PPT
Array in Java
PPTX
Thread priorities
PPT
Oomd unit1
PPTX
User Interface Analysis and Design
Chapter 7 ooad
Plsql lab mannual
Date and time functions in mysql
Array in Java
Thread priorities
Oomd unit1
User Interface Analysis and Design

What's hot (20)

PPT
2.5 backpropagation
PPTX
The Ultimate Sequence Diagram Tutorial
PPT
Android software stack
PDF
Recurrent neural networks rnn
PPT
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
DOC
SOFTWARE ENGINEERING
PDF
java-thread
PPTX
Software Testing Foundations Part 6 - Intuitive and Experience-based testing
PPT
PPTX
Perceptron & Neural Networks
PDF
3 relational model
PPTX
Relational Database Design
PPTX
Software requirement specification
PPTX
HANDWRITTEN DIGIT RECOGNITIONppt1.pptx
PPTX
Rational unified process (rup)
PPTX
Competitive Learning [Deep Learning And Nueral Networks].pptx
PPTX
V model software engineering
PDF
Matlab to vhdl
PPTX
Architectural styles and patterns
2.5 backpropagation
The Ultimate Sequence Diagram Tutorial
Android software stack
Recurrent neural networks rnn
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
SOFTWARE ENGINEERING
java-thread
Software Testing Foundations Part 6 - Intuitive and Experience-based testing
Perceptron & Neural Networks
3 relational model
Relational Database Design
Software requirement specification
HANDWRITTEN DIGIT RECOGNITIONppt1.pptx
Rational unified process (rup)
Competitive Learning [Deep Learning And Nueral Networks].pptx
V model software engineering
Matlab to vhdl
Architectural styles and patterns
Ad

Similar to JAVA PRACTICE QUESTIONS v1.4.pdf (20)

PDF
Java_Programming_by_Example_6th_Edition.pdf
PDF
Sam wd programs
PDF
Sharable_Java_Python.pdf
DOC
Cosc 1436 java programming/tutorialoutlet
DOCX
PDF
java-programming.pdf
PDF
Microsoft word java
DOCX
Java Program
DOCX
Exercise 1 [10 points]Write a static method named numUniq.docx
PDF
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
DOCX
Exercise 1 [10 points]Write a static method named num
DOCX
Java file
DOCX
Java file
DOCX
Wap to implement bitwise operators
PDF
Review Questions for Exam 10182016 1. public class .pdf
PDF
Simple Java Program for beginner with easy method.pdf
PPTX
Ch2 Elementry Programmin as per gtu oop.pptx
DOCX
PDF
Best Java Problems and Solutions
DOCX
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
Java_Programming_by_Example_6th_Edition.pdf
Sam wd programs
Sharable_Java_Python.pdf
Cosc 1436 java programming/tutorialoutlet
java-programming.pdf
Microsoft word java
Java Program
Exercise 1 [10 points]Write a static method named numUniq.docx
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Exercise 1 [10 points]Write a static method named num
Java file
Java file
Wap to implement bitwise operators
Review Questions for Exam 10182016 1. public class .pdf
Simple Java Program for beginner with easy method.pdf
Ch2 Elementry Programmin as per gtu oop.pptx
Best Java Problems and Solutions
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
Ad

More from RohitkumarYadav80 (10)

PDF
PEA305 workbook.pdf
PPT
Unit III Heaps.ppt
PPTX
atm-facilities-and-equipment.pptx
PDF
unit 1 MCQ 316.pdf
PDF
1. Combinational Logic Circutis with examples (1).pdf
PPTX
PEA305 Lec 0 1 (1).pptx
PDF
7. CPU_Unit3 (1).pdf
PDF
asymptotic_notations.pdf
PDF
1. Combinational Logic Circutis with examples (1).pdf
PEA305 workbook.pdf
Unit III Heaps.ppt
atm-facilities-and-equipment.pptx
unit 1 MCQ 316.pdf
1. Combinational Logic Circutis with examples (1).pdf
PEA305 Lec 0 1 (1).pptx
7. CPU_Unit3 (1).pdf
asymptotic_notations.pdf
1. Combinational Logic Circutis with examples (1).pdf

Recently uploaded (20)

PDF
The Internet -By the Numbers, Sri Lanka Edition
DOCX
Unit-3 cyber security network security of internet system
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
artificial intelligence overview of it and more
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PPTX
Internet___Basics___Styled_ presentation
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
Testing WebRTC applications at scale.pdf
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
The Internet -By the Numbers, Sri Lanka Edition
Unit-3 cyber security network security of internet system
presentation_pfe-universite-molay-seltan.pptx
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Tenda Login Guide: Access Your Router in 5 Easy Steps
artificial intelligence overview of it and more
522797556-Unit-2-Temperature-measurement-1-1.pptx
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Power Point - Lesson 3_2.pptx grad school presentation
RPKI Status Update, presented by Makito Lay at IDNOG 10
Internet___Basics___Styled_ presentation
Unit-1 introduction to cyber security discuss about how to secure a system
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
SAP Ariba Sourcing PPT for learning material
SASE Traffic Flow - ZTNA Connector-1.pdf
Testing WebRTC applications at scale.pdf
Module 1 - Cyber Law and Ethics 101.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx

JAVA PRACTICE QUESTIONS v1.4.pdf

  • 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); } }